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