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.

81 lines
2.6KB

  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. #pragma once
  18. //==============================================================================
  19. /**
  20. Automatically locks and unlocks a ReadWriteLock object.
  21. Use one of these as a local variable to control access to a ReadWriteLock.
  22. e.g. @code
  23. ReadWriteLock myLock;
  24. for (;;)
  25. {
  26. const ScopedReadLock myScopedLock (myLock);
  27. // myLock is now locked
  28. ...do some stuff...
  29. // myLock gets unlocked here.
  30. }
  31. @endcode
  32. @see ReadWriteLock, ScopedWriteLock
  33. */
  34. class JUCE_API ScopedReadLock
  35. {
  36. public:
  37. //==============================================================================
  38. /** Creates a ScopedReadLock.
  39. As soon as it is created, this will call ReadWriteLock::enterRead(), and
  40. when the ScopedReadLock object is deleted, the ReadWriteLock will
  41. be unlocked.
  42. Make sure this object is created and deleted by the same thread,
  43. otherwise there are no guarantees what will happen! Best just to use it
  44. as a local stack object, rather than creating one with the new() operator.
  45. */
  46. inline explicit ScopedReadLock (const ReadWriteLock& lock) noexcept : lock_ (lock) { lock.enterRead(); }
  47. /** Destructor.
  48. The ReadWriteLock's exitRead() method will be called when the destructor is called.
  49. Make sure this object is created and deleted by the same thread,
  50. otherwise there are no guarantees what will happen!
  51. */
  52. inline ~ScopedReadLock() noexcept { lock_.exitRead(); }
  53. private:
  54. //==============================================================================
  55. const ReadWriteLock& lock_;
  56. JUCE_DECLARE_NON_COPYABLE (ScopedReadLock)
  57. };