Debian Codegraph @

intl/icu/source/common/umutex.h

raw
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 **********************************************************************
5 * Copyright (C) 1997-2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 **********************************************************************
8 *
9 * File UMUTEX.H
10 *
11 * Modification History:
12 *
13 * Date Name Description
14 * 04/02/97 aliu Creation.
15 * 04/07/99 srl rewrite - C interface, multiple mutices
16 * 05/13/99 stephen Changed to umutex (from cmutex)
17 ******************************************************************************
18 */
19
20 #ifndef UMUTEX_H
21 #define UMUTEX_H
22
23 #ifndef __wasi__
24 #include <atomic>
25 #include <condition_variable>
26 #include <mutex>
27 #endif
28
29 #include <type_traits>
30
31 #include "unicode/utypes.h"
32 #include "unicode/uclean.h"
33 #include "unicode/uobject.h"
34
35 #include "putilimp.h"
36
37 #if defined(U_USER_ATOMICS_H) || defined(U_USER_MUTEX_H)
38 // Support for including an alternate implementation of atomic & mutex operations has been withdrawn.
39 // See issue ICU-20185.
40 #error U_USER_ATOMICS and U_USER_MUTEX_H are not supported
41 #endif
42
43 #ifndef __wasi__
44
45 // Export an explicit template instantiation of std::atomic<int32_t>.
46 // When building DLLs for Windows this is required as it is used as a data member of the exported SharedObject class.
47 // See digitlst.h, pluralaffix.h, datefmt.h, and others for similar examples.
48 //
49 // Similar story for std::atomic<std::mutex *>, and the exported UMutex class.
50 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN && !defined(U_IN_DOXYGEN)
51 #if defined(__clang__) || defined(_MSC_VER)
52 #if defined(__clang__)
53 // Suppress the warning that the explicit instantiation after explicit specialization has no effect.
54 #pragma clang diagnostic push
55 #pragma clang diagnostic ignored "-Winstantiation-after-specialization"
56 #endif
57 template struct U_COMMON_API std::atomic<int32_t>;
58 template struct U_COMMON_API std::atomic<std::mutex *>;
59 #if defined(__clang__)
60 #pragma clang diagnostic pop
61 #endif
62 #elif defined(__GNUC__)
63 // For GCC this class is already exported/visible, so no need for U_COMMON_API.
64 template struct std::atomic<int32_t>;
65 template struct std::atomic<std::mutex *>;
66 #endif
67 #endif
68
69 #endif
70
71 U_NAMESPACE_BEGIN
72
73 /****************************************************************************
74 *
75 * Low Level Atomic Operations, ICU wrappers for.
76 *
77 ****************************************************************************/
78
79 #ifndef __wasi__
80
81 typedef std::atomic<int32_t> u_atomic_int32_t;
82
83 inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
84 return var.load(std::memory_order_acquire);
85 }
86
87 inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) {
88 var.store(val, std::memory_order_release);
89 }
90
91 inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) {
92 return var->fetch_add(1) + 1;
93 }
94
95 inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) {
96 return var->fetch_sub(1) - 1;
97 }
98
99 #else
100
101 typedef int32_t u_atomic_int32_t;
102
103 inline int32_t umtx_loadAcquire(u_atomic_int32_t &var) {
104 return var;
105 }
106
107 inline void umtx_storeRelease(u_atomic_int32_t &var, int32_t val) {
108 var = val;
109 }
110
111 inline int32_t umtx_atomic_inc(u_atomic_int32_t *var) {
112 return ++(*var);
113 }
114
115 inline int32_t umtx_atomic_dec(u_atomic_int32_t *var) {
116 return --(*var);
117 }
118
119 #endif
120
121 /*************************************************************************************************
122 *
123 * UInitOnce Definitions.
124 *
125 *************************************************************************************************/
126
127 struct U_COMMON_API UInitOnce {
128 u_atomic_int32_t fState {0};
129 UErrorCode fErrCode {U_ZERO_ERROR};
130 void reset() {fState = 0;}
131 UBool isReset() {return umtx_loadAcquire(fState) == 0;}
132 // Note: isReset() is used by service registration code.
133 // Thread safety of this usage needs review.
134 };
135
136 U_COMMON_API UBool U_EXPORT2 umtx_initImplPreInit(UInitOnce &);
137 U_COMMON_API void U_EXPORT2 umtx_initImplPostInit(UInitOnce &);
138
139 template<class T> void umtx_initOnce(UInitOnce &uio, T *obj, void (U_CALLCONV T::*fp)()) {
140 if (umtx_loadAcquire(uio.fState) == 2) {
141 return;
142 }
143 if (umtx_initImplPreInit(uio)) {
144 (obj->*fp)();
145 umtx_initImplPostInit(uio);
146 }
147 }
148
149
150 // umtx_initOnce variant for plain functions, or static class functions.
151 // No context parameter.
152 inline void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)()) {
153 if (umtx_loadAcquire(uio.fState) == 2) {
154 return;
155 }
156 if (umtx_initImplPreInit(uio)) {
157 (*fp)();
158 umtx_initImplPostInit(uio);
159 }
160 }
161
162 // umtx_initOnce variant for plain functions, or static class functions.
163 // With ErrorCode, No context parameter.
164 inline void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(UErrorCode &), UErrorCode &errCode) {
165 if (U_FAILURE(errCode)) {
166 return;
167 }
168 if (umtx_loadAcquire(uio.fState) != 2 && umtx_initImplPreInit(uio)) {
169 // We run the initialization.
170 (*fp)(errCode);
171 uio.fErrCode = errCode;
172 umtx_initImplPostInit(uio);
173 } else {
174 // Someone else already ran the initialization.
175 if (U_FAILURE(uio.fErrCode)) {
176 errCode = uio.fErrCode;
177 }
178 }
179 }
180
181 // umtx_initOnce variant for plain functions, or static class functions,
182 // with a context parameter.
183 template<class T> void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(T), T context) {
184 if (umtx_loadAcquire(uio.fState) == 2) {
185 return;
186 }
187 if (umtx_initImplPreInit(uio)) {
188 (*fp)(context);
189 umtx_initImplPostInit(uio);
190 }
191 }
192
193 // umtx_initOnce variant for plain functions, or static class functions,
194 // with a context parameter and an error code.
195 template<class T> void umtx_initOnce(UInitOnce &uio, void (U_CALLCONV *fp)(T, UErrorCode &), T context, UErrorCode &errCode) {
196 if (U_FAILURE(errCode)) {
197 return;
198 }
199 if (umtx_loadAcquire(uio.fState) != 2 && umtx_initImplPreInit(uio)) {
200 // We run the initialization.
201 (*fp)(context, errCode);
202 uio.fErrCode = errCode;
203 umtx_initImplPostInit(uio);
204 } else {
205 // Someone else already ran the initialization.
206 if (U_FAILURE(uio.fErrCode)) {
207 errCode = uio.fErrCode;
208 }
209 }
210 }
211
212 // UMutex should be constexpr-constructible, so that no initialization code
213 // is run during startup.
214 // This works on all C++ libraries except MS VS before VS2019.
215 #if (defined(_CPPLIB_VER) && !defined(_MSVC_STL_VERSION)) || \
216 (defined(_MSVC_STL_VERSION) && _MSVC_STL_VERSION < 142)
217 // (VS std lib older than VS2017) || (VS std lib version < VS2019)
218 # define UMUTEX_CONSTEXPR
219 #else
220 # define UMUTEX_CONSTEXPR constexpr
221 #endif
222
223 /**
224 * UMutex - ICU Mutex class.
225 *
226 * This is the preferred Mutex class for use within ICU implementation code.
227 * It is a thin wrapper over C++ std::mutex, with these additions:
228 * - Static instances are safe, not triggering static construction or destruction,
229 * and the associated order of construction or destruction issues.
230 * - Plumbed into u_cleanup() for destructing the underlying std::mutex,
231 * which frees any OS level resources they may be holding.
232 *
233 * Limitations:
234 * - Static or global instances only. Cannot be heap allocated. Cannot appear as a
235 * member of another class.
236 * - No condition variables or other advanced features. If needed, you will need to use
237 * std::mutex and std::condition_variable directly. For an example, see unifiedcache.cpp
238 *
239 * Typical Usage:
240 * static UMutex myMutex;
241 *
242 * {
243 * Mutex lock(myMutex);
244 * ... // Do stuff that is protected by myMutex;
245 * } // myMutex is released when lock goes out of scope.
246 */
247
248 class U_COMMON_API UMutex {
249 public:
250 UMUTEX_CONSTEXPR UMutex() {}
251 ~UMutex() = default;
252
253 UMutex(const UMutex &other) = delete;
254 UMutex &operator =(const UMutex &other) = delete;
255 void *operator new(size_t) = delete;
256
257 // requirements for C++ BasicLockable, allows UMutex to work with std::lock_guard
258 void lock() {
259 #ifndef __wasi__
260 std::mutex *m = fMutex.load(std::memory_order_acquire);
261 if (m == nullptr) { m = getMutex(); }
262 m->lock();
263 #endif
264 }
265 void unlock() {
266 #ifndef __wasi__
267 fMutex.load(std::memory_order_relaxed)->unlock();
268 #endif
269 }
270
271 static void cleanup();
272
273 private:
274 #ifndef __wasi__
275 alignas(std::mutex) char fStorage[sizeof(std::mutex)] {};
276 std::atomic<std::mutex *> fMutex { nullptr };
277 #endif
278
279 /** All initialized UMutexes are kept in a linked list, so that they can be found,
280 * and the underlying std::mutex destructed, by u_cleanup().
281 */
282 UMutex *fListLink { nullptr };
283 static UMutex *gListHead;
284
285 /** Out-of-line function to lazily initialize a UMutex on first use.
286 * Initial fast check is inline, in lock(). The returned value may never
287 * be nullptr.
288 */
289 #ifndef __wasi__
290 std::mutex *getMutex();
291 #endif
292 };
293
294
295 /* Lock a mutex.
296 * @param mutex The given mutex to be locked. Pass NULL to specify
297 * the global ICU mutex. Recursive locks are an error
298 * and may cause a deadlock on some platforms.
299 */
300 U_CAPI void U_EXPORT2 umtx_lock(UMutex* mutex);
301
302 /* Unlock a mutex.
303 * @param mutex The given mutex to be unlocked. Pass NULL to specify
304 * the global ICU mutex.
305 */
306 U_CAPI void U_EXPORT2 umtx_unlock (UMutex* mutex);
307
308
309 U_NAMESPACE_END
310
311 #endif /* UMUTEX_H */
312 /*eof*/