The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Automatically locks and unlocks a mutex object.
  22. Use one of these as a local variable to provide RAII-based locking of a mutex.
  23. The templated class could be a CriticalSection, SpinLock, or anything else that
  24. provides enter() and exit() methods.
  25. e.g. @code
  26. CriticalSection myCriticalSection;
  27. for (;;)
  28. {
  29. const GenericScopedLock<CriticalSection> myScopedLock (myCriticalSection);
  30. // myCriticalSection is now locked
  31. ...do some stuff...
  32. // myCriticalSection gets unlocked here.
  33. }
  34. @endcode
  35. @see GenericScopedUnlock, CriticalSection, SpinLock, ScopedLock, ScopedUnlock
  36. */
  37. template <class LockType>
  38. class GenericScopedLock
  39. {
  40. public:
  41. //==============================================================================
  42. /** Creates a GenericScopedLock.
  43. As soon as it is created, this will acquire the lock, and when the GenericScopedLock
  44. object is deleted, the lock will be released.
  45. Make sure this object is created and deleted by the same thread,
  46. otherwise there are no guarantees what will happen! Best just to use it
  47. as a local stack object, rather than creating one with the new() operator.
  48. */
  49. inline explicit GenericScopedLock (const LockType& lock) noexcept : lock_ (lock) { lock.enter(); }
  50. /** Destructor.
  51. The lock will be released when the destructor is called.
  52. Make sure this object is created and deleted by the same thread, otherwise there are
  53. no guarantees what will happen!
  54. */
  55. inline ~GenericScopedLock() noexcept { lock_.exit(); }
  56. private:
  57. //==============================================================================
  58. const LockType& lock_;
  59. JUCE_DECLARE_NON_COPYABLE (GenericScopedLock)
  60. };
  61. //==============================================================================
  62. /**
  63. Automatically unlocks and re-locks a mutex object.
  64. This is the reverse of a GenericScopedLock object - instead of locking the mutex
  65. for the lifetime of this object, it unlocks it.
  66. Make sure you don't try to unlock mutexes that aren't actually locked!
  67. e.g. @code
  68. CriticalSection myCriticalSection;
  69. for (;;)
  70. {
  71. const GenericScopedLock<CriticalSection> myScopedLock (myCriticalSection);
  72. // myCriticalSection is now locked
  73. ... do some stuff with it locked ..
  74. while (xyz)
  75. {
  76. ... do some stuff with it locked ..
  77. const GenericScopedUnlock<CriticalSection> unlocker (myCriticalSection);
  78. // myCriticalSection is now unlocked for the remainder of this block,
  79. // and re-locked at the end.
  80. ...do some stuff with it unlocked ...
  81. }
  82. // myCriticalSection gets unlocked here.
  83. }
  84. @endcode
  85. @see GenericScopedLock, CriticalSection, ScopedLock, ScopedUnlock
  86. */
  87. template <class LockType>
  88. class GenericScopedUnlock
  89. {
  90. public:
  91. //==============================================================================
  92. /** Creates a GenericScopedUnlock.
  93. As soon as it is created, this will unlock the CriticalSection, and
  94. when the ScopedLock object is deleted, the CriticalSection will
  95. be re-locked.
  96. Make sure this object is created and deleted by the same thread,
  97. otherwise there are no guarantees what will happen! Best just to use it
  98. as a local stack object, rather than creating one with the new() operator.
  99. */
  100. inline explicit GenericScopedUnlock (const LockType& lock) noexcept : lock_ (lock) { lock.exit(); }
  101. /** Destructor.
  102. The CriticalSection will be unlocked when the destructor is called.
  103. Make sure this object is created and deleted by the same thread,
  104. otherwise there are no guarantees what will happen!
  105. */
  106. inline ~GenericScopedUnlock() noexcept { lock_.enter(); }
  107. private:
  108. //==============================================================================
  109. const LockType& lock_;
  110. JUCE_DECLARE_NON_COPYABLE (GenericScopedUnlock)
  111. };
  112. //==============================================================================
  113. /**
  114. Automatically locks and unlocks a mutex object.
  115. Use one of these as a local variable to provide RAII-based locking of a mutex.
  116. The templated class could be a CriticalSection, SpinLock, or anything else that
  117. provides enter() and exit() methods.
  118. e.g. @code
  119. CriticalSection myCriticalSection;
  120. for (;;)
  121. {
  122. const GenericScopedTryLock<CriticalSection> myScopedTryLock (myCriticalSection);
  123. // Unlike using a ScopedLock, this may fail to actually get the lock, so you
  124. // should test this with the isLocked() method before doing your thread-unsafe
  125. // action..
  126. if (myScopedTryLock.isLocked())
  127. {
  128. ...do some stuff...
  129. }
  130. else
  131. {
  132. ..our attempt at locking failed because another thread had already locked it..
  133. }
  134. // myCriticalSection gets unlocked here (if it was locked)
  135. }
  136. @endcode
  137. @see CriticalSection::tryEnter, GenericScopedLock, GenericScopedUnlock
  138. */
  139. template <class LockType>
  140. class GenericScopedTryLock
  141. {
  142. public:
  143. //==============================================================================
  144. /** Creates a GenericScopedTryLock.
  145. If acquireLockOnInitialisation is true then as soon as this ScopedTryLock
  146. is created, it will attempt to acquire the lock with tryEnter.
  147. You can retry acquiring the lock by calling retryLock.
  148. When GenericScopedTryLock is deleted, the lock will be released (if the lock
  149. was successfully acquired).
  150. Make sure this object is created and deleted by the same thread,
  151. otherwise there are no guarantees what will happen! Best just to use it
  152. as a local stack object, rather than creating one with the new() operator.
  153. @see retryLock, isLocked
  154. */
  155. inline explicit GenericScopedTryLock (const LockType& lock, bool acquireLockOnInitialisation = true) noexcept
  156. : lock_ (lock), lockWasSuccessful (acquireLockOnInitialisation && lock.tryEnter()) {}
  157. /** Destructor.
  158. The mutex will be unlocked (if it had been successfully locked) when the
  159. destructor is called.
  160. Make sure this object is created and deleted by the same thread,
  161. otherwise there are no guarantees what will happen!
  162. */
  163. inline ~GenericScopedTryLock() noexcept { if (lockWasSuccessful) lock_.exit(); }
  164. /** Returns true if the mutex was successfully locked. */
  165. bool isLocked() const noexcept { return lockWasSuccessful; }
  166. /** Retry gaining the lock by calling tryEnter on the underlying lock. */
  167. bool retryLock() const noexcept { lockWasSuccessful = lock_.tryEnter(); return lockWasSuccessful; }
  168. private:
  169. //==============================================================================
  170. const LockType& lock_;
  171. mutable bool lockWasSuccessful;
  172. JUCE_DECLARE_NON_COPYABLE (GenericScopedTryLock)
  173. };
  174. } // namespace juce