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.

147 lines
4.0KB

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