00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #define POSIX
00023
00024 #if defined(_WIN32)
00025 #undef POSIX
00026 #endif
00027 #if defined(macintosh)
00028 #undef POSIX
00029 #endif
00030 #if defined(OS2)
00031 #undef POSIX
00032 #endif
00033
00034
00035
00036 #include "unicode/utypes.h"
00037
00038
00039 #ifdef APP_NO_THREADS
00040 # define ICU_USE_THREADS 0
00041 #endif
00042
00043
00044 #ifndef ICU_USE_THREADS
00045 # define ICU_USE_THREADS 1
00046 #endif
00047
00048
00049 #if defined(POSIX) && (ICU_USE_THREADS==1)
00050
00051
00052
00053 # include <pthread.h>
00054
00055 # ifdef POSIX_DEBUG_REENTRANCY
00056 pthread_t gLastThread;
00057 UBool gInMutex;
00058
00059 U_EXPORT void WeAreDeadlocked();
00060
00061 void WeAreDeadlocked()
00062 {
00063 puts("ARGH!! We're deadlocked.. break on WeAreDeadlocked() next time.");
00064 }
00065 # endif
00066 #endif
00067
00068 #ifdef WIN32
00069 # include <WINDOWS.H>
00070 #endif
00071
00072 #include "umutex.h"
00073 #include "cmemory.h"
00074
00075
00076 UMTX gGlobalMutex = NULL;
00077
00078 void umtx_lock( UMTX *mutex )
00079 {
00080 #if (ICU_USE_THREADS == 1)
00081 if( mutex == NULL )
00082 {
00083 mutex = &gGlobalMutex;
00084 }
00085
00086 if(*mutex == NULL)
00087 {
00088 umtx_init(mutex);
00089 }
00090
00091 #if defined(WIN32)
00092
00093 EnterCriticalSection((CRITICAL_SECTION*) *mutex);
00094
00095 #elif defined(POSIX)
00096
00097 # ifdef POSIX_DEBUG_REENTRANCY
00098 if(gInMutex == TRUE)
00099 if(pthread_equal(gLastThread,pthread_self()))
00100 WeAreDeadlocked();
00101 # endif
00102
00103 pthread_mutex_lock((pthread_mutex_t*) *mutex);
00104
00105 # ifdef POSIX_DEBUG_REENTRANCY
00106 gLastThread = pthread_self();
00107 gInMutex = TRUE;
00108 # endif
00109 #endif
00110 #endif
00111 }
00112
00113 void umtx_unlock( UMTX* mutex )
00114 {
00115 #if (ICU_USE_THREADS==1)
00116 if( mutex == NULL )
00117 {
00118 mutex = &gGlobalMutex;
00119
00120 if(*mutex == NULL)
00121 {
00122 return;
00123 }
00124 }
00125
00126 #if defined(WIN32)
00127
00128 LeaveCriticalSection((CRITICAL_SECTION*)*mutex);
00129
00130 #elif defined(POSIX)
00131 pthread_mutex_unlock((pthread_mutex_t*)*mutex);
00132 #ifdef POSIX_DEBUG_REENTRANCY
00133 gInMutex = FALSE;
00134 #endif
00135
00136 #endif
00137 #endif
00138 }
00139
00140 U_CAPI void umtx_init( UMTX *mutex )
00141 {
00142 #if (ICU_USE_THREADS == 1)
00143
00144 if( mutex == NULL )
00145 {
00146 mutex = &gGlobalMutex;
00147 }
00148
00149 if(*mutex != NULL)
00150 return;
00151
00152 #if defined(WIN32)
00153 *mutex = uprv_malloc(sizeof(CRITICAL_SECTION));
00154 InitializeCriticalSection((CRITICAL_SECTION*)*mutex);
00155
00156 #elif defined( POSIX )
00157
00158 *mutex = uprv_malloc(sizeof(pthread_mutex_t));
00159
00160 #if defined(HPUX_CMA)
00161 pthread_mutex_init((pthread_mutex_t*)*mutex, pthread_mutexattr_default);
00162 #else
00163 pthread_mutex_init((pthread_mutex_t*)*mutex,NULL);
00164 #endif
00165
00166
00167 # ifdef POSIX_DEBUG_REENTRANCY
00168 gInMutex = FALSE;
00169 # endif
00170
00171 #endif
00172 #endif
00173 }
00174
00175
00176
00177
00178
00179
00180