1999-08-16 21:50:52 +00:00
|
|
|
/*
|
1999-12-09 23:27:55 +00:00
|
|
|
**********************************************************************
|
2011-03-29 00:47:41 +00:00
|
|
|
* Copyright (C) 1997-2011, 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"
|
2011-03-29 00:47:41 +00:00
|
|
|
#include "unicode/uclean.h"
|
2011-07-27 20:55:09 +00:00
|
|
|
#include "putilimp.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-07-23 00:10:30 +00:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER >= 1500
|
2011-03-29 00:47:41 +00:00
|
|
|
# include <intrin.h>
|
|
|
|
#endif
|
|
|
|
|
2011-07-23 00:10:30 +00:00
|
|
|
#if U_PLATFORM_IS_DARWIN_BASED
|
2011-03-29 04:02:06 +00:00
|
|
|
#if defined(__STRICT_ANSI__)
|
|
|
|
#define UPRV_REMAP_INLINE
|
|
|
|
#define inline
|
|
|
|
#endif
|
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
#define USE_MAC_OS_ATOMIC_INCREMENT 1
|
|
|
|
#if defined(UPRV_REMAP_INLINE)
|
|
|
|
#undef inline
|
|
|
|
#undef UPRV_REMAP_INLINE
|
|
|
|
#endif
|
2011-03-29 00:47:41 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we do not compile with dynamic_annotations.h then define
|
|
|
|
* empty annotation macros.
|
|
|
|
* See http://code.google.com/p/data-race-test/wiki/DynamicAnnotations
|
|
|
|
*/
|
|
|
|
#ifndef ANNOTATE_HAPPENS_BEFORE
|
|
|
|
# define ANNOTATE_HAPPENS_BEFORE(obj)
|
|
|
|
# define ANNOTATE_HAPPENS_AFTER(obj)
|
|
|
|
# define ANNOTATE_UNPROTECTED_READ(x) (x)
|
|
|
|
#endif
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2011-03-29 00:47:41 +00:00
|
|
|
#ifndef UMTX_FULL_BARRIER
|
|
|
|
# if !ICU_USE_THREADS
|
|
|
|
# define UMTX_FULL_BARRIER
|
|
|
|
# elif U_HAVE_GCC_ATOMICS
|
|
|
|
# define UMTX_FULL_BARRIER __sync_synchronize();
|
2011-07-23 00:10:30 +00:00
|
|
|
# elif defined(_MSC_VER) && _MSC_VER >= 1500
|
|
|
|
/* From MSVC intrin.h. Use _ReadWriteBarrier() only on MSVC 9 and higher. */
|
2011-03-29 00:47:41 +00:00
|
|
|
# define UMTX_FULL_BARRIER _ReadWriteBarrier();
|
2011-07-23 00:10:30 +00:00
|
|
|
# elif U_PLATFORM_IS_DARWIN_BASED
|
2011-03-29 00:47:41 +00:00
|
|
|
# define UMTX_FULL_BARRIER OSMemoryBarrier();
|
|
|
|
# else
|
|
|
|
# define UMTX_FULL_BARRIER \
|
|
|
|
{ \
|
|
|
|
umtx_lock(NULL); \
|
|
|
|
umtx_unlock(NULL); \
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UMTX_ACQUIRE_BARRIER
|
|
|
|
# define UMTX_ACQUIRE_BARRIER UMTX_FULL_BARRIER
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef UMTX_RELEASE_BARRIER
|
|
|
|
# define UMTX_RELEASE_BARRIER UMTX_FULL_BARRIER
|
2005-03-25 23:24:47 +00:00
|
|
|
#endif
|
|
|
|
|
2004-09-06 15:41:52 +00:00
|
|
|
/**
|
|
|
|
* \def UMTX_CHECK
|
2011-03-29 00:47:41 +00:00
|
|
|
* Encapsulates a safe check of an expression
|
2008-08-05 00:09:13 +00:00
|
|
|
* for use with double-checked lazy inititialization.
|
2011-03-29 00:47:41 +00:00
|
|
|
* Either memory barriers or mutexes are required, to prevent both the hardware
|
|
|
|
* and the compiler from reordering operations across the check.
|
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
|
|
|
|
*/
|
|
|
|
#define UMTX_CHECK(pMutex, expression, result) \
|
2011-03-29 00:47:41 +00:00
|
|
|
{ \
|
|
|
|
(result)=(expression); \
|
|
|
|
UMTX_ACQUIRE_BARRIER; \
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* TODO: Replace all uses of UMTX_CHECK and surrounding code
|
|
|
|
* with SimpleSingleton or TriStateSingleton, and remove UMTX_CHECK.
|
|
|
|
*/
|
2004-09-06 15:41:52 +00:00
|
|
|
|
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
|
|
|
|
2011-03-29 00:47:41 +00:00
|
|
|
/* Lock a mutex.
|
2003-08-05 01:25:54 +00:00
|
|
|
* @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 );
|
|
|
|
|
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*/
|