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.

153 lines
4.5KB

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