2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_SCOPEDLOCK_JUCEHEADER__
27 #define __JUCE_SCOPEDLOCK_JUCEHEADER__
30 //==============================================================================
32 Automatically locks and unlocks a mutex object.
34 Use one of these as a local variable to provide RAII-based locking of a mutex.
36 The templated class could be a CriticalSection, SpinLock, or anything else that
37 provides enter() and exit() methods.
40 CriticalSection myCriticalSection;
44 const GenericScopedLock<CriticalSection> myScopedLock (myCriticalSection);
45 // myCriticalSection is now locked
49 // myCriticalSection gets unlocked here.
53 @see GenericScopedUnlock, CriticalSection, SpinLock, ScopedLock, ScopedUnlock
55 template <class LockType
>
56 class GenericScopedLock
59 //==============================================================================
60 /** Creates a GenericScopedLock.
62 As soon as it is created, this will acquire the lock, and when the GenericScopedLock
63 object is deleted, the lock will be released.
65 Make sure this object is created and deleted by the same thread,
66 otherwise there are no guarantees what will happen! Best just to use it
67 as a local stack object, rather than creating one with the new() operator.
69 inline explicit GenericScopedLock (const LockType
& lock
) noexcept
: lock_ (lock
) { lock
.enter(); }
72 The lock will be released when the destructor is called.
73 Make sure this object is created and deleted by the same thread, otherwise there are
74 no guarantees what will happen!
76 inline ~GenericScopedLock() noexcept
{ lock_
.exit(); }
79 //==============================================================================
80 const LockType
& lock_
;
82 JUCE_DECLARE_NON_COPYABLE (GenericScopedLock
);
86 //==============================================================================
88 Automatically unlocks and re-locks a mutex object.
90 This is the reverse of a GenericScopedLock object - instead of locking the mutex
91 for the lifetime of this object, it unlocks it.
93 Make sure you don't try to unlock mutexes that aren't actually locked!
97 CriticalSection myCriticalSection;
101 const GenericScopedLock<CriticalSection> myScopedLock (myCriticalSection);
102 // myCriticalSection is now locked
104 ... do some stuff with it locked ..
108 ... do some stuff with it locked ..
110 const GenericScopedUnlock<CriticalSection> unlocker (myCriticalSection);
112 // myCriticalSection is now unlocked for the remainder of this block,
113 // and re-locked at the end.
115 ...do some stuff with it unlocked ...
118 // myCriticalSection gets unlocked here.
122 @see GenericScopedLock, CriticalSection, ScopedLock, ScopedUnlock
124 template <class LockType
>
125 class GenericScopedUnlock
128 //==============================================================================
129 /** Creates a GenericScopedUnlock.
131 As soon as it is created, this will unlock the CriticalSection, and
132 when the ScopedLock object is deleted, the CriticalSection will
135 Make sure this object is created and deleted by the same thread,
136 otherwise there are no guarantees what will happen! Best just to use it
137 as a local stack object, rather than creating one with the new() operator.
139 inline explicit GenericScopedUnlock (const LockType
& lock
) noexcept
: lock_ (lock
) { lock
.exit(); }
143 The CriticalSection will be unlocked when the destructor is called.
145 Make sure this object is created and deleted by the same thread,
146 otherwise there are no guarantees what will happen!
148 inline ~GenericScopedUnlock() noexcept
{ lock_
.enter(); }
152 //==============================================================================
153 const LockType
& lock_
;
155 JUCE_DECLARE_NON_COPYABLE (GenericScopedUnlock
);
159 //==============================================================================
161 Automatically locks and unlocks a mutex object.
163 Use one of these as a local variable to provide RAII-based locking of a mutex.
165 The templated class could be a CriticalSection, SpinLock, or anything else that
166 provides enter() and exit() methods.
170 CriticalSection myCriticalSection;
174 const GenericScopedTryLock<CriticalSection> myScopedTryLock (myCriticalSection);
176 // Unlike using a ScopedLock, this may fail to actually get the lock, so you
177 // should test this with the isLocked() method before doing your thread-unsafe
179 if (myScopedTryLock.isLocked())
185 ..our attempt at locking failed because another thread had already locked it..
188 // myCriticalSection gets unlocked here (if it was locked)
192 @see CriticalSection::tryEnter, GenericScopedLock, GenericScopedUnlock
194 template <class LockType
>
195 class GenericScopedTryLock
198 //==============================================================================
199 /** Creates a GenericScopedTryLock.
201 As soon as it is created, this will attempt to acquire the lock, and when the
202 GenericScopedTryLock is deleted, the lock will be released (if the lock was
203 successfully acquired).
205 Make sure this object is created and deleted by the same thread,
206 otherwise there are no guarantees what will happen! Best just to use it
207 as a local stack object, rather than creating one with the new() operator.
209 inline explicit GenericScopedTryLock (const LockType
& lock
) noexcept
210 : lock_ (lock
), lockWasSuccessful (lock
.tryEnter()) {}
214 The mutex will be unlocked (if it had been successfully locked) when the
215 destructor is called.
217 Make sure this object is created and deleted by the same thread,
218 otherwise there are no guarantees what will happen!
220 inline ~GenericScopedTryLock() noexcept
{ if (lockWasSuccessful
) lock_
.exit(); }
222 /** Returns true if the mutex was successfully locked. */
223 bool isLocked() const noexcept
{ return lockWasSuccessful
; }
226 //==============================================================================
227 const LockType
& lock_
;
228 const bool lockWasSuccessful
;
230 JUCE_DECLARE_NON_COPYABLE (GenericScopedTryLock
);
234 #endif // __JUCE_SCOPEDLOCK_JUCEHEADER__