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.

146 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_READWRITELOCK_JUCEHEADER__
  19. #define __JUCE_READWRITELOCK_JUCEHEADER__
  20. #include "juce_CriticalSection.h"
  21. #include "juce_SpinLock.h"
  22. #include "juce_WaitableEvent.h"
  23. #include "juce_Thread.h"
  24. #include "../containers/juce_Array.h"
  25. //==============================================================================
  26. /**
  27. A critical section that allows multiple simultaneous readers.
  28. Features of this type of lock are:
  29. - Multiple readers can hold the lock at the same time, but only one writer
  30. can hold it at once.
  31. - Writers trying to gain the lock will be blocked until all readers and writers
  32. have released it
  33. - Readers trying to gain the lock while a writer is waiting to acquire it will be
  34. blocked until the writer has obtained and released it
  35. - If a thread already has a read lock and tries to obtain a write lock, it will succeed if
  36. there are no other readers
  37. - If a thread already has the write lock and tries to obtain a read lock, this will succeed.
  38. - Recursive locking is supported.
  39. @see ScopedReadLock, ScopedWriteLock, CriticalSection
  40. */
  41. class JUCE_API ReadWriteLock
  42. {
  43. public:
  44. //==============================================================================
  45. /**
  46. Creates a ReadWriteLock object.
  47. */
  48. ReadWriteLock() noexcept;
  49. /** Destructor.
  50. If the object is deleted whilst locked, any subsequent behaviour
  51. is unpredictable.
  52. */
  53. ~ReadWriteLock() noexcept;
  54. //==============================================================================
  55. /** Locks this object for reading.
  56. Multiple threads can simulaneously lock the object for reading, but if another
  57. thread has it locked for writing, then this will block until it releases the
  58. lock.
  59. @see exitRead, ScopedReadLock
  60. */
  61. void enterRead() const noexcept;
  62. /** Releases the read-lock.
  63. If the caller thread hasn't got the lock, this can have unpredictable results.
  64. If the enterRead() method has been called multiple times by the thread, each
  65. call must be matched by a call to exitRead() before other threads will be allowed
  66. to take over the lock.
  67. @see enterRead, ScopedReadLock
  68. */
  69. void exitRead() const noexcept;
  70. //==============================================================================
  71. /** Locks this object for writing.
  72. This will block until any other threads that have it locked for reading or
  73. writing have released their lock.
  74. @see exitWrite, ScopedWriteLock
  75. */
  76. void enterWrite() const noexcept;
  77. /** Tries to lock this object for writing.
  78. This is like enterWrite(), but doesn't block - it returns true if it manages
  79. to obtain the lock.
  80. @see enterWrite
  81. */
  82. bool tryEnterWrite() const noexcept;
  83. /** Releases the write-lock.
  84. If the caller thread hasn't got the lock, this can have unpredictable results.
  85. If the enterWrite() method has been called multiple times by the thread, each
  86. call must be matched by a call to exit() before other threads will be allowed
  87. to take over the lock.
  88. @see enterWrite, ScopedWriteLock
  89. */
  90. void exitWrite() const noexcept;
  91. private:
  92. //==============================================================================
  93. SpinLock accessLock;
  94. WaitableEvent waitEvent;
  95. mutable int numWaitingWriters, numWriters;
  96. mutable Thread::ThreadID writerThreadId;
  97. struct ThreadRecursionCount
  98. {
  99. Thread::ThreadID threadID;
  100. int count;
  101. };
  102. mutable Array <ThreadRecursionCount> readerThreads;
  103. JUCE_DECLARE_NON_COPYABLE (ReadWriteLock);
  104. };
  105. #endif // __JUCE_READWRITELOCK_JUCEHEADER__