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.

85 lines
2.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. } // namespace juce