qb  2.0.0.0
C++17 Actor Framework
qb Issue Watch Star Fork Follow @isndev
Loading...
Searching...
No Matches
qb::lockfree::SpinLock Class Reference

A spinlock implementation for lightweight thread synchronization. More...

#include <spinlock.h>

Public Member Functions

 SpinLock () noexcept
 Default constructor that initializes the lock to unlocked state.
 SpinLock (const SpinLock &)=delete
 Copy constructor is deleted to prevent copying of locks.
 SpinLock (SpinLock &&)=delete
 Move constructor is deleted to prevent moving of locks.
 ~SpinLock ()=default
 Default destructor.
SpinLockoperator= (const SpinLock &)=delete
 Copy assignment operator is deleted to prevent copying of locks.
SpinLockoperator= (SpinLock &&)=delete
 Move assignment operator is deleted to prevent moving of locks.
bool locked () noexcept
 Check if the spinlock is currently locked.
bool trylock () noexcept
 Try to acquire the lock without spinning.
bool trylock (int64_t spin) noexcept
 Try to acquire the lock with a maximum number of spin attempts.
bool trylock_for (const Timespan &timespan) noexcept
 Try to acquire the lock for a specified time duration.
bool trylock_until (const UtcTimestamp &timestamp) noexcept
 Try to acquire the lock until a specified point in time.
void lock () noexcept
 Acquire the lock, waiting indefinitely if necessary.
void unlock () noexcept
 Release the lock.

Detailed Description

A spinlock implementation for lightweight thread synchronization.

A spinlock is a lock that causes a thread trying to acquire it to wait in a loop ("spin") while repeatedly checking if the lock is available. This implementation provides methods to acquire the lock with various strategies such as trying for a specific duration or spin count.

Member Function Documentation

◆ locked()

bool qb::lockfree::SpinLock::locked ( )
inlinenoexcept

Check if the spinlock is currently locked.

Returns
true if the lock is held, false otherwise

◆ trylock() [1/2]

bool qb::lockfree::SpinLock::trylock ( )
inlinenoexcept

Try to acquire the lock without spinning.

This method attempts to acquire the lock once and returns immediately regardless of success.

Returns
true if the lock was acquired, false if it was already locked

◆ trylock() [2/2]

bool qb::lockfree::SpinLock::trylock ( int64_t spin)
inlinenoexcept

Try to acquire the lock with a maximum number of spin attempts.

This method attempts to acquire the lock, spinning for up to 'spin' iterations if necessary.

Parameters
spinMaximum number of spin iterations
Returns
true if the lock was acquired, false if maximum spins exceeded

◆ trylock_for()

bool qb::lockfree::SpinLock::trylock_for ( const Timespan & timespan)
inlinenoexcept

Try to acquire the lock for a specified time duration.

This method attempts to acquire the lock, spinning until either the lock is acquired or the specified duration has elapsed.

Parameters
timespanMaximum duration to try acquiring the lock
Returns
true if the lock was acquired, false if the timeout expired

◆ trylock_until()

bool qb::lockfree::SpinLock::trylock_until ( const UtcTimestamp & timestamp)
inlinenoexcept

Try to acquire the lock until a specified point in time.

This method attempts to acquire the lock, spinning until either the lock is acquired or the specified timestamp is reached.

Parameters
timestampPoint in time until which to try acquiring the lock
Returns
true if the lock was acquired, false if the timeout expired

◆ lock()

void qb::lockfree::SpinLock::lock ( )
inlinenoexcept

Acquire the lock, waiting indefinitely if necessary.

This method spins until the lock is acquired, with no timeout. It should be used with caution as it can potentially cause deadlocks or waste CPU resources if the lock is held for a long time.

◆ unlock()

void qb::lockfree::SpinLock::unlock ( )
inlinenoexcept

Release the lock.

This method releases the lock, allowing other threads to acquire it.