1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
2000-01-13 23:54:23 +00:00
|
|
|
*
|
2003-06-03 06:44:40 +00:00
|
|
|
* Copyright (C) 1997-2003, 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
|
|
|
*/
|
|
|
|
|
|
|
|
/* Assume POSIX, and modify as necessary below */
|
|
|
|
#define POSIX
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#undef POSIX
|
|
|
|
#endif
|
|
|
|
#if defined(macintosh)
|
|
|
|
#undef POSIX
|
|
|
|
#endif
|
|
|
|
#if defined(OS2)
|
|
|
|
#undef POSIX
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-12-09 23:11:48 +00:00
|
|
|
/* Check our settings... */
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
2003-05-09 21:25:07 +00:00
|
|
|
#include "uassert.h"
|
2003-08-05 01:25:54 +00:00
|
|
|
#include "ucln_cmn.h"
|
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
|
|
|
|
2000-10-05 15:54:16 +00:00
|
|
|
#ifdef WIN32
|
2001-06-26 17:53:36 +00:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
2001-06-27 16:25:02 +00:00
|
|
|
# define NOGDI
|
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-08 16:23:38 +00:00
|
|
|
/* the global mutexes. */
|
|
|
|
static UMTX gGlobalMutex = NULL; /* The global ICU mutex */
|
|
|
|
static UMTX gIncDecMutex = NULL; /* mutex for atomic inc/dec, for platforms */
|
|
|
|
/* that can't do those ops directly. */
|
2003-08-05 01:25:54 +00:00
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
/* Detect Recursive locking of the global mutex. For debugging only. */
|
2003-08-05 01:25:54 +00:00
|
|
|
static int32_t gRecursionCount = 0;
|
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;
|
|
|
|
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-08 16:23:38 +00:00
|
|
|
/* Attempt to lock an uninitialized mutex. Not Supported.
|
|
|
|
* Note that earlier versions of ICU supported lazy mutex initialization.
|
2003-08-08 23:56:02 +00:00
|
|
|
* That was not thread safe on CPUs that reorder memory operations. */
|
2003-08-15 01:26:22 +00:00
|
|
|
/* U_ASSERT(FALSE); TODO: activate this assert. */
|
2003-08-08 16:23:38 +00:00
|
|
|
umtx_init(mutex); /* But, in case someone really screwed up, we will
|
|
|
|
* still do the lazy init to try to avoid a crash */
|
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)
|
2003-08-05 01:25:54 +00:00
|
|
|
#if defined(WIN32)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(WIN32) && defined(_DEBUG)
|
|
|
|
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. */
|
|
|
|
}
|
|
|
|
#endif /*_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
|
|
|
{
|
2001-07-03 20:35:27 +00:00
|
|
|
if(mutex == NULL)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2001-07-03 20:35:27 +00:00
|
|
|
mutex = &gGlobalMutex;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
2001-07-03 20:35:27 +00:00
|
|
|
if(*mutex == NULL)
|
|
|
|
{
|
2003-08-08 16:23:38 +00:00
|
|
|
U_ASSERT(FALSE); /* This mutex is not initialized. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined (WIN32) && defined (_DEBUG)
|
|
|
|
if (mutex == &gGlobalMutex) {
|
|
|
|
gRecursionCount--;
|
|
|
|
U_ASSERT(gRecursionCount == 0); /* Detect unlock of an already unlocked mutex */
|
2001-07-03 20:35:27 +00:00
|
|
|
}
|
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)
|
2001-07-03 20:35:27 +00:00
|
|
|
#if defined (WIN32)
|
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
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* umtx_raw_init Do the platform specific mutex allocation and initialization
|
|
|
|
*/
|
2003-08-08 16:23:38 +00:00
|
|
|
static void umtx_raw_init(UMTX *mutex) {
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pMutexInitFn != NULL) {
|
2003-08-05 01:25:54 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
2003-08-14 21:34:54 +00:00
|
|
|
(*pMutexInitFn)(gMutexContext, mutex, &status);
|
2003-08-05 01:25:54 +00:00
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
/* TODO: how should errors here be handled? */
|
2003-08-08 16:23:38 +00:00
|
|
|
return;
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
#if (ICU_USE_THREADS == 1)
|
2003-06-07 00:03:51 +00:00
|
|
|
#if defined (WIN32)
|
2003-08-08 16:23:38 +00:00
|
|
|
CRITICAL_SECTION *cs = uprv_malloc(sizeof(CRITICAL_SECTION));
|
|
|
|
if (cs == NULL) {
|
|
|
|
return;
|
2003-06-07 00:03:51 +00:00
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
InitializeCriticalSection(cs);
|
|
|
|
*mutex = cs;
|
2003-06-07 00:03:51 +00:00
|
|
|
#elif defined( POSIX )
|
2003-08-08 16:23:38 +00:00
|
|
|
pthread_mutex_t *m = uprv_malloc(sizeof(pthread_mutex_t));
|
|
|
|
if (m == NULL) {
|
|
|
|
return;
|
2003-06-07 00:03:51 +00:00
|
|
|
}
|
|
|
|
# if defined (HPUX_CMA)
|
2003-08-08 16:23:38 +00:00
|
|
|
pthread_mutex_init(m, pthread_mutexattr_default);
|
2003-06-07 00:03:51 +00:00
|
|
|
# else
|
2003-08-08 16:23:38 +00:00
|
|
|
pthread_mutex_init(m, NULL);
|
2003-06-07 00:03:51 +00:00
|
|
|
# endif
|
2003-08-08 16:23:38 +00:00
|
|
|
*mutex = m;
|
2003-08-05 01:25:54 +00:00
|
|
|
#endif /* cascade of platforms */
|
2003-08-08 16:23:38 +00:00
|
|
|
#else /* ICU_USE_THREADS */
|
|
|
|
*mutex = mutex; /* 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.
|
|
|
|
*/
|
|
|
|
#endif /* ICU_USE_THREADS */
|
2003-08-05 01:25:54 +00:00
|
|
|
}
|
2003-06-07 00:03:51 +00:00
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
|
|
|
|
|
2001-11-21 01:02:11 +00:00
|
|
|
U_CAPI void U_EXPORT2
|
|
|
|
umtx_init(UMTX *mutex)
|
1999-08-16 21:50:52 +00:00
|
|
|
{
|
2003-08-08 16:23:38 +00:00
|
|
|
if (mutex == NULL) {
|
|
|
|
mutex = &gGlobalMutex;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
if (mutex == &gGlobalMutex) {
|
|
|
|
/* Initialization of the global mutex. */
|
|
|
|
if (*mutex != NULL) {
|
|
|
|
/* Global mutex is already initialized. Nothing more required */
|
2003-06-07 00:03:51 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-08-08 16:23:38 +00:00
|
|
|
umtx_raw_init(mutex);
|
|
|
|
gRecursionCount = 0;
|
2003-06-07 00:03:51 +00:00
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
/* Initialize the inc/dec mutex, if needed, at the same time as the global ICU mutex */
|
2003-06-08 23:01:42 +00:00
|
|
|
#ifdef POSIX
|
|
|
|
umtx_raw_init(&gIncDecMutex);
|
|
|
|
#endif
|
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
} else {
|
2003-08-08 16:23:38 +00:00
|
|
|
/* The mutex to initialize is not the global mutex.
|
2003-06-07 00:03:51 +00:00
|
|
|
* Thread safe initialization, using the global mutex.
|
|
|
|
*/
|
|
|
|
UBool isInitialized;
|
|
|
|
UMTX tMutex = NULL;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
umtx_lock(NULL);
|
|
|
|
isInitialized = (*mutex != NULL);
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
if (isInitialized) {
|
|
|
|
return;
|
|
|
|
}
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-08 16:23:38 +00:00
|
|
|
umtx_raw_init(&tMutex);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-06-07 00:03:51 +00:00
|
|
|
umtx_lock(NULL);
|
|
|
|
if (*mutex == NULL) {
|
|
|
|
*mutex = tMutex;
|
|
|
|
tMutex = NULL;
|
|
|
|
}
|
|
|
|
umtx_unlock(NULL);
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
if (tMutex != NULL) {
|
|
|
|
umtx_destroy(&tMutex);
|
|
|
|
}
|
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-08 16:23:38 +00:00
|
|
|
|
2003-08-14 21:34:54 +00:00
|
|
|
if (pMutexDestroyFn != NULL) {
|
|
|
|
(*pMutexDestroyFn)(gMutexContext, mutex);
|
2003-08-08 16:23:38 +00:00
|
|
|
} else {
|
|
|
|
#if (ICU_USE_THREADS == 1)
|
2001-07-03 20:35:27 +00:00
|
|
|
#if defined (WIN32)
|
2003-08-08 16:23:38 +00:00
|
|
|
DeleteCriticalSection((CRITICAL_SECTION*)*mutex);
|
2003-08-10 23:57:35 +00:00
|
|
|
uprv_free(*mutex);
|
2001-07-03 20:35:27 +00:00
|
|
|
#elif defined (POSIX)
|
2003-08-08 16:23:38 +00:00
|
|
|
pthread_mutex_destroy((pthread_mutex_t*)*mutex);
|
2003-08-10 23:57:35 +00:00
|
|
|
uprv_free(*mutex);
|
2001-07-03 20:35:27 +00:00
|
|
|
#endif
|
2003-08-08 16:23:38 +00:00
|
|
|
#endif /* ICU_USE_THREADS==1 */
|
|
|
|
|
2001-07-03 20:35:27 +00:00
|
|
|
}
|
|
|
|
*mutex = NULL;
|
2003-08-08 16:23:38 +00:00
|
|
|
|
|
|
|
#if defined (POSIX)
|
|
|
|
if (mutex == &gGlobalMutex) {
|
|
|
|
umtx_destroy(&gIncDecMutex);
|
|
|
|
}
|
|
|
|
#endif /* POSIX */
|
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-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
|
|
|
|
2003-08-08 23:56:02 +00:00
|
|
|
/* Pointers to user-supplied inc/dec functions. Null if not funcs have been set. */
|
2003-08-14 21:34:54 +00:00
|
|
|
static UMtxAtomicFn *pIncFn = NULL;
|
|
|
|
static UMtxAtomicFn *pDecFn = NULL;
|
2003-08-08 23:56:02 +00:00
|
|
|
static void *gIncDecContext;
|
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 {
|
|
|
|
#if defined (WIN32) && ICU_USE_THREADS == 1
|
|
|
|
retVal = InterlockedIncrement(p);
|
|
|
|
#elif defined (POSIX) && ICU_USE_THREADS == 1
|
|
|
|
pthread_mutex_t *m = (pthread_mutex_t*) gIncDecMutex;
|
|
|
|
pthread_mutex_lock(m);
|
|
|
|
retVal = ++(*p);
|
|
|
|
pthread_mutex_unlock(m);
|
|
|
|
#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 {
|
|
|
|
#if defined (WIN32) && ICU_USE_THREADS == 1
|
|
|
|
retVal = InterlockedDecrement(p);
|
|
|
|
#elif defined (POSIX) && ICU_USE_THREADS == 1
|
|
|
|
pthread_mutex_t *m = (pthread_mutex_t*) gIncDecMutex;
|
|
|
|
pthread_mutex_lock(m);
|
|
|
|
retVal = --(*p);
|
|
|
|
pthread_mutex_unlock(m);
|
|
|
|
#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;
|
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;
|
|
|
|
|
|
|
|
pIncFn = NULL;
|
|
|
|
pDecFn = NULL;
|
2003-08-08 23:56:02 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-03-09 00:36:54 +00:00
|
|
|
|
|
|
|
|