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.

82 lines
3.0KB

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