2026-05-24 04:28:30 -04:00
|
|
|
#ifndef INVOKER_BASE_H
|
|
|
|
|
#define INVOKER_BASE_H
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <coroutine>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <type_traits>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
#include <spinscale/co/promiseChainLink.h>
|
|
|
|
|
#include <spinscale/co/returnValues.h>
|
|
|
|
|
|
|
|
|
|
namespace sscl::co {
|
|
|
|
|
|
2026-05-24 04:32:44 -04:00
|
|
|
/** Shared callee-frame owner and awaiter for posting and non-posting promises.
|
|
|
|
|
* Posting vs non-posting completion is implemented in each promise's PostBackStatus
|
|
|
|
|
* and final_suspend; this type only wires caller handles and reads return values.
|
|
|
|
|
*/
|
2026-05-24 04:28:30 -04:00
|
|
|
template <typename PromiseType, typename T>
|
2026-05-24 04:32:44 -04:00
|
|
|
class Invoker
|
2026-05-24 04:28:30 -04:00
|
|
|
{
|
|
|
|
|
public:
|
2026-05-24 04:32:44 -04:00
|
|
|
explicit Invoker(PromiseType &_calleePromise) noexcept
|
2026-05-24 04:28:30 -04:00
|
|
|
: calleePromise(_calleePromise)
|
|
|
|
|
{}
|
|
|
|
|
|
2026-05-24 04:32:44 -04:00
|
|
|
Invoker(const Invoker &) = delete;
|
|
|
|
|
Invoker &operator=(const Invoker &) = delete;
|
2026-05-24 04:28:30 -04:00
|
|
|
|
2026-05-24 04:32:44 -04:00
|
|
|
Invoker(Invoker &&other) noexcept
|
2026-05-24 04:28:30 -04:00
|
|
|
: calleePromise(other.calleePromise),
|
|
|
|
|
ownsFrameDestroy_(std::exchange(other.ownsFrameDestroy_, false))
|
|
|
|
|
{}
|
|
|
|
|
|
2026-05-24 04:32:44 -04:00
|
|
|
Invoker &operator=(Invoker &&other) = delete;
|
2026-05-24 04:28:30 -04:00
|
|
|
|
2026-05-24 04:32:44 -04:00
|
|
|
~Invoker() noexcept
|
2026-05-24 04:28:30 -04:00
|
|
|
{
|
|
|
|
|
if (!ownsFrameDestroy_) { return; }
|
|
|
|
|
|
|
|
|
|
std::coroutine_handle<> handle = calleePromise.selfSchedHandle;
|
|
|
|
|
if (handle) {
|
|
|
|
|
handle.destroy();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename CallerPromise>
|
|
|
|
|
bool setCallerSchedHandle(
|
|
|
|
|
std::coroutine_handle<CallerPromise> callerSchedHandle) noexcept
|
|
|
|
|
{
|
|
|
|
|
static_assert(
|
|
|
|
|
std::is_base_of_v<PromiseChainLink, CallerPromise>,
|
2026-05-24 04:32:44 -04:00
|
|
|
"Invoker caller promise must derive from PromiseChainLink");
|
2026-05-24 04:28:30 -04:00
|
|
|
|
|
|
|
|
calleePromise.callerSchedHandle = callerSchedHandle;
|
|
|
|
|
calleePromise.setCallerPromiseChainLink(&callerSchedHandle.promise());
|
|
|
|
|
#ifdef CONFIG_LIBSSCL_DEBUG_CO
|
|
|
|
|
std::cout << __func__ << ": " << std::this_thread::get_id()
|
|
|
|
|
<< " Done setting callerSchedHandle; running CallerFlowExecutor.\n";
|
|
|
|
|
#endif
|
|
|
|
|
return calleePromise.postBackStatus.getCallerFlowExecutor()();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReturnValues<T> &completedReturnValues() noexcept
|
|
|
|
|
{ return calleePromise.returnValues; }
|
|
|
|
|
|
|
|
|
|
const ReturnValues<T> &completedReturnValues() const noexcept
|
|
|
|
|
{ return calleePromise.returnValues; }
|
|
|
|
|
|
|
|
|
|
auto await_resume()
|
|
|
|
|
{
|
|
|
|
|
calleePromise.postBackStatus.reset();
|
|
|
|
|
|
|
|
|
|
ReturnValues<T> &returnValues = calleePromise.returnValues;
|
|
|
|
|
#ifdef CONFIG_LIBSSCL_DEBUG_CO
|
|
|
|
|
std::cout << __func__ << ": " << std::this_thread::get_id()
|
|
|
|
|
<< " About to check for and rethrow any exception.\n";
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if (returnValues.myExceptionPtr)
|
|
|
|
|
{
|
|
|
|
|
std::exception_ptr const captured = returnValues.myExceptionPtr;
|
|
|
|
|
std::rethrow_exception(captured);
|
|
|
|
|
}
|
|
|
|
|
if constexpr (!std::is_void_v<T>)
|
|
|
|
|
{
|
|
|
|
|
T result = std::move(returnValues.myReturnValue);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PromiseType &calleePromise;
|
|
|
|
|
|
|
|
|
|
/** EXPLANATION:
|
|
|
|
|
* Every live invoker owns destruction of its callee coroutine frame in
|
2026-05-24 04:32:44 -04:00
|
|
|
* ~Invoker (via calleePromise.selfSchedHandle).
|
2026-05-24 04:28:30 -04:00
|
|
|
*
|
|
|
|
|
* The only time frame destruction is skipped is for a moved-from invoker
|
2026-05-24 04:32:44 -04:00
|
|
|
* after move construction, so we do not double-destroy the same handle
|
|
|
|
|
* when get_return_object() returns the invoker by value.
|
2026-05-24 04:28:30 -04:00
|
|
|
*
|
|
|
|
|
* This is not an opt-out for viral vs non-viral callers or for "callee
|
|
|
|
|
* still running"; callers must keep the invoker alive until the callee
|
|
|
|
|
* frame is no longer needed.
|
|
|
|
|
*/
|
|
|
|
|
bool ownsFrameDestroy_ = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template <typename PromiseType, typename T>
|
2026-05-24 04:32:44 -04:00
|
|
|
using PostingInvoker = Invoker<PromiseType, T>;
|
2026-05-24 04:28:30 -04:00
|
|
|
|
2026-05-24 04:32:44 -04:00
|
|
|
template <typename PromiseType, typename T>
|
|
|
|
|
using NonPostingInvoker = Invoker<PromiseType, T>;
|
2026-05-24 04:28:30 -04:00
|
|
|
|
|
|
|
|
} // namespace sscl::co
|
|
|
|
|
|
|
|
|
|
#endif // INVOKER_BASE_H
|