runtime 1.1.4
Checked CEL programs, owned values, and reusable execution stacks
Loading...
Searching...
No Matches
retirement.hpp
1// SPDX-License-Identifier: BSL-1.0
2#ifndef CEL_RUNTIME_DETAIL_RETIREMENT_HPP
3#define CEL_RUNTIME_DETAIL_RETIREMENT_HPP
4#include <atomic>
5#include <cassert>
6#include <cstddef>
7#include <type_traits>
8#include <utility>
9
10namespace cel::detail {
11static_assert(std::atomic<std::size_t>::is_always_lock_free,
12 "runtime ownership requires lock-free reference counts");
13class retirement_queue;
14
19struct retirement_node {
20 // [knowledge] Live shared nodes never mutate next; only the last-owner queue links
21 // dying nodes. Keep the embedded count lock-free and the disposal callback nonthrowing.
22 std::atomic<std::size_t> owners{1};
23 retirement_node* next = nullptr;
24 void (*dispose)(retirement_node&, retirement_queue&) noexcept;
25
31 explicit retirement_node(void (*operation)(retirement_node&, retirement_queue&) noexcept) :
32 dispose(operation) {}
33};
34
40class retirement_queue {
41 // [knowledge] Reuse dying nodes' links as the worklist; never allocate a traversal
42 // container or recursively destroy runtime-owned child edges.
44 retirement_node* _pending = nullptr;
45
46public:
48 retirement_queue() noexcept = default;
50 retirement_queue(const retirement_queue&) = delete;
52 retirement_queue& operator=(const retirement_queue&) = delete;
57 ~retirement_queue() {
58 while (_pending) {
59 auto* current = _pending;
60 _pending = current->next;
61 current->next = nullptr;
62 current->dispose(*current, *this);
63 }
64 }
71 void release(retirement_node* node) noexcept {
72 // [knowledge] Identify last ownership at decrement, not with use_count plus reset:
73 // another handle may release concurrently between a snapshot and the decrement.
74 if (node && node->owners.fetch_sub(1, std::memory_order_acq_rel) == 1) {
75 node->next = _pending;
76 _pending = node;
77 }
78 }
79};
80
92// [knowledge] Keep the reference count in the node, with no separate control-block allocation.
93template <class T>
94class retirement_ptr {
96 retirement_node* _node = nullptr;
97
98public:
100 retirement_ptr() noexcept = default;
104 explicit retirement_ptr(std::remove_const_t<T>* node) noexcept : _node(node) {}
106 retirement_ptr(const retirement_ptr& other) noexcept : _node(other._node) {
107 if (_node) _node->owners.fetch_add(1, std::memory_order_relaxed);
108 }
110 retirement_ptr(retirement_ptr&& other) noexcept : _node(std::exchange(other._node, nullptr)) {}
114 ~retirement_ptr() {
115 retirement_queue queue;
116 retire(queue);
117 }
123 // NOLINTNEXTLINE(bugprone-unhandled-self-assignment): copy-and-swap is self-copy safe.
124 retirement_ptr& operator=(const retirement_ptr& other) noexcept {
125 retirement_ptr next(other);
126 swap(next);
127 return *this;
128 }
134 retirement_ptr& operator=(retirement_ptr&& other) noexcept {
135 if (this != &other) {
136 retirement_ptr next(std::move(other));
137 swap(next);
138 }
139 return *this;
140 }
142 void swap(retirement_ptr& other) noexcept { std::swap(_node, other._node); }
146 void retire(retirement_queue& queue) noexcept { queue.release(std::exchange(_node, nullptr)); }
148 explicit operator bool() const noexcept { return _node != nullptr; }
152 T* get() const noexcept { return static_cast<T*>(_node); }
154 friend bool operator==(const retirement_ptr&, const retirement_ptr&) noexcept = default;
160 T* operator->() const noexcept {
161 assert(_node);
162 return get();
163 }
164};
165} // namespace cel::detail
166#endif