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.

156 lines
4.4KB

  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. ReadWriteLock::ReadWriteLock() noexcept
  19. : numWaitingWriters (0),
  20. numWriters (0),
  21. writerThreadId (0)
  22. {
  23. readerThreads.ensureStorageAllocated (16);
  24. }
  25. ReadWriteLock::~ReadWriteLock() noexcept
  26. {
  27. jassert (readerThreads.size() == 0);
  28. jassert (numWriters == 0);
  29. }
  30. //==============================================================================
  31. void ReadWriteLock::enterRead() const noexcept
  32. {
  33. while (! tryEnterRead())
  34. waitEvent.wait (100);
  35. }
  36. bool ReadWriteLock::tryEnterRead() const noexcept
  37. {
  38. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  39. const SpinLock::ScopedLockType sl (accessLock);
  40. for (int i = 0; i < readerThreads.size(); ++i)
  41. {
  42. ThreadRecursionCount& trc = readerThreads.getReference(i);
  43. if (trc.threadID == threadId)
  44. {
  45. trc.count++;
  46. return true;
  47. }
  48. }
  49. if (numWriters + numWaitingWriters == 0
  50. || (threadId == writerThreadId && numWriters > 0))
  51. {
  52. ThreadRecursionCount trc = { threadId, 1 };
  53. readerThreads.add (trc);
  54. return true;
  55. }
  56. return false;
  57. }
  58. void ReadWriteLock::exitRead() const noexcept
  59. {
  60. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  61. const SpinLock::ScopedLockType sl (accessLock);
  62. for (int i = 0; i < readerThreads.size(); ++i)
  63. {
  64. ThreadRecursionCount& trc = readerThreads.getReference(i);
  65. if (trc.threadID == threadId)
  66. {
  67. if (--(trc.count) == 0)
  68. {
  69. readerThreads.remove (i);
  70. waitEvent.signal();
  71. }
  72. return;
  73. }
  74. }
  75. jassertfalse; // unlocking a lock that wasn't locked..
  76. }
  77. //==============================================================================
  78. void ReadWriteLock::enterWrite() const noexcept
  79. {
  80. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  81. const SpinLock::ScopedLockType sl (accessLock);
  82. for (;;)
  83. {
  84. if (readerThreads.size() + numWriters == 0
  85. || threadId == writerThreadId
  86. || (readerThreads.size() == 1
  87. && readerThreads.getReference(0).threadID == threadId))
  88. {
  89. writerThreadId = threadId;
  90. ++numWriters;
  91. break;
  92. }
  93. ++numWaitingWriters;
  94. accessLock.exit();
  95. waitEvent.wait (100);
  96. accessLock.enter();
  97. --numWaitingWriters;
  98. }
  99. }
  100. bool ReadWriteLock::tryEnterWrite() const noexcept
  101. {
  102. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  103. const SpinLock::ScopedLockType sl (accessLock);
  104. if (readerThreads.size() + numWriters == 0
  105. || threadId == writerThreadId
  106. || (readerThreads.size() == 1
  107. && readerThreads.getReference(0).threadID == threadId))
  108. {
  109. writerThreadId = threadId;
  110. ++numWriters;
  111. return true;
  112. }
  113. return false;
  114. }
  115. void ReadWriteLock::exitWrite() const noexcept
  116. {
  117. const SpinLock::ScopedLockType sl (accessLock);
  118. // check this thread actually had the lock..
  119. jassert (numWriters > 0 && writerThreadId == Thread::getCurrentThreadId());
  120. if (--numWriters == 0)
  121. {
  122. writerThreadId = 0;
  123. waitEvent.signal();
  124. }
  125. }