1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2009-07-09 22:50:01 +00:00
|
|
|
* Copyright (C) 1997-2009, 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
|
|
|
*
|
2009-09-21 04:21:41 +00:00
|
|
|
* File umutex.c
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
/*
|
|
|
|
* ICU Mutex wrappers. Wrap operating system mutexes, giving the rest of ICU a
|
|
|
|
* platform independent set of mutex operations. For internal ICU use only.
|
|
|
|
*/
|
|
|
|
|
2005-09-23 21:12:34 +00:00
|
|
|
#if defined(U_DARWIN)
|
|
|
|
#include <AvailabilityMacros.h>
|
|
|
|
#if (ICU_USE_THREADS == 1) && defined(MAC_OS_X_VERSION_10_4) && defined(MAC_OS_X_VERSION_MIN_REQUIRED) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_4)
|
2008-03-12 21:48:14 +00:00
|
|
|
#if defined(__STRICT_ANSI__)
|
|
|
|
#define UPRV_REMAP_INLINE
|
|
|
|
#define inline
|
|
|
|
#endif
|
2005-09-23 21:12:34 +00:00
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
#define USE_MAC_OS_ATOMIC_INCREMENT 1
|
2008-03-12 21:48:14 +00:00
|
|
|
#if defined(UPRV_REMAP_INLINE)
|
|
|
|
#undef inline
|
|
|
|
#undef UPRV_REMAP_INLINE
|
|
|
|
#endif
|
2005-09-23 21:12:34 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
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
|
2009-09-21 04:21:41 +00:00
|
|
|
* to safely initialize it on first use. This becomes important when two or more
|
|
|
|
* threads are more or less simultaenously the first to use ICU in a process, and
|
|
|
|
* are racing into the mutex initialization code.
|
|
|
|
*
|
2003-08-28 00:00:00 +00:00
|
|
|
*
|
|
|
|
* The solution for the global mutex init is platform dependent.
|
2009-09-21 04:21:41 +00:00
|
|
|
* On POSIX systems, plain C-style initialization can be used on a mutex, with the
|
2003-08-28 00:00:00 +00:00
|
|
|
* 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
|
2009-09-21 04:21:41 +00:00
|
|
|
* swap it in as the global mutex while avoid problems with race conditions.
|
2003-08-28 00:00:00 +00:00
|
|
|
*/
|
2004-01-09 01:07:54 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
/* On WIN32 mutexes are reentrant. On POSIX platforms they are not, and a deadlock
|
|
|
|
* will occur if a thread attempts to acquire a mutex it already has locked.
|
|
|
|
* ICU mutexes (in debug builds) include checking code that will cause an assertion
|
|
|
|
* failure if a mutex is reentered. If you are having deadlock problems
|
|
|
|
* on a POSIX machine, debugging may be easier on Windows.
|
|
|
|
*/
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2004-06-04 21:14:48 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
#if (ICU_USE_THREADS == 0)
|
|
|
|
#define MUTEX_TYPE void *
|
|
|
|
#define PLATFORM_MUTEX_INIT(m)
|
|
|
|
#define PLATFORM_MUTEX_LOCK(m)
|
|
|
|
#define PLATFORM_MUTEX_UNLOCK(m)
|
|
|
|
#define PLATFORM_MUTEX_DESTROY(m)
|
|
|
|
#define PLATFORM_MUTEX_INITIALIZER NULL
|
|
|
|
#define SYNC_COMPARE_AND_SWAP(dest, oldval, newval) \
|
|
|
|
mutexed_compare_and_swap(dest, newval, oldval)
|
2004-06-04 21:14:48 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
#elif defined(U_WINDOWS)
|
|
|
|
#define MUTEX_TYPE CRITICAL_SECTION
|
|
|
|
#define PLATFORM_MUTEX_INIT(m) InitializeCriticalSection(m)
|
|
|
|
#define PLATFORM_MUTEX_LOCK(m) EnterCriticalSection(m)
|
|
|
|
#define PLATFORM_MUTEX_UNLOCK(m) LeaveCriticalSection(m)
|
|
|
|
#define PLATFORM_MUTEX_DESTROY(m) DeleteCriticalSection(m)
|
|
|
|
#define SYNC_COMPARE_AND_SWAP(dest, oldval, newval) \
|
|
|
|
InterlockedCompareExchangePointer(dest, newval, oldval)
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2004-05-17 15:56:05 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
#elif defined(POSIX)
|
|
|
|
#define MUTEX_TYPE pthread_mutex_t
|
|
|
|
#define PLATFORM_MUTEX_INIT(m) pthread_mutex_init(m, NULL)
|
|
|
|
#define PLATFORM_MUTEX_LOCK(m) pthread_mutex_lock(m)
|
|
|
|
#define PLATFORM_MUTEX_UNLOCK(m) pthread_mutex_unlock(m)
|
|
|
|
#define PLATFORM_MUTEX_DESTROY(m) pthread_mutex_destroy(m)
|
|
|
|
#define PLATFORM_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
|
|
|
|
#if (U_HAVE_GCC_ATOMICS == 1)
|
|
|
|
#define SYNC_COMPARE_AND_SWAP(dest, oldval, newval) \
|
|
|
|
__sync_val_compare_and_swap(dest, oldval, newval)
|
|
|
|
#else
|
|
|
|
#define SYNC_COMPARE_AND_SWAP(dest, oldval, newval) \
|
|
|
|
mutexed_compare_and_swap(dest, newval, oldval)
|
2004-01-09 01:07:54 +00:00
|
|
|
#endif
|
2009-09-21 04:21:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Unknown platform. Note that user can still set mutex functions at run time. */
|
|
|
|
#define MUTEX_TYPE void *
|
|
|
|
#define PLATFORM_MUTEX_INIT(m)
|
|
|
|
#define PLATFORM_MUTEX_LOCK(m)
|
|
|
|
#define PLATFORM_MUTEX_UNLOCK(m)
|
|
|
|
#define PLATFORM_MUTEX_DESTROY(m)
|
|
|
|
#define SYNC_COMPARE_AND_SWAP(dest, oldval, newval) \
|
|
|
|
mutexed_compare_and_swap(dest, newval, oldval)
|
|
|
|
|
2004-01-09 01:07:54 +00:00
|
|
|
#endif
|
2009-09-21 04:21:41 +00:00
|
|
|
|
|
|
|
/* Forward declarations */
|
|
|
|
static void *mutexed_compare_and_swap(void **dest, void *newval, void *oldval);
|
|
|
|
typedef struct ICUMutex ICUMutex;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ICUMutex One of these is set up for each UMTX that is used by other ICU code.
|
|
|
|
* The opaque UMTX points to the corresponding ICUMutex struct.
|
|
|
|
*
|
|
|
|
* Because the total number of ICU mutexes is quite small, no effort has
|
|
|
|
* been made to squeeze every byte out of this struct.
|
|
|
|
*/
|
|
|
|
struct ICUMutex {
|
|
|
|
UMTX *owner; /* Points back to the UMTX corrsponding to this */
|
|
|
|
/* ICUMutex object. */
|
|
|
|
|
|
|
|
UBool heapAllocated; /* Set if this ICUMutex is heap allocated, and */
|
|
|
|
/* will need to be deleted. The global mutex */
|
|
|
|
/* is static on POSIX platforms; all others */
|
|
|
|
/* will be heap allocated. */
|
|
|
|
|
|
|
|
ICUMutex *next; /* All ICUMutexes are chained into a list so that */
|
|
|
|
/* they can be found and deleted by u_cleanup(). */
|
|
|
|
|
|
|
|
int32_t recursionCount; /* For debugging, detect recursive mutex locks. */
|
|
|
|
|
|
|
|
MUTEX_TYPE platformMutex; /* The underlying OS mutex being wrapped. */
|
|
|
|
|
|
|
|
UMTX userMutex; /* For use with u_setMutexFunctions operations, */
|
|
|
|
/* corresponds to platformMutex. */
|
|
|
|
};
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
/* The global ICU mutex.
|
|
|
|
* For POSIX platforms, it gets a C style initialization, and is ready to use
|
|
|
|
* at program startup.
|
|
|
|
* For Windows, it will be lazily instantiated on first use.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(POSIX)
|
|
|
|
static UMTX globalUMTX;
|
|
|
|
static ICUMutex globalMutex = {&globalUMTX, FALSE, NULL, 0, PLATFORM_MUTEX_INITIALIZER, NULL};
|
|
|
|
static UMTX globalUMTX = &globalMutex;
|
|
|
|
#else
|
|
|
|
static UMTX globalUMTX = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Head of the list of all ICU mutexes.
|
|
|
|
* Linked list is through ICUMutex::next
|
|
|
|
* Modifications to the list are synchronized with the global mutex.
|
|
|
|
* The list is used by u_cleanup(), which needs to dispose of all of the ICU mutexes.
|
|
|
|
*
|
|
|
|
* The statically initialized global mutex on POSIX platforms does not get added to this
|
|
|
|
* mutex list, but that's not a problem - the global mutex gets special handling
|
|
|
|
* during u_cleanup().
|
|
|
|
*/
|
|
|
|
static ICUMutex *mutexListHead;
|
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
|
2009-09-21 04:21:41 +00:00
|
|
|
* directly using the system (Posix or Windows) APIs. See u_setMutexFunctions().
|
2003-08-05 01:25:54 +00:00
|
|
|
* (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
|
|
|
{
|
2009-09-21 04:21:41 +00:00
|
|
|
ICUMutex *m;
|
|
|
|
|
2003-08-08 23:56:02 +00:00
|
|
|
if (mutex == NULL) {
|
2009-09-21 04:21:41 +00:00
|
|
|
mutex = &globalUMTX;
|
1999-11-22 16:24:43 +00:00
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
m = (ICUMutex *)*mutex;
|
|
|
|
if (m == NULL) {
|
|
|
|
/* See note on lazy initialization, above. We can get away with it here, with mutexes,
|
|
|
|
* where we couldn't with normal user level data.
|
|
|
|
*/
|
2003-08-20 22:38:01 +00:00
|
|
|
umtx_init(mutex);
|
2009-09-21 04:21:41 +00:00
|
|
|
m = (ICUMutex *)*mutex;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
U_ASSERT(m->owner == mutex);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pMutexLockFn != NULL) {
|
2009-09-21 04:21:41 +00:00
|
|
|
(*pMutexLockFn)(gMutexContext, &m->userMutex);
|
2003-08-05 01:25:54 +00:00
|
|
|
} else {
|
2009-09-21 04:21:41 +00:00
|
|
|
PLATFORM_MUTEX_LOCK(&m->platformMutex);
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
#if defined(U_DEBUG)
|
|
|
|
m->recursionCount++; /* Recursion causes deadlock on Unixes. */
|
|
|
|
U_ASSERT(m->recursionCount == 1); /* Recursion detection works on Windows. */
|
|
|
|
/* Assertion failure on non-Windows indicates a */
|
|
|
|
/* problem with the mutex implementation itself. */
|
|
|
|
#endif
|
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
|
|
|
{
|
2009-09-21 04:21:41 +00:00
|
|
|
ICUMutex *m;
|
2003-08-20 22:38:01 +00:00
|
|
|
if(mutex == NULL) {
|
2009-09-21 04:21:41 +00:00
|
|
|
mutex = &globalUMTX;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
m = (ICUMutex *)*mutex;
|
|
|
|
if (m == NULL) {
|
2003-08-08 16:23:38 +00:00
|
|
|
U_ASSERT(FALSE); /* This mutex is not initialized. */
|
|
|
|
return;
|
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
U_ASSERT(m->owner == mutex);
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
#if defined (U_DEBUG)
|
|
|
|
m->recursionCount--;
|
|
|
|
U_ASSERT(m->recursionCount == 0); /* Detect unlock of an already unlocked mutex */
|
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) {
|
2009-09-21 04:21:41 +00:00
|
|
|
(*pMutexUnlockFn)(gMutexContext, &m->userMutex);
|
2003-08-05 01:25:54 +00:00
|
|
|
} else {
|
2009-09-21 04:21:41 +00:00
|
|
|
PLATFORM_MUTEX_UNLOCK(&m->platformMutex);
|
2003-08-08 16:23:38 +00:00
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
/* umtx_ct Allocate and initialize a new ICUMutex.
|
|
|
|
* If a non-null pointer is supplied, initialize an existing ICU Mutex.
|
2003-08-20 22:38:01 +00:00
|
|
|
*/
|
2009-09-21 04:21:41 +00:00
|
|
|
static ICUMutex *umtx_ct(ICUMutex *m) {
|
|
|
|
if (m == NULL) {
|
|
|
|
m = (ICUMutex *)uprv_malloc(sizeof(ICUMutex));
|
|
|
|
m->heapAllocated = TRUE;
|
2004-02-23 04:36:02 +00:00
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
m->next = NULL; /* List of mutexes is maintained at a higher level. */
|
|
|
|
m->recursionCount = 0;
|
|
|
|
m->userMutex = NULL;
|
|
|
|
if (pMutexInitFn != NULL) {
|
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
|
|
|
(*pMutexInitFn)(gMutexContext, &m->userMutex, &status);
|
|
|
|
U_ASSERT(U_SUCCESS(status));
|
|
|
|
} else {
|
|
|
|
PLATFORM_MUTEX_INIT(&m->platformMutex);
|
2003-08-20 22:38:01 +00:00
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
return m;
|
2003-08-20 22:38:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
/* umtx_dt Delete a ICUMutex. Destroy the underlying OS Platform mutex.
|
|
|
|
* Does not touch the linked list of ICU Mutexes.
|
|
|
|
*/
|
|
|
|
static void umtx_dt(ICUMutex *m) {
|
|
|
|
if (pMutexDestroyFn != NULL) {
|
|
|
|
(*pMutexDestroyFn)(gMutexContext, &m->userMutex);
|
|
|
|
m->userMutex = NULL;
|
|
|
|
} else {
|
|
|
|
PLATFORM_MUTEX_DESTROY(&m->platformMutex);
|
|
|
|
}
|
2003-06-07 00:03:51 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
if (m->heapAllocated) {
|
|
|
|
uprv_free(m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-20 22:38:01 +00:00
|
|
|
|
|
|
|
U_CAPI void U_EXPORT2
|
2009-09-21 04:21:41 +00:00
|
|
|
umtx_init(UMTX *mutex) {
|
|
|
|
ICUMutex *m = NULL;
|
|
|
|
void *originalValue;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
if (*mutex != NULL) {
|
|
|
|
/* Mutex is already initialized.
|
|
|
|
* Multiple umtx_init()s of a UMTX by other ICU code are explicitly permitted.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#if defined(POSIX)
|
|
|
|
if (mutex == &globalUMTX) {
|
|
|
|
m = &globalMutex;
|
|
|
|
}
|
2004-05-17 15:56:05 +00:00
|
|
|
#endif
|
2004-02-23 04:36:02 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
m = umtx_ct(m);
|
|
|
|
originalValue = SYNC_COMPARE_AND_SWAP(mutex, NULL, m);
|
|
|
|
if (originalValue != NULL) {
|
|
|
|
umtx_dt(m);
|
|
|
|
return;
|
2003-06-07 00:03:51 +00:00
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
|
|
|
|
m->owner = mutex;
|
|
|
|
|
|
|
|
/* Hook the new mutex into the list of all ICU mutexes, so that we can find and
|
|
|
|
* delete it for u_cleanup().
|
|
|
|
*/
|
|
|
|
|
|
|
|
umtx_lock(NULL);
|
|
|
|
m->next = mutexListHead;
|
|
|
|
mutexListHead = m;
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
return;
|
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) {
|
2009-09-21 04:21:41 +00:00
|
|
|
ICUMutex *m;
|
|
|
|
|
|
|
|
/* No one should be deleting the global ICU mutex.
|
|
|
|
* (u_cleanup() does delete it, but does so explicitly, not by passing NULL)
|
|
|
|
*/
|
|
|
|
U_ASSERT(mutex != NULL);
|
|
|
|
if (mutex == NULL) {
|
|
|
|
return;
|
2001-07-03 20:35:27 +00:00
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
m = (ICUMutex *)*mutex;
|
|
|
|
if (m == NULL) { /* Mutex not initialized, or already destroyed. */
|
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
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
U_ASSERT(m->owner == mutex);
|
|
|
|
if (m->owner != mutex) {
|
|
|
|
return;
|
2003-08-20 22:38:01 +00:00
|
|
|
}
|
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
/* Remove this mutex from the linked list of mutexes. */
|
|
|
|
umtx_lock(NULL);
|
|
|
|
if (mutexListHead == m) {
|
|
|
|
mutexListHead = m->next;
|
|
|
|
} else {
|
|
|
|
ICUMutex *prev;
|
|
|
|
for (prev = mutexListHead; prev!=NULL && prev->next!=m; prev = prev->next);
|
|
|
|
/* Empty for loop body */
|
|
|
|
if (prev != NULL) {
|
|
|
|
prev->next = m->next;
|
2004-02-23 04:36:02 +00:00
|
|
|
}
|
2001-07-03 20:35:27 +00:00
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
umtx_unlock(NULL);
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
umtx_dt(m); /* Delete the internal ICUMutex */
|
|
|
|
*mutex = NULL; /* Clear the caller's UMTX */
|
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;
|
|
|
|
}
|
2009-09-21 04:21:41 +00:00
|
|
|
|
|
|
|
/* Kill any existing global mutex. POSIX platforms have a global mutex
|
|
|
|
* even before any other part of ICU is initialized.
|
|
|
|
*/
|
|
|
|
umtx_destroy(&globalUMTX);
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
/* Swap in the mutex function pointers. */
|
2003-08-14 21:34:54 +00:00
|
|
|
pMutexInitFn = i;
|
|
|
|
pMutexDestroyFn = d;
|
|
|
|
pMutexLockFn = l;
|
|
|
|
pMutexUnlockFn = u;
|
|
|
|
gMutexContext = context;
|
2009-09-21 04:21:41 +00:00
|
|
|
|
|
|
|
#if defined (POSIX)
|
|
|
|
/* POSIX platforms must have a pre-initialized global mutex
|
|
|
|
* to allow other mutexes to initialize safely. */
|
|
|
|
umtx_init(&globalUMTX);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* synchronized compare and swap function, for use when OS or compiler built-in
|
|
|
|
* equivalents aren't available.
|
|
|
|
*
|
|
|
|
* This operation relies on the ICU global mutex for synchronization.
|
|
|
|
*
|
|
|
|
* There are two cases where this function can be entered when the global mutex is not
|
|
|
|
* yet initialized - at the end u_cleanup(), and at the end of u_setMutexFunctions, both
|
|
|
|
* of which re-init the global mutex. But neither function is thread-safe, so the lack of
|
|
|
|
* synchronization at these points doesn't matter.
|
|
|
|
*/
|
|
|
|
static void *mutexed_compare_and_swap(void **dest, void *newval, void *oldval) {
|
|
|
|
void *temp;
|
|
|
|
UBool needUnlock = FALSE;
|
|
|
|
|
|
|
|
if (globalUMTX != NULL) {
|
|
|
|
umtx_lock(&globalUMTX);
|
|
|
|
needUnlock = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = *dest;
|
|
|
|
if (temp == oldval) {
|
|
|
|
*dest = newval;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (needUnlock) {
|
|
|
|
umtx_unlock(&globalUMTX);
|
|
|
|
}
|
|
|
|
return temp;
|
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
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
static UMTX gIncDecMutex = 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);
|
2005-09-23 21:12:34 +00:00
|
|
|
#elif defined(USE_MAC_OS_ATOMIC_INCREMENT)
|
2006-08-19 21:27:08 +00:00
|
|
|
retVal = OSAtomicIncrement32Barrier(p);
|
2009-09-21 04:21:41 +00:00
|
|
|
#elif (U_HAVE_GCC_ATOMICS == 1)
|
|
|
|
retVal = __sync_add_and_fetch(p, 1);
|
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);
|
2005-09-23 21:12:34 +00:00
|
|
|
#elif defined(USE_MAC_OS_ATOMIC_INCREMENT)
|
2006-08-19 21:27:08 +00:00
|
|
|
retVal = OSAtomicDecrement32Barrier(p);
|
2009-09-21 04:21:41 +00:00
|
|
|
#elif (U_HAVE_GCC_ATOMICS == 1)
|
|
|
|
retVal = __sync_sub_and_fetch(p, 1);
|
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-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) {
|
|
|
|
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
|
|
|
|
2006-03-21 09:14:27 +00:00
|
|
|
#if !U_RELEASE
|
|
|
|
{
|
|
|
|
int32_t 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);
|
|
|
|
}
|
|
|
|
#endif
|
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) {
|
2009-09-21 04:21:41 +00:00
|
|
|
ICUMutex *thisMutex = NULL;
|
|
|
|
ICUMutex *nextMutex = NULL;
|
|
|
|
|
|
|
|
/* Extra, do-nothing function call to suppress compiler warnings on platforms where
|
|
|
|
* mutexed_compare_and_swap is not otherwise used. */
|
|
|
|
mutexed_compare_and_swap(&globalUMTX, NULL, NULL);
|
|
|
|
|
|
|
|
/* Delete all of the ICU mutexes. Do the global mutex last because it is used during
|
|
|
|
* the umtx_destroy operation of other mutexes.
|
|
|
|
*/
|
|
|
|
for (thisMutex=mutexListHead; thisMutex!=NULL; thisMutex=nextMutex) {
|
|
|
|
UMTX *umtx = thisMutex->owner;
|
|
|
|
nextMutex = thisMutex->next;
|
|
|
|
U_ASSERT(*umtx = (void *)thisMutex);
|
|
|
|
if (umtx != &globalUMTX) {
|
|
|
|
umtx_destroy(umtx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
umtx_destroy(&globalUMTX);
|
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
pMutexInitFn = NULL;
|
|
|
|
pMutexDestroyFn = NULL;
|
|
|
|
pMutexLockFn = NULL;
|
|
|
|
pMutexUnlockFn = NULL;
|
|
|
|
gMutexContext = 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;
|
|
|
|
|
2009-09-21 04:21:41 +00:00
|
|
|
#if defined (POSIX)
|
|
|
|
/* POSIX platforms must come out of u_cleanup() with a functioning global mutex
|
|
|
|
* to permit the safe resumption of use of ICU in multi-threaded environments.
|
|
|
|
*/
|
|
|
|
umtx_init(&globalUMTX);
|
2004-02-23 04:36:02 +00:00
|
|
|
#endif
|
2003-08-08 23:56:02 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2002-03-09 00:36:54 +00:00
|
|
|
|
|
|
|
|