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.

93 lines
3.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_SCOPEDREADLOCK_H_INCLUDED
  24. #define JUCE_SCOPEDREADLOCK_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. Automatically locks and unlocks a ReadWriteLock object.
  28. Use one of these as a local variable to control access to a ReadWriteLock.
  29. e.g. @code
  30. ReadWriteLock myLock;
  31. for (;;)
  32. {
  33. const ScopedReadLock myScopedLock (myLock);
  34. // myLock is now locked
  35. ...do some stuff...
  36. // myLock gets unlocked here.
  37. }
  38. @endcode
  39. @see ReadWriteLock, ScopedWriteLock
  40. */
  41. class JUCE_API ScopedReadLock
  42. {
  43. public:
  44. //==============================================================================
  45. /** Creates a ScopedReadLock.
  46. As soon as it is created, this will call ReadWriteLock::enterRead(), and
  47. when the ScopedReadLock object is deleted, the ReadWriteLock will
  48. be unlocked.
  49. Make sure this object is created and deleted by the same thread,
  50. otherwise there are no guarantees what will happen! Best just to use it
  51. as a local stack object, rather than creating one with the new() operator.
  52. */
  53. inline explicit ScopedReadLock (const ReadWriteLock& lock) noexcept : lock_ (lock) { lock.enterRead(); }
  54. /** Destructor.
  55. The ReadWriteLock's exitRead() method will be called when the destructor is called.
  56. Make sure this object is created and deleted by the same thread,
  57. otherwise there are no guarantees what will happen!
  58. */
  59. inline ~ScopedReadLock() noexcept { lock_.exitRead(); }
  60. private:
  61. //==============================================================================
  62. const ReadWriteLock& lock_;
  63. JUCE_DECLARE_NON_COPYABLE (ScopedReadLock)
  64. };
  65. #endif // JUCE_SCOPEDREADLOCK_H_INCLUDED