1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
|
// Copyright (C) 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
**********************************************************************
* Copyright (C) 1997-2015, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
*
* 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)
******************************************************************************
*/
#ifndef UMUTEX_H
#define UMUTEX_H
#include "unicode/utypes.h"
#include "unicode/uclean.h"
#include "putilimp.h"
// Forward Declarations. UMutex is not in the ICU namespace (yet) because
// there are some remaining references from plain C.
struct UMutex;
struct UConditionVar;
U_NAMESPACE_BEGIN
struct UInitOnce;
U_NAMESPACE_END
// Stringify macros, to allow #include of user supplied atomic & mutex files.
#define U_MUTEX_STR(s) #s
#define U_MUTEX_XSTR(s) U_MUTEX_STR(s)
/****************************************************************************
*
* Low Level Atomic Operations.
* Compiler dependent. Not operating system dependent.
*
****************************************************************************/
#if defined (U_USER_ATOMICS_H)
#include U_MUTEX_XSTR(U_USER_ATOMICS_H)
#elif U_HAVE_STD_ATOMICS
// C++11 atomics are available.
#include <atomic>
U_NAMESPACE_BEGIN
typedef std::atomic<int32_t> u_atomic_int32_t;
#define ATOMIC_INT32_T_INITIALIZER(val) ATOMIC_VAR_INIT(val)
inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
return var.load(std::memory_order_acquire);
}
inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) {
var.store(val, std::memory_order_release);
}
inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) {
return var->fetch_add(1) + 1;
}
inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) {
return var->fetch_sub(1) - 1;
}
U_NAMESPACE_END
#elif U_PLATFORM_HAS_WIN32_API
// MSVC compiler. Reads and writes of volatile variables have
// acquire and release memory semantics, respectively.
// This is a Microsoft extension, not standard C++ behavior.
//
// Update: can't use this because of MinGW, built with gcc.
// Original plan was to use gcc atomics for MinGW, but they
// aren't supported, so we fold MinGW into this path.
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
U_NAMESPACE_BEGIN
typedef volatile LONG u_atomic_int32_t;
#define ATOMIC_INT32_T_INITIALIZER(val) val
inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
return InterlockedCompareExchange(&var, 0, 0);
}
inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) {
InterlockedExchange(&var, val);
}
inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) {
return InterlockedIncrement(var);
}
inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) {
return InterlockedDecrement(var);
}
U_NAMESPACE_END
#elif U_HAVE_CLANG_ATOMICS
/*
* Clang __c11 atomic built-ins
*/
U_NAMESPACE_BEGIN
typedef _Atomic(int32_t) u_atomic_int32_t;
#define ATOMIC_INT32_T_INITIALIZER(val) val
inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
return __c11_atomic_load(&var, __ATOMIC_ACQUIRE);
}
inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) {
return __c11_atomic_store(&var, val, __ATOMIC_RELEASE);
}
inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) {
return __c11_atomic_fetch_add(var, 1, __ATOMIC_SEQ_CST) + 1;
}
inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) {
return __c11_atomic_fetch_sub(var, 1, __ATOMIC_SEQ_CST) - 1;
}
U_NAMESPACE_END
#elif U_HAVE_GCC_ATOMICS
/*
* gcc atomic ops. These are available on several other compilers as well.
*/
U_NAMESPACE_BEGIN
typedef int32_t u_atomic_int32_t;
#define ATOMIC_INT32_T_INITIALIZER(val) val
inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
int32_t val = var;
__sync_synchronize();
return val;
}
inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) {
__sync_synchronize();
var = val;
}
inline int32_t umtx_atomic_inc(u_atomic_int32_t *p) {
return __sync_add_and_fetch(p, 1);
}
inline int32_t umtx_atomic_dec(u_atomic_int32_t *p) {
return __sync_sub_and_fetch(p, 1);
}
U_NAMESPACE_END
#else
/*
* Unknown Platform. Use out-of-line functions, which in turn use mutexes.
* Slow but correct.
*/
#define U_NO_PLATFORM_ATOMICS
U_NAMESPACE_BEGIN
typedef int32_t u_atomic_int32_t;
#define ATOMIC_INT32_T_INITIALIZER(val) val
U_COMMON_API int32_t U_EXPORT2
umtx_loadAcquire(u_atomic_int32_t &var);
U_COMMON_API void U_EXPORT2
umtx_storeRelease(u_atomic_int32_t &var, int32_t val);
U_COMMON_API int32_t U_EXPORT2
umtx_atomic_inc(u_atomic_int32_t *p);
U_COMMON_API int32_t U_EXPORT2
umtx_atomic_dec(u_atomic_int32_t *p);
U_NAMESPACE_END
#endif /* Low Level Atomic Ops Platfrom Chain */
/*************************************************************************************************
*
* UInitOnce Definitions.
* These are platform neutral.
*
*************************************************************************************************/
U_NAMESPACE_BEGIN
struct UInitOnce {
u_atomic_int32_t fState;
UErrorCode fErrCode;
void reset() {fState = 0;};
UBool isReset() {return umtx_loadAcquire(fState) == 0;};
// Note: isReset() is used by service registration code.
// Thread safety of this usage needs review.
};
#define U_INITONCE_INITIALIZER {ATOMIC_INT32_T_INITIALIZER(0), U_ZERO_ERROR}
U_COMMON_API UBool U_EXPORT2 umtx_initImplPreInit(UInitOnce &);
U_COMMON_API void U_EXPORT2 umtx_initImplPostInit(UInitOnce &);
template<class T> void umtx_initOnce(UInitOnce &uio, T *obj, void (U_CALLCONV T::*fp)()) {
if (umtx_loadAcquire(uio.fState) == 2) {
return;
}
if (umtx_initImplPreInit(uio)) {
(obj->*fp)();
umtx_initImplPostInit(uio);
}
}
// umtx_initOnce variant for plain functions, or static class functions.
// No context parameter.
inline void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)()) {
if (umtx_loadAcquire(uio.fState) == 2) {
return;
}
if (umtx_initImplPreInit(uio)) {
(*fp)();
umtx_initImplPostInit(uio);
}
}
// umtx_initOnce variant for plain functions, or static class functions.
// With ErrorCode, No context parameter.
inline void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(UErrorCode &), UErrorCode &errCode) {
if (U_FAILURE(errCode)) {
return;
}
if (umtx_loadAcquire(uio.fState) != 2 && umtx_initImplPreInit(uio)) {
// We run the initialization.
(*fp)(errCode);
uio.fErrCode = errCode;
umtx_initImplPostInit(uio);
} else {
// Someone else already ran the initialization.
if (U_FAILURE(uio.fErrCode)) {
errCode = uio.fErrCode;
}
}
}
// umtx_initOnce variant for plain functions, or static class functions,
// with a context parameter.
template<class T> void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(T), T context) {
if (umtx_loadAcquire(uio.fState) == 2) {
return;
}
if (umtx_initImplPreInit(uio)) {
(*fp)(context);
umtx_initImplPostInit(uio);
}
}
// umtx_initOnce variant for plain functions, or static class functions,
// with a context parameter and an error code.
template<class T> void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(T, UErrorCode &), T context, UErrorCode &errCode) {
if (U_FAILURE(errCode)) {
return;
}
if (umtx_loadAcquire(uio.fState) != 2 && umtx_initImplPreInit(uio)) {
// We run the initialization.
(*fp)(context, errCode);
uio.fErrCode = errCode;
umtx_initImplPostInit(uio);
} else {
// Someone else already ran the initialization.
if (U_FAILURE(uio.fErrCode)) {
errCode = uio.fErrCode;
}
}
}
U_NAMESPACE_END
/*************************************************************************************************
*
* Mutex Definitions. Platform Dependent, #if platform chain follows.
* TODO: Add a C++11 version.
* Need to convert all mutex using files to C++ first.
*
*************************************************************************************************/
#if defined(U_USER_MUTEX_H)
// #inlcude "U_USER_MUTEX_H"
#include U_MUTEX_XSTR(U_USER_MUTEX_H)
#elif U_PLATFORM_USES_ONLY_WIN32_API
/* For CRITICAL_SECTION */
/*
* Note: there is an earlier include of windows.h in this file, but it is in
* different conditionals.
* This one is needed if we are using C++11 for atomic ops, but
* win32 APIs for Critical Sections.
*/
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# define NOUSER
# define NOSERVICE
# define NOIME
# define NOMCX
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
typedef struct UMutex {
icu::UInitOnce fInitOnce;
CRITICAL_SECTION fCS;
} UMutex;
/* Initializer for a static UMUTEX. Deliberately contains no value for the
* CRITICAL_SECTION.
*/
#define U_MUTEX_INITIALIZER {U_INITONCE_INITIALIZER}
struct UConditionVar {
HANDLE fEntryGate;
HANDLE fExitGate;
int32_t fWaitCount;
};
#define U_CONDITION_INITIALIZER {NULL, NULL, 0}
#elif U_PLATFORM_IMPLEMENTS_POSIX
/*
* POSIX platform
*/
#include <pthread.h>
struct UMutex {
pthread_mutex_t fMutex;
};
typedef struct UMutex UMutex;
#define U_MUTEX_INITIALIZER {PTHREAD_MUTEX_INITIALIZER}
struct UConditionVar {
pthread_cond_t fCondition;
};
#define U_CONDITION_INITIALIZER {PTHREAD_COND_INITIALIZER}
#else
/*
* Unknow platform type.
* This is an error condition. ICU requires mutexes.
*/
#error Unknown Platform.
#endif
/**************************************************************************************
*
* Mutex Implementation function declaratations.
* Declarations are platform neutral.
* Implementations, in umutex.cpp, are platform specific.
*
************************************************************************************/
/* 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.
*/
U_INTERNAL void U_EXPORT2 umtx_lock(UMutex* mutex);
/* Unlock a mutex.
* @param mutex The given mutex to be unlocked. Pass NULL to specify
* the global ICU mutex.
*/
U_INTERNAL void U_EXPORT2 umtx_unlock (UMutex* mutex);
/*
* Wait on a condition variable.
* The calling thread will unlock the mutex and wait on the condition variable.
* The mutex must be locked by the calling thread when invoking this function.
*
* @param cond the condition variable to wait on.
* @param mutex the associated mutex.
*/
U_INTERNAL void U_EXPORT2 umtx_condWait(UConditionVar *cond, UMutex *mutex);
/*
* Broadcast wakeup of all threads waiting on a Condition.
* The associated mutex must be locked by the calling thread when calling
* this function; this is a temporary ICU restriction.
*
* @param cond the condition variable.
*/
U_INTERNAL void U_EXPORT2 umtx_condBroadcast(UConditionVar *cond);
/*
* Signal a condition variable, waking up one waiting thread.
* CAUTION: Do not use. Place holder only. Not implemented for Windows.
*/
U_INTERNAL void U_EXPORT2 umtx_condSignal(UConditionVar *cond);
#endif /* UMUTEX_H */
/*eof*/
|