Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
worker_storage.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/background_options.hpp>
6
7#include <cstdint>
8#include <expected>
9#include <functional>
10#include <memory>
11
12namespace portals {
13namespace detail {
14struct WorkerStorageControl;
15struct WorkerStorageWake;
16}
17
20public:
21 virtual ~WorkerStorageValue() = default;
22};
23
26 std::uint64_t value{};
27 std::uint64_t generation{};
29 bool operator==(const WorkerStorageId &) const noexcept = default;
30};
31
34public:
35 WorkerStorageLease() = default;
40 WorkerStorageLease &operator=(WorkerStorageLease &&) noexcept;
41 WorkerStorageLease(const WorkerStorageLease &) = delete;
42 WorkerStorageLease &operator=(const WorkerStorageLease &) = delete;
44 WorkerStorageId id() const noexcept { return id_; }
46 void retire() noexcept;
47
48private:
49 friend class BackgroundWorkerStorage;
50 WorkerStorageLease(WorkerStorageId id, std::weak_ptr<detail::WorkerStorageControl> control)
51 : id_(id), control_(std::move(control)) {}
53 std::weak_ptr<detail::WorkerStorageControl> control_;
54};
55
57class WorkerStorageBuild {
58public:
59 WorkerStorageBuild(const WorkerStorageBuild &) = delete;
60 WorkerStorageBuild &operator=(const WorkerStorageBuild &) = delete;
61 WorkerStorageBuild(WorkerStorageBuild &&) = delete;
62 WorkerStorageBuild &operator=(WorkerStorageBuild &&) = delete;
68 std::expected<void, BackgroundError> commit(std::unique_ptr<WorkerStorageValue> &value, std::size_t bytes);
70 std::size_t residentCap() const noexcept { return residentCap_; }
72 std::size_t scratchCap() const noexcept { return scratchCap_; }
73
74private:
75 friend class BackgroundWorkerStorage;
76 WorkerStorageBuild(std::size_t resident, std::size_t scratch) : residentCap_(resident), scratchCap_(scratch) {}
77 std::size_t residentCap_{}, scratchCap_{}, bytes_{};
78 std::unique_ptr<WorkerStorageValue> value_;
79};
80
83 std::size_t chargedBytes{};
84 std::size_t peakBytes{};
85 std::size_t slots{};
86 std::uint64_t evictions{};
87};
88
94class BackgroundWorkerStorage {
95public:
97 BackgroundWorkerStorage(const BackgroundWorkerStorage &) = delete;
98 BackgroundWorkerStorage &operator=(const BackgroundWorkerStorage &) = delete;
100 std::expected<WorkerStorageLease, BackgroundError> registerSlot();
102 std::uint64_t changeEpoch() const noexcept;
104 WorkerStorageStats stats() const noexcept;
109 std::expected<void, BackgroundError>
110 withRead(WorkerStorageId id, const std::function<std::expected<void, BackgroundError>(const WorkerStorageValue &)> &read);
118 std::expected<void, BackgroundError>
119 withBuild(WorkerStorageId id, std::size_t residentCap, std::size_t scratchCap,
120 const std::function<std::expected<void, BackgroundError>(WorkerStorageBuild &)> &build);
121
122private:
123 friend class BackgroundPool;
124 struct State;
125 BackgroundWorkerStorage(std::size_t slots, std::size_t bytes, std::shared_ptr<detail::WorkerStorageWake> wake);
126 void execute(void *context, void (*run)(void *) noexcept) noexcept;
127 void drain() noexcept;
128 bool retirementPending() const noexcept;
129 void close() noexcept;
130 std::unique_ptr<State> state_;
131};
132} // namespace portals
Optional executor capability for bounded opaque worker-owned caches. Direct operations borrow the liv...
Definition worker_storage.hpp:94
std::expected< void, BackgroundError > withBuild(WorkerStorageId id, std::size_t residentCap, std::size_t scratchCap, const std::function< std::expected< void, BackgroundError >(WorkerStorageBuild &)> &build)
std::uint64_t changeEpoch() const noexcept
std::expected< void, BackgroundError > withRead(WorkerStorageId id, const std::function< std::expected< void, BackgroundError >(const WorkerStorageValue &)> &read)
WorkerStorageStats stats() const noexcept
std::expected< WorkerStorageLease, BackgroundError > registerSlot()
~BackgroundWorkerStorage()
Final host teardown after all workers join, never scene retirement.
Nonmovable build scope, valid only inside withBuild's worker callback.
Definition worker_storage.hpp:57
std::size_t scratchCap() const noexcept
Definition worker_storage.hpp:72
std::size_t residentCap() const noexcept
Definition worker_storage.hpp:70
std::expected< void, BackgroundError > commit(std::unique_ptr< WorkerStorageValue > &value, std::size_t bytes)
WorkerStorageLease(WorkerStorageLease &&) noexcept
Transfer retirement ownership.
~WorkerStorageLease()
Idempotent retirement; no allocation, traversal, join or payload destruction.
void retire() noexcept
Mark retired and notify separate light wake storage; safe after host destruction.
WorkerStorageLease()=default
Empty lease.
Opaque worker-owned payload. Destruction must be worker-safe and nonthrowing.
Definition worker_storage.hpp:19
virtual ~WorkerStorageValue()=default
Released on a worker or final post-UI host teardown.
Public Portals API.
Definition action.hpp:8
BackgroundError
Executor admission and startup failures; domain errors stay in the result value.
Definition background_options.hpp:11
Generation-fenced slot identity; carries no payload ownership.
Definition worker_storage.hpp:25
std::uint64_t value
One-based slot number.
Definition worker_storage.hpp:26
bool operator==(const WorkerStorageId &) const noexcept=default
std::uint64_t generation
Nonzero registration generation, never reused.
Definition worker_storage.hpp:27
Allocation-capacity observations, distinct from process RSS.
Definition worker_storage.hpp:82
std::size_t peakBytes
High-water charged bytes; never exceeds aggregate limit.
Definition worker_storage.hpp:84
std::uint64_t evictions
Idle cache values evicted under pressure.
Definition worker_storage.hpp:86
std::size_t slots
Registered plus retiring slots.
Definition worker_storage.hpp:85
std::size_t chargedBytes
Resident, detached reclaiming, and full active build reservations.
Definition worker_storage.hpp:83