Audio plugin host https://kx.studio/carla
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.

240 lines
8.4KB

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