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.

89 lines
3.2KB

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