StuBS
Spinlock Class Reference

By the use of Spinlocks, it is possible to serialize blocks of code that might run parallel on multiple CPU cores. More...

#include <sync/spinlock.h>

Public Member Functions

 Spinlock ()
 Constructor; Initializes as unlocked.
 
void lock ()
 Enters the critical area. In case the area is already locked, lock() will actively wait for the area can be entered.
 
void unlock ()
 Unblocks the critical area.
 

Private Member Functions

 Spinlock (const Spinlock &copy)=delete
 
Spinlockoperator= (const Spinlock &)=delete
 

Private Attributes

cache_aligned bool lock_status
 

Detailed Description

By the use of Spinlocks, it is possible to serialize blocks of code that might run parallel on multiple CPU cores.

Synchronization is implemented using a lock variable. Once a thread enters the critical area, it sets the lock variable (to a non-zero value); when this thread leaves the critical area, it resets the lock variable to zero. When trying to enter an already locked critical area, the trying thread actively waits until the critical area is free again.

Use the following two GCC intrinsics

  • bool __atomic_test_and_set(void *ptr, int memorder)
  • void __atomic_clear (bool *ptr, int memorder)

These intrinsics are translated into atomic, architecture-specific CPU instructions.

Note
If you want that things just work, choose __ATOMIC_SEQ_CST as memorder. This is not the most efficient memory order but works reasonably well.

Atomic Builtins in GCC manual

Constructor & Destructor Documentation

◆ Spinlock() [1/2]

Spinlock::Spinlock ( const Spinlock copy)
privatedelete

◆ Spinlock() [2/2]

Spinlock::Spinlock ( )
inline

Constructor; Initializes as unlocked.

Todo:
Complete Constructor (for MPStuBS)

Member Function Documentation

◆ operator=()

Spinlock & Spinlock::operator= ( const Spinlock )
privatedelete

◆ lock()

void Spinlock::lock ( )
inline

Enters the critical area. In case the area is already locked, lock() will actively wait for the area can be entered.

See also
Core::pause()
Todo:
Implement Method (for MPStuBS)

◆ unlock()

void Spinlock::unlock ( )
inline

Unblocks the critical area.

Todo:
Implement Method (for MPStuBS)

Member Data Documentation

◆ lock_status

cache_aligned bool Spinlock::lock_status
private

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