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.

265 lines
8.9KB

  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. #pragma once
  24. //==============================================================================
  25. /**
  26. A re-entrant mutex.
  27. A CriticalSection acts as a re-entrant mutex object. The best way to lock and unlock
  28. one of these is by using RAII in the form of a local ScopedLock object - have a look
  29. through the codebase for many examples of how to do this.
  30. In almost all cases you'll want to declare your CriticalSection as a member variable.
  31. Occasionally you may want to declare one as a static variable, but in that case the usual
  32. C++ static object order-of-construction warnings should be heeded.
  33. @see ScopedLock, ScopedTryLock, ScopedUnlock, SpinLock, ReadWriteLock, Thread, InterProcessLock
  34. */
  35. class JUCE_API CriticalSection
  36. {
  37. public:
  38. //==============================================================================
  39. /** Creates a CriticalSection object. */
  40. CriticalSection() noexcept;
  41. /** Destructor.
  42. If the critical section is deleted whilst locked, any subsequent behaviour
  43. is unpredictable.
  44. */
  45. ~CriticalSection() noexcept;
  46. //==============================================================================
  47. /** Acquires the lock.
  48. If the lock is already held by the caller thread, the method returns immediately.
  49. If the lock is currently held by another thread, this will wait until it becomes free.
  50. It's strongly recommended that you never call this method directly - instead use the
  51. ScopedLock class to manage the locking using an RAII pattern instead.
  52. @see exit, tryEnter, ScopedLock
  53. */
  54. void enter() const noexcept;
  55. /** Attempts to lock this critical section without blocking.
  56. This method behaves identically to CriticalSection::enter, except that the caller thread
  57. does not wait if the lock is currently held by another thread but returns false immediately.
  58. @returns false if the lock is currently held by another thread, true otherwise.
  59. @see enter
  60. */
  61. bool tryEnter() const noexcept;
  62. /** Releases the lock.
  63. If the caller thread hasn't got the lock, this can have unpredictable results.
  64. If the enter() method has been called multiple times by the thread, each
  65. call must be matched by a call to exit() before other threads will be allowed
  66. to take over the lock.
  67. @see enter, ScopedLock
  68. */
  69. void exit() const noexcept;
  70. //==============================================================================
  71. /** Provides the type of scoped lock to use with a CriticalSection. */
  72. typedef GenericScopedLock <CriticalSection> ScopedLockType;
  73. /** Provides the type of scoped unlocker to use with a CriticalSection. */
  74. typedef GenericScopedUnlock <CriticalSection> ScopedUnlockType;
  75. /** Provides the type of scoped try-locker to use with a CriticalSection. */
  76. typedef GenericScopedTryLock <CriticalSection> ScopedTryLockType;
  77. private:
  78. //==============================================================================
  79. #if JUCE_WINDOWS
  80. // To avoid including windows.h in the public JUCE headers, we'll just allocate
  81. // a block of memory here that's big enough to be used internally as a windows
  82. // CRITICAL_SECTION structure.
  83. #if JUCE_64BIT
  84. uint8 lock[44];
  85. #else
  86. uint8 lock[24];
  87. #endif
  88. #else
  89. mutable pthread_mutex_t lock;
  90. #endif
  91. JUCE_DECLARE_NON_COPYABLE (CriticalSection)
  92. };
  93. //==============================================================================
  94. /**
  95. A class that can be used in place of a real CriticalSection object, but which
  96. doesn't perform any locking.
  97. This is currently used by some templated classes, and most compilers should
  98. manage to optimise it out of existence.
  99. @see CriticalSection, Array, OwnedArray, ReferenceCountedArray
  100. */
  101. class JUCE_API DummyCriticalSection
  102. {
  103. public:
  104. inline DummyCriticalSection() noexcept {}
  105. inline ~DummyCriticalSection() noexcept {}
  106. inline void enter() const noexcept {}
  107. inline bool tryEnter() const noexcept { return true; }
  108. inline void exit() const noexcept {}
  109. //==============================================================================
  110. /** A dummy scoped-lock type to use with a dummy critical section. */
  111. struct ScopedLockType
  112. {
  113. ScopedLockType (const DummyCriticalSection&) noexcept {}
  114. };
  115. /** A dummy scoped-unlocker type to use with a dummy critical section. */
  116. typedef ScopedLockType ScopedUnlockType;
  117. private:
  118. JUCE_DECLARE_NON_COPYABLE (DummyCriticalSection)
  119. };
  120. //==============================================================================
  121. /**
  122. Automatically locks and unlocks a CriticalSection object.
  123. You can use a ScopedLock as a local variable to provide RAII-based locking of a CriticalSection.
  124. e.g. @code
  125. struct MyObject
  126. {
  127. CriticalSection objectLock;
  128. // assuming that this example function will be called by multiple threads
  129. void foo()
  130. {
  131. const ScopedLock myScopedLock (objectLock);
  132. // objectLock is now locked..
  133. ...do some thread-safe work here...
  134. // ..and objectLock gets unlocked here, as myScopedLock goes out of
  135. // scope at the end of the block
  136. }
  137. };
  138. @endcode
  139. @see CriticalSection, ScopedUnlock
  140. */
  141. typedef CriticalSection::ScopedLockType ScopedLock;
  142. //==============================================================================
  143. /**
  144. Automatically unlocks and re-locks a CriticalSection object.
  145. This is the reverse of a ScopedLock object - instead of locking the critical
  146. section for the lifetime of this object, it unlocks it.
  147. Make sure you don't try to unlock critical sections that aren't actually locked!
  148. e.g. @code
  149. struct MyObject
  150. {
  151. CriticalSection objectLock;
  152. void foo()
  153. {
  154. {
  155. const ScopedLock myScopedLock (objectLock);
  156. // objectLock is now locked..
  157. {
  158. ScopedUnlock myUnlocker (objectLock);
  159. // ..and now unlocked..
  160. }
  161. // ..and now locked again..
  162. }
  163. // ..and finally unlocked.
  164. }
  165. };
  166. @endcode
  167. @see CriticalSection, ScopedLock
  168. */
  169. typedef CriticalSection::ScopedUnlockType ScopedUnlock;
  170. //==============================================================================
  171. /**
  172. Automatically tries to lock and unlock a CriticalSection object.
  173. Use one of these as a local variable to control access to a CriticalSection.
  174. e.g. @code
  175. struct MyObject
  176. {
  177. CriticalSection objectLock;
  178. void foo()
  179. {
  180. const ScopedTryLock myScopedTryLock (objectLock);
  181. // Unlike using a ScopedLock, this may fail to actually get the lock, so you
  182. // must call the isLocked() method before making any assumptions..
  183. if (myScopedTryLock.isLocked())
  184. {
  185. ...safely do some work...
  186. }
  187. else
  188. {
  189. // If we get here, then our attempt at locking failed because another thread had already locked it..
  190. }
  191. }
  192. };
  193. @endcode
  194. @see CriticalSection::tryEnter, ScopedLock, ScopedUnlock, ScopedReadLock
  195. */
  196. typedef CriticalSection::ScopedTryLockType ScopedTryLock;