juce_ScopedLock.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_SCOPEDLOCK_H_INCLUDED
  22. #define JUCE_SCOPEDLOCK_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Automatically locks and unlocks a mutex object.
  26. Use one of these as a local variable to provide RAII-based locking of a mutex.
  27. The templated class could be a CriticalSection, SpinLock, or anything else that
  28. provides enter() and exit() methods.
  29. e.g. @code
  30. CriticalSection myCriticalSection;
  31. for (;;)
  32. {
  33. const GenericScopedLock<CriticalSection> myScopedLock (myCriticalSection);
  34. // myCriticalSection is now locked
  35. ...do some stuff...
  36. // myCriticalSection gets unlocked here.
  37. }
  38. @endcode
  39. @see GenericScopedUnlock, CriticalSection, SpinLock, ScopedLock, ScopedUnlock
  40. */
  41. template <class LockType>
  42. class GenericScopedLock
  43. {
  44. public:
  45. //==============================================================================
  46. /** Creates a GenericScopedLock.
  47. As soon as it is created, this will acquire the lock, and when the GenericScopedLock
  48. object is deleted, the lock will be released.
  49. Make sure this object is created and deleted by the same thread,
  50. otherwise there are no guarantees what will happen! Best just to use it
  51. as a local stack object, rather than creating one with the new() operator.
  52. */
  53. inline explicit GenericScopedLock (const LockType& lock) noexcept : lock_ (lock) { lock.enter(); }
  54. /** Destructor.
  55. The lock will be released when the destructor is called.
  56. Make sure this object is created and deleted by the same thread, otherwise there are
  57. no guarantees what will happen!
  58. */
  59. inline ~GenericScopedLock() noexcept { lock_.exit(); }
  60. private:
  61. //==============================================================================
  62. const LockType& lock_;
  63. JUCE_DECLARE_NON_COPYABLE (GenericScopedLock)
  64. };
  65. //==============================================================================
  66. /**
  67. Automatically unlocks and re-locks a mutex object.
  68. This is the reverse of a GenericScopedLock object - instead of locking the mutex
  69. for the lifetime of this object, it unlocks it.
  70. Make sure you don't try to unlock mutexes that aren't actually locked!
  71. e.g. @code
  72. CriticalSection myCriticalSection;
  73. for (;;)
  74. {
  75. const GenericScopedLock<CriticalSection> myScopedLock (myCriticalSection);
  76. // myCriticalSection is now locked
  77. ... do some stuff with it locked ..
  78. while (xyz)
  79. {
  80. ... do some stuff with it locked ..
  81. const GenericScopedUnlock<CriticalSection> unlocker (myCriticalSection);
  82. // myCriticalSection is now unlocked for the remainder of this block,
  83. // and re-locked at the end.
  84. ...do some stuff with it unlocked ...
  85. }
  86. // myCriticalSection gets unlocked here.
  87. }
  88. @endcode
  89. @see GenericScopedLock, CriticalSection, ScopedLock, ScopedUnlock
  90. */
  91. template <class LockType>
  92. class GenericScopedUnlock
  93. {
  94. public:
  95. //==============================================================================
  96. /** Creates a GenericScopedUnlock.
  97. As soon as it is created, this will unlock the CriticalSection, and
  98. when the ScopedLock object is deleted, the CriticalSection will
  99. be re-locked.
  100. Make sure this object is created and deleted by the same thread,
  101. otherwise there are no guarantees what will happen! Best just to use it
  102. as a local stack object, rather than creating one with the new() operator.
  103. */
  104. inline explicit GenericScopedUnlock (const LockType& lock) noexcept : lock_ (lock) { lock.exit(); }
  105. /** Destructor.
  106. The CriticalSection will be unlocked when the destructor is called.
  107. Make sure this object is created and deleted by the same thread,
  108. otherwise there are no guarantees what will happen!
  109. */
  110. inline ~GenericScopedUnlock() noexcept { lock_.enter(); }
  111. private:
  112. //==============================================================================
  113. const LockType& lock_;
  114. JUCE_DECLARE_NON_COPYABLE (GenericScopedUnlock)
  115. };
  116. //==============================================================================
  117. /**
  118. Automatically locks and unlocks a mutex object.
  119. Use one of these as a local variable to provide RAII-based locking of a mutex.
  120. The templated class could be a CriticalSection, SpinLock, or anything else that
  121. provides enter() and exit() methods.
  122. e.g. @code
  123. CriticalSection myCriticalSection;
  124. for (;;)
  125. {
  126. const GenericScopedTryLock<CriticalSection> myScopedTryLock (myCriticalSection);
  127. // Unlike using a ScopedLock, this may fail to actually get the lock, so you
  128. // should test this with the isLocked() method before doing your thread-unsafe
  129. // action..
  130. if (myScopedTryLock.isLocked())
  131. {
  132. ...do some stuff...
  133. }
  134. else
  135. {
  136. ..our attempt at locking failed because another thread had already locked it..
  137. }
  138. // myCriticalSection gets unlocked here (if it was locked)
  139. }
  140. @endcode
  141. @see CriticalSection::tryEnter, GenericScopedLock, GenericScopedUnlock
  142. */
  143. template <class LockType>
  144. class GenericScopedTryLock
  145. {
  146. public:
  147. //==============================================================================
  148. /** Creates a GenericScopedTryLock.
  149. As soon as it is created, this will attempt to acquire the lock, and when the
  150. GenericScopedTryLock is deleted, the lock will be released (if the lock was
  151. successfully acquired).
  152. Make sure this object is created and deleted by the same thread,
  153. otherwise there are no guarantees what will happen! Best just to use it
  154. as a local stack object, rather than creating one with the new() operator.
  155. */
  156. inline explicit GenericScopedTryLock (const LockType& lock) noexcept
  157. : lock_ (lock), lockWasSuccessful (lock.tryEnter()) {}
  158. /** Destructor.
  159. The mutex will be unlocked (if it had been successfully locked) when the
  160. destructor is called.
  161. Make sure this object is created and deleted by the same thread,
  162. otherwise there are no guarantees what will happen!
  163. */
  164. inline ~GenericScopedTryLock() noexcept { if (lockWasSuccessful) lock_.exit(); }
  165. /** Returns true if the mutex was successfully locked. */
  166. bool isLocked() const noexcept { return lockWasSuccessful; }
  167. private:
  168. //==============================================================================
  169. const LockType& lock_;
  170. const bool lockWasSuccessful;
  171. JUCE_DECLARE_NON_COPYABLE (GenericScopedTryLock)
  172. };
  173. #endif // JUCE_SCOPEDLOCK_H_INCLUDED