Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

umutex.c

00001 /*
00002 *******************************************************************************
00003 *
00004 *   Copyright (C) 1997-2000, International Business Machines
00005 *   Corporation and others.  All Rights Reserved.
00006 *
00007 *******************************************************************************
00008 *
00009 * File CMUTEX.C
00010 *
00011 * Modification History:
00012 *
00013 *   Date        Name        Description
00014 *   04/02/97    aliu        Creation.
00015 *   04/07/99    srl         updated
00016 *   05/13/99    stephen     Changed to umutex (from cmutex).
00017 *   11/22/99    aliu        Make non-global mutex autoinitialize [j151]
00018 *******************************************************************************
00019 */
00020 
00021 /* Assume POSIX, and modify as necessary below */
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 /* Check our settings... */
00036 #include "unicode/utypes.h"
00037 
00038 /* APP_NO_THREADS is an old symbol. We'll honour it if present. */
00039 #ifdef APP_NO_THREADS
00040 # define ICU_USE_THREADS 0
00041 #endif
00042 
00043 /* Default: use threads. */
00044 #ifndef ICU_USE_THREADS
00045 # define ICU_USE_THREADS 1
00046 #endif
00047 
00048 
00049 #if defined(POSIX) && (ICU_USE_THREADS==1)
00050   /* Usage: uncomment the following, and breakpoint WeAreDeadlocked to
00051      find reentrant issues. */
00052 /* # define POSIX_DEBUG_REENTRANCY 1 */
00053 # include <pthread.h> /* must be first, so that we get the multithread versions of things. */
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 /* POSIX_DEBUG_REENTRANCY */
00066 #endif /* POSIX && (ICU_USE_THREADS==1) */
00067 
00068 #ifdef WIN32
00069 # include <WINDOWS.H>
00070 #endif
00071 
00072 #include "umutex.h"
00073 #include "cmemory.h"
00074 
00075 /* the global mutex. Use it proudly and wash it often. */
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) /* in the mutex -- possible deadlock*/
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 /* ICU_USE_THREADS==1 */
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; /* jitterbug 135, fix for multiprocessor machines */
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 /* ICU_USE_THREADS == 1 */
00138 }
00139 
00140 U_CAPI void umtx_init( UMTX *mutex )
00141 {
00142 #if (ICU_USE_THREADS == 1)
00143 
00144 if( mutex == NULL ) /* initialize the global mutex */
00145     {
00146       mutex = &gGlobalMutex;
00147     }
00148 
00149   if(*mutex != NULL) /* someone already did it. */
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 /* ICU_USE_THREADS==1 */
00173 }
00174 
00175 
00176 
00177 
00178 
00179 
00180 

Generated at Tue Dec 5 10:48:07 2000 for ICU by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000