Audio plugin host https://kx.studio/carla
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.

SpinLock.h 3.8KB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. ==============================================================================
  3. This file is part of the Water library.
  4. Copyright (c) 2016 ROLI Ltd.
  5. Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
  6. Permission is granted to use this software under the terms of the ISC license
  7. http://www.isc.org/downloads/software-support-policy/isc-license/
  8. Permission to use, copy, modify, and/or distribute this software for any
  9. purpose with or without fee is hereby granted, provided that the above
  10. copyright notice and this permission notice appear in all copies.
  11. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  12. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  13. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  14. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  15. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  16. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  17. OF THIS SOFTWARE.
  18. ==============================================================================
  19. */
  20. #ifndef WATER_SPINLOCK_H_INCLUDED
  21. #define WATER_SPINLOCK_H_INCLUDED
  22. #include "ScopedLock.h"
  23. #include "../memory/Atomic.h"
  24. namespace water {
  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 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. {
  51. if (! tryEnter())
  52. {
  53. for (int i = 20; --i >= 0;)
  54. if (tryEnter())
  55. return;
  56. while (! tryEnter())
  57. {
  58. #ifdef CARLA_OS_WIN
  59. Sleep (0);
  60. #else
  61. sched_yield();
  62. #endif
  63. }
  64. }
  65. }
  66. /** Attempts to acquire the lock, returning true if this was successful. */
  67. inline bool tryEnter() const noexcept
  68. {
  69. return lock.compareAndSetBool (1, 0);
  70. }
  71. /** Releases the lock. */
  72. inline void exit() const noexcept
  73. {
  74. CARLA_SAFE_ASSERT_RETURN(lock.get() == 1,);
  75. lock = 0;
  76. }
  77. //==============================================================================
  78. /** Provides the type of scoped lock to use for locking a SpinLock. */
  79. typedef GenericScopedLock <SpinLock> ScopedLockType;
  80. /** Provides the type of scoped unlocker to use with a SpinLock. */
  81. typedef GenericScopedUnlock <SpinLock> ScopedUnlockType;
  82. private:
  83. //==============================================================================
  84. mutable Atomic<int> lock;
  85. CARLA_DECLARE_NON_COPY_CLASS (SpinLock)
  86. };
  87. }
  88. #endif // WATER_SPINLOCK_H_INCLUDED