OpenShot Library | OpenShotAudio  0.2.2
juce_ScopedReadLock.h
1 
2 /** @weakgroup juce_core-threads
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  Automatically locks and unlocks a ReadWriteLock object.
33 
34  Use one of these as a local variable to control access to a ReadWriteLock.
35 
36  e.g. @code
37 
38  ReadWriteLock myLock;
39 
40  for (;;)
41  {
42  const ScopedReadLock myScopedLock (myLock);
43  // myLock is now locked
44 
45  ...do some stuff...
46 
47  // myLock gets unlocked here.
48  }
49  @endcode
50 
51  @see ReadWriteLock, ScopedWriteLock
52 
53  @tags{Core}
54 */
56 {
57 public:
58  //==============================================================================
59  /** Creates a ScopedReadLock.
60 
61  As soon as it is created, this will call ReadWriteLock::enterRead(), and
62  when the ScopedReadLock object is deleted, the ReadWriteLock will
63  be unlocked.
64 
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.
68  */
69  inline explicit ScopedReadLock (const ReadWriteLock& lock) noexcept : lock_ (lock) { lock.enterRead(); }
70 
71  /** Destructor.
72 
73  The ReadWriteLock's exitRead() method will be called when the destructor is called.
74 
75  Make sure this object is created and deleted by the same thread,
76  otherwise there are no guarantees what will happen!
77  */
78  inline ~ScopedReadLock() noexcept { lock_.exitRead(); }
79 
80 
81 private:
82  //==============================================================================
83  const ReadWriteLock& lock_;
84 
85  JUCE_DECLARE_NON_COPYABLE (ScopedReadLock)
86 };
87 
88 } // namespace juce
89 
90 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
A critical section that allows multiple simultaneous readers.
~ScopedReadLock() noexcept
Destructor.
Automatically locks and unlocks a ReadWriteLock object.
ScopedReadLock(const ReadWriteLock &lock) noexcept
Creates a ScopedReadLock.