1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-12-09 23:27:55 +00:00
|
|
|
**********************************************************************
|
2008-08-05 00:09:13 +00:00
|
|
|
* Copyright (C) 1997-2008, International Business Machines
|
1999-12-09 23:27:55 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File UMUTEX.H
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 04/02/97 aliu Creation.
|
|
|
|
* 04/07/99 srl rewrite - C interface, multiple mutices
|
|
|
|
* 05/13/99 stephen Changed to umutex (from cmutex)
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef UMUTEX_H
|
|
|
|
#define UMUTEX_H
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
2003-08-05 01:25:54 +00:00
|
|
|
#include "unicode/uclean.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
2001-08-30 17:52:53 +00:00
|
|
|
/* APP_NO_THREADS is an old symbol. We'll honour it if present. */
|
|
|
|
#ifdef APP_NO_THREADS
|
|
|
|
# define ICU_USE_THREADS 0
|
|
|
|
#endif
|
|
|
|
|
2003-08-28 00:00:00 +00:00
|
|
|
/* ICU_USE_THREADS
|
|
|
|
*
|
|
|
|
* Allows thread support (use of mutexes) to be compiled out of ICU.
|
|
|
|
* Default: use threads.
|
|
|
|
* Even with thread support compiled out, applications may override the
|
|
|
|
* (empty) mutex implementation with the u_setMutexFunctions() functions.
|
|
|
|
*/
|
2001-08-30 17:52:53 +00:00
|
|
|
#ifndef ICU_USE_THREADS
|
|
|
|
# define ICU_USE_THREADS 1
|
|
|
|
#endif
|
|
|
|
|
2005-03-25 23:24:47 +00:00
|
|
|
/**
|
|
|
|
* By default assume that we are on a machine with a weak memory model,
|
|
|
|
* and the double check lock won't work reliably.
|
|
|
|
*/
|
|
|
|
#if !defined(UMTX_STRONG_MEMORY_MODEL)
|
|
|
|
#define UMTX_STRONG_MEMORY_MODEL 0
|
|
|
|
#endif
|
|
|
|
|
2004-09-06 15:41:52 +00:00
|
|
|
/**
|
|
|
|
* \def UMTX_CHECK
|
2008-08-05 00:09:13 +00:00
|
|
|
* Encapsulates a safe check of an expression
|
|
|
|
* for use with double-checked lazy inititialization.
|
2004-09-06 15:41:52 +00:00
|
|
|
* On CPUs with weak memory models, this must use memory fence instructions
|
|
|
|
* or mutexes.
|
2008-08-05 00:09:13 +00:00
|
|
|
* The expression must involve only a _single_ variable, typically
|
|
|
|
* a possibly null pointer or a boolean that indicates whether some service
|
|
|
|
* is initialized or not.
|
|
|
|
* The setting of the variable involved in the test must be the last step of
|
|
|
|
* the initialization process.
|
|
|
|
*
|
|
|
|
*
|
2004-09-06 15:41:52 +00:00
|
|
|
* @internal
|
|
|
|
*/
|
2005-03-25 23:24:47 +00:00
|
|
|
#if UMTX_STRONG_MEMORY_MODEL
|
2004-09-06 15:41:52 +00:00
|
|
|
|
|
|
|
#define UMTX_CHECK(pMutex, expression, result) \
|
2006-08-29 04:57:05 +00:00
|
|
|
(result)=(expression)
|
2004-09-06 15:41:52 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define UMTX_CHECK(pMutex, expression, result) \
|
|
|
|
umtx_lock(pMutex); \
|
|
|
|
(result)=(expression); \
|
2006-08-29 04:57:05 +00:00
|
|
|
umtx_unlock(pMutex)
|
2004-09-06 15:41:52 +00:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2003-08-05 01:25:54 +00:00
|
|
|
* Code within ICU that accesses shared static or global data should
|
|
|
|
* instantiate a Mutex object while doing so. The unnamed global mutex
|
|
|
|
* is used throughout ICU, so keep locking short and sweet.
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* For example:
|
|
|
|
*
|
|
|
|
* void Function(int arg1, int arg2)
|
|
|
|
* {
|
2003-08-05 01:25:54 +00:00
|
|
|
* static Object* foo; // Shared read-write object
|
|
|
|
* umtx_lock(NULL); // Lock the ICU global mutex
|
1999-08-16 21:50:52 +00:00
|
|
|
* foo->Method();
|
2003-08-05 01:25:54 +00:00
|
|
|
* umtx_unlock(NULL);
|
1999-08-16 21:50:52 +00:00
|
|
|
* }
|
|
|
|
*
|
2003-08-05 01:25:54 +00:00
|
|
|
* an alternative C++ mutex API is defined in the file common/mutex.h
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
/* Lock a mutex.
|
|
|
|
* @param mutex The given mutex to be locked. Pass NULL to specify
|
|
|
|
* the global ICU mutex. Recursive locks are an error
|
|
|
|
* and may cause a deadlock on some platforms.
|
2002-07-03 12:05:56 +00:00
|
|
|
*/
|
2001-07-03 20:35:27 +00:00
|
|
|
U_CAPI void U_EXPORT2 umtx_lock ( UMTX* mutex );
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* Unlock a mutex. Pass in NULL if you want the single global
|
2002-07-03 12:05:56 +00:00
|
|
|
mutex.
|
2003-08-05 01:25:54 +00:00
|
|
|
* @param mutex The given mutex to be unlocked. Pass NULL to specify
|
|
|
|
* the global ICU mutex.
|
2002-07-03 12:05:56 +00:00
|
|
|
*/
|
1999-10-18 23:44:20 +00:00
|
|
|
U_CAPI void U_EXPORT2 umtx_unlock ( UMTX* mutex );
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/* Initialize a mutex. Use it this way:
|
2002-07-03 12:05:56 +00:00
|
|
|
umtx_init( &aMutex );
|
2003-08-28 00:00:00 +00:00
|
|
|
* ICU Mutexes do not need explicit initialization before use. Use of this
|
|
|
|
* function is not necessary.
|
2003-08-08 16:23:38 +00:00
|
|
|
* Initialization of an already initialized mutex has no effect, and is safe to do.
|
2003-08-28 00:00:00 +00:00
|
|
|
* Initialization of mutexes is thread safe. Two threads can concurrently
|
|
|
|
* initialize the same mutex without causing problems.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param mutex The given mutex to be initialized
|
|
|
|
*/
|
1999-10-18 23:44:20 +00:00
|
|
|
U_CAPI void U_EXPORT2 umtx_init ( UMTX* mutex );
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-07-03 20:35:27 +00:00
|
|
|
/* Destroy a mutex. This will free the resources of a mutex.
|
2003-08-08 16:23:38 +00:00
|
|
|
* Use it this way:
|
|
|
|
* umtx_destroy( &aMutex );
|
|
|
|
* Destroying an already destroyed mutex has no effect, and causes no problems.
|
|
|
|
* This function is not thread safe. Two threads must not attempt to concurrently
|
|
|
|
* destroy the same mutex.
|
|
|
|
* @param mutex The given mutex to be destroyed.
|
2002-07-03 12:05:56 +00:00
|
|
|
*/
|
2001-07-03 20:35:27 +00:00
|
|
|
U_CAPI void U_EXPORT2 umtx_destroy( UMTX *mutex );
|
|
|
|
|
2003-08-05 01:25:54 +00:00
|
|
|
|
|
|
|
|
2002-03-09 00:36:54 +00:00
|
|
|
/*
|
|
|
|
* Atomic Increment and Decrement of an int32_t value.
|
|
|
|
*
|
|
|
|
* Return Values:
|
|
|
|
* If the result of the operation is zero, the return zero.
|
|
|
|
* If the result of the operation is not zero, the sign of returned value
|
|
|
|
* is the same as the sign of the result, but the returned value itself may
|
|
|
|
* be different from the result of the operation.
|
|
|
|
*/
|
|
|
|
U_CAPI int32_t U_EXPORT2 umtx_atomic_inc(int32_t *);
|
|
|
|
U_CAPI int32_t U_EXPORT2 umtx_atomic_dec(int32_t *);
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
#endif /*_CMUTEX*/
|
|
|
|
/*eof*/
|
|
|
|
|
|
|
|
|
|
|
|
|