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