ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
EventCount.h File Reference

An event count, beyond the book: the way for a thread to sleep until a condition on a lock-free structure holds, instead of looking at it again and again or between sleeps of a fixed length, with no lock and nearly no cost for the threads that make the condition true while nobody waits. More...

#include "ADS/concurrent/impl/Spin.tpp"
#include <atomic>
#include <chrono>
#include <cstdint>

Go to the source code of this file.

Classes

class  ADS::EventCount
 the sleep of threads until a condition holds, woken by notify() More...

Namespaces

namespace  ADS

Detailed description

An event count, beyond the book: the way for a thread to sleep until a condition on a lock-free structure holds, instead of looking at it again and again or between sleeps of a fixed length, with no lock and nearly no cost for the threads that make the condition true while nobody waits.

It holds an epoch and the number of the waiters, the two halves of one word of 64 bits on a line of its own. A waiter announces itself (prepareWait(), which adds 1 to the waiters and reads the epoch \( e \) in the same read-modify-write), looks at the condition once more, and if it is still false sleeps until the epoch is no longer \( e \) (commitWait( e ), by std::atomic::wait); otherwise it withdraws (cancelWait( e )). A notifier makes the condition true, then notify() reads the word after a fence and, only if there are waiters, advances the epoch and sets the waiters to 0 in one compare-and-swap, and wakes them (std::atomic::notify_all): the notifications that follow, while the woken threads are still on their way, find nobody to wake and cost no system call.

The notifier and the waiter each run a full fence of the standard (std::atomic_thread_fence( std::memory_order_seq_cst )), so that when nobody waits a notification costs the fence and the read of a line that stays shared. With ADS_EVENTCOUNT_RMW defined, the notifier reads the waiters by a read-modify-write instead and neither side runs a fence: reading the last value in the order of the modifications of the word, it either sees the waiter or is seen by it through the release sequence; this also is the form that the sanitizer of threads, which does not model the fences, checks, and the one that ADS_SANITIZE_THREAD, which the build defines when the sanitizer is on, chooses. Which of the two costs less depends on the processor (a full fence against an exclusive line), and the benchmarks decide the default.

No wake-up is lost: the fence of the notifier and the read-modify-write and fence of the waiter are in the single total order of the sequentially consistent operations; if the notifier reads 0 waiters, its fence comes before the announcement of the waiter, which then sees the condition made true when it looks again, and if it reads 1 or more it advances the epoch by a release on the same word, which the announcement either precedes (and the wait on \( e \) returns at once or is woken) or follows, reading the new epoch with an acquire and seeing the condition made true. A spurious wake-up only makes the waiter look again.

await( p ) packs the loop: it returns when p() is true, and p is called by the waiting thread only, so that it may take an item; it first looks again and again for 20 \( \mu\mathrm{s} \) (a parameter), with a pause of the processor between the looks, since an item that comes within that time costs no sleep and no wake-up (each about the time of a system call and of a switch of threads), and a waiter that does not sleep costs its notifier only the fence.

The contracts [see Contract.h]: each prepareWait() is followed by one commitWait() or cancelWait() of the same thread, with the epoch it returned.

See also
D. P. Reed, R. K. Kanodia, Synchronization with eventcounts and sequencers, Communications of the ACM 22(2), 115-123, 1979, doi:10.1145/359060.359076 (the event count)
A. Williams, C++ Concurrency in Action, 2nd ed., Manning, 2019, chapter 5 (the total order of the sequentially consistent operations and the fences)
H. Boehm, S. V. Adve, Foundations of the C++ concurrency memory model, ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI), 68-78, 2008, doi:10.1145/1375581.1375591
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa