rippled
Public Member Functions | Private Attributes | List of all members
ripple::spinlock< T > Class Template Reference

A spinlock implemented on top of an atomic integer. More...

Public Member Functions

 spinlock (spinlock const &)=delete
 
spinlockoperator= (spinlock const &)=delete
 
 spinlock (std::atomic< T > &lock)
 Grabs the. More...
 
bool try_lock ()
 
void lock ()
 
void unlock ()
 

Private Attributes

std::atomic< T > & lock_
 

Detailed Description

template<class T>
class ripple::spinlock< T >

A spinlock implemented on top of an atomic integer.

Note
Using packed_spinlock and spinlock against the same underlying atomic integer can result in spinlock not being able to actually acquire the lock during periods of high contention, because of how the two locks operate: spinlock will spin trying to grab all the bits at once, whereas any given packed_spinlock will only try to grab one bit at a time. Caveat emptor.

This class meets the requirements of Lockable: https://en.cppreference.com/w/cpp/named_req/Lockable

Definition at line 163 of file spinlock.h.

Constructor & Destructor Documentation

◆ spinlock() [1/2]

template<class T >
ripple::spinlock< T >::spinlock ( spinlock< T > const &  )
delete

◆ spinlock() [2/2]

template<class T >
ripple::spinlock< T >::spinlock ( std::atomic< T > &  lock)

Grabs the.

Parameters
lockThe atomic integer to spin against.
Note
For performance reasons, you should strive to have lock be on a cacheline by itself.

Definition at line 183 of file spinlock.h.

Member Function Documentation

◆ operator=()

template<class T >
spinlock& ripple::spinlock< T >::operator= ( spinlock< T > const &  )
delete

◆ try_lock()

template<class T >
bool ripple::spinlock< T >::try_lock ( )

Definition at line 188 of file spinlock.h.

◆ lock()

template<class T >
void ripple::spinlock< T >::lock ( )

Definition at line 200 of file spinlock.h.

◆ unlock()

template<class T >
void ripple::spinlock< T >::unlock ( )

Definition at line 214 of file spinlock.h.

Member Data Documentation

◆ lock_

template<class T >
std::atomic<T>& ripple::spinlock< T >::lock_
private

Definition at line 165 of file spinlock.h.