|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Single-use deterministic start gate for concurrent tests. More...
#include <concurrency_test_utils.H>
Public Member Functions | |
| Deterministic_Start_Gate (const size_t expected) | |
| Construct a gate for the given number of workers. | |
| bool | arrive_and_wait () |
| Mark the caller ready and block until release() or cancel(). | |
| void | wait_until_ready () |
| Wait until all expected workers are ready, or the gate is cancelled. | |
| void | release () |
| Release all waiting workers. | |
| void | cancel () noexcept |
| Abort the gate, waking every worker currently blocked in arrive_and_wait() or wait_until_ready(). | |
| size_t | arrived () const |
| Return the number of arrived workers. | |
Private Attributes | |
| const size_t | expected_ = 0 |
| size_t | arrived_ = 0 |
| bool | released_ = false |
| bool | cancelled_ = false |
| std::mutex | mutex_ |
| std::condition_variable | cv_ |
Single-use deterministic start gate for concurrent tests.
Worker threads call arrive_and_wait(). The test thread calls wait_until_ready() and release(). This ensures all workers are parked before the measured/stressed section starts.
Definition at line 71 of file concurrency_test_utils.H.
|
inlineexplicit |
Construct a gate for the given number of workers.
| [in] | expected | Number of workers that must arrive before release. |
| std::invalid_argument | if expected == 0 (a gate with no participants would never distinguish "nobody has arrived
yet" from "everybody has arrived", which is almost always a miscounted caller rather than an intentional no-op gate). |
Definition at line 89 of file concurrency_test_utils.H.
References ah_invalid_argument_if.
|
inline |
Mark the caller ready and block until release() or cancel().
This function is intended for worker threads.
true if the gate was released normally, false if cancel() unblocked this call instead. Definition at line 103 of file concurrency_test_utils.H.
References arrived_, Aleph::blossom_maximum_cardinality_matching(), cancelled_, cv_, mutex_, and released_.
|
inline |
Return the number of arrived workers.
Definition at line 157 of file concurrency_test_utils.H.
References arrived_, and mutex_.
Referenced by TEST().
|
inlinenoexcept |
Abort the gate, waking every worker currently blocked in arrive_and_wait() or wait_until_ready().
Woken participants observe arrive_and_wait() returning false instead of waiting forever. Intended for exception-safety cleanup: if launching one worker thread of a cohort fails partway through, the threads already started would otherwise stay parked indefinitely, since the full expected count will never arrive.
Definition at line 146 of file concurrency_test_utils.H.
References cancelled_, cv_, and mutex_.
Referenced by Aleph::Testing::detail::Joining_Thread_Guard::~Joining_Thread_Guard().
|
inline |
|
inline |
Wait until all expected workers are ready, or the gate is cancelled.
This function is intended for the controlling test thread.
Definition at line 117 of file concurrency_test_utils.H.
References arrived_, Aleph::blossom_maximum_cardinality_matching(), cancelled_, cv_, expected_, and mutex_.
Referenced by TEST().
|
private |
Definition at line 74 of file concurrency_test_utils.H.
Referenced by arrive_and_wait(), arrived(), and wait_until_ready().
Definition at line 76 of file concurrency_test_utils.H.
Referenced by arrive_and_wait(), cancel(), and wait_until_ready().
|
private |
Definition at line 78 of file concurrency_test_utils.H.
Referenced by arrive_and_wait(), cancel(), release(), and wait_until_ready().
|
private |
Definition at line 73 of file concurrency_test_utils.H.
Referenced by wait_until_ready().
|
mutableprivate |
Definition at line 77 of file concurrency_test_utils.H.
Referenced by arrive_and_wait(), arrived(), cancel(), release(), and wait_until_ready().
Definition at line 75 of file concurrency_test_utils.H.
Referenced by arrive_and_wait(), and release().