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.

257 lines
8.4KB

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