Unify [Non]PostingInvoker into Invoker

This commit is contained in:
2026-05-24 04:32:44 -04:00
parent 5d139abff2
commit e7707dacdf
4 changed files with 24 additions and 101 deletions
+18 -95
View File
@@ -13,25 +13,29 @@
namespace sscl::co { namespace sscl::co {
/** 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.
*/
template <typename PromiseType, typename T> template <typename PromiseType, typename T>
class PostingInvoker class Invoker
{ {
public: public:
explicit PostingInvoker(PromiseType &_calleePromise) noexcept explicit Invoker(PromiseType &_calleePromise) noexcept
: calleePromise(_calleePromise) : calleePromise(_calleePromise)
{} {}
PostingInvoker(const PostingInvoker &) = delete; Invoker(const Invoker &) = delete;
PostingInvoker &operator=(const PostingInvoker &) = delete; Invoker &operator=(const Invoker &) = delete;
PostingInvoker(PostingInvoker &&other) noexcept Invoker(Invoker &&other) noexcept
: calleePromise(other.calleePromise), : calleePromise(other.calleePromise),
ownsFrameDestroy_(std::exchange(other.ownsFrameDestroy_, false)) ownsFrameDestroy_(std::exchange(other.ownsFrameDestroy_, false))
{} {}
PostingInvoker &operator=(PostingInvoker &&other) = delete; Invoker &operator=(Invoker &&other) = delete;
~PostingInvoker() noexcept ~Invoker() noexcept
{ {
if (!ownsFrameDestroy_) { return; } if (!ownsFrameDestroy_) { return; }
@@ -47,7 +51,7 @@ public:
{ {
static_assert( static_assert(
std::is_base_of_v<PromiseChainLink, CallerPromise>, std::is_base_of_v<PromiseChainLink, CallerPromise>,
"PostingInvoker caller promise must derive from PromiseChainLink"); "Invoker caller promise must derive from PromiseChainLink");
calleePromise.callerSchedHandle = callerSchedHandle; calleePromise.callerSchedHandle = callerSchedHandle;
calleePromise.setCallerPromiseChainLink(&callerSchedHandle.promise()); calleePromise.setCallerPromiseChainLink(&callerSchedHandle.promise());
@@ -91,11 +95,11 @@ private:
/** EXPLANATION: /** EXPLANATION:
* Every live invoker owns destruction of its callee coroutine frame in * Every live invoker owns destruction of its callee coroutine frame in
* ~PostingInvoker (via calleePromise.selfSchedHandle). * ~Invoker (via calleePromise.selfSchedHandle).
* *
* The only time frame destruction is skipped is for a moved-from invoker * The only time frame destruction is skipped is for a moved-from invoker
* after move construction or move assignment, so we do not double-destroy * after move construction, so we do not double-destroy the same handle
* the same handle when get_return_object() returns the invoker by value. * when get_return_object() returns the invoker by value.
* *
* This is not an opt-out for viral vs non-viral callers or for "callee * 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 * still running"; callers must keep the invoker alive until the callee
@@ -105,91 +109,10 @@ private:
}; };
template <typename PromiseType, typename T> template <typename PromiseType, typename T>
class NonPostingInvoker using PostingInvoker = Invoker<PromiseType, T>;
{
public:
explicit NonPostingInvoker(PromiseType &_calleePromise) noexcept
: calleePromise(_calleePromise)
{}
NonPostingInvoker(const NonPostingInvoker &) = delete; template <typename PromiseType, typename T>
NonPostingInvoker &operator=(const NonPostingInvoker &) = delete; using NonPostingInvoker = Invoker<PromiseType, T>;
NonPostingInvoker(NonPostingInvoker &&other) noexcept
: calleePromise(other.calleePromise),
ownsFrameDestroy_(std::exchange(other.ownsFrameDestroy_, false))
{}
NonPostingInvoker &operator=(NonPostingInvoker &&other) = delete;
~NonPostingInvoker() noexcept
{
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>,
"NonPostingInvoker caller promise must derive from PromiseChainLink");
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;
}
}
protected:
PromiseType &calleePromise;
private:
/** Every live invoker owns destruction of its callee coroutine frame in
* ~NonPostingInvoker (via calleePromise.selfSchedHandle).
*
* The only time frame destruction is skipped is for a moved-from invoker
* after move construction, so we do not double-destroy the same handle
* when get_return_object() returns the invoker by value.
*/
bool ownsFrameDestroy_ = true;
};
} // namespace sscl::co } // namespace sscl::co
+3 -3
View File
@@ -19,7 +19,7 @@ namespace sscl::co {
* PostingPromiseTemplate<void> (no return-value path to a caller). * PostingPromiseTemplate<void> (no return-value path to a caller).
* *
* The invoker must outlive the callee frame: do not discard the return object * The invoker must outlive the callee frame: do not discard the return object
* from get_return_object(). ~PostingInvoker destroys the callee frame. * from get_return_object(). ~Invoker destroys the callee frame.
*/ */
template <template <typename> class PostingPromiseTemplate> template <template <typename> class PostingPromiseTemplate>
struct NonViralPostingInvoker struct NonViralPostingInvoker
@@ -77,7 +77,7 @@ struct NonViralPostingInvoker
* target chosen by the posting-promise alias, e.g. BodyPostingPromise<int>). * target chosen by the posting-promise alias, e.g. BodyPostingPromise<int>).
* *
* The invoker must outlive the callee frame until results are read. * The invoker must outlive the callee frame until results are read.
* ~PostingInvoker destroys the callee frame (not await_resume). * ~Invoker destroys the callee frame (not await_resume).
*/ */
template <template <typename> class PostingPromiseTemplate, typename T> template <template <typename> class PostingPromiseTemplate, typename T>
struct ViralPostingInvoker struct ViralPostingInvoker
@@ -153,7 +153,7 @@ struct ViralPostingInvoker
* from final_suspend (no cross-thread posting). * from final_suspend (no cross-thread posting).
* *
* The invoker must outlive the callee frame: do not discard the return object * The invoker must outlive the callee frame: do not discard the return object
* from get_return_object(). ~NonPostingInvoker destroys the callee frame. * from get_return_object(). ~Invoker destroys the callee frame.
*/ */
struct NonViralNonPostingInvoker struct NonViralNonPostingInvoker
: public NonPostingInvoker<NonPostingPromise<void>, void> : public NonPostingInvoker<NonPostingPromise<void>, void>
+1 -1
View File
@@ -223,7 +223,7 @@ struct NonPostingPromise
PromiseChainLink *callerChainLink = nullptr; PromiseChainLink *callerChainLink = nullptr;
template <typename, typename> template <typename, typename>
friend class NonPostingInvoker; friend class Invoker;
}; };
} // namespace sscl::co } // namespace sscl::co
+2 -2
View File
@@ -24,7 +24,7 @@
namespace sscl::co { namespace sscl::co {
template <typename PromiseType, typename T> template <typename PromiseType, typename T>
class PostingInvoker; class Invoker;
template <typename T> template <typename T>
struct PostingPromise struct PostingPromise
@@ -272,7 +272,7 @@ protected:
} }
template <typename, typename> template <typename, typename>
friend class PostingInvoker; friend class Invoker;
}; };
template <typename T, typename ThreadTag> template <typename T, typename ThreadTag>