juce_CriticalSection.h 9.1 KB

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