39#ifdef KCENON_HAS_COMMON_SYSTEM
40#include <kcenon/common/interfaces/executor_interface.h>
41#include <kcenon/common/interfaces/thread_pool_interface.h>
52#ifdef KCENON_HAS_COMMON_SYSTEM
53 using executor_ptr = std::shared_ptr<kcenon::common::interfaces::IExecutor>;
76 :
work_(std::move(work)) {}
93 std::shared_ptr<executor_state<T>>
state_;
116 state->continuation_ = handle;
118#ifdef KCENON_HAS_COMMON_SYSTEM
122 class work_job :
public kcenon::common::interfaces::IJob
126 : state_(std::move(s)) {}
128 kcenon::common::VoidResult execute()
override
131 state_->result_.emplace(state_->work_());
133 state_->exception_ = std::current_exception();
135 state_->ready_.store(
true, std::memory_order_release);
136 if (state_->continuation_) {
137 state_->continuation_.resume();
139 return kcenon::common::ok();
142 std::string get_name()
const override {
143 return "async_container_work";
147 std::shared_ptr<executor_state<T>> state_;
150 auto job = std::make_unique<work_job>(state);
151 auto result =
executor_->execute(std::move(job));
152 if (!result.is_ok()) {
165 while (!
state_->ready_.load(std::memory_order_acquire)) {
169 std::rethrow_exception(
state_->exception_);
171 return std::move(*
state_->result_);
177 std::coroutine_handle<> handle)
179 std::thread([state, handle]()
mutable {
181 state->result_.emplace(state->work_());
183 state->exception_ = std::current_exception();
185 state->ready_.store(
true, std::memory_order_release);
198 using result_type = std::invoke_result_t<F>;
200 std::forward<F>(func), std::move(executor));
247#ifdef KCENON_HAS_COMMON_SYSTEM
259#ifdef KCENON_HAS_COMMON_SYSTEM
Global executor for async operations.
executor_ptr get_executor() const
Get the global executor.
bool has_executor() const
Check if an executor is configured.
void set_executor(executor_ptr executor)
Set the global executor.
async_executor_context()=default
static async_executor_context & instance()
Get the singleton instance.
void clear_executor()
Clear the global executor.
RAII guard for setting executor context.
~executor_context_guard()
executor_context_guard & operator=(const executor_context_guard &)=delete
executor_context_guard(const executor_context_guard &)=delete
executor_context_guard(executor_ptr executor)
auto make_executor_awaitable(F &&func, executor_ptr executor) -> executor_awaitable< std::invoke_result_t< F > >
Helper to create executor awaitable.
std::nullptr_t executor_ptr
Executor type for async operations.
Awaitable that runs work using an executor or fallback thread.
executor_awaitable(std::function< T()> work, executor_ptr executor)
bool await_ready() const noexcept
static void run_in_thread(std::shared_ptr< executor_state< T > > state, std::coroutine_handle<> handle)
std::shared_ptr< executor_state< T > > state_
executor_awaitable(executor_awaitable &&) noexcept=default
void await_suspend(std::coroutine_handle<> handle)
Shared state for executor-based async operations.
executor_state & operator=(executor_state &&)=delete
executor_state & operator=(const executor_state &)=delete
executor_state(executor_state &&)=delete
std::atomic< bool > ready_
executor_state(std::function< T()> work)
executor_state(const executor_state &)=delete
std::function< T()> work_
std::coroutine_handle continuation_
std::optional< T > result_
std::exception_ptr exception_
C++20 coroutine task type for async operations.