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.

155 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_READWRITELOCK_H_INCLUDED
  24. #define JUCE_READWRITELOCK_H_INCLUDED
  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 is undefined.
  51. */
  52. ~ReadWriteLock() noexcept;
  53. //==============================================================================
  54. /** Locks this object for reading.
  55. Multiple threads can simultaneously lock the object for reading, but if another
  56. thread has it locked for writing, then this will block until it releases the lock.
  57. @see exitRead, ScopedReadLock
  58. */
  59. void enterRead() const noexcept;
  60. /** Tries to lock this object for reading.
  61. Multiple threads can simultaneously lock the object for reading, but if another
  62. thread has it locked for writing, then this will fail and return false.
  63. @returns true if the lock is successfully gained.
  64. @see exitRead, ScopedReadLock
  65. */
  66. bool tryEnterRead() const noexcept;
  67. /** Releases the read-lock.
  68. If the caller thread hasn't got the lock, this can have unpredictable results.
  69. If the enterRead() method has been called multiple times by the thread, each
  70. call must be matched by a call to exitRead() before other threads will be allowed
  71. to take over the lock.
  72. @see enterRead, ScopedReadLock
  73. */
  74. void exitRead() const noexcept;
  75. //==============================================================================
  76. /** Locks this object for writing.
  77. This will block until any other threads that have it locked for reading or
  78. writing have released their lock.
  79. @see exitWrite, ScopedWriteLock
  80. */
  81. void enterWrite() const noexcept;
  82. /** Tries to lock this object for writing.
  83. This is like enterWrite(), but doesn't block - it returns true if it manages
  84. to obtain the lock.
  85. @returns true if the lock is successfully gained.
  86. @see enterWrite
  87. */
  88. bool tryEnterWrite() const noexcept;
  89. /** Releases the write-lock.
  90. If the caller thread hasn't got the lock, this can have unpredictable results.
  91. If the enterWrite() method has been called multiple times by the thread, each
  92. call must be matched by a call to exit() before other threads will be allowed
  93. to take over the lock.
  94. @see enterWrite, ScopedWriteLock
  95. */
  96. void exitWrite() const noexcept;
  97. private:
  98. //==============================================================================
  99. SpinLock accessLock;
  100. WaitableEvent waitEvent;
  101. mutable int numWaitingWriters, numWriters;
  102. mutable Thread::ThreadID writerThreadId;
  103. struct ThreadRecursionCount
  104. {
  105. Thread::ThreadID threadID;
  106. int count;
  107. };
  108. mutable Array <ThreadRecursionCount> readerThreads;
  109. bool tryEnterWriteInternal (Thread::ThreadID) const noexcept;
  110. JUCE_DECLARE_NON_COPYABLE (ReadWriteLock)
  111. };
  112. #endif // JUCE_READWRITELOCK_H_INCLUDED