Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
worker_storage_wake.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/scheduler.hpp>
6
7#include <atomic>
8#include <condition_variable>
9#include <mutex>
10
11namespace portals::detail {
13// This shared light object has no path back to payloads or the uniquely owned host State.
14struct WorkerStorageWake {
15 std::condition_variable_any changed;
16 std::atomic<bool> retired{};
17 std::atomic<bool> closed{};
18 Publisher<int> ui;
19
20 explicit WorkerStorageWake(Publisher<int> readiness) : ui(std::move(readiness)) {
21 }
22
23 std::uint64_t observe() noexcept {
24 std::lock_guard lock(mutex_);
25 return epoch_;
26 }
27
28 template <class Predicate> void wait(std::uint64_t observed, Predicate ready) {
29 std::unique_lock lock(mutex_);
30 changed.wait(lock, [&] {
31 return epoch_ != observed || ready();
32 });
33 }
34
35 void notify(bool wakeUi = true) noexcept {
36 {
37 std::lock_guard lock(mutex_);
38 ++epoch_;
39 }
40 changed.notify_all();
41 if (wakeUi) {
42 ui.wake();
43 }
44 }
45
46private:
47 std::mutex mutex_;
48 std::uint64_t epoch_{};
49};
50
52} // namespace portals::detail