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.

160 lines
5.6KB

  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_READWRITELOCK_JUCEHEADER__
  22. #define __JUCE_READWRITELOCK_JUCEHEADER__
  23. #include "juce_CriticalSection.h"
  24. #include "juce_SpinLock.h"
  25. #include "juce_WaitableEvent.h"
  26. #include "juce_Thread.h"
  27. #include "../containers/juce_Array.h"
  28. //==============================================================================
  29. /**
  30. A critical section that allows multiple simultaneous readers.
  31. Features of this type of lock are:
  32. - Multiple readers can hold the lock at the same time, but only one writer
  33. can hold it at once.
  34. - Writers trying to gain the lock will be blocked until all readers and writers
  35. have released it
  36. - Readers trying to gain the lock while a writer is waiting to acquire it will be
  37. blocked until the writer has obtained and released it
  38. - If a thread already has a read lock and tries to obtain a write lock, it will succeed if
  39. there are no other readers
  40. - If a thread already has the write lock and tries to obtain a read lock, this will succeed.
  41. - Recursive locking is supported.
  42. @see ScopedReadLock, ScopedWriteLock, CriticalSection
  43. */
  44. class JUCE_API ReadWriteLock
  45. {
  46. public:
  47. //==============================================================================
  48. /**
  49. Creates a ReadWriteLock object.
  50. */
  51. ReadWriteLock() noexcept;
  52. /** Destructor.
  53. If the object is deleted whilst locked, any subsequent behaviour
  54. is unpredictable.
  55. */
  56. ~ReadWriteLock() noexcept;
  57. //==============================================================================
  58. /** Locks this object for reading.
  59. Multiple threads can simulaneously lock the object for reading, but if another
  60. thread has it locked for writing, then this will block until it releases the
  61. lock.
  62. @see exitRead, ScopedReadLock
  63. */
  64. void enterRead() const noexcept;
  65. /** Tries to lock this object for reading.
  66. Multiple threads can simulaneously lock the object for reading, but if another
  67. thread has it locked for writing, then this will fail and return false.
  68. @returns true if the lock is successfully gained.
  69. @see exitRead, ScopedReadLock
  70. */
  71. bool tryEnterRead() const noexcept;
  72. /** Releases the read-lock.
  73. If the caller thread hasn't got the lock, this can have unpredictable results.
  74. If the enterRead() method has been called multiple times by the thread, each
  75. call must be matched by a call to exitRead() before other threads will be allowed
  76. to take over the lock.
  77. @see enterRead, ScopedReadLock
  78. */
  79. void exitRead() const noexcept;
  80. //==============================================================================
  81. /** Locks this object for writing.
  82. This will block until any other threads that have it locked for reading or
  83. writing have released their lock.
  84. @see exitWrite, ScopedWriteLock
  85. */
  86. void enterWrite() const noexcept;
  87. /** Tries to lock this object for writing.
  88. This is like enterWrite(), but doesn't block - it returns true if it manages
  89. to obtain the lock.
  90. @returns true if the lock is successfully gained.
  91. @see enterWrite
  92. */
  93. bool tryEnterWrite() const noexcept;
  94. /** Releases the write-lock.
  95. If the caller thread hasn't got the lock, this can have unpredictable results.
  96. If the enterWrite() method has been called multiple times by the thread, each
  97. call must be matched by a call to exit() before other threads will be allowed
  98. to take over the lock.
  99. @see enterWrite, ScopedWriteLock
  100. */
  101. void exitWrite() const noexcept;
  102. private:
  103. //==============================================================================
  104. SpinLock accessLock;
  105. WaitableEvent waitEvent;
  106. mutable int numWaitingWriters, numWriters;
  107. mutable Thread::ThreadID writerThreadId;
  108. struct ThreadRecursionCount
  109. {
  110. Thread::ThreadID threadID;
  111. int count;
  112. };
  113. mutable Array <ThreadRecursionCount> readerThreads;
  114. JUCE_DECLARE_NON_COPYABLE (ReadWriteLock)
  115. };
  116. #endif // __JUCE_READWRITELOCK_JUCEHEADER__