juce_ScopedReadLock.h 3.2 KB

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