VirtualBox

source: vbox/trunk/include/iprt/cpp/lock.h@ 30254

Last change on this file since 30254 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
File size: 4.0 KB
Line 
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
34RT_C_DECLS_BEGIN
35
36/** @defgroup grp_rt_lock RTLock - Scope-based Locking (C++).
37 * @ingroup grp_rt
38 * @{
39 */
40
41class 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 */
52class 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 */
127class 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
160RT_C_DECLS_END
161
162#endif
163
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette