carb::thread::detail::SpinlockImpl

Defined in carb/thread/Spinlock.h

template<class RecursionPolicy>
class SpinlockImpl

Spinlock and RecursiveSpinlock are locking primitives that never enter the kernel to wait.

This class meets Cpp17BasicLockable and Cpp17Lockable named requirements.

Note

Do not use SpinlockImpl directly; instead use Spinlock or RecursiveSpinlock.

Warning

Using Spinlock is generally discouraged and can lead to worse performance than using carb::thread::mutex or another synchronization primitive that can wait.

Public Functions

constexpr SpinlockImpl() = default

Constructor.

~SpinlockImpl() = default

Destructor.

inline void lock()

Locks the spinlock, spinning the current thread until it becomes available.

If not called from RecursiveSpinlock and the calling thread already owns the lock, std::terminate() is called. The calling thread must call unlock() at a later time to release the lock.

inline void unlock()

Unlocks the spinlock.

Warning

std::terminate() is called if the calling thread does not own the spinlock.

inline bool try_lock()

Attempts to immediately lock the spinlock.

If not called from RecursiveSpinlock and the calling thread already owns the lock, std::terminate() is called.

Returns

true if the spinlock was available and the lock was taken by the calling thread (unlock() must be called from the calling thread at a later time to release the lock); false if the spinlock could not be locked by the calling thread.

inline bool isLockedByThisThread() const

Returns true if the calling thread owns this spinlock.

Returns

true if the calling thread owns this spinlock; false otherwise.