2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
20 ==============================================================================
26 //==============================================================================
28 Automatically locks and unlocks a ReadWriteLock object.
30 Use one of these as a local variable to control access to a ReadWriteLock.
38 const ScopedWriteLock myScopedLock (myLock);
39 // myLock is now locked
43 // myLock gets unlocked here.
47 @see ReadWriteLock, ScopedReadLock
51 class JUCE_API ScopedWriteLock
54 //==============================================================================
55 /** Creates a ScopedWriteLock.
57 As soon as it is created, this will call ReadWriteLock::enterWrite(), and
58 when the ScopedWriteLock object is deleted, the ReadWriteLock will
61 Make sure this object is created and deleted by the same thread,
62 otherwise there are no guarantees what will happen! Best just to use it
63 as a local stack object, rather than creating one with the new() operator.
65 inline explicit ScopedWriteLock (const ReadWriteLock
& lock
) noexcept
: lock_ (lock
) { lock
.enterWrite(); }
69 The ReadWriteLock's exitWrite() method will be called when the destructor is called.
71 Make sure this object is created and deleted by the same thread,
72 otherwise there are no guarantees what will happen!
74 inline ~ScopedWriteLock() noexcept
{ lock_
.exitWrite(); }
78 //==============================================================================
79 const ReadWriteLock
& lock_
;
81 JUCE_DECLARE_NON_COPYABLE (ScopedWriteLock
)
84 //==============================================================================
86 Automatically locks and unlocks a ReadWriteLock object.
88 Use one of these as a local variable to control access to a ReadWriteLock.
96 const ScopedTryWriteLock myScopedTryLock (myLock);
98 // Unlike using a ScopedWriteLock, this may fail to actually get the lock, so you
99 // should test this with the isLocked() method before doing your thread-unsafe
102 if (myScopedTryLock.isLocked())
108 ..our attempt at locking failed because some other thread has already locked the object..
111 // myLock gets unlocked here (if it was locked).
115 @see ReadWriteLock, ScopedTryWriteLock
119 class JUCE_API ScopedTryWriteLock
122 //==============================================================================
123 /** Creates a ScopedTryWriteLock and calls ReadWriteLock::tryEnterWrite() immediately.
124 When the ScopedTryWriteLock object is destructed, the ReadWriteLock will be unlocked
125 (if it was successfully acquired).
127 Make sure this object is created and destructed by the same thread, otherwise there are no
128 guarantees what will happen! Best just to use it as a local stack object, rather than creating
129 one with the new() operator.
131 ScopedTryWriteLock (ReadWriteLock
& lockIn
) noexcept
132 : ScopedTryWriteLock (lockIn
, true) {}
134 /** Creates a ScopedTryWriteLock.
136 If acquireLockOnInitialisation is true then as soon as it is created, this will call
137 ReadWriteLock::tryEnterWrite(), and when the ScopedTryWriteLock object is destructed, the
138 ReadWriteLock will be unlocked (if it was successfully acquired).
140 Make sure this object is created and destructed by the same thread, otherwise there are no
141 guarantees what will happen! Best just to use it as a local stack object, rather than creating
142 one with the new() operator.
144 ScopedTryWriteLock (ReadWriteLock
& lockIn
, bool acquireLockOnInitialisation
) noexcept
145 : lock (lockIn
), lockWasSuccessful (acquireLockOnInitialisation
&& lock
.tryEnterWrite()) {}
149 The ReadWriteLock's exitWrite() method will be called when the destructor is called.
151 Make sure this object is created and destructed by the same thread,
152 otherwise there are no guarantees what will happen!
154 ~ScopedTryWriteLock() noexcept
{ if (lockWasSuccessful
) lock
.exitWrite(); }
156 /** Returns true if the mutex was successfully locked. */
157 bool isLocked() const noexcept
{ return lockWasSuccessful
; }
159 /** Retry gaining the lock by calling tryEnter on the underlying lock. */
160 bool retryLock() noexcept
{ return lockWasSuccessful
= lock
.tryEnterWrite(); }
163 //==============================================================================
165 bool lockWasSuccessful
;
167 JUCE_DECLARE_NON_COPYABLE (ScopedTryWriteLock
)