ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
EventCount.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File EventCount.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
72/*--------------------------------------------------------------------------*/
73/*----------------------------- DEFINITIONS --------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76#ifndef __ADS_EventCount
77 #define __ADS_EventCount
78/* self-identification: #endif constant */
79
80// under the sanitizer of threads, which does not model the fences, the
81// notifier reads the waiters by a read-modify-write instead (reading the
82// last value in the order of the modifications, it either sees the
83// waiter or is seen by it through the release sequence); the build tells
84// that the sanitizer is on by ADS_SANITIZE_THREAD [see CMakeLists.txt]
85#if defined( ADS_SANITIZE_THREAD ) && ! defined( ADS_EVENTCOUNT_RMW )
86 #define ADS_EVENTCOUNT_RMW
87#endif
88
89/*--------------------------------------------------------------------------*/
90/*------------------------------ INCLUDES ----------------------------------*/
91/*--------------------------------------------------------------------------*/
92
93#include "ADS/concurrent/impl/Spin.tpp"
94
95#include <atomic>
96#include <chrono>
97#include <cstdint>
98
99/*--------------------------------------------------------------------------*/
100/*--------------------------- NAMESPACE ------------------------------------*/
101/*--------------------------------------------------------------------------*/
102
103namespace ADS
104{
105
106/*--------------------------------------------------------------------------*/
107/*--------------------------- CLASS EventCount -----------------------------*/
108/*--------------------------------------------------------------------------*/
109
112{
113
114/*--------------------------------------------------------------------------*/
115 public:
116 using Key = std::uint32_t;
117
119 EventCount( void ) = default;
121 EventCount( const EventCount & ) = delete;
123 EventCount & operator=( const EventCount & ) = delete;
124
140 [[nodiscard]] Key prepareWait( void ) noexcept {
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 );
144#endif
145 return( Key( v >> 32 ) );
146 }
147
160 void cancelWait( Key k ) noexcept {
161 std::uint64_t v = f_state.load( std::memory_order_relaxed );
162 while( ( Key( v >> 32 ) == k ) && ! f_state.compare_exchange_weak(
163 v , v - 1 ,
164 std::memory_order_relaxed ) ) {
165 }
166 }
167
180 void commitWait( Key k ) noexcept {
181 while( f_epoch.load( std::memory_order_acquire ) == k )
182 f_epoch.wait( k , std::memory_order_acquire );
183 }
184
208 void notify( void ) noexcept {
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 );
212#else
213 std::uint64_t v = f_state.fetch_add( 0 , std::memory_order_seq_cst );
214#endif
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();
221 return;
222 }
223 }
224
243 template< class P >
244 void await( P && p , std::int64_t spin = 20000 ) {
245 if( p() )
246 return;
247 const auto end =
248 std::chrono::steady_clock::now() + std::chrono::nanoseconds( spin );
249 for( unsigned i = 1 ; ; ++i ) { // the clock every 64 looks
250 relax();
251 if( p() )
252 return;
253 if( ( ( i & 63 ) == 0 ) && ( std::chrono::steady_clock::now() >= end ) )
254 break;
255 }
256 while( ! p() ) {
257 const Key k = prepareWait();
258 if( p() ) {
259 cancelWait( k );
260 return;
261 }
262 commitWait( k );
263 }
264 }
265
267 [[nodiscard]] std::uint32_t waiters( void ) const noexcept {
268 return(
269 std::uint32_t( f_state.load( std::memory_order_relaxed ) & waiterMask ) );
270 }
271
272/*--------------------------------------------------------------------------*/
273 private:
275 static void relax( void ) noexcept { detail::spinPause(); }
276
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 ) ) {
295 }
296 }
297
298 static constexpr std::uint64_t waiterMask = 0xFFFFFFFFu;
299
301 alignas( 64 ) std::atomic< std::uint64_t > f_state{ 0 };
305 std::atomic< Key > f_epoch{ 0 };
306 [[maybe_unused]]
307 char f_pad[ 64 - sizeof( std::uint64_t ) - sizeof( Key ) ] = {};
308
309 }; // end( class EventCount )
310
311/*--------------------------------------------------------------------------*/
312
313} // end( namespace ADS )
314
315/*--------------------------------------------------------------------------*/
316
317#endif /* EventCount.h included */
318
319/*--------------------------------------------------------------------------*/
320/*------------------------- End File EventCount.h --------------------------*/
321/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96