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.

259 lines
8.5KB

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