xrpld
Loading...
Searching...
No Matches
xrpl::Spinlock< T > Class Template Reference

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

#include <spinlock.h>

Public Member Functions

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

Private Attributes

std::atomic< T > & lock_

Detailed Description

template<class T>
class xrpl::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 154 of file spinlock.h.

Constructor & Destructor Documentation

◆ Spinlock() [1/2]

template<class T>
xrpl::Spinlock< T >::Spinlock ( Spinlock< T > const & )
delete

◆ Spinlock() [2/2]

template<class T>
xrpl::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 175 of file spinlock.h.

Member Function Documentation

◆ operator=()

template<class T>
Spinlock & xrpl::Spinlock< T >::operator= ( Spinlock< T > const & )
delete

◆ try_lock()

template<class T>
bool xrpl::Spinlock< T >::try_lock ( )
nodiscard

Definition at line 180 of file spinlock.h.

◆ lock()

template<class T>
void xrpl::Spinlock< T >::lock ( )

Definition at line 192 of file spinlock.h.

◆ unlock()

template<class T>
void xrpl::Spinlock< T >::unlock ( )

Definition at line 206 of file spinlock.h.

Member Data Documentation

◆ lock_

template<class T>
std::atomic<T>& xrpl::Spinlock< T >::lock_
private

Definition at line 160 of file spinlock.h.