OpenShot Audio Library | OpenShotAudio  0.3.1
juce_ScopedLock.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
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.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
26 //==============================================================================
53 template <class LockType>
55 {
56 public:
57  //==============================================================================
67  inline explicit GenericScopedLock (const LockType& lock) noexcept : lock_ (lock) { lock.enter(); }
68 
74  inline ~GenericScopedLock() noexcept { lock_.exit(); }
75 
76 private:
77  //==============================================================================
78  const LockType& lock_;
79 
80  JUCE_DECLARE_NON_COPYABLE (GenericScopedLock)
81 };
82 
83 
84 //==============================================================================
124 template <class LockType>
126 {
127 public:
128  //==============================================================================
139  inline explicit GenericScopedUnlock (const LockType& lock) noexcept : lock_ (lock) { lock.exit(); }
140 
148  inline ~GenericScopedUnlock() noexcept { lock_.enter(); }
149 
150 
151 private:
152  //==============================================================================
153  const LockType& lock_;
154 
155  JUCE_DECLARE_NON_COPYABLE (GenericScopedUnlock)
156 };
157 
158 
159 //==============================================================================
196 template <class LockType>
198 {
199 public:
200  //==============================================================================
217  inline explicit GenericScopedTryLock (const LockType& lock, bool acquireLockOnInitialisation = true) noexcept
218  : lock_ (lock), lockWasSuccessful (acquireLockOnInitialisation && lock.tryEnter()) {}
219 
228  inline ~GenericScopedTryLock() noexcept { if (lockWasSuccessful) lock_.exit(); }
229 
231  bool isLocked() const noexcept { return lockWasSuccessful; }
232 
234  bool retryLock() const noexcept { lockWasSuccessful = lock_.tryEnter(); return lockWasSuccessful; }
235 
236 private:
237  //==============================================================================
238  const LockType& lock_;
239  mutable bool lockWasSuccessful;
240 
241  JUCE_DECLARE_NON_COPYABLE (GenericScopedTryLock)
242 };
243 
244 } // namespace juce
GenericScopedLock(const LockType &lock) noexcept
GenericScopedTryLock(const LockType &lock, bool acquireLockOnInitialisation=true) noexcept
bool isLocked() const noexcept
bool retryLock() const noexcept
GenericScopedUnlock(const LockType &lock) noexcept