mirror of
https://github.com/latentPrion/libspinscale.git
synced 2026-06-23 19:48:32 +00:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e7707dacdf | |||
| 5d139abff2 |
@@ -1,5 +1,5 @@
|
||||
#ifndef POSTING_INVOKER_H
|
||||
#define POSTING_INVOKER_H
|
||||
#ifndef INVOKER_BASE_H
|
||||
#define INVOKER_BASE_H
|
||||
|
||||
#include <config.h>
|
||||
#include <coroutine>
|
||||
@@ -8,29 +8,34 @@
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include <spinscale/co/promises.h>
|
||||
#include <spinscale/co/promiseChainLink.h>
|
||||
#include <spinscale/co/returnValues.h>
|
||||
|
||||
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>
|
||||
class PostingInvoker
|
||||
class Invoker
|
||||
{
|
||||
public:
|
||||
explicit PostingInvoker(PromiseType &_calleePromise) noexcept
|
||||
explicit Invoker(PromiseType &_calleePromise) noexcept
|
||||
: calleePromise(_calleePromise)
|
||||
{}
|
||||
|
||||
PostingInvoker(const PostingInvoker &) = delete;
|
||||
PostingInvoker &operator=(const PostingInvoker &) = delete;
|
||||
Invoker(const Invoker &) = delete;
|
||||
Invoker &operator=(const Invoker &) = delete;
|
||||
|
||||
PostingInvoker(PostingInvoker &&other) noexcept
|
||||
Invoker(Invoker &&other) noexcept
|
||||
: calleePromise(other.calleePromise),
|
||||
ownsFrameDestroy_(std::exchange(other.ownsFrameDestroy_, false))
|
||||
{}
|
||||
|
||||
PostingInvoker &operator=(PostingInvoker &&other) = delete;
|
||||
Invoker &operator=(Invoker &&other) = delete;
|
||||
|
||||
~PostingInvoker() noexcept
|
||||
~Invoker() noexcept
|
||||
{
|
||||
if (!ownsFrameDestroy_) { return; }
|
||||
|
||||
@@ -41,11 +46,12 @@ public:
|
||||
}
|
||||
|
||||
template <typename CallerPromise>
|
||||
bool setCallerSchedHandle(std::coroutine_handle<CallerPromise> callerSchedHandle) noexcept
|
||||
bool setCallerSchedHandle(
|
||||
std::coroutine_handle<CallerPromise> callerSchedHandle) noexcept
|
||||
{
|
||||
static_assert(
|
||||
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.setCallerPromiseChainLink(&callerSchedHandle.promise());
|
||||
@@ -89,11 +95,11 @@ private:
|
||||
|
||||
/** EXPLANATION:
|
||||
* 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
|
||||
* after move construction or move assignment, so we do not double-destroy
|
||||
* the same handle when get_return_object() returns the invoker by value.
|
||||
* after move construction, 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
|
||||
@@ -102,6 +108,12 @@ private:
|
||||
bool ownsFrameDestroy_ = true;
|
||||
};
|
||||
|
||||
template <typename PromiseType, typename T>
|
||||
using PostingInvoker = Invoker<PromiseType, T>;
|
||||
|
||||
template <typename PromiseType, typename T>
|
||||
using NonPostingInvoker = Invoker<PromiseType, T>;
|
||||
|
||||
} // namespace sscl::co
|
||||
|
||||
#endif // POSTING_INVOKER_H
|
||||
#endif // INVOKER_BASE_H
|
||||
@@ -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 {
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace sscl::co {
|
||||
* PostingPromiseTemplate<void> (no return-value path to a caller).
|
||||
*
|
||||
* 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>
|
||||
struct NonViralPostingInvoker
|
||||
@@ -77,7 +77,7 @@ struct NonViralPostingInvoker
|
||||
* target chosen by the posting-promise alias, e.g. BodyPostingPromise<int>).
|
||||
*
|
||||
* 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>
|
||||
struct ViralPostingInvoker
|
||||
@@ -153,7 +153,7 @@ struct ViralPostingInvoker
|
||||
* from final_suspend (no cross-thread posting).
|
||||
*
|
||||
* 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
|
||||
: public NonPostingInvoker<NonPostingPromise<void>, void>
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
{
|
||||
@@ -222,7 +223,7 @@ struct NonPostingPromise
|
||||
PromiseChainLink *callerChainLink = nullptr;
|
||||
|
||||
template <typename, typename>
|
||||
friend class NonPostingInvoker;
|
||||
friend class Invoker;
|
||||
};
|
||||
|
||||
} // namespace sscl::co
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef PROMISES_H
|
||||
#define PROMISES_H
|
||||
#ifndef POSTING_PROMISE_H
|
||||
#define POSTING_PROMISE_H
|
||||
|
||||
#include <config.h>
|
||||
#include <coroutine>
|
||||
@@ -16,82 +16,15 @@
|
||||
|
||||
#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 {
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
class Invoker;
|
||||
|
||||
template <typename T>
|
||||
struct PostingPromise
|
||||
@@ -339,13 +272,13 @@ protected:
|
||||
}
|
||||
|
||||
template <typename, typename>
|
||||
friend class PostingInvoker;
|
||||
friend class Invoker;
|
||||
};
|
||||
|
||||
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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user