1 | /** @file
|
---|
2 | * IPRT - RTLock Classes for Scope-based Locking.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2007 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_cpp_lock_h
|
---|
27 | #define ___iprt_cpp_lock_h
|
---|
28 |
|
---|
29 | #include <iprt/critsect.h>
|
---|
30 | #ifdef RT_LOCK_STRICT
|
---|
31 | # include <iprt/lockvalidator.h>
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | RT_C_DECLS_BEGIN
|
---|
35 |
|
---|
36 | /** @defgroup grp_rt_lock RTLock - Scope-based Locking (C++).
|
---|
37 | * @ingroup grp_rt
|
---|
38 | * @{
|
---|
39 | */
|
---|
40 |
|
---|
41 | class RTLock;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * The mutex lock.
|
---|
45 | *
|
---|
46 | * This is used as a object data member if the intention is to lock
|
---|
47 | * a single object. This can also be used statically, initialized in
|
---|
48 | * a global variable, for class wide purposes.
|
---|
49 | *
|
---|
50 | * This is best used together with RTLock.
|
---|
51 | */
|
---|
52 | class RTLockMtx
|
---|
53 | {
|
---|
54 | friend class RTLock;
|
---|
55 |
|
---|
56 | private:
|
---|
57 | RTCRITSECT mMtx;
|
---|
58 |
|
---|
59 | public:
|
---|
60 | RTLockMtx()
|
---|
61 | {
|
---|
62 | #ifdef RT_LOCK_STRICT_ORDER
|
---|
63 | RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
|
---|
64 | RTLockValidatorClassCreateUnique(RT_SRC_POS, NULL),
|
---|
65 | RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
66 | #else
|
---|
67 | RTCritSectInit(&mMtx);
|
---|
68 | #endif
|
---|
69 | }
|
---|
70 |
|
---|
71 | /** Use to when creating locks that belongs in the same "class". */
|
---|
72 | RTLockMtx(RT_SRC_POS_DECL, uint32_t uSubClass = RTLOCKVAL_SUB_CLASS_NONE)
|
---|
73 | {
|
---|
74 | #ifdef RT_LOCK_STRICT_ORDER
|
---|
75 | RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
|
---|
76 | RTLockValidatorClassForSrcPos(RT_SRC_POS_ARGS, NULL),
|
---|
77 | uSubClass, NULL);
|
---|
78 | #else
|
---|
79 | RTCritSectInit(&mMtx);
|
---|
80 | RT_SRC_POS_NOREF();
|
---|
81 | #endif
|
---|
82 | }
|
---|
83 |
|
---|
84 | ~RTLockMtx()
|
---|
85 | {
|
---|
86 | RTCritSectDelete(&mMtx);
|
---|
87 | }
|
---|
88 |
|
---|
89 | // lock() and unlock() are private so that only
|
---|
90 | // friend RTLock can access them
|
---|
91 | private:
|
---|
92 | inline void lock()
|
---|
93 | {
|
---|
94 | RTCritSectEnter(&mMtx);
|
---|
95 | }
|
---|
96 |
|
---|
97 | inline void unlock()
|
---|
98 | {
|
---|
99 | RTCritSectLeave(&mMtx);
|
---|
100 | }
|
---|
101 | };
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * The stack object for automatic locking and unlocking.
|
---|
106 | *
|
---|
107 | * This is a helper class for automatic locks, to simplify
|
---|
108 | * requesting a RTLockMtx and to not forget releasing it.
|
---|
109 | * To request a RTLockMtx, simply create an instance of RTLock
|
---|
110 | * on the stack and pass the mutex to it:
|
---|
111 | *
|
---|
112 | * @code
|
---|
113 | extern RTLockMtx gMtx; // wherever this is
|
---|
114 | ...
|
---|
115 | if (...)
|
---|
116 | {
|
---|
117 | RTLock lock(gMtx);
|
---|
118 | ... // do stuff
|
---|
119 | // when lock goes out of scope, destructor releases the mutex
|
---|
120 | }
|
---|
121 | @endcode
|
---|
122 | *
|
---|
123 | * You can also explicitly release the mutex by calling RTLock::release().
|
---|
124 | * This might be helpful if the lock doesn't go out of scope early enough
|
---|
125 | * for your mutex to be released.
|
---|
126 | */
|
---|
127 | class RTLock
|
---|
128 | {
|
---|
129 | private:
|
---|
130 | RTLockMtx &mMtx;
|
---|
131 | bool mfLocked;
|
---|
132 |
|
---|
133 | public:
|
---|
134 | RTLock(RTLockMtx &aMtx)
|
---|
135 | : mMtx(aMtx)
|
---|
136 | {
|
---|
137 | mMtx.lock();
|
---|
138 | mfLocked = true;
|
---|
139 | }
|
---|
140 |
|
---|
141 | ~RTLock()
|
---|
142 | {
|
---|
143 | if (mfLocked)
|
---|
144 | mMtx.unlock();
|
---|
145 | }
|
---|
146 |
|
---|
147 | inline void release()
|
---|
148 | {
|
---|
149 | if (mfLocked)
|
---|
150 | {
|
---|
151 | mMtx.unlock();
|
---|
152 | mfLocked = false;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | };
|
---|
156 |
|
---|
157 |
|
---|
158 | /** @} */
|
---|
159 |
|
---|
160 | RT_C_DECLS_END
|
---|
161 |
|
---|
162 | #endif
|
---|
163 |
|
---|