home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / umutex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-09  |  3.9 KB  |  173 lines

  1. /*
  2. *******************************************************************************
  3. *                                                                             *
  4. * COPYRIGHT:                                                                  *
  5. *   (C) Copyright Taligent, Inc.,  1997                                       *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1999     *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.        *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure   *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                    *
  10. *                                                                             *
  11. *******************************************************************************
  12. *
  13. * File CMUTEX.C
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   04/02/97    aliu        Creation.
  19. *   04/07/99    srl         updated
  20. *   05/13/99    stephen     Changed to umutex (from cmutex).
  21. *******************************************************************************
  22. */
  23.  
  24. /* Assume POSIX, and modify as necessary below */
  25. #define POSIX
  26.  
  27. #if defined(_WIN32)
  28. #undef POSIX
  29. #endif
  30. #if defined(macintosh)
  31. #undef POSIX
  32. #endif
  33. #if defined(OS2)
  34. #undef POSIX
  35. #endif
  36. #if defined(__amigaos__)
  37. #undef POSIX
  38. #endif
  39.  
  40.  
  41. #if defined(POSIX) && !defined(APP_NO_THREADS)
  42.   /* Usage: uncomment the following, and breakpoint WeAreDeadlocked to
  43.      find reentrant issues. */
  44. /* # define POSIX_DEBUG_REENTRANCY 1 */
  45. # include <pthread.h> /* must be first, so that we get the multithread versions of things. */
  46.  
  47. # ifdef POSIX_DEBUG_REENTRANCY
  48.  pthread_t      gLastThread;
  49.  bool_t         gInMutex;
  50.  
  51.  U_EXPORT void WeAreDeadlocked();
  52.  
  53.  void WeAreDeadlocked()
  54.  {
  55.     puts("ARGH!! We're deadlocked.. break on WeAreDeadlocked() next time.");
  56.  }
  57. # endif /* POSIX_DEBUG_REENTRANCY */
  58. #endif /* POSIX && APP_NO_THREADS not defined */
  59.  
  60. #ifdef _WIN32
  61. # include <WINDOWS.H>
  62. #endif
  63.  
  64. #include "umutex.h"
  65. #include "cmemory.h"
  66.  
  67. /* the global mutex. Use it proudly and wash it often. */
  68. UMTX    gGlobalMutex = NULL;
  69.  
  70. void umtx_lock( UMTX *mutex )
  71. {
  72. #ifndef APP_NO_THREADS
  73.   if( mutex == NULL )
  74.     {
  75.       mutex = &gGlobalMutex;
  76.  
  77.       if(*mutex == NULL)
  78.         {
  79.           umtx_init(NULL);
  80.         }
  81.     }
  82.  
  83. #if defined(_WIN32)
  84.  
  85.     EnterCriticalSection((CRITICAL_SECTION*) *mutex);
  86.  
  87. #elif defined(POSIX)
  88.  
  89. #  ifdef POSIX_DEBUG_REENTRANCY
  90.     if(gInMutex == TRUE) // in the mutex -- possible deadlock
  91.       if(pthread_equal(gLastThread,pthread_self()))
  92.         WeAreDeadlocked();
  93. #  endif
  94.  
  95.     pthread_mutex_lock((pthread_mutex_t*) *mutex);
  96.  
  97. #  ifdef POSIX_DEBUG_REENTRANCY
  98.     gLastThread = pthread_self();
  99.     gInMutex = TRUE;
  100. #  endif
  101. #endif
  102. #endif /* APP_NO_THREADS not defined */
  103. }
  104.  
  105. void umtx_unlock( UMTX* mutex )
  106. {
  107. #ifndef APP_NO_THREADS
  108.   if( mutex == NULL )
  109.     {
  110.       mutex = &gGlobalMutex;
  111.  
  112.       if(*mutex == NULL)
  113.         {
  114.           umtx_init(NULL);
  115.         }
  116.     }
  117.  
  118. #if defined(_WIN32)
  119.  
  120.     LeaveCriticalSection((CRITICAL_SECTION*)*mutex);
  121.  
  122. #elif defined(POSIX)
  123.     pthread_mutex_unlock((pthread_mutex_t*)*mutex);
  124. #ifdef POSIX_DEBUG_REENTRANCY
  125.     gInMutex = FALSE;
  126. #endif
  127.  
  128. #endif
  129. #endif /* APP_NO_THREADS not defined */
  130. }
  131.  
  132. U_CAPI void umtx_init( UMTX *mutex )
  133. {
  134. #ifndef APP_NO_THREADS
  135.  
  136. if( mutex == NULL ) /* initialize the global mutex */
  137.     {
  138.       mutex = &gGlobalMutex;
  139.     }
  140.  
  141.   if(*mutex != NULL) /* someone already did it. */
  142.     return;
  143.  
  144. #if defined( _WIN32 )
  145.   *mutex = icu_malloc(sizeof(CRITICAL_SECTION));
  146.   InitializeCriticalSection((CRITICAL_SECTION*)*mutex);
  147.  
  148. #elif defined( POSIX )
  149.  
  150.   *mutex = icu_malloc(sizeof(pthread_mutex_t));
  151.  
  152. #if defined(HPUX)
  153.     pthread_mutex_init((pthread_mutex_t*)*mutex, pthread_mutexattr_default);
  154. #else
  155.     pthread_mutex_init((pthread_mutex_t*)*mutex,NULL);
  156. #endif
  157.  
  158.  
  159. # ifdef POSIX_DEBUG_REENTRANCY
  160.     gInMutex = FALSE;
  161. # endif
  162.  
  163. #endif
  164. #endif /* APP_NO_THREADS not defined */
  165. }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.