YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
one_shot_event.cpp
Go to the documentation of this file.
2
3namespace yaclib {
4namespace {
5
6void SetImpl(yaclib_std::atomic_uintptr_t& self, std::uintptr_t value) {
7 auto head = self.exchange(value, std::memory_order_acq_rel);
8 auto* job = reinterpret_cast<Job*>(head);
9 while (job != nullptr) {
10 auto* next = static_cast<Job*>(job->next);
11 job->Call();
12 job = next;
13 }
14}
15
16} // namespace
17
18bool OneShotEvent::TryAdd(Job& job) noexcept {
19 auto head = _head.load(std::memory_order_acquire);
20 auto node = reinterpret_cast<std::uintptr_t>(&job);
21 while (head != OneShotEvent::kAllDone) {
22 job.next = reinterpret_cast<Job*>(head);
23 if (_head.compare_exchange_weak(head, node, std::memory_order_release, std::memory_order_acquire)) {
24 return true;
25 }
26 }
27 return false;
28}
29
31 return _head.load(std::memory_order_acquire) == OneShotEvent::kAllDone;
32}
33
36 if (TryAdd(waiter)) {
37 auto token = waiter.Make();
38 waiter.Wait(token);
39 }
40}
41
43 SetImpl(_head, kEmpty);
44}
45
47 SetImpl(_head, kAllDone);
48}
49
51#ifdef YACLIB_LOG_DEBUG
52 auto head = _head.load(std::memory_order_relaxed);
53 YACLIB_ASSERT(head == kEmpty || head == kAllDone);
54#endif
55 _head.store(kEmpty, std::memory_order_relaxed);
56}
57
58} // namespace yaclib
Callable that can be executed in an IExecutor.
Definition job.hpp:12
void Set() noexcept
Prevent pushing new jobs and Call()
void Call() noexcept
Get all jobs and Call them.
bool TryAdd(Job &job) noexcept
Add job to the MPSC event queue.
void Wait() noexcept
Wait Call or Set immediately return if Event is Ready.
bool Ready() noexcept
was or not Set
void Reset() noexcept
Reinitializes OneShotEvent, semantically the same as *this = {};
#define YACLIB_ASSERT(cond)
Definition log.hpp:85
atomic< std::uintptr_t > atomic_uintptr_t
Definition atomic.hpp:85
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
Waiter is public for advanced users.