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.

143 lines
4.9KB

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