| 
    rippled
    
   | 
 
Classes to handle arrays of spinlocks packed into a single atomic integer: More...
#include <spinlock.h>
Public Member Functions | |
| packed_spinlock (packed_spinlock const &)=delete | |
| packed_spinlock & | operator= (packed_spinlock const &)=delete | 
| packed_spinlock (std::atomic< T > &lock, int index) | |
| A single spinlock packed inside the specified atomic.   | |
| bool | try_lock () | 
| void | lock () | 
| void | unlock () | 
Private Attributes | |
| std::atomic< T > & | bits_ | 
| T const | mask_ | 
Classes to handle arrays of spinlocks packed into a single atomic integer:
Packed spinlocks allow for tremendously space-efficient lock-sharding but they come at a cost.
First, the implementation is necessarily low-level and uses advanced features like memory ordering and highly platform-specific tricks to maximize performance. This imposes a significant and ongoing cost to developers.
Second, and perhaps most important, is that the packing of multiple locks into a single integer which, albeit space-efficient, also has performance implications stemming from data dependencies, increased cache-coherency traffic between processors and heavier loads on the processor's load/store units.
To be sure, these locks can have advantages but they are definitely not general purpose locks and should not be thought of or used that way. The use cases for them are likely few and far between; without a compelling reason to use them, backed by profiling data, it might be best to use one of the standard locking primitives instead. Note that in most common platforms, std::mutex is so heavily optimized that it can, usually, outperform spinlocks.
| T | An unsigned integral type (e.g. std::uint16_t) A class that grabs a single packed spinlock from an atomic integer. | 
This class meets the requirements of Lockable: https://en.cppreference.com/w/cpp/named_req/Lockable
Definition at line 90 of file spinlock.h.
      
  | 
  delete | 
| ripple::packed_spinlock< T >::packed_spinlock | ( | std::atomic< T > & | lock, | 
| int | index | ||
| ) | 
A single spinlock packed inside the specified atomic.
| lock | The atomic integer inside which the spinlock is packed. | 
| index | The index of the spinlock this object acquires. | 
lock be on a cacheline by itself. Definition at line 118 of file spinlock.h.
      
  | 
  delete | 
| bool ripple::packed_spinlock< T >::try_lock | ( | ) | 
Definition at line 127 of file spinlock.h.
| void ripple::packed_spinlock< T >::lock | ( | ) | 
Definition at line 133 of file spinlock.h.
| void ripple::packed_spinlock< T >::unlock | ( | ) | 
Definition at line 147 of file spinlock.h.
      
  | 
  private | 
Definition at line 102 of file spinlock.h.
      
  | 
  private | 
Definition at line 103 of file spinlock.h.