Split classes into neater header units

This commit is contained in:
2026-05-24 04:28:30 -04:00
parent e29bee52cf
commit 5d139abff2
8 changed files with 307 additions and 289 deletions
+196
View File
@@ -0,0 +1,196 @@
#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 {
template <typename PromiseType, typename T>
class PostingInvoker
{
public:
explicit PostingInvoker(PromiseType &_calleePromise) noexcept
: calleePromise(_calleePromise)
{}
PostingInvoker(const PostingInvoker &) = delete;
PostingInvoker &operator=(const PostingInvoker &) = delete;
PostingInvoker(PostingInvoker &&other) noexcept
: calleePromise(other.calleePromise),
ownsFrameDestroy_(std::exchange(other.ownsFrameDestroy_, false))
{}
PostingInvoker &operator=(PostingInvoker &&other) = delete;
~PostingInvoker() 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>,
"PostingInvoker 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;
}
}
private:
PromiseType &calleePromise;
/** EXPLANATION:
* Every live invoker owns destruction of its callee coroutine frame in
* ~PostingInvoker (via calleePromise.selfSchedHandle).
*
* 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
* the same handle when get_return_object() returns the invoker by value.
*
* 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>
class NonPostingInvoker
{
public:
explicit NonPostingInvoker(PromiseType &_calleePromise) noexcept
: calleePromise(_calleePromise)
{}
NonPostingInvoker(const NonPostingInvoker &) = delete;
NonPostingInvoker &operator=(const NonPostingInvoker &) = delete;
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
#endif // INVOKER_BASE_H
+2 -2
View File
@@ -10,8 +10,8 @@
#include <thread>
#include <type_traits>
#include <spinscale/co/nonPostingInvoker.h>
#include <spinscale/co/postingInvoker.h>
#include <spinscale/co/invokerBase.h>
#include <spinscale/co/nonPostingPromise.h>
namespace sscl::co {
-104
View File
@@ -1,104 +0,0 @@
#ifndef NON_POSTING_INVOKER_H
#define NON_POSTING_INVOKER_H
#include <config.h>
#include <coroutine>
#include <iostream>
#include <thread>
#include <type_traits>
#include <utility>
#include <spinscale/co/nonPostingPromise.h>
namespace sscl::co {
template <typename PromiseType, typename T>
class NonPostingInvoker
{
public:
explicit NonPostingInvoker(PromiseType &_calleePromise) noexcept
: calleePromise(_calleePromise)
{}
NonPostingInvoker(const NonPostingInvoker &) = delete;
NonPostingInvoker &operator=(const NonPostingInvoker &) = delete;
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
#endif // NON_POSTING_INVOKER_H
+3 -2
View File
@@ -12,14 +12,15 @@
#include <spinscale/spinLock.h>
#include <spinscale/co/coQutex.h>
#include <spinscale/co/promiseChainLink.h>
#include <spinscale/co/promises.h>
#include <spinscale/co/promiseReturnOps.h>
#include <spinscale/co/returnValues.h>
namespace sscl::co {
template <typename T>
struct NonPostingPromise
: public PromiseChainLink,
public PostingPromiseReturnOps<NonPostingPromise<T>, T>
public PromiseReturnOps<NonPostingPromise<T>, T>
{
struct PostBackStatus
{
-107
View File
@@ -1,107 +0,0 @@
#ifndef POSTING_INVOKER_H
#define POSTING_INVOKER_H
#include <config.h>
#include <coroutine>
#include <iostream>
#include <thread>
#include <type_traits>
#include <utility>
#include <spinscale/co/promises.h>
namespace sscl::co {
template <typename PromiseType, typename T>
class PostingInvoker
{
public:
explicit PostingInvoker(PromiseType &_calleePromise) noexcept
: calleePromise(_calleePromise)
{}
PostingInvoker(const PostingInvoker &) = delete;
PostingInvoker &operator=(const PostingInvoker &) = delete;
PostingInvoker(PostingInvoker &&other) noexcept
: calleePromise(other.calleePromise),
ownsFrameDestroy_(std::exchange(other.ownsFrameDestroy_, false))
{}
PostingInvoker &operator=(PostingInvoker &&other) = delete;
~PostingInvoker() 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>,
"PostingInvoker 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;
}
}
private:
PromiseType &calleePromise;
/** EXPLANATION:
* Every live invoker owns destruction of its callee coroutine frame in
* ~PostingInvoker (via calleePromise.selfSchedHandle).
*
* 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
* the same handle when get_return_object() returns the invoker by value.
*
* 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;
};
} // namespace sscl::co
#endif // POSTING_INVOKER_H
@@ -1,5 +1,5 @@
#ifndef PROMISES_H
#define PROMISES_H
#ifndef POSTING_PROMISE_H
#define POSTING_PROMISE_H
#include <config.h>
#include <coroutine>
@@ -16,6 +16,9 @@
#include <spinscale/componentThread.h>
#include <spinscale/co/coQutex.h>
#include <spinscale/co/promiseChainLink.h>
#include <spinscale/co/promiseReturnOps.h>
#include <spinscale/co/returnValues.h>
#include <spinscale/spinLock.h>
namespace sscl::co {
@@ -23,76 +26,6 @@ namespace sscl::co {
template <typename PromiseType, typename T>
class PostingInvoker;
template <typename T, bool IsVoid = std::is_void_v<T>>
struct ReturnValueStorage;
template <typename T>
struct ReturnValueStorage<T, false>
{
T myReturnValue{};
};
template <typename T>
struct ReturnValueStorage<T, true>
{
};
template <typename T>
struct ReturnValues
: public ReturnValueStorage<T>
{
ReturnValues() noexcept
: myExceptionPtr(myMemberExceptionPtr)
{}
explicit ReturnValues(std::exception_ptr &callerExceptionPtr) noexcept
: myExceptionPtr(callerExceptionPtr)
{}
~ReturnValues() noexcept
{
#ifdef CONFIG_LIBSSCL_DEBUG_CO
std::cout << __func__ << ": " << std::this_thread::get_id() << " Destructing.\n";
#endif
}
/** EXPLANATION:
* The exception_ptr ref here can either point to the exception_ptr
* a non-viral coroutine supplied to us as its storage space for
* where we should store any exception that is thrown;
*
* Or it could point to the member exception_ptr in this very class,
* which is used for viral coroutines that can bubble their exception
* up and automatically via the language runtime.
*/
std::exception_ptr &myExceptionPtr;
std::exception_ptr myMemberExceptionPtr = nullptr;
};
/** `return_value` / `return_void` only. ThreadTag is not a template parameter here:
* for tagged promises, PromiseType is `TaggedPostingPromise<T, ThreadTag>`.
*/
template <typename PromiseType, typename T, bool IsVoid = std::is_void_v<T>>
struct PostingPromiseReturnOps;
template <typename PromiseType, typename T>
struct PostingPromiseReturnOps<PromiseType, T, false>
{
void return_value(T returnValue) noexcept
{
static_cast<PromiseType *>(this)->returnValues.myReturnValue = std::move(returnValue);
}
};
template <typename PromiseType, typename T>
struct PostingPromiseReturnOps<PromiseType, T, true>
{
void return_void() noexcept
{
return;
}
};
template <typename T>
struct PostingPromise
: public PromiseChainLink
@@ -345,7 +278,7 @@ protected:
template <typename T, typename ThreadTag>
struct TaggedPostingPromise
: public PostingPromise<T>,
public PostingPromiseReturnOps<TaggedPostingPromise<T, ThreadTag>, T>
public PromiseReturnOps<TaggedPostingPromise<T, ThreadTag>, T>
{
TaggedPostingPromise() noexcept
: PostingPromise<T>()
@@ -389,4 +322,4 @@ struct TaggedPostingPromise
} // namespace sscl::co
#endif // PROMISES_H
#endif // POSTING_PROMISE_H
+38
View File
@@ -0,0 +1,38 @@
#ifndef PROMISE_RETURN_OPS_H
#define PROMISE_RETURN_OPS_H
#include <type_traits>
#include <utility>
#include <spinscale/co/returnValues.h>
namespace sscl::co {
/** `return_value` / `return_void` only. ThreadTag is not a template parameter here:
* for tagged promises, PromiseType is `TaggedPostingPromise<T, ThreadTag>`.
*/
template <typename PromiseType, typename T, bool IsVoid = std::is_void_v<T>>
struct PromiseReturnOps;
template <typename PromiseType, typename T>
struct PromiseReturnOps<PromiseType, T, false>
{
void return_value(T returnValue) noexcept
{
static_cast<PromiseType *>(this)->returnValues.myReturnValue =
std::move(returnValue);
}
};
template <typename PromiseType, typename T>
struct PromiseReturnOps<PromiseType, T, true>
{
void return_void() noexcept
{
return;
}
};
} // namespace sscl::co
#endif // PROMISE_RETURN_OPS_H
+61
View File
@@ -0,0 +1,61 @@
#ifndef RETURN_VALUES_H
#define RETURN_VALUES_H
#include <config.h>
#include <exception>
#include <iostream>
#include <thread>
#include <type_traits>
namespace sscl::co {
template <typename T, bool IsVoid = std::is_void_v<T>>
struct ReturnValueStorage;
template <typename T>
struct ReturnValueStorage<T, false>
{
T myReturnValue{};
};
template <typename T>
struct ReturnValueStorage<T, true>
{
};
template <typename T>
struct ReturnValues
: public ReturnValueStorage<T>
{
ReturnValues() noexcept
: myExceptionPtr(myMemberExceptionPtr)
{}
explicit ReturnValues(std::exception_ptr &callerExceptionPtr) noexcept
: myExceptionPtr(callerExceptionPtr)
{}
~ReturnValues() noexcept
{
#ifdef CONFIG_LIBSSCL_DEBUG_CO
std::cout << __func__ << ": " << std::this_thread::get_id()
<< " Destructing.\n";
#endif
}
/** EXPLANATION:
* The exception_ptr ref here can either point to the exception_ptr
* a non-viral coroutine supplied to us as its storage space for
* where we should store any exception that is thrown;
*
* Or it could point to the member exception_ptr in this very class,
* which is used for viral coroutines that can bubble their exception
* up and automatically via the language runtime.
*/
std::exception_ptr &myExceptionPtr;
std::exception_ptr myMemberExceptionPtr = nullptr;
};
} // namespace sscl::co
#endif // RETURN_VALUES_H