QuickCppLib 0.10
Eliminate all the tedious hassle when making state-of-the-art C++ 14 - 23 libraries!
Loading...
Searching...
No Matches
quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T > Struct Template Reference

#include "spinlock.hpp"

Public Types

typedef T value_type
 

Public Member Functions

constexpr shared_spinlockbase () noexcept
 
 shared_spinlockbase (const shared_spinlockbase &)=delete
 
 shared_spinlockbase (shared_spinlockbase &&) noexcept
 Atomically move constructs.
 
 ~shared_spinlockbase ()
 
shared_spinlockbaseoperator= (const shared_spinlockbase &)=delete
 
shared_spinlockbaseoperator= (shared_spinlockbase &&)=delete
 
constexpr T load (memory_order o=memory_order_seq_cst) const noexcept
 Returns the raw atomic.
 
void store (T a, memory_order o=memory_order_seq_cst) noexcept
 Sets the raw atomic.
 
bool try_lock () noexcept
 Tries to lock the spinlock for exclusive access, returning true if successful.
 
void unlock () noexcept
 Releases the lock from exclusive access.
 
bool try_lock_shared () noexcept
 Tries to lock the spinlock for shared access, returning true if successful.
 
void unlock_shared () noexcept
 Releases the lock from shared access.
 
bool try_convert_lock_to_shared () noexcept
 Tries to convert an exclusive lock to a shared lock, returning true if successful.
 
bool try_convert_lock_to_exclusive () noexcept
 Tries to convert a shared lock to an exclusive lock, returning true if successful.
 
bool int_yield (size_t) noexcept
 

Protected Attributes

atomic< value_type_v
 

Member Typedef Documentation

◆ value_type

template<typename T >
typedef T quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::value_type

Constructor & Destructor Documentation

◆ shared_spinlockbase() [1/3]

template<typename T >
constexpr quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::shared_spinlockbase ( )
inlineconstexprnoexcept
457 : _v(0)
458 {
460#if QUICKCPPLIB_IN_THREAD_SANITIZER
461 _v.store(0, memory_order_release);
462#endif
463 }
#define QUICKCPPLIB_ANNOTATE_RWLOCK_CREATE(p)
Definition config.hpp:102
atomic< value_type > _v
Definition spinlock.hpp:450

◆ shared_spinlockbase() [2/3]

template<typename T >
quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::shared_spinlockbase ( const shared_spinlockbase< T > &  )
delete

◆ shared_spinlockbase() [3/3]

template<typename T >
quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::shared_spinlockbase ( shared_spinlockbase< T > &&  )
inlinenoexcept

Atomically move constructs.

467 : _v(0)
468 {
470// v.store(o.v.exchange(0, memory_order_acq_rel));
471#if QUICKCPPLIB_IN_THREAD_SANITIZER
472 _v.store(0, memory_order_release);
473#endif
474 }

◆ ~shared_spinlockbase()

476 {
477#ifdef QUICKCPPLIB_ENABLE_VALGRIND
478 value_type i = _v.load(memory_order_relaxed);
479 if(i == 1)
480 {
482 }
483 else if(i != 0)
484 {
486 }
487#endif
489 }
#define QUICKCPPLIB_ANNOTATE_RWLOCK_DESTROY(p)
Definition config.hpp:103
#define QUICKCPPLIB_ANNOTATE_RWLOCK_RELEASED(p, s)
Definition config.hpp:105

Member Function Documentation

◆ operator=() [1/2]

template<typename T >
shared_spinlockbase & quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::operator= ( const shared_spinlockbase< T > &  )
delete

◆ operator=() [2/2]

◆ load()

template<typename T >
constexpr T quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::load ( memory_order  o = memory_order_seq_cst) const
inlineconstexprnoexcept

Returns the raw atomic.

493{ return _v.load(o); }

◆ store()

template<typename T >
void quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::store ( a,
memory_order  o = memory_order_seq_cst 
)
inlinenoexcept

Sets the raw atomic.

495{ _v.store(a, o); }

◆ try_lock()

template<typename T >
bool quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::try_lock ( )
inlinenoexcept

Tries to lock the spinlock for exclusive access, returning true if successful.

499 {
500 value_type i = _v.load(memory_order_relaxed), o = i;
501 // If locked by anybody, bail out immediately
502 if(i)
503 return false;
504 o = 1;
505 if(_v.compare_exchange_weak(i, o, memory_order_acquire, memory_order_relaxed))
506 {
508 return true;
509 }
510 return false;
511 }
#define QUICKCPPLIB_ANNOTATE_RWLOCK_ACQUIRED(p, s)
Definition config.hpp:104

◆ unlock()

template<typename T >
void quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::unlock ( )
inlinenoexcept

Releases the lock from exclusive access.

514 {
515 // assert(_v == 1);
517 _v.store(0, memory_order_release);
518 }

◆ try_lock_shared()

template<typename T >
bool quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::try_lock_shared ( )
inlinenoexcept

Tries to lock the spinlock for shared access, returning true if successful.

522 {
523 // OR in the exclusive lock bit
524 value_type i = _v.fetch_or(1, memory_order_acquire);
525 if(i & 1)
526 return false;
527 // If not locked, increment reader count and unlock
528 i += 2;
529 i &= ~1;
530 _v.store(i, memory_order_release);
532 return true;
533 }

◆ unlock_shared()

template<typename T >
void quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::unlock_shared ( )
inlinenoexcept

Releases the lock from shared access.

536 {
537 value_type i;
538 for(size_t n = 0;; n++)
539 {
540 i = _v.fetch_or(1, memory_order_acquire);
541 // assert(i > 1);
542 if(!(i & 1))
543 break;
544 // For very heavily contended locks, stop thrashing the cache line
545 if(n > 2)
546 {
547 for(size_t m = 0; m < 15000; m = m + 1)
548 {
549 volatile int x = 1;
550 (void) x;
551 }
552 }
553 }
555 i -= 2;
556 i &= ~1;
557 _v.store(i, memory_order_release);
558 }

◆ try_convert_lock_to_shared()

template<typename T >
bool quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::try_convert_lock_to_shared ( )
inlinenoexcept

Tries to convert an exclusive lock to a shared lock, returning true if successful.

562 {
563 value_type expected = 1;
564 return _v.compare_exchange_strong(expected, 2, memory_order_acquire, memory_order_relaxed);
565 }

◆ try_convert_lock_to_exclusive()

template<typename T >
bool quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::try_convert_lock_to_exclusive ( )
inlinenoexcept

Tries to convert a shared lock to an exclusive lock, returning true if successful.

568 {
569 value_type expected = 2;
570 return _v.compare_exchange_strong(expected, 1, memory_order_acquire, memory_order_relaxed);
571 }

◆ int_yield()

template<typename T >
bool quickcpplib::_xxx::configurable_spinlock::shared_spinlockbase< T >::int_yield ( size_t  )
inlinenoexcept
573{ return false; }

Member Data Documentation

◆ _v


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