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.

239 lines
8.2KB

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