include/boost/capy/ex/frame_allocator.hpp

100.0% Lines (10 / 10) 100.0% Functions (3 / 3)
frame_allocator.hpp
f(x) Functions (3)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/capy
9 //
10
11 #ifndef BOOST_CAPY_FRAME_ALLOCATOR_HPP
12 #define BOOST_CAPY_FRAME_ALLOCATOR_HPP
13
14 #include <boost/capy/detail/config.hpp>
15
16 #include <coroutine>
17 #include <memory_resource>
18
19 /* Design rationale (pdimov):
20
21 This accessor is a thin wrapper over a thread-local pointer.
22 It returns exactly what was stored, including nullptr. No
23 dynamic initializer on the thread-local; a dynamic TLS
24 initializer moves you into a costlier implementation bucket
25 on some platforms, so avoid it.
26
27 Null handling is the caller's responsibility (e.g. in
28 promise_type::operator new). The accessor must not substitute
29 a default, because there are multiple valid choices
30 (new_delete_resource, the default pmr resource, etc.). If
31 the allocator is not set, it reports "not set" and the
32 caller interprets that however it wants.
33 */
34
35 namespace boost {
36 namespace capy {
37
38 namespace detail {
39
40 #if (defined(BOOST_CAPY_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && \
41 !defined(BOOST_CAPY_STATIC_LINK)
42
43 /** Return this thread's frame allocator slot.
44
45 A shared library build defines it in the library: an inline
46 definition gives each module its own slot, and a coroutine created
47 in one would not see the allocator a launcher in another installed.
48 A static build keeps it inline, since resuming a coroutine writes
49 it.
50
51 @return A reference to the calling thread's slot.
52 */
53 BOOST_CAPY_DECL
54 std::pmr::memory_resource*&
55 current_frame_allocator_ref() noexcept;
56
57 #else
58
59 // MSVC may keep an inlined thread_local's address in a coroutine
60 // frame across a suspension, so a resumption on another thread
61 // would write the first thread's slot.
62 #if BOOST_CAPY_WORKAROUND(BOOST_CAPY_MSVC, >= 1)
63 BOOST_CAPY_NOINLINE
64 #endif
65 inline std::pmr::memory_resource*&
66 current_frame_allocator_ref() noexcept
67 {
68 static thread_local std::pmr::memory_resource* mr = nullptr;
69 return mr;
70 }
71
72 #endif
73
74 } // namespace detail
75
76 /** Return the current frame allocator for this thread.
77
78 This accessor exists to implement the allocator
79 propagation portion of the @ref ContextAwaitable protocol.
80 Launcher functions (`run_async`, `run`) set the
81 thread-local value before invoking a child coroutine, and
82 the @ref ContextAwaitable machinery reinstalls it each time
83 a coroutine resumes. The child's `promise_type::operator new`
84 reads it to allocate the coroutine frame from the correct
85 resource.
86
87 The value is only valid during a narrow execution
88 window. Between a coroutine's resumption
89 and the next suspension point, the protocol guarantees
90 that TLS contains the allocator associated with the
91 currently running chain. Outside that window the value
92 is indeterminate. Only promise allocation functions,
93 promise constructors, launchers, and @ref ContextAwaitable
94 machinery should call this function.
95
96 A return value of `nullptr` means "not specified":
97 no allocator is established for this chain.
98 The awaitable is free to use whatever allocation
99 strategy makes best sense (e.g.
100 `std::pmr::new_delete_resource()`).
101
102 Use of the frame allocator is optional. An awaitable
103 that does not consult this value to allocate its
104 coroutine frame is never wrong. However, a conforming
105 awaitable must still propagate the allocator faithfully
106 so that downstream coroutines can use it.
107
108 @return The thread-local memory_resource pointer,
109 or `nullptr` if none is set.
110
111 @see @ref set_current_frame_allocator, @ref ContextAwaitable
112 */
113 inline
114 std::pmr::memory_resource*
115 373012x get_current_frame_allocator() noexcept
116 {
117 373012x return detail::current_frame_allocator_ref();
118 }
119
120 /** Set the current frame allocator for this thread.
121
122 Installs `mr` as the frame allocator read by the
123 next coroutine's `promise_type::operator new` on
124 this thread. Only launcher functions and
125 @ref ContextAwaitable machinery should call this; see
126 @ref get_current_frame_allocator for the full protocol
127 description.
128
129 Passing `nullptr` means "not specified" - no
130 particular allocator is established for the chain.
131
132 @param mr The memory_resource to install, or
133 `nullptr` to clear.
134
135 @see @ref get_current_frame_allocator, @ref ContextAwaitable
136 */
137 inline void
138 483713x set_current_frame_allocator(
139 std::pmr::memory_resource* mr) noexcept
140 {
141 483713x detail::current_frame_allocator_ref() = mr;
142 483713x }
143
144 /** Resume a coroutine handle with frame-allocator TLS protection.
145
146 Saves the current thread-local frame allocator before
147 calling `h.resume()`, then restores it after the call
148 returns. This prevents a resumed coroutine's
149 `await_resume` from permanently overwriting the caller's
150 allocator value.
151
152 Between a coroutine's resumption and its next child
153 invocation, arbitrary user code may run. If that code
154 resumes a coroutine from a different chain on this
155 thread, the other coroutine's `await_resume` overwrites
156 TLS with its own allocator. Without save/restore, the
157 original coroutine's next child would allocate from
158 the wrong resource.
159
160 Event loops, strand dispatch loops, and any code that
161 calls `.resume()` on a coroutine handle should use
162 this function instead of calling `.resume()` directly.
163 See the @ref Executor concept documentation for details.
164
165 @note An exception escaping the resumed coroutine calls
166 `std::terminate`.
167
168 @param h The coroutine handle to resume.
169
170 @see @ref get_current_frame_allocator, @ref set_current_frame_allocator
171 */
172 // tag::safe_resume[]
173 inline void
174 157598x safe_resume(std::coroutine_handle<> h) noexcept
175 {
176 157598x auto* saved = get_current_frame_allocator();
177 157598x h.resume();
178 157598x set_current_frame_allocator(saved);
179 157598x }
180 // end::safe_resume[]
181
182 } // namespace capy
183 } // namespace boost
184
185 #endif
186