sl@0
|
1 |
/*
|
sl@0
|
2 |
**********************************************************************
|
sl@0
|
3 |
* Copyright (C) 1997-2005, International Business Machines
|
sl@0
|
4 |
* Corporation and others. All Rights Reserved.
|
sl@0
|
5 |
**********************************************************************
|
sl@0
|
6 |
*
|
sl@0
|
7 |
* File UMUTEX.H
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Modification History:
|
sl@0
|
10 |
*
|
sl@0
|
11 |
* Date Name Description
|
sl@0
|
12 |
* 04/02/97 aliu Creation.
|
sl@0
|
13 |
* 04/07/99 srl rewrite - C interface, multiple mutices
|
sl@0
|
14 |
* 05/13/99 stephen Changed to umutex (from cmutex)
|
sl@0
|
15 |
******************************************************************************
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
#ifndef UMUTEX_H
|
sl@0
|
19 |
#define UMUTEX_H
|
sl@0
|
20 |
|
sl@0
|
21 |
#include "unicode/utypes.h"
|
sl@0
|
22 |
#include "unicode/uclean.h"
|
sl@0
|
23 |
|
sl@0
|
24 |
|
sl@0
|
25 |
/* APP_NO_THREADS is an old symbol. We'll honour it if present. */
|
sl@0
|
26 |
#ifdef APP_NO_THREADS
|
sl@0
|
27 |
# define ICU_USE_THREADS 0
|
sl@0
|
28 |
#endif
|
sl@0
|
29 |
|
sl@0
|
30 |
/* ICU_USE_THREADS
|
sl@0
|
31 |
*
|
sl@0
|
32 |
* Allows thread support (use of mutexes) to be compiled out of ICU.
|
sl@0
|
33 |
* Default: use threads.
|
sl@0
|
34 |
* Even with thread support compiled out, applications may override the
|
sl@0
|
35 |
* (empty) mutex implementation with the u_setMutexFunctions() functions.
|
sl@0
|
36 |
*/
|
sl@0
|
37 |
#ifndef ICU_USE_THREADS
|
sl@0
|
38 |
# define ICU_USE_THREADS 1
|
sl@0
|
39 |
#endif
|
sl@0
|
40 |
|
sl@0
|
41 |
/**
|
sl@0
|
42 |
* By default assume that we are on a machine with a weak memory model,
|
sl@0
|
43 |
* and the double check lock won't work reliably.
|
sl@0
|
44 |
*/
|
sl@0
|
45 |
#if !defined(UMTX_STRONG_MEMORY_MODEL)
|
sl@0
|
46 |
#define UMTX_STRONG_MEMORY_MODEL 0
|
sl@0
|
47 |
#endif
|
sl@0
|
48 |
|
sl@0
|
49 |
/**
|
sl@0
|
50 |
* \def UMTX_CHECK
|
sl@0
|
51 |
* Encapsulates a safe check for an expression (usually a condition)
|
sl@0
|
52 |
* for lazy variable inititialization.
|
sl@0
|
53 |
* On CPUs with weak memory models, this must use memory fence instructions
|
sl@0
|
54 |
* or mutexes.
|
sl@0
|
55 |
* @internal
|
sl@0
|
56 |
*/
|
sl@0
|
57 |
#if UMTX_STRONG_MEMORY_MODEL
|
sl@0
|
58 |
|
sl@0
|
59 |
#define UMTX_CHECK(pMutex, expression, result) \
|
sl@0
|
60 |
(result)=(expression);
|
sl@0
|
61 |
|
sl@0
|
62 |
#else
|
sl@0
|
63 |
|
sl@0
|
64 |
#define UMTX_CHECK(pMutex, expression, result) \
|
sl@0
|
65 |
umtx_lock(pMutex); \
|
sl@0
|
66 |
(result)=(expression); \
|
sl@0
|
67 |
umtx_unlock(pMutex);
|
sl@0
|
68 |
|
sl@0
|
69 |
#endif
|
sl@0
|
70 |
|
sl@0
|
71 |
/*
|
sl@0
|
72 |
* Code within ICU that accesses shared static or global data should
|
sl@0
|
73 |
* instantiate a Mutex object while doing so. The unnamed global mutex
|
sl@0
|
74 |
* is used throughout ICU, so keep locking short and sweet.
|
sl@0
|
75 |
*
|
sl@0
|
76 |
* For example:
|
sl@0
|
77 |
*
|
sl@0
|
78 |
* void Function(int arg1, int arg2)
|
sl@0
|
79 |
* {
|
sl@0
|
80 |
* static Object* foo; // Shared read-write object
|
sl@0
|
81 |
* umtx_lock(NULL); // Lock the ICU global mutex
|
sl@0
|
82 |
* foo->Method();
|
sl@0
|
83 |
* umtx_unlock(NULL);
|
sl@0
|
84 |
* }
|
sl@0
|
85 |
*
|
sl@0
|
86 |
* an alternative C++ mutex API is defined in the file common/mutex.h
|
sl@0
|
87 |
*/
|
sl@0
|
88 |
|
sl@0
|
89 |
/* Lock a mutex.
|
sl@0
|
90 |
* @param mutex The given mutex to be locked. Pass NULL to specify
|
sl@0
|
91 |
* the global ICU mutex. Recursive locks are an error
|
sl@0
|
92 |
* and may cause a deadlock on some platforms.
|
sl@0
|
93 |
*/
|
sl@0
|
94 |
U_CAPI void U_EXPORT2 umtx_lock ( UMTX* mutex );
|
sl@0
|
95 |
|
sl@0
|
96 |
/* Unlock a mutex. Pass in NULL if you want the single global
|
sl@0
|
97 |
mutex.
|
sl@0
|
98 |
* @param mutex The given mutex to be unlocked. Pass NULL to specify
|
sl@0
|
99 |
* the global ICU mutex.
|
sl@0
|
100 |
*/
|
sl@0
|
101 |
U_CAPI void U_EXPORT2 umtx_unlock ( UMTX* mutex );
|
sl@0
|
102 |
|
sl@0
|
103 |
/* Initialize a mutex. Use it this way:
|
sl@0
|
104 |
umtx_init( &aMutex );
|
sl@0
|
105 |
* ICU Mutexes do not need explicit initialization before use. Use of this
|
sl@0
|
106 |
* function is not necessary.
|
sl@0
|
107 |
* Initialization of an already initialized mutex has no effect, and is safe to do.
|
sl@0
|
108 |
* Initialization of mutexes is thread safe. Two threads can concurrently
|
sl@0
|
109 |
* initialize the same mutex without causing problems.
|
sl@0
|
110 |
* @param mutex The given mutex to be initialized
|
sl@0
|
111 |
*/
|
sl@0
|
112 |
U_CAPI void U_EXPORT2 umtx_init ( UMTX* mutex );
|
sl@0
|
113 |
|
sl@0
|
114 |
/* Destroy a mutex. This will free the resources of a mutex.
|
sl@0
|
115 |
* Use it this way:
|
sl@0
|
116 |
* umtx_destroy( &aMutex );
|
sl@0
|
117 |
* Destroying an already destroyed mutex has no effect, and causes no problems.
|
sl@0
|
118 |
* This function is not thread safe. Two threads must not attempt to concurrently
|
sl@0
|
119 |
* destroy the same mutex.
|
sl@0
|
120 |
* @param mutex The given mutex to be destroyed.
|
sl@0
|
121 |
*/
|
sl@0
|
122 |
U_CAPI void U_EXPORT2 umtx_destroy( UMTX *mutex );
|
sl@0
|
123 |
|
sl@0
|
124 |
|
sl@0
|
125 |
|
sl@0
|
126 |
/*
|
sl@0
|
127 |
* Atomic Increment and Decrement of an int32_t value.
|
sl@0
|
128 |
*
|
sl@0
|
129 |
* Return Values:
|
sl@0
|
130 |
* If the result of the operation is zero, the return zero.
|
sl@0
|
131 |
* If the result of the operation is not zero, the sign of returned value
|
sl@0
|
132 |
* is the same as the sign of the result, but the returned value itself may
|
sl@0
|
133 |
* be different from the result of the operation.
|
sl@0
|
134 |
*/
|
sl@0
|
135 |
U_CAPI int32_t U_EXPORT2 umtx_atomic_inc(int32_t *);
|
sl@0
|
136 |
U_CAPI int32_t U_EXPORT2 umtx_atomic_dec(int32_t *);
|
sl@0
|
137 |
|
sl@0
|
138 |
|
sl@0
|
139 |
#endif /*_CMUTEX*/
|
sl@0
|
140 |
/*eof*/
|
sl@0
|
141 |
|
sl@0
|
142 |
|
sl@0
|
143 |
|