Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::async::detail::async_awaitable< T > Struct Template Reference

Awaitable that runs work in a separate thread. More...

#include <async_container.h>

Collaboration diagram for kcenon::container::async::detail::async_awaitable< T >:
Collaboration graph

Public Member Functions

 async_awaitable (std::function< T()> work)
 
 async_awaitable (async_awaitable &&) noexcept=default
 
async_awaitableoperator= (async_awaitable &&) noexcept=default
 
 async_awaitable (const async_awaitable &)=delete
 
async_awaitableoperator= (const async_awaitable &)=delete
 
bool await_ready () const noexcept
 
void await_suspend (std::coroutine_handle<> handle)
 
await_resume ()
 

Public Attributes

std::shared_ptr< async_state< T > > state_
 

Detailed Description

template<typename T>
struct kcenon::container::async::detail::async_awaitable< T >

Awaitable that runs work in a separate thread.

Uses shared_ptr to manage state, ensuring the worker thread can safely access state even if the awaitable is moved.

Definition at line 90 of file async_container.h.

Constructor & Destructor Documentation

◆ async_awaitable() [1/3]

template<typename T >
kcenon::container::async::detail::async_awaitable< T >::async_awaitable ( std::function< T()> work)
inlineexplicit

Definition at line 94 of file async_container.h.

95 : state_(std::make_shared<async_state<T>>(std::move(work))) {}
std::shared_ptr< async_state< T > > state_

◆ async_awaitable() [2/3]

template<typename T >
kcenon::container::async::detail::async_awaitable< T >::async_awaitable ( async_awaitable< T > && )
defaultnoexcept

◆ async_awaitable() [3/3]

template<typename T >
kcenon::container::async::detail::async_awaitable< T >::async_awaitable ( const async_awaitable< T > & )
delete

Member Function Documentation

◆ await_ready()

template<typename T >
bool kcenon::container::async::detail::async_awaitable< T >::await_ready ( ) const
inlinenodiscardnoexcept

Definition at line 103 of file async_container.h.

104 {
105 return false;
106 }

◆ await_resume()

template<typename T >
T kcenon::container::async::detail::async_awaitable< T >::await_resume ( )
inline

Definition at line 125 of file async_container.h.

126 {
127 // Acquire barrier synchronizes with the release store above,
128 // ensuring all writes in the worker thread are visible here
129 while (!state_->ready_.load(std::memory_order_acquire)) {
130 // Spin until ready - in practice, handle.resume()
131 // ensures we only get here after ready_ is set
132 }
133 if (state_->exception_) {
134 std::rethrow_exception(state_->exception_);
135 }
136 return std::move(*state_->result_);
137 }

References kcenon::container::async::detail::async_awaitable< T >::state_.

◆ await_suspend()

template<typename T >
void kcenon::container::async::detail::async_awaitable< T >::await_suspend ( std::coroutine_handle<> handle)
inline

Definition at line 108 of file async_container.h.

109 {
110 // Copy shared_ptr for thread to ensure state lifetime
111 auto state = state_;
112 std::thread([state, handle]() mutable {
113 try {
114 state->result_.emplace(state->work_());
115 } catch (...) {
116 state->exception_ = std::current_exception();
117 }
118 // Release barrier ensures all writes above are visible
119 // to the thread that reads with acquire semantics
120 state->ready_.store(true, std::memory_order_release);
121 handle.resume();
122 }).detach();
123 }

References kcenon::container::async::detail::async_awaitable< T >::state_.

◆ operator=() [1/2]

template<typename T >
async_awaitable & kcenon::container::async::detail::async_awaitable< T >::operator= ( async_awaitable< T > && )
defaultnoexcept

◆ operator=() [2/2]

template<typename T >
async_awaitable & kcenon::container::async::detail::async_awaitable< T >::operator= ( const async_awaitable< T > & )
delete

Member Data Documentation

◆ state_


The documentation for this struct was generated from the following file: