Kagome
Polkadot Runtime Engine in C++17
kagome::common::spin_lock Class Reference

#include <spin_lock.hpp>

Collaboration diagram for kagome::common::spin_lock:

Public Member Functions

 spin_lock ()=default
 
 spin_lock (const spin_lock &)=delete
 
 spin_lock (spin_lock &&)=delete
 
spin_lockoperator= (const spin_lock &)=delete
 
spin_lockoperator= (spin_lock &&)=delete
 
void lock ()
 
void unlock ()
 

Private Attributes

std::atomic_flag flag_ = ATOMIC_FLAG_INIT
 

Detailed Description

Implements a spin lock synchronization primitive.

Does busy-waiting over an atomic flag, thus the best application would be for the cases when the lock is required for a very short amount of time (to perform very few and finite computational steps under the lock).

An advantage over std::mutex is locking without a call to system's kernel and eliminating expensive context switches.

Can be used with std::lock_guard:

spin_lock mutex;
{
std::lock_guard lock(mutex);
// synchronized computations go here
}

Definition at line 32 of file spin_lock.hpp.

Constructor & Destructor Documentation

kagome::common::spin_lock::spin_lock ( )
default
kagome::common::spin_lock::spin_lock ( const spin_lock )
delete
kagome::common::spin_lock::spin_lock ( spin_lock &&  )
delete

Member Function Documentation

void kagome::common::spin_lock::lock ( )

Acquires the lock.

Blocks thread execution until the other thread releases the lock in a busy-waiting manner.

Definition at line 10 of file spin_lock.cpp.

spin_lock& kagome::common::spin_lock::operator= ( const spin_lock )
delete
spin_lock& kagome::common::spin_lock::operator= ( spin_lock &&  )
delete
void kagome::common::spin_lock::unlock ( )

Releases the lock.

Definition at line 15 of file spin_lock.cpp.

Member Data Documentation

std::atomic_flag kagome::common::spin_lock::flag_ = ATOMIC_FLAG_INIT
private

Definition at line 54 of file spin_lock.hpp.


The documentation for this class was generated from the following files: