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.

171 lines
5.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. //==============================================================================
  20. /**
  21. Automatically locks and unlocks a ReadWriteLock object.
  22. Use one of these as a local variable to control access to a ReadWriteLock.
  23. e.g. @code
  24. ReadWriteLock myLock;
  25. for (;;)
  26. {
  27. const ScopedReadLock myScopedLock (myLock);
  28. // myLock is now locked
  29. ...do some stuff...
  30. // myLock gets unlocked here.
  31. }
  32. @endcode
  33. @see ReadWriteLock, ScopedWriteLock
  34. @tags{Core}
  35. */
  36. class JUCE_API ScopedReadLock
  37. {
  38. public:
  39. //==============================================================================
  40. /** Creates a ScopedReadLock.
  41. As soon as it is created, this will call ReadWriteLock::enterRead(), and
  42. when the ScopedReadLock object is deleted, the ReadWriteLock will
  43. be unlocked.
  44. Make sure this object is created and deleted by the same thread,
  45. otherwise there are no guarantees what will happen! Best just to use it
  46. as a local stack object, rather than creating one with the new() operator.
  47. */
  48. inline explicit ScopedReadLock (const ReadWriteLock& lock) noexcept : lock_ (lock) { lock.enterRead(); }
  49. /** Destructor.
  50. The ReadWriteLock's exitRead() method will be called when the destructor is called.
  51. Make sure this object is created and deleted by the same thread,
  52. otherwise there are no guarantees what will happen!
  53. */
  54. inline ~ScopedReadLock() noexcept { lock_.exitRead(); }
  55. private:
  56. //==============================================================================
  57. const ReadWriteLock& lock_;
  58. JUCE_DECLARE_NON_COPYABLE (ScopedReadLock)
  59. };
  60. //==============================================================================
  61. /**
  62. Automatically locks and unlocks a ReadWriteLock object.
  63. Use one of these as a local variable to control access to a ReadWriteLock.
  64. e.g. @code
  65. ReadWriteLock myLock;
  66. for (;;)
  67. {
  68. const ScopedTryReadLock myScopedTryLock (myLock);
  69. // Unlike using a ScopedReadLock, this may fail to actually get the lock, so you
  70. // should test this with the isLocked() method before doing your thread-unsafe
  71. // action.
  72. if (myScopedTryLock.isLocked())
  73. {
  74. ...do some stuff...
  75. }
  76. else
  77. {
  78. ..our attempt at locking failed because a write lock has already been issued..
  79. }
  80. // myLock gets unlocked here (if it was locked).
  81. }
  82. @endcode
  83. @see ReadWriteLock, ScopedTryWriteLock
  84. @tags{Core}
  85. */
  86. class JUCE_API ScopedTryReadLock
  87. {
  88. public:
  89. //==============================================================================
  90. /** Creates a ScopedTryReadLock and calls ReadWriteLock::tryEnterRead() as soon as it is
  91. created. When the ScopedTryReadLock object is destructed, the ReadWriteLock will be unlocked
  92. (if it was successfully acquired).
  93. Make sure this object is created and destructed by the same thread, otherwise there are no
  94. guarantees what will happen! Best just to use it as a local stack object, rather than creating
  95. one with the new() operator.
  96. */
  97. explicit ScopedTryReadLock (ReadWriteLock& lockIn)
  98. : ScopedTryReadLock (lockIn, true) {}
  99. /** Creates a ScopedTryReadLock.
  100. If acquireLockOnInitialisation is true then as soon as it is created, this will call
  101. ReadWriteLock::tryEnterRead(), and when the ScopedTryReadLock object is destructed, the
  102. ReadWriteLock will be unlocked (if it was successfully acquired).
  103. Make sure this object is created and destructed by the same thread, otherwise there are no
  104. guarantees what will happen! Best just to use it as a local stack object, rather than creating
  105. one with the new() operator.
  106. */
  107. ScopedTryReadLock (ReadWriteLock& lockIn, bool acquireLockOnInitialisation) noexcept
  108. : lock (lockIn), lockWasSuccessful (acquireLockOnInitialisation && lock.tryEnterRead()) {}
  109. /** Destructor.
  110. The ReadWriteLock's exitRead() method will be called when the destructor is called.
  111. Make sure this object is created and destructed by the same thread, otherwise there are no
  112. guarantees what will happen!
  113. */
  114. ~ScopedTryReadLock() noexcept { if (lockWasSuccessful) lock.exitRead(); }
  115. /** Returns true if the mutex was successfully locked. */
  116. bool isLocked() const noexcept { return lockWasSuccessful; }
  117. /** Retry gaining the lock by calling tryEnter on the underlying lock. */
  118. bool retryLock() noexcept { return lockWasSuccessful = lock.tryEnterRead(); }
  119. private:
  120. //==============================================================================
  121. ReadWriteLock& lock;
  122. bool lockWasSuccessful;
  123. JUCE_DECLARE_NON_COPYABLE (ScopedTryReadLock)
  124. };
  125. } // namespace juce