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.

148 lines
3.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. readWaitEvent.wait (100);
  33. }
  34. bool ReadWriteLock::tryEnterRead() const noexcept
  35. {
  36. auto threadId = Thread::getCurrentThreadId();
  37. const SpinLock::ScopedLockType sl (accessLock);
  38. for (auto& readerThread : readerThreads)
  39. {
  40. if (readerThread.threadID == threadId)
  41. {
  42. readerThread.count++;
  43. return true;
  44. }
  45. }
  46. if (numWriters + numWaitingWriters == 0
  47. || (threadId == writerThreadId && numWriters > 0))
  48. {
  49. readerThreads.add ({ threadId, 1 });
  50. return true;
  51. }
  52. return false;
  53. }
  54. void ReadWriteLock::exitRead() const noexcept
  55. {
  56. auto threadId = Thread::getCurrentThreadId();
  57. const SpinLock::ScopedLockType sl (accessLock);
  58. for (int i = 0; i < readerThreads.size(); ++i)
  59. {
  60. auto& readerThread = readerThreads.getReference (i);
  61. if (readerThread.threadID == threadId)
  62. {
  63. if (--(readerThread.count) == 0)
  64. {
  65. readerThreads.remove (i);
  66. readWaitEvent.signal();
  67. writeWaitEvent.signal();
  68. }
  69. return;
  70. }
  71. }
  72. jassertfalse; // unlocking a lock that wasn't locked..
  73. }
  74. //==============================================================================
  75. void ReadWriteLock::enterWrite() const noexcept
  76. {
  77. auto threadId = Thread::getCurrentThreadId();
  78. const SpinLock::ScopedLockType sl (accessLock);
  79. while (! tryEnterWriteInternal (threadId))
  80. {
  81. ++numWaitingWriters;
  82. accessLock.exit();
  83. writeWaitEvent.wait (100);
  84. accessLock.enter();
  85. --numWaitingWriters;
  86. }
  87. }
  88. bool ReadWriteLock::tryEnterWrite() const noexcept
  89. {
  90. const SpinLock::ScopedLockType sl (accessLock);
  91. return tryEnterWriteInternal (Thread::getCurrentThreadId());
  92. }
  93. bool ReadWriteLock::tryEnterWriteInternal (Thread::ThreadID threadId) const noexcept
  94. {
  95. if (readerThreads.size() + numWriters == 0
  96. || threadId == writerThreadId
  97. || (readerThreads.size() == 1 && readerThreads.getReference (0).threadID == threadId))
  98. {
  99. writerThreadId = threadId;
  100. ++numWriters;
  101. return true;
  102. }
  103. return false;
  104. }
  105. void ReadWriteLock::exitWrite() const noexcept
  106. {
  107. const SpinLock::ScopedLockType sl (accessLock);
  108. // check this thread actually had the lock..
  109. jassert (numWriters > 0 && writerThreadId == Thread::getCurrentThreadId());
  110. if (--numWriters == 0)
  111. {
  112. writerThreadId = {};
  113. readWaitEvent.signal();
  114. writeWaitEvent.signal();
  115. }
  116. }
  117. } // namespace juce