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.

91 lines
3.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_SPINLOCK_JUCEHEADER__
  19. #define __JUCE_SPINLOCK_JUCEHEADER__
  20. #include "juce_ScopedLock.h"
  21. //==============================================================================
  22. /**
  23. A simple spin-lock class that can be used as a simple, low-overhead mutex for
  24. uncontended situations.
  25. Note that unlike a CriticalSection, this type of lock is not re-entrant, and may
  26. be less efficient when used it a highly contended situation, but it's very small and
  27. requires almost no initialisation.
  28. It's most appropriate for simple situations where you're only going to hold the
  29. lock for a very brief time.
  30. @see CriticalSection
  31. */
  32. class JUCE_API SpinLock
  33. {
  34. public:
  35. inline SpinLock() noexcept {}
  36. inline ~SpinLock() noexcept {}
  37. /** Acquires the lock.
  38. This will block until the lock has been successfully acquired by this thread.
  39. Note that a SpinLock is NOT re-entrant, and is not smart enough to know whether the
  40. caller thread already has the lock - so if a thread tries to acquire a lock that it
  41. already holds, this method will never return!
  42. It's strongly recommended that you never call this method directly - instead use the
  43. ScopedLockType class to manage the locking using an RAII pattern instead.
  44. */
  45. void enter() const noexcept;
  46. /** Attempts to acquire the lock, returning true if this was successful. */
  47. inline bool tryEnter() const noexcept
  48. {
  49. return lock.compareAndSetBool (1, 0);
  50. }
  51. /** Releases the lock. */
  52. inline void exit() const noexcept
  53. {
  54. jassert (lock.value == 1); // Agh! Releasing a lock that isn't currently held!
  55. lock = 0;
  56. }
  57. //==============================================================================
  58. /** Provides the type of scoped lock to use for locking a SpinLock. */
  59. typedef GenericScopedLock <SpinLock> ScopedLockType;
  60. /** Provides the type of scoped unlocker to use with a SpinLock. */
  61. typedef GenericScopedUnlock <SpinLock> ScopedUnlockType;
  62. private:
  63. //==============================================================================
  64. mutable Atomic<int> lock;
  65. JUCE_DECLARE_NON_COPYABLE (SpinLock);
  66. };
  67. #endif // __JUCE_SPINLOCK_JUCEHEADER__