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.

94 lines
3.7KB

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