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.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_SPINLOCK_H_INCLUDED
  24. #define JUCE_SPINLOCK_H_INCLUDED
  25. //==============================================================================
  26. /**
  27. A simple spin-lock class that can be used as a simple, low-overhead mutex for
  28. uncontended situations.
  29. Note that unlike a CriticalSection, this type of lock is not re-entrant, and may
  30. be less efficient when used it a highly contended situation, but it's very small and
  31. requires almost no initialisation.
  32. It's most appropriate for simple situations where you're only going to hold the
  33. lock for a very brief time.
  34. @see CriticalSection
  35. */
  36. class JUCE_API SpinLock
  37. {
  38. public:
  39. inline SpinLock() noexcept {}
  40. inline ~SpinLock() noexcept {}
  41. /** Acquires the lock.
  42. This will block until the lock has been successfully acquired by this thread.
  43. Note that a SpinLock is NOT re-entrant, and is not smart enough to know whether the
  44. caller thread already has the lock - so if a thread tries to acquire a lock that it
  45. already holds, this method will never return!
  46. It's strongly recommended that you never call this method directly - instead use the
  47. ScopedLockType class to manage the locking using an RAII pattern instead.
  48. */
  49. void enter() const noexcept;
  50. /** Attempts to acquire the lock, returning true if this was successful. */
  51. inline bool tryEnter() const noexcept
  52. {
  53. return lock.compareAndSetBool (1, 0);
  54. }
  55. /** Releases the lock. */
  56. inline void exit() const noexcept
  57. {
  58. jassert (lock.value == 1); // Agh! Releasing a lock that isn't currently held!
  59. lock = 0;
  60. }
  61. //==============================================================================
  62. /** Provides the type of scoped lock to use for locking a SpinLock. */
  63. typedef GenericScopedLock <SpinLock> ScopedLockType;
  64. /** Provides the type of scoped unlocker to use with a SpinLock. */
  65. typedef GenericScopedUnlock <SpinLock> ScopedUnlockType;
  66. private:
  67. //==============================================================================
  68. mutable Atomic<int> lock;
  69. JUCE_DECLARE_NON_COPYABLE (SpinLock)
  70. };
  71. #endif // JUCE_SPINLOCK_H_INCLUDED