1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2005-03-08 22:49:59 +00:00
|
|
|
* Copyright (C) 1997-2005, International Business Machines
|
2000-01-13 23:54:23 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File CMUTEX.C
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 04/02/97 aliu Creation.
|
|
|
|
* 04/07/99 srl updated
|
|
|
|
* 05/13/99 stephen Changed to umutex (from cmutex).
|
1999-11-22 16:24:43 +00:00
|
|
|
* 11/22/99 aliu Make non-global mutex autoinitialize [j151]
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
2005-02-17 00:19:44 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "uassert.h"
|
|
|
|
#include "ucln_cmn.h"
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/* Assume POSIX, and modify as necessary below */
|
|
|
|
#define POSIX
|
|
|
|
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined(U_WINDOWS)
|
1999-08-16 21:50:52 +00:00
|
|
|
#undef POSIX
|
|
|
|
#endif
|
|
|
|
#if defined(macintosh)
|
|
|
|
#undef POSIX
|
|
|
|
#endif
|
|
|
|
#if defined(OS2)
|
|
|
|
#undef POSIX
|
|
|
|
#endif
|
|
|
|
|
1999-12-09 23:11:48 +00:00
|
|
|
#if defined(POSIX) && (ICU_USE_THREADS==1)
|
1999-08-16 21:50:52 +00:00
|
|
|
# include <pthread.h> /* must be first, so that we get the multithread versions of things. */
|
|
|
|
|
1999-12-09 23:11:48 +00:00
|
|
|
#endif /* POSIX && (ICU_USE_THREADS==1) */
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2005-02-17 00:19:44 +00:00
|
|
|
#ifdef U_WINDOWS
|
2001-06-26 17:53:36 +00:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
2004-10-21 06:40:36 +00:00
|
|
|
# define VC_EXTRALEAN
|
2001-06-26 17:53:36 +00:00
|
|
|
# define NOUSER
|
|
|
|
# define NOSERVICE
|
|
|
|
# define NOIME
|
|
|
|
# define NOMCX
|
|
|
|
# include <windows.h>
|
1999-08-16 21:50:52 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "umutex.h"
|
1999-10-12 00:07:24 +00:00
|
|
|
#include "cmemory.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-28 00:00:00 +00:00
|
|
|
/*
|
|
|
|
* A note on ICU Mutex Initialization and ICU startup:
|
|
|
|
*
|
|
|
|
* ICU mutexes, as used through the rest of the ICU code, are self-initializing.
|
|
|
|
* To make this work, ICU uses the _ICU GLobal Mutex_ to synchronize the lazy init
|
|
|
|
* of other ICU mutexes. For the global mutex itself, we need some other mechanism
|
|
|
|
* to safely initialize it on first use. This becomes important if two or more
|
|
|
|
* threads were more or less simultaenously the first to use ICU in a process, and
|
|
|
|
* were racing into the mutex initialization code.
|
|
|
|
*
|
|
|
|
* The solution for the global mutex init is platform dependent.
|
|
|
|
* On POSIX systems, C-style init can be used on a mutex, with the
|
|
|
|
* macro PTHREAD_MUTEX_INITIALIZER. The mutex is then ready for use, without
|
|
|
|
* first calling pthread_mutex_init().
|
|
|
|
*
|
|
|
|
* Windows has no equivalent statically initialized mutex or CRITICAL SECION.
|
|
|
|
* InitializeCriticalSection() must be called. If the global mutex does not
|
|
|
|
* appear to be initialized, a thread will create and initialize a new
|
|
|
|
* CRITICAL_SECTION, then use a Windows InterlockedCompareAndExchange to
|
|
|
|
* avoid problems with race conditions.
|
|
|
|
*
|
|
|
|
* If an application has overridden the ICU mutex implementation
|
|
|
|
* by calling u_setMutexFunctions(), the user supplied init function must
|
|
|
|
* be safe in the event that multiple threads concurrently attempt to init
|
|
|
|
* the same mutex. The first thread should do the init, and the others should
|
|
|
|
* have no effect.
|
|
|
|
*
|
|
|
|
*/
|
2004-01-09 01:07:54 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
#define MAX_MUTEXES 30
|
|
|
|
static UMTX gGlobalMutex = NULL;
|
|
|
|
static UMTX gIncDecMutex = NULL;
|
2004-05-17 15:56:05 +00:00
|
|
|
#if (ICU_USE_THREADS == 1)
|
|
|
|
static UBool gMutexPoolInitialized = FALSE;
|
|
|
|
static char gMutexesInUse[MAX_MUTEXES];
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined(U_WINDOWS)
|
2004-06-04 21:14:48 +00:00
|
|
|
/*-------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* WINDOWS platform variable declarations
|
2004-06-04 16:12:21 +00:00
|
|
|
*
|
2004-06-04 21:14:48 +00:00
|
|
|
*-------------------------------------------------------------*/
|
|
|
|
static CRITICAL_SECTION gMutexes[MAX_MUTEXES];
|
|
|
|
static CRITICAL_SECTION gGlobalWinMutex;
|
|
|
|
|
|
|
|
|
|
|
|
/* On WIN32 mutexes are reentrant. This makes it difficult to debug
|
2004-06-04 16:12:21 +00:00
|
|
|
* deadlocking problems that show up on POSIXy platforms, where
|
2004-06-04 21:14:48 +00:00
|
|
|
* mutexes deadlock upon reentry. ICU contains checking code for
|
|
|
|
* the global mutex as well as for other mutexes in the pool.
|
2004-06-04 16:12:21 +00:00
|
|
|
*
|
|
|
|
* This is for debugging purposes.
|
|
|
|
*
|
|
|
|
* This has no effect on non-WIN32 platforms, non-DEBUG builds, and
|
|
|
|
* non-ICU_USE_THREADS builds.
|
|
|
|
*
|
|
|
|
* Note: The CRITICAL_SECTION structure already has a RecursionCount
|
|
|
|
* member that can be used for this purpose, but portability to
|
|
|
|
* Win98/NT/2K needs to be tested before use. Works fine on XP.
|
|
|
|
* After portability is confirmed, the built-in RecursionCount can be
|
|
|
|
* used, and the gRecursionCountPool can be removed.
|
|
|
|
*
|
2004-06-04 21:14:48 +00:00
|
|
|
* Note: Non-global mutex checking only happens if there is no custom
|
|
|
|
* pMutexLockFn defined. Use one function, not two (don't use
|
|
|
|
* pMutexLockFn and pMutexUnlockFn) so the increment and decrement of
|
|
|
|
* the recursion count don't get out of sync. Users might set just
|
|
|
|
* one function, e.g., to perform a custom action, followed by a
|
|
|
|
* standard call to EnterCriticalSection.
|
2004-06-04 16:12:21 +00:00
|
|
|
*/
|
2004-06-07 18:37:01 +00:00
|
|
|
#if defined(U_DEBUG) && (ICU_USE_THREADS==1)
|
2004-06-04 16:12:21 +00:00
|
|
|
static int32_t gRecursionCount = 0; /* detect global mutex locking */
|
2004-06-04 21:14:48 +00:00
|
|
|
static int32_t gRecursionCountPool[MAX_MUTEXES]; /* ditto for non-global */
|
2004-02-23 04:36:02 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-05-17 15:56:05 +00:00
|
|
|
#elif defined(POSIX)
|
2004-02-23 04:36:02 +00:00
|
|
|
/*-------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* POSIX platform variable declarations
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------*/
|
|
|
|
static pthread_mutex_t gMutexes[MAX_MUTEXES] = {
|
|
|
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER,
|
|
|
|
PTHREAD_MUTEX_INITIALIZER, PTHREAD_MUTEX_INITIALIZER
|
|
|
|
};
|
2004-05-17 15:56:05 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
/*-------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* UNKNOWN platform declarations
|
|
|
|
*
|
|
|
|
*-------------------------------------------------------------*/
|
2004-04-21 20:21:25 +00:00
|
|
|
static void *gMutexes[MAX_MUTEXES] = {
|
2004-11-11 23:34:58 +00:00
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
NULL, NULL };
|
2003-08-20 22:38:01 +00:00
|
|
|
|
2004-01-09 01:07:54 +00:00
|
|
|
/* Unknown platform. OK so long as ICU_USE_THREAD is not set.
|
|
|
|
Note that user can still set mutex functions at run time,
|
|
|
|
and that the global mutex variable is still needed in that case. */
|
|
|
|
#if (ICU_USE_THREADS == 1)
|
|
|
|
#error no ICU mutex implementation for this platform
|
|
|
|
#endif
|
|
|
|
#endif
|
2004-05-17 15:56:05 +00:00
|
|
|
#endif /* ICU_USE_THREADS==1 */
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
/*
|
2003-08-08 23:56:02 +00:00
|
|
|
* User mutex implementation functions. If non-null, call back to these rather than
|
2003-08-05 01:25:54 +00:00
|
|
|
* directly using the system (Posix or Windows) APIs.
|
|
|
|
* (declarations are in uclean.h)
|
|
|
|
*/
|
2003-08-14 21:34:54 +00:00
|
|
|
static UMtxInitFn *pMutexInitFn = NULL;
|
|
|
|
static UMtxFn *pMutexDestroyFn = NULL;
|
|
|
|
static UMtxFn *pMutexLockFn = NULL;
|
|
|
|
static UMtxFn *pMutexUnlockFn = NULL;
|
2003-08-20 22:38:01 +00:00
|
|
|
static const void *gMutexContext = NULL;
|
2001-07-03 20:35:27 +00:00
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2003-08-08 23:56:02 +00:00
|
|
|
* umtx_lock
|
2003-08-05 01:25:54 +00:00
|
|
|
*/
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
umtx_lock(UMTX *mutex)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-08 23:56:02 +00:00
|
|
|
if (mutex == NULL) {
|
2001-07-03 20:35:27 +00:00
|
|
|
mutex = &gGlobalMutex;
|
1999-11-22 16:24:43 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-08 23:56:02 +00:00
|
|
|
if (*mutex == NULL) {
|
2003-08-20 22:38:01 +00:00
|
|
|
/* Lock of an uninitialized mutex. Initialize it before proceeding. */
|
|
|
|
umtx_init(mutex);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pMutexLockFn != NULL) {
|
|
|
|
(*pMutexLockFn)(gMutexContext, mutex);
|
2003-08-05 01:25:54 +00:00
|
|
|
} else {
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
#if (ICU_USE_THREADS == 1)
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined(U_WINDOWS)
|
2003-08-05 01:25:54 +00:00
|
|
|
EnterCriticalSection((CRITICAL_SECTION*) *mutex);
|
1999-08-16 21:50:52 +00:00
|
|
|
#elif defined(POSIX)
|
2003-08-08 16:23:38 +00:00
|
|
|
pthread_mutex_lock((pthread_mutex_t*) *mutex);
|
2003-08-05 01:25:54 +00:00
|
|
|
#endif /* cascade of platforms */
|
1999-12-09 23:11:48 +00:00
|
|
|
#endif /* ICU_USE_THREADS==1 */
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
|
|
|
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined(U_WINDOWS) && defined(U_DEBUG) && (ICU_USE_THREADS==1)
|
2004-05-17 15:56:05 +00:00
|
|
|
if (mutex == &gGlobalMutex) { /* Detect Reentrant locking of the global mutex. */
|
|
|
|
gRecursionCount++; /* Recursion causes deadlocks on Unixes. */
|
|
|
|
U_ASSERT(gRecursionCount == 1); /* Detection works on Windows. Debug problems there. */
|
|
|
|
}
|
2004-06-04 16:12:21 +00:00
|
|
|
/* This handles gGlobalMutex too, but only if there is no pMutexLockFn */
|
|
|
|
else if (pMutexLockFn == NULL) { /* see comments above */
|
2004-12-08 01:05:40 +00:00
|
|
|
size_t i = ((CRITICAL_SECTION*)*mutex) - &gMutexes[0];
|
2004-06-04 16:12:21 +00:00
|
|
|
U_ASSERT(i >= 0 && i < MAX_MUTEXES);
|
|
|
|
++gRecursionCountPool[i];
|
|
|
|
|
|
|
|
U_ASSERT(gRecursionCountPool[i] == 1); /* !Detect Deadlock! */
|
|
|
|
|
|
|
|
/* This works and is fast, but needs testing on Win98/NT/2K.
|
|
|
|
See comments above. [alan]
|
|
|
|
U_ASSERT((CRITICAL_SECTION*)*mutex >= &gMutexes[0] &&
|
|
|
|
(CRITICAL_SECTION*)*mutex <= &gMutexes[MAX_MUTEXES]);
|
|
|
|
U_ASSERT(((CRITICAL_SECTION*)*mutex)->RecursionCount == 1);
|
|
|
|
*/
|
|
|
|
}
|
2004-06-07 18:37:01 +00:00
|
|
|
#endif /*U_DEBUG*/
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* umtx_unlock
|
|
|
|
*/
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
umtx_unlock(UMTX* mutex)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-20 22:38:01 +00:00
|
|
|
if(mutex == NULL) {
|
2001-07-03 20:35:27 +00:00
|
|
|
mutex = &gGlobalMutex;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-08-20 22:38:01 +00:00
|
|
|
if(*mutex == NULL) {
|
2004-06-08 03:19:44 +00:00
|
|
|
#if (ICU_USE_THREADS == 1)
|
2003-08-08 16:23:38 +00:00
|
|
|
U_ASSERT(FALSE); /* This mutex is not initialized. */
|
2004-06-08 03:19:44 +00:00
|
|
|
#endif
|
2003-08-08 16:23:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined (U_WINDOWS) && defined (U_DEBUG) && (ICU_USE_THREADS==1)
|
2003-08-08 16:23:38 +00:00
|
|
|
if (mutex == &gGlobalMutex) {
|
|
|
|
gRecursionCount--;
|
|
|
|
U_ASSERT(gRecursionCount == 0); /* Detect unlock of an already unlocked mutex */
|
2001-07-03 20:35:27 +00:00
|
|
|
}
|
2004-06-04 16:12:21 +00:00
|
|
|
/* This handles gGlobalMutex too, but only if there is no pMutexLockFn */
|
|
|
|
else if (pMutexLockFn == NULL) { /* see comments above */
|
2004-12-08 01:05:40 +00:00
|
|
|
size_t i = ((CRITICAL_SECTION*)*mutex) - &gMutexes[0];
|
2004-06-04 16:12:21 +00:00
|
|
|
U_ASSERT(i >= 0 && i < MAX_MUTEXES);
|
|
|
|
--gRecursionCountPool[i];
|
|
|
|
|
|
|
|
U_ASSERT(gRecursionCountPool[i] == 0); /* !Detect Deadlock! */
|
|
|
|
|
|
|
|
/* This works and is fast, but needs testing on Win98/NT/2K.
|
|
|
|
Note that RecursionCount will be 1, not 0, since we haven't
|
|
|
|
left the CRITICAL_SECTION yet. See comments above. [alan]
|
|
|
|
U_ASSERT((CRITICAL_SECTION*)*mutex >= &gMutexes[0] &&
|
|
|
|
(CRITICAL_SECTION*)*mutex <= &gMutexes[MAX_MUTEXES]);
|
|
|
|
U_ASSERT(((CRITICAL_SECTION*)*mutex)->RecursionCount == 1);
|
|
|
|
*/
|
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pMutexUnlockFn) {
|
|
|
|
(*pMutexUnlockFn)(gMutexContext, mutex);
|
2003-08-05 01:25:54 +00:00
|
|
|
} else {
|
2003-08-08 16:23:38 +00:00
|
|
|
#if (ICU_USE_THREADS==1)
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined (U_WINDOWS)
|
2003-08-08 16:23:38 +00:00
|
|
|
LeaveCriticalSection((CRITICAL_SECTION*)*mutex);
|
2001-07-03 20:35:27 +00:00
|
|
|
#elif defined (POSIX)
|
2003-08-08 16:23:38 +00:00
|
|
|
pthread_mutex_unlock((pthread_mutex_t*)*mutex);
|
2003-08-05 01:25:54 +00:00
|
|
|
#endif /* cascade of platforms */
|
1999-12-09 23:11:48 +00:00
|
|
|
#endif /* ICU_USE_THREADS == 1 */
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-20 22:38:01 +00:00
|
|
|
/*
|
|
|
|
* initGlobalMutex Do the platform specific initialization of the ICU global mutex.
|
|
|
|
* Separated out from the other mutexes because it is different:
|
|
|
|
* Mutex storage is static for POSIX, init must be thread safe
|
|
|
|
* without the use of another mutex.
|
|
|
|
*/
|
|
|
|
static void initGlobalMutex() {
|
|
|
|
/*
|
2004-02-23 04:36:02 +00:00
|
|
|
* If User Supplied mutex functions are in use
|
|
|
|
* init the icu global mutex using them.
|
2003-08-20 22:38:01 +00:00
|
|
|
*/
|
|
|
|
if (pMutexInitFn != NULL) {
|
2003-08-28 00:00:00 +00:00
|
|
|
if (gGlobalMutex==NULL) {
|
2003-08-20 22:38:01 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
(*pMutexInitFn)(gMutexContext, &gGlobalMutex, &status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
/* TODO: how should errors here be handled? */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-20 22:38:01 +00:00
|
|
|
/* No user override of mutex functions.
|
|
|
|
* Use default ICU mutex implementations.
|
|
|
|
*/
|
|
|
|
#if (ICU_USE_THREADS == 1)
|
2004-02-23 04:36:02 +00:00
|
|
|
/*
|
|
|
|
* for Windows, init the pool of critical sections that we
|
|
|
|
* will use as needed for ICU mutexes.
|
|
|
|
*/
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined (U_WINDOWS)
|
2004-02-23 04:36:02 +00:00
|
|
|
if (gMutexPoolInitialized == FALSE) {
|
2004-05-17 15:56:05 +00:00
|
|
|
int i;
|
2004-02-23 04:36:02 +00:00
|
|
|
for (i=0; i<MAX_MUTEXES; i++) {
|
|
|
|
InitializeCriticalSection(&gMutexes[i]);
|
2004-06-07 18:37:01 +00:00
|
|
|
#if defined (U_DEBUG)
|
2004-06-04 16:12:21 +00:00
|
|
|
gRecursionCountPool[i] = 0; /* see comments above */
|
2004-06-07 18:37:01 +00:00
|
|
|
#endif
|
2004-02-23 04:36:02 +00:00
|
|
|
}
|
2004-05-17 15:56:05 +00:00
|
|
|
gMutexPoolInitialized = TRUE;
|
2004-02-23 04:36:02 +00:00
|
|
|
}
|
|
|
|
#elif defined (POSIX)
|
|
|
|
/* TODO: experimental code. Shouldn't need to explicitly init the mutexes. */
|
|
|
|
if (gMutexPoolInitialized == FALSE) {
|
2004-05-17 15:56:05 +00:00
|
|
|
int i;
|
2004-02-23 04:36:02 +00:00
|
|
|
for (i=0; i<MAX_MUTEXES; i++) {
|
|
|
|
pthread_mutex_init(&gMutexes[i], NULL);
|
2003-06-07 00:03:51 +00:00
|
|
|
}
|
2004-05-17 15:56:05 +00:00
|
|
|
gMutexPoolInitialized = TRUE;
|
2003-08-20 22:38:01 +00:00
|
|
|
}
|
2004-05-17 15:56:05 +00:00
|
|
|
#endif
|
2004-02-23 04:36:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* for both Windows & POSIX, the first mutex in the array is used
|
|
|
|
* for the ICU global mutex.
|
|
|
|
*/
|
|
|
|
gGlobalMutex = &gMutexes[0];
|
|
|
|
gMutexesInUse[0] = 1;
|
|
|
|
|
2003-08-20 22:38:01 +00:00
|
|
|
#else /* ICU_USE_THREADS */
|
2004-05-17 15:56:05 +00:00
|
|
|
gGlobalMutex = &gGlobalMutex; /* With no threads, we must still set the mutex to
|
|
|
|
* some non-null value to make the rest of the
|
|
|
|
* (not ifdefed) mutex code think that it is initialized.
|
|
|
|
*/
|
2003-08-20 22:38:01 +00:00
|
|
|
#endif /* ICU_USE_THREADS */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
|
2003-06-08 23:01:42 +00:00
|
|
|
|
2003-08-20 22:38:01 +00:00
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
umtx_init(UMTX *mutex)
|
|
|
|
{
|
|
|
|
if (mutex == NULL || mutex == &gGlobalMutex) {
|
|
|
|
initGlobalMutex();
|
2003-06-07 00:03:51 +00:00
|
|
|
} else {
|
|
|
|
umtx_lock(NULL);
|
2004-02-23 04:36:02 +00:00
|
|
|
if (*mutex != NULL) {
|
|
|
|
/* Another thread initialized this mutex first. */
|
|
|
|
umtx_unlock(NULL);
|
2003-06-07 00:03:51 +00:00
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
if (pMutexInitFn != NULL) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
(*pMutexInitFn)(gMutexContext, mutex, &status);
|
|
|
|
/* TODO: how to report failure on init? */
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
return;
|
|
|
|
}
|
2004-05-17 15:56:05 +00:00
|
|
|
else {
|
|
|
|
#if (ICU_USE_THREADS == 1)
|
|
|
|
/* Search through our pool of pre-allocated mutexes for one that is not
|
|
|
|
* already in use. */
|
|
|
|
int i;
|
|
|
|
for (i=0; i<MAX_MUTEXES; i++) {
|
|
|
|
if (gMutexesInUse[i] == 0) {
|
|
|
|
gMutexesInUse[i] = 1;
|
|
|
|
*mutex = &gMutexes[i];
|
|
|
|
break;
|
|
|
|
}
|
2004-02-23 04:36:02 +00:00
|
|
|
}
|
2004-05-17 15:56:05 +00:00
|
|
|
#endif
|
2003-06-07 00:03:51 +00:00
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2004-06-03 18:43:48 +00:00
|
|
|
#if (ICU_USE_THREADS == 1)
|
2004-02-23 04:36:02 +00:00
|
|
|
/* No more mutexes were available from our pre-allocated pool. */
|
|
|
|
/* TODO: how best to deal with this? */
|
|
|
|
U_ASSERT(*mutex != NULL);
|
2004-06-03 18:43:48 +00:00
|
|
|
#endif
|
2003-06-07 00:03:51 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* umtx_destroy. Un-initialize a mutex, releasing any underlying resources
|
|
|
|
* that it may be holding. Destroying an already destroyed
|
|
|
|
* mutex has no effect. Unlike umtx_init(), this function
|
|
|
|
* is not thread safe; two threads must not concurrently try to
|
|
|
|
* destroy the same mutex.
|
|
|
|
*/
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
umtx_destroy(UMTX *mutex) {
|
2003-08-08 23:56:02 +00:00
|
|
|
if (mutex == NULL) { /* destroy the global mutex */
|
2001-07-03 20:35:27 +00:00
|
|
|
mutex = &gGlobalMutex;
|
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2003-08-08 23:56:02 +00:00
|
|
|
if (*mutex == NULL) { /* someone already did it. */
|
2001-07-03 20:35:27 +00:00
|
|
|
return;
|
2003-08-08 23:56:02 +00:00
|
|
|
}
|
2003-08-20 22:38:01 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
/* The life of the inc/dec mutex is tied to that of the global mutex. */
|
2003-08-20 22:38:01 +00:00
|
|
|
if (mutex == &gGlobalMutex) {
|
|
|
|
umtx_destroy(&gIncDecMutex);
|
|
|
|
}
|
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pMutexDestroyFn != NULL) {
|
2004-02-23 04:36:02 +00:00
|
|
|
/* Mutexes are being managed by the app. Call back to it for the destroy. */
|
2003-08-14 21:34:54 +00:00
|
|
|
(*pMutexDestroyFn)(gMutexContext, mutex);
|
2004-05-17 15:56:05 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
#if (ICU_USE_THREADS == 1)
|
2004-02-23 04:36:02 +00:00
|
|
|
/* Return this mutex to the pool of available mutexes, if it came from the
|
|
|
|
* pool in the first place.
|
|
|
|
*/
|
2004-06-04 16:12:21 +00:00
|
|
|
/* TODO use pointer math here, instead of iterating! */
|
2004-02-23 04:36:02 +00:00
|
|
|
int i;
|
|
|
|
for (i=0; i<MAX_MUTEXES; i++) {
|
|
|
|
if (*mutex == &gMutexes[i]) {
|
|
|
|
gMutexesInUse[i] = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-05-17 15:56:05 +00:00
|
|
|
#endif
|
2001-07-03 20:35:27 +00:00
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
*mutex = NULL;
|
2001-07-03 20:35:27 +00:00
|
|
|
}
|
2002-03-09 00:36:54 +00:00
|
|
|
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
2003-08-14 21:34:54 +00:00
|
|
|
u_setMutexFunctions(const void *context, UMtxInitFn *i, UMtxFn *d, UMtxFn *l, UMtxFn *u,
|
2003-08-05 01:25:54 +00:00
|
|
|
UErrorCode *status) {
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return;
|
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
|
|
|
|
/* Can not set a mutex function to a NULL value */
|
2003-08-05 01:25:54 +00:00
|
|
|
if (i==NULL || d==NULL || l==NULL || u==NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If ICU is not in an initial state, disallow this operation. */
|
|
|
|
if (cmemory_inUse()) {
|
|
|
|
*status = U_INVALID_STATE_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Swap in the mutex function pointers. */
|
2003-08-14 21:34:54 +00:00
|
|
|
pMutexInitFn = i;
|
|
|
|
pMutexDestroyFn = d;
|
|
|
|
pMutexLockFn = l;
|
|
|
|
pMutexUnlockFn = u;
|
|
|
|
gMutexContext = context;
|
2003-08-20 22:38:01 +00:00
|
|
|
gGlobalMutex = NULL; /* For POSIX, the global mutex will be pre-initialized */
|
|
|
|
/* Undo that, force re-initialization when u_init() */
|
|
|
|
/* happens. */
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
|
|
|
|
2002-03-09 00:36:54 +00:00
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
|
|
|
|
/*-----------------------------------------------------------------
|
|
|
|
*
|
2003-08-08 23:56:02 +00:00
|
|
|
* Atomic Increment and Decrement
|
|
|
|
* umtx_atomic_inc
|
|
|
|
* umtx_atomic_dec
|
2003-08-08 16:23:38 +00:00
|
|
|
*
|
|
|
|
*----------------------------------------------------------------*/
|
2002-03-11 23:39:43 +00:00
|
|
|
|
2004-01-09 01:07:54 +00:00
|
|
|
/* Pointers to user-supplied inc/dec functions. Null if no funcs have been set. */
|
2003-08-14 21:34:54 +00:00
|
|
|
static UMtxAtomicFn *pIncFn = NULL;
|
|
|
|
static UMtxAtomicFn *pDecFn = NULL;
|
2005-06-14 05:35:06 +00:00
|
|
|
static const void *gIncDecContext = NULL;
|
2002-03-09 00:36:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2003-08-08 23:56:02 +00:00
|
|
|
umtx_atomic_inc(int32_t *p) {
|
|
|
|
int32_t retVal;
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pIncFn) {
|
|
|
|
retVal = (*pIncFn)(gIncDecContext, p);
|
2003-08-08 23:56:02 +00:00
|
|
|
} else {
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined (U_WINDOWS) && ICU_USE_THREADS == 1
|
2004-10-21 06:40:36 +00:00
|
|
|
retVal = InterlockedIncrement((LONG*)p);
|
2003-08-08 23:56:02 +00:00
|
|
|
#elif defined (POSIX) && ICU_USE_THREADS == 1
|
2003-08-20 22:38:01 +00:00
|
|
|
umtx_lock(&gIncDecMutex);
|
2003-08-08 23:56:02 +00:00
|
|
|
retVal = ++(*p);
|
2003-08-20 22:38:01 +00:00
|
|
|
umtx_unlock(&gIncDecMutex);
|
2003-08-08 23:56:02 +00:00
|
|
|
#else
|
|
|
|
/* Unknown Platform, or ICU thread support compiled out. */
|
|
|
|
retVal = ++(*p);
|
|
|
|
#endif
|
|
|
|
}
|
2002-03-09 00:36:54 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
U_CAPI int32_t U_EXPORT2
|
2003-08-08 23:56:02 +00:00
|
|
|
umtx_atomic_dec(int32_t *p) {
|
|
|
|
int32_t retVal;
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pDecFn) {
|
|
|
|
retVal = (*pDecFn)(gIncDecContext, p);
|
2003-08-08 23:56:02 +00:00
|
|
|
} else {
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined (U_WINDOWS) && ICU_USE_THREADS == 1
|
2004-10-21 06:40:36 +00:00
|
|
|
retVal = InterlockedDecrement((LONG*)p);
|
2003-08-08 23:56:02 +00:00
|
|
|
#elif defined (POSIX) && ICU_USE_THREADS == 1
|
2003-08-20 22:38:01 +00:00
|
|
|
umtx_lock(&gIncDecMutex);
|
2003-08-08 23:56:02 +00:00
|
|
|
retVal = --(*p);
|
2003-08-20 22:38:01 +00:00
|
|
|
umtx_unlock(&gIncDecMutex);
|
2003-08-08 23:56:02 +00:00
|
|
|
#else
|
|
|
|
/* Unknown Platform, or ICU thread support compiled out. */
|
|
|
|
retVal = --(*p);
|
|
|
|
#endif
|
|
|
|
}
|
2002-03-09 00:36:54 +00:00
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
/* TODO: Some POSIXy platforms have atomic inc/dec functions available. Use them. */
|
2002-03-09 00:36:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-03 00:52:13 +00:00
|
|
|
|
2002-03-09 00:36:54 +00:00
|
|
|
|
2003-08-08 23:56:02 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
2003-08-14 21:34:54 +00:00
|
|
|
u_setAtomicIncDecFunctions(const void *context, UMtxAtomicFn *ip, UMtxAtomicFn *dp,
|
2003-08-08 23:56:02 +00:00
|
|
|
UErrorCode *status) {
|
|
|
|
int32_t testInt;
|
|
|
|
if (U_FAILURE(*status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Can not set a mutex function to a NULL value */
|
|
|
|
if (ip==NULL || dp==NULL) {
|
|
|
|
*status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* If ICU is not in an initial state, disallow this operation. */
|
|
|
|
if (cmemory_inUse()) {
|
|
|
|
*status = U_INVALID_STATE_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2002-03-09 00:36:54 +00:00
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
pIncFn = ip;
|
|
|
|
pDecFn = dp;
|
2005-06-14 05:35:06 +00:00
|
|
|
gIncDecContext = context;
|
2003-08-08 23:56:02 +00:00
|
|
|
|
|
|
|
testInt = 0;
|
|
|
|
U_ASSERT(umtx_atomic_inc(&testInt) == 1); /* Sanity Check. Do the functions work at all? */
|
|
|
|
U_ASSERT(testInt == 1);
|
|
|
|
U_ASSERT(umtx_atomic_dec(&testInt) == 0);
|
|
|
|
U_ASSERT(testInt == 0);
|
2002-03-09 00:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-08 23:56:02 +00:00
|
|
|
/*
|
|
|
|
* Mutex Cleanup Function
|
|
|
|
*
|
|
|
|
* Destroy the global mutex(es), and reset the mutex function callback pointers.
|
|
|
|
*/
|
|
|
|
U_CFUNC UBool umtx_cleanup(void) {
|
|
|
|
umtx_destroy(NULL);
|
2003-08-14 21:34:54 +00:00
|
|
|
pMutexInitFn = NULL;
|
|
|
|
pMutexDestroyFn = NULL;
|
|
|
|
pMutexLockFn = NULL;
|
|
|
|
pMutexUnlockFn = NULL;
|
|
|
|
gMutexContext = NULL;
|
2003-08-20 22:38:01 +00:00
|
|
|
gGlobalMutex = NULL;
|
2004-05-17 15:56:05 +00:00
|
|
|
pIncFn = NULL;
|
|
|
|
pDecFn = NULL;
|
2005-06-14 05:35:06 +00:00
|
|
|
gIncDecContext = NULL;
|
2004-02-23 04:36:02 +00:00
|
|
|
gIncDecMutex = NULL;
|
|
|
|
|
2004-05-17 15:56:05 +00:00
|
|
|
#if (ICU_USE_THREADS == 1)
|
2004-02-23 04:36:02 +00:00
|
|
|
if (gMutexPoolInitialized) {
|
2004-05-17 15:56:05 +00:00
|
|
|
int i;
|
2004-02-23 04:36:02 +00:00
|
|
|
for (i=0; i<MAX_MUTEXES; i++) {
|
2004-05-17 15:56:05 +00:00
|
|
|
if (gMutexesInUse[i]) {
|
2005-02-17 00:19:44 +00:00
|
|
|
#if defined (U_WINDOWS)
|
2004-05-17 15:56:05 +00:00
|
|
|
DeleteCriticalSection(&gMutexes[i]);
|
2004-02-23 04:36:02 +00:00
|
|
|
#elif defined (POSIX)
|
2004-05-17 15:56:05 +00:00
|
|
|
pthread_mutex_destroy(&gMutexes[i]);
|
|
|
|
#endif
|
|
|
|
gMutexesInUse[i] = 0;
|
|
|
|
}
|
2004-05-13 04:21:56 +00:00
|
|
|
}
|
2004-02-23 04:36:02 +00:00
|
|
|
}
|
|
|
|
gMutexPoolInitialized = FALSE;
|
|
|
|
#endif
|
2003-08-08 23:56:02 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-03-09 00:36:54 +00:00
|
|
|
|
|
|
|
|