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.

245 lines
8.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. @tags{Core}
  37. */
  38. template <class LockType>
  39. class GenericScopedLock
  40. {
  41. public:
  42. //==============================================================================
  43. /** Creates a GenericScopedLock.
  44. As soon as it is created, this will acquire the lock, and when the GenericScopedLock
  45. object is deleted, the lock will be released.
  46. Make sure this object is created and deleted by the same thread,
  47. otherwise there are no guarantees what will happen! Best just to use it
  48. as a local stack object, rather than creating one with the new() operator.
  49. */
  50. inline explicit GenericScopedLock (const LockType& lock) noexcept : lock_ (lock) { lock.enter(); }
  51. /** Destructor.
  52. The lock will be released when the destructor is called.
  53. Make sure this object is created and deleted by the same thread, otherwise there are
  54. no guarantees what will happen!
  55. */
  56. inline ~GenericScopedLock() noexcept { lock_.exit(); }
  57. private:
  58. //==============================================================================
  59. const LockType& lock_;
  60. JUCE_DECLARE_NON_COPYABLE (GenericScopedLock)
  61. };
  62. //==============================================================================
  63. /**
  64. Automatically unlocks and re-locks a mutex object.
  65. This is the reverse of a GenericScopedLock object - instead of locking the mutex
  66. for the lifetime of this object, it unlocks it.
  67. Make sure you don't try to unlock mutexes that aren't actually locked!
  68. e.g. @code
  69. CriticalSection myCriticalSection;
  70. for (;;)
  71. {
  72. const GenericScopedLock<CriticalSection> myScopedLock (myCriticalSection);
  73. // myCriticalSection is now locked
  74. ... do some stuff with it locked ..
  75. while (xyz)
  76. {
  77. ... do some stuff with it locked ..
  78. const GenericScopedUnlock<CriticalSection> unlocker (myCriticalSection);
  79. // myCriticalSection is now unlocked for the remainder of this block,
  80. // and re-locked at the end.
  81. ...do some stuff with it unlocked ...
  82. }
  83. // myCriticalSection gets unlocked here.
  84. }
  85. @endcode
  86. @see GenericScopedLock, CriticalSection, ScopedLock, ScopedUnlock
  87. @tags{Core}
  88. */
  89. template <class LockType>
  90. class GenericScopedUnlock
  91. {
  92. public:
  93. //==============================================================================
  94. /** Creates a GenericScopedUnlock.
  95. As soon as it is created, this will unlock the CriticalSection, and
  96. when the ScopedLock object is deleted, the CriticalSection will
  97. be re-locked.
  98. Make sure this object is created and deleted by the same thread,
  99. otherwise there are no guarantees what will happen! Best just to use it
  100. as a local stack object, rather than creating one with the new() operator.
  101. */
  102. inline explicit GenericScopedUnlock (const LockType& lock) noexcept : lock_ (lock) { lock.exit(); }
  103. /** Destructor.
  104. The CriticalSection will be unlocked when the destructor is called.
  105. Make sure this object is created and deleted by the same thread,
  106. otherwise there are no guarantees what will happen!
  107. */
  108. inline ~GenericScopedUnlock() noexcept { lock_.enter(); }
  109. private:
  110. //==============================================================================
  111. const LockType& lock_;
  112. JUCE_DECLARE_NON_COPYABLE (GenericScopedUnlock)
  113. };
  114. //==============================================================================
  115. /**
  116. Automatically locks and unlocks a mutex object.
  117. Use one of these as a local variable to provide RAII-based locking of a mutex.
  118. The templated class could be a CriticalSection, SpinLock, or anything else that
  119. provides enter() and exit() methods.
  120. e.g. @code
  121. CriticalSection myCriticalSection;
  122. for (;;)
  123. {
  124. const GenericScopedTryLock<CriticalSection> myScopedTryLock (myCriticalSection);
  125. // Unlike using a ScopedLock, this may fail to actually get the lock, so you
  126. // should test this with the isLocked() method before doing your thread-unsafe
  127. // action..
  128. if (myScopedTryLock.isLocked())
  129. {
  130. ...do some stuff...
  131. }
  132. else
  133. {
  134. ..our attempt at locking failed because another thread had already locked it..
  135. }
  136. // myCriticalSection gets unlocked here (if it was locked)
  137. }
  138. @endcode
  139. @see CriticalSection::tryEnter, GenericScopedLock, GenericScopedUnlock
  140. @tags{Core}
  141. */
  142. template <class LockType>
  143. class GenericScopedTryLock
  144. {
  145. public:
  146. //==============================================================================
  147. /** Creates a GenericScopedTryLock.
  148. If acquireLockOnInitialisation is true then as soon as this ScopedTryLock
  149. is created, it will attempt to acquire the lock with tryEnter.
  150. You can retry acquiring the lock by calling retryLock.
  151. When GenericScopedTryLock is deleted, the lock will be released (if the lock
  152. was successfully acquired).
  153. Make sure this object is created and deleted by the same thread,
  154. otherwise there are no guarantees what will happen! Best just to use it
  155. as a local stack object, rather than creating one with the new() operator.
  156. @see retryLock, isLocked
  157. */
  158. inline explicit GenericScopedTryLock (const LockType& lock, bool acquireLockOnInitialisation = true) noexcept
  159. : lock_ (lock), lockWasSuccessful (acquireLockOnInitialisation && lock.tryEnter()) {}
  160. /** Destructor.
  161. The mutex will be unlocked (if it had been successfully locked) when the
  162. destructor is called.
  163. Make sure this object is created and deleted by the same thread,
  164. otherwise there are no guarantees what will happen!
  165. */
  166. inline ~GenericScopedTryLock() noexcept { if (lockWasSuccessful) lock_.exit(); }
  167. /** Returns true if the mutex was successfully locked. */
  168. bool isLocked() const noexcept { return lockWasSuccessful; }
  169. /** Retry gaining the lock by calling tryEnter on the underlying lock. */
  170. bool retryLock() const noexcept { lockWasSuccessful = lock_.tryEnter(); return lockWasSuccessful; }
  171. private:
  172. //==============================================================================
  173. const LockType& lock_;
  174. mutable bool lockWasSuccessful;
  175. JUCE_DECLARE_NON_COPYABLE (GenericScopedTryLock)
  176. };
  177. } // namespace juce