]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
b75a7d8f A |
3 | /* |
4 | ****************************************************************************** | |
5 | * | |
57a6839d | 6 | * Copyright (C) 1997-2013, International Business Machines |
b75a7d8f A |
7 | * Corporation and others. All Rights Reserved. |
8 | * | |
9 | ****************************************************************************** | |
10 | */ | |
11 | //---------------------------------------------------------------------------- | |
12 | // File: mutex.h | |
13 | // | |
14 | // Lightweight C++ wrapper for umtx_ C mutex functions | |
15 | // | |
16 | // Author: Alan Liu 1/31/97 | |
17 | // History: | |
18 | // 06/04/97 helena Updated setImplementation as per feedback from 5/21 drop. | |
19 | // 04/07/1999 srl refocused as a thin wrapper | |
20 | // | |
21 | //---------------------------------------------------------------------------- | |
22 | #ifndef MUTEX_H | |
23 | #define MUTEX_H | |
24 | ||
25 | #include "unicode/utypes.h" | |
26 | #include "unicode/uobject.h" | |
27 | #include "umutex.h" | |
28 | ||
29 | U_NAMESPACE_BEGIN | |
30 | ||
3d1f044b A |
31 | /** |
32 | * Mutex is a helper class for convenient locking and unlocking of a UMutex. | |
33 | * | |
34 | * Creating a local scope Mutex will lock a UMutex, holding the lock until the Mutex | |
35 | * goes out of scope. | |
36 | * | |
37 | * If no UMutex is specified, the ICU global mutex is implied. | |
38 | * | |
39 | * For example: | |
40 | * | |
41 | * UMutex *myMutex() { | |
42 | * static UMutex *m = STATIC_NEW(UMutex); | |
43 | * return m; | |
44 | * } | |
45 | * | |
46 | * void Function(int arg1, int arg2) | |
47 | * { | |
48 | * static Object* foo; // Shared read-write object | |
49 | * Mutex mutex(myMutex()); // or no args for the global lock | |
50 | * foo->Method(); | |
51 | * // When 'mutex' goes out of scope and gets destroyed here, the lock is released | |
52 | * } | |
53 | * | |
54 | * Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function | |
55 | * returning a Mutex. This is a common mistake which silently slips through the | |
56 | * compiler!! | |
57 | */ | |
b75a7d8f A |
58 | |
59 | class U_COMMON_API Mutex : public UMemory { | |
60 | public: | |
3d1f044b A |
61 | Mutex(UMutex *mutex = nullptr) : fMutex(mutex) { |
62 | umtx_lock(fMutex); | |
63 | }; | |
64 | ~Mutex() { | |
65 | umtx_unlock(fMutex); | |
66 | }; | |
b75a7d8f | 67 | |
3d1f044b A |
68 | Mutex(const Mutex &other) = delete; // forbid assigning of this class |
69 | Mutex &operator=(const Mutex &other) = delete; // forbid copying of this class | |
70 | void *operator new(size_t s) = delete; // forbid heap allocation. Locals only. | |
b75a7d8f | 71 | |
3d1f044b A |
72 | private: |
73 | UMutex *fMutex; | |
b75a7d8f A |
74 | }; |
75 | ||
b75a7d8f A |
76 | |
77 | U_NAMESPACE_END | |
78 | ||
79 | #endif //_MUTEX_ | |
80 | //eof |