juce_ReadWriteLock.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. ReadWriteLock::ReadWriteLock() noexcept
  22. : numWaitingWriters (0),
  23. numWriters (0),
  24. writerThreadId (0)
  25. {
  26. readerThreads.ensureStorageAllocated (16);
  27. }
  28. ReadWriteLock::~ReadWriteLock() noexcept
  29. {
  30. jassert (readerThreads.size() == 0);
  31. jassert (numWriters == 0);
  32. }
  33. //==============================================================================
  34. void ReadWriteLock::enterRead() const noexcept
  35. {
  36. while (! tryEnterRead())
  37. waitEvent.wait (100);
  38. }
  39. bool ReadWriteLock::tryEnterRead() const noexcept
  40. {
  41. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  42. const SpinLock::ScopedLockType sl (accessLock);
  43. for (int i = 0; i < readerThreads.size(); ++i)
  44. {
  45. ThreadRecursionCount& trc = readerThreads.getReference(i);
  46. if (trc.threadID == threadId)
  47. {
  48. trc.count++;
  49. return true;
  50. }
  51. }
  52. if (numWriters + numWaitingWriters == 0
  53. || (threadId == writerThreadId && numWriters > 0))
  54. {
  55. ThreadRecursionCount trc = { threadId, 1 };
  56. readerThreads.add (trc);
  57. return true;
  58. }
  59. return false;
  60. }
  61. void ReadWriteLock::exitRead() const noexcept
  62. {
  63. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  64. const SpinLock::ScopedLockType sl (accessLock);
  65. for (int i = 0; i < readerThreads.size(); ++i)
  66. {
  67. ThreadRecursionCount& trc = readerThreads.getReference(i);
  68. if (trc.threadID == threadId)
  69. {
  70. if (--(trc.count) == 0)
  71. {
  72. readerThreads.remove (i);
  73. waitEvent.signal();
  74. }
  75. return;
  76. }
  77. }
  78. jassertfalse; // unlocking a lock that wasn't locked..
  79. }
  80. //==============================================================================
  81. void ReadWriteLock::enterWrite() const noexcept
  82. {
  83. const Thread::ThreadID threadId = Thread::getCurrentThreadId();
  84. const SpinLock::ScopedLockType sl (accessLock);
  85. while (! tryEnterWriteInternal (threadId))
  86. {
  87. ++numWaitingWriters;
  88. accessLock.exit();
  89. waitEvent.wait (100);
  90. accessLock.enter();
  91. --numWaitingWriters;
  92. }
  93. }
  94. bool ReadWriteLock::tryEnterWrite() const noexcept
  95. {
  96. const SpinLock::ScopedLockType sl (accessLock);
  97. return tryEnterWriteInternal (Thread::getCurrentThreadId());
  98. }
  99. bool ReadWriteLock::tryEnterWriteInternal (Thread::ThreadID threadId) const noexcept
  100. {
  101. if (readerThreads.size() + numWriters == 0
  102. || threadId == writerThreadId
  103. || (readerThreads.size() == 1 && readerThreads.getReference(0).threadID == threadId))
  104. {
  105. writerThreadId = threadId;
  106. ++numWriters;
  107. return true;
  108. }
  109. return false;
  110. }
  111. void ReadWriteLock::exitWrite() const noexcept
  112. {
  113. const SpinLock::ScopedLockType sl (accessLock);
  114. // check this thread actually had the lock..
  115. jassert (numWriters > 0 && writerThreadId == Thread::getCurrentThreadId());
  116. if (--numWriters == 0)
  117. {
  118. writerThreadId = 0;
  119. waitEvent.signal();
  120. }
  121. }