1 | /** @file
|
---|
2 | * IPRT - RTLock Classes for Scope-based Locking.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef ___iprt_lock_h
|
---|
31 | #define ___iprt_lock_h
|
---|
32 |
|
---|
33 | #include <iprt/critsect.h>
|
---|
34 |
|
---|
35 | __BEGIN_DECLS
|
---|
36 |
|
---|
37 | /** @defgroup grp_rt_lock RTLock - Scope-based Locking (C++).
|
---|
38 | * @ingroup grp_rt
|
---|
39 | * @{
|
---|
40 | */
|
---|
41 |
|
---|
42 | class RTLock;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * The mutex lock.
|
---|
46 | *
|
---|
47 | * This is used as a object data member if the intention is to lock
|
---|
48 | * a single object. This can also be used statically, initialized in
|
---|
49 | * a global variable, for class wide purposes.
|
---|
50 | *
|
---|
51 | * This is best used together with RTLock.
|
---|
52 | */
|
---|
53 | class RTLockMtx
|
---|
54 | {
|
---|
55 | friend class RTLock;
|
---|
56 |
|
---|
57 | private:
|
---|
58 | RTCRITSECT mMtx;
|
---|
59 |
|
---|
60 | public:
|
---|
61 | RTLockMtx()
|
---|
62 | {
|
---|
63 | RTCritSectInit(&mMtx);
|
---|
64 | }
|
---|
65 |
|
---|
66 | ~RTLockMtx()
|
---|
67 | {
|
---|
68 | RTCritSectDelete(&mMtx);
|
---|
69 | }
|
---|
70 |
|
---|
71 | // lock() and unlock() are private so that only
|
---|
72 | // friend RTLock can access them
|
---|
73 | private:
|
---|
74 | inline void lock()
|
---|
75 | {
|
---|
76 | RTCritSectEnter(&mMtx);
|
---|
77 | }
|
---|
78 |
|
---|
79 | inline void unlock()
|
---|
80 | {
|
---|
81 | RTCritSectLeave(&mMtx);
|
---|
82 | }
|
---|
83 | };
|
---|
84 |
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * The stack object for automatic locking and unlocking.
|
---|
88 | *
|
---|
89 | * This is a helper class for automatic locks, to simplify
|
---|
90 | * requesting a RTLockMtx and to not forget releasing it.
|
---|
91 | * To request a RTLockMtx, simply create an instance of RTLock
|
---|
92 | * on the stack and pass the mutex to it:
|
---|
93 | *
|
---|
94 | * @code
|
---|
95 | extern RTLockMtx gMtx; // wherever this is
|
---|
96 | ...
|
---|
97 | if (...)
|
---|
98 | {
|
---|
99 | RTLock lock(gMtx);
|
---|
100 | ... // do stuff
|
---|
101 | // when lock goes out of scope, destructor releases the mutex
|
---|
102 | }
|
---|
103 | @endcode
|
---|
104 | *
|
---|
105 | * You can also explicitly release the mutex by calling RTLock::release().
|
---|
106 | * This might be helpful if the lock doesn't go out of scope early enough
|
---|
107 | * for your mutex to be released.
|
---|
108 | */
|
---|
109 | class RTLock
|
---|
110 | {
|
---|
111 | private:
|
---|
112 | RTLockMtx &mMtx;
|
---|
113 | bool mfLocked;
|
---|
114 |
|
---|
115 | public:
|
---|
116 | RTLock(RTLockMtx &aMtx)
|
---|
117 | : mMtx(aMtx)
|
---|
118 | {
|
---|
119 | mMtx.lock();
|
---|
120 | mfLocked = true;
|
---|
121 | }
|
---|
122 |
|
---|
123 | ~RTLock()
|
---|
124 | {
|
---|
125 | if (mfLocked)
|
---|
126 | mMtx.unlock();
|
---|
127 | }
|
---|
128 |
|
---|
129 | inline void release()
|
---|
130 | {
|
---|
131 | if (mfLocked)
|
---|
132 | {
|
---|
133 | mMtx.unlock();
|
---|
134 | mfLocked = false;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | };
|
---|
138 |
|
---|
139 |
|
---|
140 | /** @} */
|
---|
141 |
|
---|
142 | __END_DECLS
|
---|
143 |
|
---|
144 | #endif
|
---|
145 |
|
---|