1 | /** @file
|
---|
2 | * IPRT - Classes for Scope-based Locking.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2007-2019 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_INCLUDED_cpp_lock_h
|
---|
27 | #define IPRT_INCLUDED_cpp_lock_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/critsect.h>
|
---|
33 | #ifdef RT_LOCK_STRICT
|
---|
34 | # include <iprt/lockvalidator.h>
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | RT_C_DECLS_BEGIN
|
---|
38 |
|
---|
39 | /** @defgroup grp_rt_cpp_lock C++ Scope-based Locking
|
---|
40 | * @ingroup grp_rt_cpp
|
---|
41 | * @{
|
---|
42 | */
|
---|
43 |
|
---|
44 | class RTCLock;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * The mutex lock.
|
---|
48 | *
|
---|
49 | * This is used as an object data member if the intention is to lock
|
---|
50 | * a single object. This can also be used statically, initialized in
|
---|
51 | * a global variable, for class wide purposes.
|
---|
52 | *
|
---|
53 | * This is best used together with RTCLock.
|
---|
54 | */
|
---|
55 | class RTCLockMtx
|
---|
56 | {
|
---|
57 | friend class RTCLock;
|
---|
58 |
|
---|
59 | private:
|
---|
60 | RTCRITSECT mMtx;
|
---|
61 |
|
---|
62 | public:
|
---|
63 | RTCLockMtx()
|
---|
64 | {
|
---|
65 | #ifdef RT_LOCK_STRICT_ORDER
|
---|
66 | RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
|
---|
67 | RTLockValidatorClassCreateUnique(RT_SRC_POS, NULL),
|
---|
68 | RTLOCKVAL_SUB_CLASS_NONE, NULL);
|
---|
69 | #else
|
---|
70 | RTCritSectInit(&mMtx);
|
---|
71 | #endif
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** Use to when creating locks that belongs in the same "class". */
|
---|
75 | RTCLockMtx(RT_SRC_POS_DECL, uint32_t uSubClass = RTLOCKVAL_SUB_CLASS_NONE)
|
---|
76 | {
|
---|
77 | #ifdef RT_LOCK_STRICT_ORDER
|
---|
78 | RTCritSectInitEx(&mMtx, 0 /*fFlags*/,
|
---|
79 | RTLockValidatorClassForSrcPos(RT_SRC_POS_ARGS, NULL),
|
---|
80 | uSubClass, NULL);
|
---|
81 | #else
|
---|
82 | NOREF(uSubClass);
|
---|
83 | RTCritSectInit(&mMtx);
|
---|
84 | RT_SRC_POS_NOREF();
|
---|
85 | #endif
|
---|
86 | }
|
---|
87 |
|
---|
88 | ~RTCLockMtx()
|
---|
89 | {
|
---|
90 | RTCritSectDelete(&mMtx);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /* lock() and unlock() are private so that only friend RTCLock can access
|
---|
94 | them. */
|
---|
95 | private:
|
---|
96 | inline void lock()
|
---|
97 | {
|
---|
98 | RTCritSectEnter(&mMtx);
|
---|
99 | }
|
---|
100 |
|
---|
101 | inline void unlock()
|
---|
102 | {
|
---|
103 | RTCritSectLeave(&mMtx);
|
---|
104 | }
|
---|
105 | };
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * The stack object for automatic locking and unlocking.
|
---|
110 | *
|
---|
111 | * This is a helper class for automatic locks, to simplify requesting a
|
---|
112 | * RTCLockMtx and to not forget releasing it. To request a RTCLockMtx, simply
|
---|
113 | * create an instance of RTCLock on the stack and pass the mutex to it:
|
---|
114 | *
|
---|
115 | * @code
|
---|
116 | extern RTCLockMtx gMtx; // wherever this is
|
---|
117 | ...
|
---|
118 | if (...)
|
---|
119 | {
|
---|
120 | RTCLock lock(gMtx);
|
---|
121 | ... // do stuff
|
---|
122 | // when lock goes out of scope, destructor releases the mutex
|
---|
123 | }
|
---|
124 | @endcode
|
---|
125 | *
|
---|
126 | * You can also explicitly release the mutex by calling RTCLock::release().
|
---|
127 | * This might be helpful if the lock doesn't go out of scope early enough
|
---|
128 | * for your mutex to be released.
|
---|
129 | */
|
---|
130 | class RTCLock
|
---|
131 | {
|
---|
132 | private:
|
---|
133 | /** Reference to the lock we're holding. */
|
---|
134 | RTCLockMtx &m_rMtx;
|
---|
135 | /** Whether we're currently holding the lock of if it was already
|
---|
136 | * explictily released by the release() method. */
|
---|
137 | bool m_fLocked;
|
---|
138 |
|
---|
139 | public:
|
---|
140 | RTCLock(RTCLockMtx &a_rMtx)
|
---|
141 | : m_rMtx(a_rMtx)
|
---|
142 | {
|
---|
143 | m_rMtx.lock();
|
---|
144 | m_fLocked = true;
|
---|
145 | }
|
---|
146 |
|
---|
147 | ~RTCLock()
|
---|
148 | {
|
---|
149 | if (m_fLocked)
|
---|
150 | m_rMtx.unlock();
|
---|
151 | }
|
---|
152 |
|
---|
153 | inline void release()
|
---|
154 | {
|
---|
155 | if (m_fLocked)
|
---|
156 | {
|
---|
157 | m_rMtx.unlock();
|
---|
158 | m_fLocked = false;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | };
|
---|
162 |
|
---|
163 |
|
---|
164 | /** @} */
|
---|
165 |
|
---|
166 | RT_C_DECLS_END
|
---|
167 |
|
---|
168 | #endif /* !IPRT_INCLUDED_cpp_lock_h */
|
---|
169 |
|
---|