/src/icu/source/common/mutex.h
Line | Count | Source |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ****************************************************************************** |
5 | | * |
6 | | * Copyright (C) 1997-2013, International Business Machines |
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 | | |
31 | | //---------------------------------------------------------------------------- |
32 | | // Code within that accesses shared static or global data should |
33 | | // should instantiate a Mutex object while doing so. You should make your own |
34 | | // private mutex where possible. |
35 | | |
36 | | // For example: |
37 | | // |
38 | | // UMutex myMutex; |
39 | | // |
40 | | // void Function(int arg1, int arg2) |
41 | | // { |
42 | | // static Object* foo; // Shared read-write object |
43 | | // Mutex mutex(&myMutex); // or no args for the global lock |
44 | | // foo->Method(); |
45 | | // // When 'mutex' goes out of scope and gets destroyed here, the lock is released |
46 | | // } |
47 | | // |
48 | | // Note: Do NOT use the form 'Mutex mutex();' as that merely forward-declares a function |
49 | | // returning a Mutex. This is a common mistake which silently slips through the |
50 | | // compiler!! |
51 | | // |
52 | | |
53 | | class U_COMMON_API Mutex : public UMemory { |
54 | | public: |
55 | | inline Mutex(UMutex *mutex = NULL); |
56 | | inline ~Mutex(); |
57 | | |
58 | | private: |
59 | | UMutex *fMutex; |
60 | | |
61 | | Mutex(const Mutex &other); // forbid copying of this class |
62 | | Mutex &operator=(const Mutex &other); // forbid copying of this class |
63 | | }; |
64 | | |
65 | | inline Mutex::Mutex(UMutex *mutex) |
66 | 18.5k | : fMutex(mutex) |
67 | 18.5k | { |
68 | 18.5k | umtx_lock(fMutex); |
69 | 18.5k | } |
70 | | |
71 | | inline Mutex::~Mutex() |
72 | 18.5k | { |
73 | 18.5k | umtx_unlock(fMutex); |
74 | 18.5k | } |
75 | | |
76 | | U_NAMESPACE_END |
77 | | |
78 | | #endif //_MUTEX_ |
79 | | //eof |