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.

228 lines
7.8KB

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