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.

90 lines
3.5KB

  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. #pragma once
  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. };