2026-05-19 09:57:24 -04:00
|
|
|
#ifndef NON_POSTING_PROMISE_H
|
|
|
|
|
#define NON_POSTING_PROMISE_H
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <coroutine>
|
|
|
|
|
#include <exception>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2026-05-24 02:25:04 -04:00
|
|
|
#include <spinscale/spinLock.h>
|
2026-05-19 09:57:24 -04:00
|
|
|
#include <spinscale/co/coQutex.h>
|
|
|
|
|
#include <spinscale/co/promiseChainLink.h>
|
2026-05-24 04:28:30 -04:00
|
|
|
#include <spinscale/co/promiseReturnOps.h>
|
|
|
|
|
#include <spinscale/co/returnValues.h>
|
2026-05-19 09:57:24 -04:00
|
|
|
|
|
|
|
|
namespace sscl::co {
|
|
|
|
|
|
2026-05-24 02:25:04 -04:00
|
|
|
template <typename T>
|
2026-05-19 09:57:24 -04:00
|
|
|
struct NonPostingPromise
|
2026-05-24 02:25:04 -04:00
|
|
|
: public PromiseChainLink,
|
2026-05-24 04:28:30 -04:00
|
|
|
public PromiseReturnOps<NonPostingPromise<T>, T>
|
2026-05-19 09:57:24 -04:00
|
|
|
{
|
2026-05-24 02:25:04 -04:00
|
|
|
struct PostBackStatus
|
|
|
|
|
{
|
|
|
|
|
struct CalleeFlowExecutor;
|
|
|
|
|
struct CallerFlowExecutor;
|
|
|
|
|
friend struct CalleeFlowExecutor;
|
|
|
|
|
friend struct CallerFlowExecutor;
|
|
|
|
|
|
|
|
|
|
explicit PostBackStatus(NonPostingPromise &calleePromiseIn) noexcept
|
|
|
|
|
: calleePromise(calleePromiseIn)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
void reset() noexcept
|
|
|
|
|
{
|
|
|
|
|
sscl::SpinLock::Guard guard(lock);
|
|
|
|
|
callerHasSetCallerSchedHandle = false;
|
|
|
|
|
calleeIsReadyToPostBack = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct FlowExecutor
|
|
|
|
|
{
|
|
|
|
|
explicit FlowExecutor(PostBackStatus &parentIn) noexcept
|
|
|
|
|
: parent(parentIn)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
PostBackStatus &parent;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CalleeFlowExecutor
|
|
|
|
|
: public FlowExecutor
|
|
|
|
|
{
|
|
|
|
|
explicit CalleeFlowExecutor(PostBackStatus &parentIn) noexcept
|
|
|
|
|
: FlowExecutor(parentIn)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool operator()() noexcept
|
|
|
|
|
{
|
|
|
|
|
sscl::SpinLock::Guard guard(this->parent.lock);
|
|
|
|
|
this->parent.calleeIsReadyToPostBack = true;
|
|
|
|
|
if (this->parent.callerHasSetCallerSchedHandle) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct CallerFlowExecutor
|
|
|
|
|
: public FlowExecutor
|
|
|
|
|
{
|
|
|
|
|
explicit CallerFlowExecutor(PostBackStatus &parentIn) noexcept
|
|
|
|
|
: FlowExecutor(parentIn)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool operator()() noexcept
|
|
|
|
|
{
|
|
|
|
|
sscl::SpinLock::Guard guard(this->parent.lock);
|
|
|
|
|
this->parent.callerHasSetCallerSchedHandle = true;
|
|
|
|
|
if (this->parent.calleeIsReadyToPostBack) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
CalleeFlowExecutor getCalleeFlowExecutor() noexcept
|
|
|
|
|
{
|
|
|
|
|
return CalleeFlowExecutor(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CallerFlowExecutor getCallerFlowExecutor() noexcept
|
|
|
|
|
{
|
|
|
|
|
return CallerFlowExecutor(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NonPostingPromise &calleePromise;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
sscl::SpinLock lock;
|
|
|
|
|
bool callerHasSetCallerSchedHandle = false;
|
|
|
|
|
bool calleeIsReadyToPostBack = false;
|
|
|
|
|
};
|
|
|
|
|
|
2026-05-19 09:57:24 -04:00
|
|
|
/** Completion work must run from this awaiter's await_suspend, not
|
|
|
|
|
* synchronously inside promise.final_suspend() before it returns: the
|
|
|
|
|
* hidden coroutine segment index in the coroutine state is only advanced
|
|
|
|
|
* after final_suspend exits. See docs/prompts/post-to-and-back-in-invokables.md.
|
|
|
|
|
*/
|
|
|
|
|
struct FinalSuspendNonPostingInvoker
|
|
|
|
|
: public std::suspend_always
|
|
|
|
|
{
|
2026-05-24 02:25:04 -04:00
|
|
|
explicit FinalSuspendNonPostingInvoker(
|
|
|
|
|
NonPostingPromise &calleePromiseIn) noexcept
|
|
|
|
|
: calleePromise(calleePromiseIn)
|
2026-05-19 09:57:24 -04:00
|
|
|
{}
|
|
|
|
|
|
2026-05-24 02:25:04 -04:00
|
|
|
std::coroutine_handle<> await_suspend(
|
|
|
|
|
std::coroutine_handle<> const) noexcept
|
2026-05-19 09:57:24 -04:00
|
|
|
{
|
|
|
|
|
if (calleePromise.callerLambda)
|
|
|
|
|
{
|
|
|
|
|
#ifdef CONFIG_LIBSSCL_DEBUG_CO
|
2026-05-24 02:25:04 -04:00
|
|
|
std::cout << "final_suspend" << ": "
|
|
|
|
|
<< std::this_thread::get_id()
|
2026-05-19 09:57:24 -04:00
|
|
|
<< " Non-viral non-posting: invoking callerLambda directly.\n";
|
|
|
|
|
#endif
|
|
|
|
|
if (calleePromise.returnValues.myExceptionPtr) {
|
|
|
|
|
std::rethrow_exception(
|
|
|
|
|
calleePromise.returnValues.myExceptionPtr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
calleePromise.callerLambda();
|
2026-05-24 02:25:04 -04:00
|
|
|
return std::noop_coroutine();
|
2026-05-19 09:57:24 -04:00
|
|
|
}
|
2026-05-24 02:25:04 -04:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_LIBSSCL_DEBUG_CO
|
|
|
|
|
std::cout << "final_suspend" << ": " << std::this_thread::get_id()
|
|
|
|
|
<< " Viral non-posting: running CalleeFlowExecutor.\n";
|
|
|
|
|
#endif
|
|
|
|
|
const bool symmetricTransferToCaller =
|
|
|
|
|
calleePromise.postBackStatus.getCalleeFlowExecutor()();
|
|
|
|
|
|
|
|
|
|
if (symmetricTransferToCaller && calleePromise.callerSchedHandle) {
|
|
|
|
|
return calleePromise.callerSchedHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::noop_coroutine();
|
2026-05-19 09:57:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NonPostingPromise &calleePromise;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
NonPostingPromise() noexcept
|
2026-05-24 02:25:04 -04:00
|
|
|
: returnValues(),
|
|
|
|
|
postBackStatus(*this)
|
2026-05-19 09:57:24 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <typename... TailArgs>
|
|
|
|
|
NonPostingPromise(
|
|
|
|
|
std::exception_ptr &callerExceptionPtr,
|
|
|
|
|
std::function<void()> callerLambdaIn,
|
|
|
|
|
TailArgs &&...) noexcept
|
|
|
|
|
: returnValues(callerExceptionPtr),
|
2026-05-24 02:25:04 -04:00
|
|
|
callerLambda(std::move(callerLambdaIn)),
|
|
|
|
|
postBackStatus(*this)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
template <typename ObjectArg, typename... TailArgs>
|
|
|
|
|
requires (!std::same_as<std::remove_cvref_t<ObjectArg>, std::exception_ptr>)
|
|
|
|
|
NonPostingPromise(
|
|
|
|
|
ObjectArg &&,
|
|
|
|
|
std::exception_ptr &callerExceptionPtr,
|
|
|
|
|
std::function<void()> callerLambdaIn,
|
|
|
|
|
TailArgs &&...) noexcept
|
|
|
|
|
: NonPostingPromise(
|
|
|
|
|
callerExceptionPtr,
|
|
|
|
|
std::move(callerLambdaIn))
|
2026-05-19 09:57:24 -04:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
~NonPostingPromise() noexcept
|
|
|
|
|
{
|
|
|
|
|
#ifdef CONFIG_LIBSSCL_DEBUG_CO
|
2026-05-24 02:25:04 -04:00
|
|
|
std::cout << __func__ << ": " << std::this_thread::get_id()
|
|
|
|
|
<< " Destructing.\n";
|
2026-05-19 09:57:24 -04:00
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::suspend_never initial_suspend() noexcept
|
|
|
|
|
{ return {}; }
|
|
|
|
|
|
|
|
|
|
auto final_suspend() noexcept
|
|
|
|
|
{ return FinalSuspendNonPostingInvoker(*this); }
|
|
|
|
|
|
|
|
|
|
void unhandled_exception() noexcept
|
|
|
|
|
{
|
|
|
|
|
returnValues.myExceptionPtr = std::current_exception();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void removeAcquiredLock(CoQutex &coQutex) noexcept override
|
|
|
|
|
{
|
|
|
|
|
eraseFirstMatchingAcquiredLock(coQutex);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-24 02:25:04 -04:00
|
|
|
const PromiseChainLink *callerPromiseChainLink() const noexcept override
|
|
|
|
|
{ return callerChainLink; }
|
|
|
|
|
|
|
|
|
|
PromiseChainLink *callerPromiseChainLink() noexcept override
|
|
|
|
|
{ return callerChainLink; }
|
|
|
|
|
|
2026-05-19 09:57:24 -04:00
|
|
|
void setSelfSchedHandle(std::coroutine_handle<> schedHandle) noexcept
|
2026-05-24 02:25:04 -04:00
|
|
|
{ selfSchedHandle = schedHandle; }
|
|
|
|
|
|
|
|
|
|
void setCallerPromiseChainLink(PromiseChainLink *chainLink) noexcept
|
|
|
|
|
{ callerChainLink = chainLink; }
|
2026-05-19 09:57:24 -04:00
|
|
|
|
2026-05-24 02:25:04 -04:00
|
|
|
ReturnValues<T> returnValues;
|
2026-05-19 09:57:24 -04:00
|
|
|
std::function<void()> callerLambda;
|
2026-05-24 02:25:04 -04:00
|
|
|
PostBackStatus postBackStatus;
|
2026-05-19 09:57:24 -04:00
|
|
|
std::coroutine_handle<> selfSchedHandle;
|
2026-05-24 02:25:04 -04:00
|
|
|
std::coroutine_handle<> callerSchedHandle;
|
|
|
|
|
PromiseChainLink *callerChainLink = nullptr;
|
2026-05-19 09:57:24 -04:00
|
|
|
|
2026-05-24 02:25:04 -04:00
|
|
|
template <typename, typename>
|
2026-05-24 04:32:44 -04:00
|
|
|
friend class Invoker;
|
2026-05-19 09:57:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace sscl::co
|
|
|
|
|
|
|
|
|
|
#endif // NON_POSTING_PROMISE_H
|