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.

256 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_CRITICALSECTION_H_INCLUDED
  22. #define JUCE_CRITICALSECTION_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A re-entrant mutex.
  26. A CriticalSection acts as a re-entrant mutex object. The best way to lock and unlock
  27. one of these is by using RAII in the form of a local ScopedLock object - have a look
  28. through the codebase for many examples of how to do this.
  29. @see ScopedLock, ScopedTryLock, ScopedUnlock, SpinLock, ReadWriteLock, Thread, InterProcessLock
  30. */
  31. class JUCE_API CriticalSection
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a CriticalSection object. */
  36. CriticalSection() noexcept;
  37. /** Destructor.
  38. If the critical section is deleted whilst locked, any subsequent behaviour
  39. is unpredictable.
  40. */
  41. ~CriticalSection() noexcept;
  42. //==============================================================================
  43. /** Acquires the lock.
  44. If the lock is already held by the caller thread, the method returns immediately.
  45. If the lock is currently held by another thread, this will wait until it becomes free.
  46. It's strongly recommended that you never call this method directly - instead use the
  47. ScopedLock class to manage the locking using an RAII pattern instead.
  48. @see exit, tryEnter, ScopedLock
  49. */
  50. void enter() const noexcept;
  51. /** Attempts to lock this critical section without blocking.
  52. This method behaves identically to CriticalSection::enter, except that the caller thread
  53. does not wait if the lock is currently held by another thread but returns false immediately.
  54. @returns false if the lock is currently held by another thread, true otherwise.
  55. @see enter
  56. */
  57. bool tryEnter() const noexcept;
  58. /** Releases the lock.
  59. If the caller thread hasn't got the lock, this can have unpredictable results.
  60. If the enter() method has been called multiple times by the thread, each
  61. call must be matched by a call to exit() before other threads will be allowed
  62. to take over the lock.
  63. @see enter, ScopedLock
  64. */
  65. void exit() const noexcept;
  66. //==============================================================================
  67. /** Provides the type of scoped lock to use with a CriticalSection. */
  68. typedef GenericScopedLock <CriticalSection> ScopedLockType;
  69. /** Provides the type of scoped unlocker to use with a CriticalSection. */
  70. typedef GenericScopedUnlock <CriticalSection> ScopedUnlockType;
  71. /** Provides the type of scoped try-locker to use with a CriticalSection. */
  72. typedef GenericScopedTryLock <CriticalSection> ScopedTryLockType;
  73. private:
  74. //==============================================================================
  75. #if JUCE_WINDOWS
  76. // To avoid including windows.h in the public JUCE headers, we'll just allocate
  77. // a block of memory here that's big enough to be used internally as a windows
  78. // CRITICAL_SECTION structure.
  79. #if JUCE_64BIT
  80. uint8 lock[44];
  81. #else
  82. uint8 lock[24];
  83. #endif
  84. #else
  85. mutable pthread_mutex_t lock;
  86. #endif
  87. JUCE_DECLARE_NON_COPYABLE (CriticalSection)
  88. };
  89. //==============================================================================
  90. /**
  91. A class that can be used in place of a real CriticalSection object, but which
  92. doesn't perform any locking.
  93. This is currently used by some templated classes, and most compilers should
  94. manage to optimise it out of existence.
  95. @see CriticalSection, Array, OwnedArray, ReferenceCountedArray
  96. */
  97. class JUCE_API DummyCriticalSection
  98. {
  99. public:
  100. inline DummyCriticalSection() noexcept {}
  101. inline ~DummyCriticalSection() noexcept {}
  102. inline void enter() const noexcept {}
  103. inline bool tryEnter() const noexcept { return true; }
  104. inline void exit() const noexcept {}
  105. //==============================================================================
  106. /** A dummy scoped-lock type to use with a dummy critical section. */
  107. struct ScopedLockType
  108. {
  109. ScopedLockType (const DummyCriticalSection&) noexcept {}
  110. };
  111. /** A dummy scoped-unlocker type to use with a dummy critical section. */
  112. typedef ScopedLockType ScopedUnlockType;
  113. private:
  114. JUCE_DECLARE_NON_COPYABLE (DummyCriticalSection)
  115. };
  116. //==============================================================================
  117. /**
  118. Automatically locks and unlocks a CriticalSection object.
  119. Use one of these as a local variable to provide RAII-based locking of a CriticalSection.
  120. e.g. @code
  121. CriticalSection myCriticalSection;
  122. for (;;)
  123. {
  124. const ScopedLock myScopedLock (myCriticalSection);
  125. // myCriticalSection is now locked
  126. ...do some stuff...
  127. // myCriticalSection gets unlocked here.
  128. }
  129. @endcode
  130. @see CriticalSection, ScopedUnlock
  131. */
  132. typedef CriticalSection::ScopedLockType ScopedLock;
  133. //==============================================================================
  134. /**
  135. Automatically unlocks and re-locks a CriticalSection object.
  136. This is the reverse of a ScopedLock object - instead of locking the critical
  137. section for the lifetime of this object, it unlocks it.
  138. Make sure you don't try to unlock critical sections that aren't actually locked!
  139. e.g. @code
  140. CriticalSection myCriticalSection;
  141. for (;;)
  142. {
  143. const ScopedLock myScopedLock (myCriticalSection);
  144. // myCriticalSection is now locked
  145. ... do some stuff with it locked ..
  146. while (xyz)
  147. {
  148. ... do some stuff with it locked ..
  149. const ScopedUnlock unlocker (myCriticalSection);
  150. // myCriticalSection is now unlocked for the remainder of this block,
  151. // and re-locked at the end.
  152. ...do some stuff with it unlocked ...
  153. }
  154. // myCriticalSection gets unlocked here.
  155. }
  156. @endcode
  157. @see CriticalSection, ScopedLock
  158. */
  159. typedef CriticalSection::ScopedUnlockType ScopedUnlock;
  160. //==============================================================================
  161. /**
  162. Automatically tries to lock and unlock a CriticalSection object.
  163. Use one of these as a local variable to control access to a CriticalSection.
  164. e.g. @code
  165. CriticalSection myCriticalSection;
  166. for (;;)
  167. {
  168. const ScopedTryLock myScopedTryLock (myCriticalSection);
  169. // Unlike using a ScopedLock, this may fail to actually get the lock, so you
  170. // should test this with the isLocked() method before doing your thread-unsafe
  171. // action..
  172. if (myScopedTryLock.isLocked())
  173. {
  174. ...do some stuff...
  175. }
  176. else
  177. {
  178. ..our attempt at locking failed because another thread had already locked it..
  179. }
  180. // myCriticalSection gets unlocked here (if it was locked)
  181. }
  182. @endcode
  183. @see CriticalSection::tryEnter, ScopedLock, ScopedUnlock, ScopedReadLock
  184. */
  185. typedef CriticalSection::ScopedTryLockType ScopedTryLock;
  186. #endif // JUCE_CRITICALSECTION_H_INCLUDED