76#ifndef __ADS_EventCount
77 #define __ADS_EventCount
85#if defined( ADS_SANITIZE_THREAD ) && ! defined( ADS_EVENTCOUNT_RMW )
86 #define ADS_EVENTCOUNT_RMW
93#include "ADS/concurrent/impl/Spin.tpp"
116 using Key = std::uint32_t;
141 const std::uint64_t v = f_state.fetch_add( 1 , std::memory_order_seq_cst );
142#ifndef ADS_EVENTCOUNT_RMW
143 std::atomic_thread_fence( std::memory_order_seq_cst );
145 return(
Key( v >> 32 ) );
161 std::uint64_t v = f_state.load( std::memory_order_relaxed );
162 while( (
Key( v >> 32 ) == k ) && ! f_state.compare_exchange_weak(
164 std::memory_order_relaxed ) ) {
181 while( f_epoch.load( std::memory_order_acquire ) == k )
182 f_epoch.wait( k , std::memory_order_acquire );
209#ifndef ADS_EVENTCOUNT_RMW
210 std::atomic_thread_fence( std::memory_order_seq_cst );
211 std::uint64_t v = f_state.load( std::memory_order_relaxed );
213 std::uint64_t v = f_state.fetch_add( 0 , std::memory_order_seq_cst );
215 while( v & waiterMask ) [[unlikely]]
216 if( f_state.compare_exchange_weak( v , ( ( v >> 32 ) + 1 ) << 32 ,
217 std::memory_order_acq_rel ,
218 std::memory_order_relaxed ) ) {
219 raiseEpoch(
Key( ( v >> 32 ) + 1 ) );
220 f_epoch.notify_all();
244 void await( P && p , std::int64_t spin = 20000 ) {
248 std::chrono::steady_clock::now() + std::chrono::nanoseconds( spin );
249 for(
unsigned i = 1 ; ; ++i ) {
253 if( ( ( i & 63 ) == 0 ) && ( std::chrono::steady_clock::now() >= end ) )
267 [[nodiscard]] std::uint32_t
waiters(
void )
const noexcept {
269 std::uint32_t( f_state.load( std::memory_order_relaxed ) & waiterMask ) );
275 static void relax(
void )
noexcept { detail::spinPause(); }
290 void raiseEpoch(
Key e )
noexcept {
291 Key c = f_epoch.load( std::memory_order_relaxed );
292 while( ( std::int32_t( e - c ) > 0 ) &&
293 ! f_epoch.compare_exchange_weak( c , e , std::memory_order_release ,
294 std::memory_order_relaxed ) ) {
298 static constexpr std::uint64_t waiterMask = 0xFFFFFFFFu;
301 alignas( 64 ) std::atomic< std::uint64_t > f_state{ 0 };
305 std::atomic< Key > f_epoch{ 0 };
307 char f_pad[ 64 -
sizeof( std::uint64_t ) -
sizeof(
Key ) ] = {};
void cancelWait(Key k) noexcept
withdraws the waiter, the condition being true, unless a notifier already withdrew it (the epoch is n...
Definition EventCount.h:160
std::uint32_t waiters(void) const noexcept
the number of the threads announced as waiters at some moment
Definition EventCount.h:267
EventCount(void)=default
the event count of epoch 0 and no waiter
Key prepareWait(void) noexcept
announces a waiter and returns the epoch, before the last look at the condition
Definition EventCount.h:140
std::uint32_t Key
the epoch a waiter saw
Definition EventCount.h:116
EventCount & operator=(const EventCount &)=delete
no copy, since the waiters sleep on its address
void notify(void) noexcept
wakes the waiters, after the condition was made true: it advances the epoch and withdraws them all at...
Definition EventCount.h:208
void commitWait(Key k) noexcept
sleeps while the epoch is ; the notifier that advanced it withdrew the waiter
Definition EventCount.h:180
void await(P &&p, std::int64_t spin=20000)
returns when p() is true: it looks again and again for up to spin nanoseconds, with a pause of the pr...
Definition EventCount.h:244
EventCount(const EventCount &)=delete
no copy, since the waiters sleep on its address