VirtualBox

source: vbox/trunk/include/iprt/lock.h@ 6951

Last change on this file since 6951 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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