juce_ReadWriteLock.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 critical section that allows multiple simultaneous readers.
  22. Features of this type of lock are:
  23. - Multiple readers can hold the lock at the same time, but only one writer
  24. can hold it at once.
  25. - Writers trying to gain the lock will be blocked until all readers and writers
  26. have released it
  27. - Readers trying to gain the lock while a writer is waiting to acquire it will be
  28. blocked until the writer has obtained and released it
  29. - If a thread already has a read lock and tries to obtain a write lock, it will succeed if
  30. there are no other readers
  31. - If a thread already has the write lock and tries to obtain a read lock, this will succeed.
  32. - Recursive locking is supported.
  33. @see ScopedReadLock, ScopedWriteLock, CriticalSection
  34. @tags{Core}
  35. */
  36. class JUCE_API ReadWriteLock
  37. {
  38. public:
  39. //==============================================================================
  40. /**
  41. Creates a ReadWriteLock object.
  42. */
  43. ReadWriteLock() noexcept;
  44. /** Destructor.
  45. If the object is deleted whilst locked, any subsequent behaviour is undefined.
  46. */
  47. ~ReadWriteLock() noexcept;
  48. //==============================================================================
  49. /** Locks this object for reading.
  50. Multiple threads can simultaneously lock the object for reading, but if another
  51. thread has it locked for writing, then this will block until it releases the lock.
  52. @see exitRead, ScopedReadLock
  53. */
  54. void enterRead() const noexcept;
  55. /** Tries to lock this object for reading.
  56. Multiple threads can simultaneously lock the object for reading, but if another
  57. thread has it locked for writing, then this will fail and return false.
  58. @returns true if the lock is successfully gained.
  59. @see exitRead, ScopedReadLock
  60. */
  61. bool tryEnterRead() 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. @returns true if the lock is successfully gained.
  81. @see enterWrite
  82. */
  83. bool tryEnterWrite() const noexcept;
  84. /** Releases the write-lock.
  85. If the caller thread hasn't got the lock, this can have unpredictable results.
  86. If the enterWrite() method has been called multiple times by the thread, each
  87. call must be matched by a call to exit() before other threads will be allowed
  88. to take over the lock.
  89. @see enterWrite, ScopedWriteLock
  90. */
  91. void exitWrite() const noexcept;
  92. private:
  93. //==============================================================================
  94. SpinLock accessLock;
  95. WaitableEvent readWaitEvent, writeWaitEvent;
  96. mutable int numWaitingWriters = 0, numWriters = 0;
  97. mutable Thread::ThreadID writerThreadId = {};
  98. struct ThreadRecursionCount
  99. {
  100. Thread::ThreadID threadID;
  101. int count;
  102. };
  103. mutable Array <ThreadRecursionCount> readerThreads;
  104. bool tryEnterWriteInternal (Thread::ThreadID) const noexcept;
  105. JUCE_DECLARE_NON_COPYABLE (ReadWriteLock)
  106. };
  107. } // namespace juce