diff --git a/src/extra/ScopedLock.hpp b/src/extra/ScopedLock.hpp deleted file mode 100644 index 419e60a..0000000 --- a/src/extra/ScopedLock.hpp +++ /dev/null @@ -1,238 +0,0 @@ -/* - ============================================================================== - - This file is part of the Water library. - Copyright (c) 2016 ROLI Ltd. - Copyright (C) 2017 Filipe Coelho - - Permission is granted to use this software under the terms of the ISC license - http://www.isc.org/downloads/software-support-policy/isc-license/ - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD - TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, - OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF - USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER - TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE - OF THIS SOFTWARE. - - ============================================================================== -*/ - -#ifndef WATER_SCOPEDLOCK_HPP_INCLUDED -#define WATER_SCOPEDLOCK_HPP_INCLUDED - -#include "DistrhoUtils.hpp" - -START_NAMESPACE_DISTRHO - -//============================================================================== -/** - Automatically locks and unlocks a mutex object. - - Use one of these as a local variable to provide RAII-based locking of a mutex. - - The templated class could be a CriticalSection, SpinLock, or anything else that - provides enter() and exit() methods. - - e.g. @code - CriticalSection myCriticalSection; - - for (;;) - { - const GenericScopedLock myScopedLock (myCriticalSection); - // myCriticalSection is now locked - - ...do some stuff... - - // myCriticalSection gets unlocked here. - } - @endcode - - @see GenericScopedUnlock, CriticalSection, SpinLock, ScopedLock, ScopedUnlock -*/ -template -class GenericScopedLock -{ -public: - //============================================================================== - /** Creates a GenericScopedLock. - - As soon as it is created, this will acquire the lock, and when the GenericScopedLock - object is deleted, the lock will be released. - - Make sure this object is created and deleted by the same thread, - otherwise there are no guarantees what will happen! Best just to use it - as a local stack object, rather than creating one with the new() operator. - */ - inline explicit GenericScopedLock (const LockType& lock) noexcept : lock_ (lock) { lock.enter(); } - - /** Destructor. - The lock will be released when the destructor is called. - Make sure this object is created and deleted by the same thread, otherwise there are - no guarantees what will happen! - */ - inline ~GenericScopedLock() noexcept { lock_.exit(); } - -private: - //============================================================================== - const LockType& lock_; - - DISTRHO_DECLARE_NON_COPYABLE (GenericScopedLock) -}; - - -//============================================================================== -/** - Automatically unlocks and re-locks a mutex object. - - This is the reverse of a GenericScopedLock object - instead of locking the mutex - for the lifetime of this object, it unlocks it. - - Make sure you don't try to unlock mutexes that aren't actually locked! - - e.g. @code - - CriticalSection myCriticalSection; - - for (;;) - { - const GenericScopedLock myScopedLock (myCriticalSection); - // myCriticalSection is now locked - - ... do some stuff with it locked .. - - while (xyz) - { - ... do some stuff with it locked .. - - const GenericScopedUnlock unlocker (myCriticalSection); - - // myCriticalSection is now unlocked for the remainder of this block, - // and re-locked at the end. - - ...do some stuff with it unlocked ... - } - - // myCriticalSection gets unlocked here. - } - @endcode - - @see GenericScopedLock, CriticalSection, ScopedLock, ScopedUnlock -*/ -template -class GenericScopedUnlock -{ -public: - //============================================================================== - /** Creates a GenericScopedUnlock. - - As soon as it is created, this will unlock the CriticalSection, and - when the ScopedLock object is deleted, the CriticalSection will - be re-locked. - - Make sure this object is created and deleted by the same thread, - otherwise there are no guarantees what will happen! Best just to use it - as a local stack object, rather than creating one with the new() operator. - */ - inline explicit GenericScopedUnlock (const LockType& lock) noexcept : lock_ (lock) { lock.exit(); } - - /** Destructor. - - The CriticalSection will be unlocked when the destructor is called. - - Make sure this object is created and deleted by the same thread, - otherwise there are no guarantees what will happen! - */ - inline ~GenericScopedUnlock() noexcept { lock_.enter(); } - - -private: - //============================================================================== - const LockType& lock_; - - DISTRHO_DECLARE_NON_COPYABLE (GenericScopedUnlock) -}; - - -//============================================================================== -/** - Automatically locks and unlocks a mutex object. - - Use one of these as a local variable to provide RAII-based locking of a mutex. - - The templated class could be a CriticalSection, SpinLock, or anything else that - provides enter() and exit() methods. - - e.g. @code - - CriticalSection myCriticalSection; - - for (;;) - { - const GenericScopedTryLock myScopedTryLock (myCriticalSection); - - // Unlike using a ScopedLock, this may fail to actually get the lock, so you - // should test this with the isLocked() method before doing your thread-unsafe - // action.. - if (myScopedTryLock.isLocked()) - { - ...do some stuff... - } - else - { - ..our attempt at locking failed because another thread had already locked it.. - } - - // myCriticalSection gets unlocked here (if it was locked) - } - @endcode - - @see CriticalSection::tryEnter, GenericScopedLock, GenericScopedUnlock -*/ -template -class GenericScopedTryLock -{ -public: - //============================================================================== - /** Creates a GenericScopedTryLock. - - As soon as it is created, this will attempt to acquire the lock, and when the - GenericScopedTryLock is deleted, the lock will be released (if the lock was - successfully acquired). - - Make sure this object is created and deleted by the same thread, - otherwise there are no guarantees what will happen! Best just to use it - as a local stack object, rather than creating one with the new() operator. - */ - inline explicit GenericScopedTryLock (const LockType& lock) noexcept - : lock_ (lock), lockWasSuccessful (lock.tryEnter()) {} - - /** Destructor. - - The mutex will be unlocked (if it had been successfully locked) when the - destructor is called. - - Make sure this object is created and deleted by the same thread, - otherwise there are no guarantees what will happen! - */ - inline ~GenericScopedTryLock() noexcept { if (lockWasSuccessful) lock_.exit(); } - - /** Returns true if the mutex was successfully locked. */ - bool isLocked() const noexcept { return lockWasSuccessful; } - -private: - //============================================================================== - const LockType& lock_; - const bool lockWasSuccessful; - - DISTRHO_DECLARE_NON_COPYABLE (GenericScopedTryLock) -}; - -END_NAMESPACE_DISTRHO - -#endif // WATER_SCOPEDLOCK_HPP_INCLUDED diff --git a/src/extra/SharedResourcePointer.hpp b/src/extra/SharedResourcePointer.hpp index 4688ade..fdfc4ca 100644 --- a/src/extra/SharedResourcePointer.hpp +++ b/src/extra/SharedResourcePointer.hpp @@ -3,7 +3,7 @@ This file is part of the Water library. Copyright (c) 2016 ROLI Ltd. - Copyright (C) 2017-2019 Filipe Coelho + Copyright (C) 2017-2022 Filipe Coelho Permission is granted to use this software under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license/ @@ -27,7 +27,7 @@ #define WATER_SHAREDRESOURCEPOINTER_HPP_INCLUDED #include "ReferenceCountedObject.hpp" -#include "SpinLock.hpp" +#include "extra/Mutex.hpp" #include "extra/ScopedPointer.hpp" START_NAMESPACE_DISTRHO @@ -127,7 +127,7 @@ public: ~SharedResourcePointer() { SharedObjectHolder& holder = getSharedObjectHolder(); - const SpinLock::ScopedLockType sl (holder.lock); + const MutexLocker cml (holder.lock); if (--(holder.refCount) == 0) holder.sharedInstance = nullptr; @@ -150,7 +150,7 @@ public: private: struct SharedObjectHolder : public ReferenceCountedObject { - SpinLock lock; + Mutex lock; ScopedPointer sharedInstance; int refCount; }; @@ -166,7 +166,7 @@ private: void initialise() { SharedObjectHolder& holder = getSharedObjectHolder(); - const SpinLock::ScopedLockType sl (holder.lock); + const MutexLocker cml (holder.lock); if (++(holder.refCount) == 1) holder.sharedInstance = new SharedObjectType(); @@ -178,7 +178,7 @@ private: void initialise_variant(const T* const variant) { SharedObjectHolder& holder = getSharedObjectHolder(); - const SpinLock::ScopedLockType sl (holder.lock); + const MutexLocker cml (holder.lock); if (++(holder.refCount) == 1) holder.sharedInstance = new SharedObjectType(variant); @@ -190,7 +190,7 @@ private: void initialise_variant2(const T1* const v1, const T2* const v2) { SharedObjectHolder& holder = getSharedObjectHolder(); - const SpinLock::ScopedLockType sl (holder.lock); + const MutexLocker cml (holder.lock); if (++(holder.refCount) == 1) holder.sharedInstance = new SharedObjectType(v1, v2); diff --git a/src/extra/SpinLock.hpp b/src/extra/SpinLock.hpp deleted file mode 100644 index 210f1dd..0000000 --- a/src/extra/SpinLock.hpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - ============================================================================== - - This file is part of the Water library. - Copyright (c) 2016 ROLI Ltd. - Copyright (C) 2017 Filipe Coelho - - Permission is granted to use this software under the terms of the ISC license - http://www.isc.org/downloads/software-support-policy/isc-license/ - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD - TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, - OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF - USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER - TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE - OF THIS SOFTWARE. - - ============================================================================== -*/ - -#ifndef WATER_SPINLOCK_HPP_INCLUDED -#define WATER_SPINLOCK_HPP_INCLUDED - -#include "Atomic.hpp" -#include "ScopedLock.hpp" - -START_NAMESPACE_DISTRHO - -//============================================================================== -/** - A simple spin-lock class that can be used as a simple, low-overhead mutex for - uncontended situations. - - Note that unlike a CriticalSection, this type of lock is not re-entrant, and may - be less efficient when used it a highly contended situation, but it's very small and - requires almost no initialisation. - It's most appropriate for simple situations where you're only going to hold the - lock for a very brief time. - - @see CriticalSection -*/ -class SpinLock -{ -public: - inline SpinLock() noexcept : lock() {} - inline ~SpinLock() noexcept {} - - /** Acquires the lock. - This will block until the lock has been successfully acquired by this thread. - Note that a SpinLock is NOT re-entrant, and is not smart enough to know whether the - caller thread already has the lock - so if a thread tries to acquire a lock that it - already holds, this method will never return! - - It's strongly recommended that you never call this method directly - instead use the - ScopedLockType class to manage the locking using an RAII pattern instead. - */ - void enter() const noexcept - { - if (! tryEnter()) - { - for (int i = 20; --i >= 0;) - if (tryEnter()) - return; - - while (! tryEnter()) - { -#ifdef DISTRHO_OS_WINDOWS - Sleep (0); -#else - sched_yield(); -#endif - } - } - } - - /** Attempts to acquire the lock, returning true if this was successful. */ - inline bool tryEnter() const noexcept - { - return lock.compareAndSetBool (1, 0); - } - - /** Releases the lock. */ - inline void exit() const noexcept - { - DISTRHO_SAFE_ASSERT_RETURN (lock.get() == 1,); - lock = 0; - } - - //============================================================================== - /** Provides the type of scoped lock to use for locking a SpinLock. */ - typedef GenericScopedLock ScopedLockType; - - /** Provides the type of scoped unlocker to use with a SpinLock. */ - typedef GenericScopedUnlock ScopedUnlockType; - -private: - //============================================================================== - mutable Atomic lock; - - DISTRHO_DECLARE_NON_COPYABLE (SpinLock) -}; - -END_NAMESPACE_DISTRHO - -#endif // WATER_SPINLOCK_HPP_INCLUDED