1 /* 2 (C) 2007 by Argonne National Laboratory. 3 See COPYRIGHT in top-level directory. 4 */ 5 #include "mpe_logging_conf.h" 6 7 #if defined( STDC_HEADERS ) || defined( HAVE_STDIO_H ) 8 #include <stdio.h> 9 #endif 10 #if defined( STDC_HEADERS ) || defined( HAVE_STDLIB_H ) 11 #include <stdlib.h> 12 #endif 13 #if defined( HAVE_PTHREAD_H ) 14 #include <pthread.h> 15 #endif 16 17 #if defined( HAVE_LIBPTHREAD ) 18 19 #include "mpe_callstack.h" 20 21 #define MPE_ThreadID_t int 22 #define MPE_THREADID_NULL -9999 23 24 #define MPE_LOG_THREAD_PRINTSTACK() \ 25 do { \ 26 MPE_CallStack_t cstk; \ 27 MPE_CallStack_init( &cstk ); \ 28 MPE_CallStack_fancyprint( &cstk, 2, \ 29 "\t", 1, MPE_CALLSTACK_UNLIMITED ); \ 30 } while (0) 31 32 /* MPE coarse-grained lock support mechanism */ 33 /* 34 pthread_mutex_t MPE_Thread_mutex = PTHREAD_MUTEX_INITIALIZER; 35 36 Replaced PTHREAD_MUTEX_INITIALIZER by pthread_mutex_init() as 37 Sun's cc for linux failed to compile PTHREAD_MUTEX_INITIALIZER because 38 Sun's cc is a strict C compiler and PTHREAD_MUTEX_INITIALIZER 39 implementation on 32bit linux uses C++ feature. 40 */ 41 pthread_mutex_t MPE_Thread_mutex; 42 pthread_key_t MPE_ThreadStm_key; 43 MPE_ThreadID_t MPE_Thread_count = MPE_THREADID_NULL; 44 45 void MPE_ThreadStm_free( void *thdstm ); MPE_ThreadStm_free(void * thdstm)46void MPE_ThreadStm_free( void *thdstm ) 47 { 48 if ( thdstm != NULL ) { 49 free( thdstm ); 50 thdstm = NULL; 51 } 52 } 53 54 void MPE_Log_thread_init( void ); MPE_Log_thread_init(void)55void MPE_Log_thread_init( void ) 56 { 57 int ierr; 58 /* 59 Check if MPE_Thread_count == MPE_THREADID_NULL to protect 60 the initialization routines from invoking more than once. 61 */ 62 if ( MPE_Thread_count == MPE_THREADID_NULL ) { 63 MPE_Thread_count = 0; 64 ierr = pthread_mutex_init( &MPE_Thread_mutex, NULL ); 65 if ( ierr != 0 ) { 66 perror( "pthread_mutex_init() fails!" ); 67 MPE_LOG_THREAD_PRINTSTACK(); 68 pthread_exit( NULL ); 69 } 70 ierr = pthread_key_create( &MPE_ThreadStm_key, MPE_ThreadStm_free ); 71 if ( ierr != 0 ) { 72 perror( "pthread_key_create() fails!" ); 73 MPE_LOG_THREAD_PRINTSTACK(); 74 pthread_exit( NULL ); 75 } 76 } 77 } 78 79 #else 80 81 void MPE_ThreadStm_free( void *thdstm ); MPE_ThreadStm_free(void * thdstm)82void MPE_ThreadStm_free( void *thdstm ) 83 {} 84 85 void MPE_Log_thread_init( void ); MPE_Log_thread_init(void)86void MPE_Log_thread_init( void ) 87 {} 88 89 #endif 90