juce_SpinLock.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_SPINLOCK_H_INCLUDED
  22. #define JUCE_SPINLOCK_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. A simple spin-lock class that can be used as a simple, low-overhead mutex for
  26. uncontended situations.
  27. Note that unlike a CriticalSection, this type of lock is not re-entrant, and may
  28. be less efficient when used it a highly contended situation, but it's very small and
  29. requires almost no initialisation.
  30. It's most appropriate for simple situations where you're only going to hold the
  31. lock for a very brief time.
  32. @see CriticalSection
  33. */
  34. class JUCE_API SpinLock
  35. {
  36. public:
  37. inline SpinLock() noexcept {}
  38. inline ~SpinLock() noexcept {}
  39. /** Acquires the lock.
  40. This will block until the lock has been successfully acquired by this thread.
  41. Note that a SpinLock is NOT re-entrant, and is not smart enough to know whether the
  42. caller thread already has the lock - so if a thread tries to acquire a lock that it
  43. already holds, this method will never return!
  44. It's strongly recommended that you never call this method directly - instead use the
  45. ScopedLockType class to manage the locking using an RAII pattern instead.
  46. */
  47. void enter() const noexcept;
  48. /** Attempts to acquire the lock, returning true if this was successful. */
  49. inline bool tryEnter() const noexcept
  50. {
  51. return lock.compareAndSetBool (1, 0);
  52. }
  53. /** Releases the lock. */
  54. inline void exit() const noexcept
  55. {
  56. jassert (lock.value == 1); // Agh! Releasing a lock that isn't currently held!
  57. lock = 0;
  58. }
  59. //==============================================================================
  60. /** Provides the type of scoped lock to use for locking a SpinLock. */
  61. typedef GenericScopedLock <SpinLock> ScopedLockType;
  62. /** Provides the type of scoped unlocker to use with a SpinLock. */
  63. typedef GenericScopedUnlock <SpinLock> ScopedUnlockType;
  64. private:
  65. //==============================================================================
  66. mutable Atomic<int> lock;
  67. JUCE_DECLARE_NON_COPYABLE (SpinLock)
  68. };
  69. #endif // JUCE_SPINLOCK_H_INCLUDED