mirror of
https://github.com/latentPrion/libspinscale.git
synced 2026-08-12 15:42:10 +00:00
Add sscl::co::syncAwaitNonViralCoro for blocking non-viral coroutine launch.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#ifndef SYNC_AWAIT_NON_VIRAL_CORO_H
|
||||
#define SYNC_AWAIT_NON_VIRAL_CORO_H
|
||||
|
||||
#include <boostAsioLinkageFix.h>
|
||||
|
||||
#include <spinscale/componentThread.h>
|
||||
#include <spinscale/co/nonViralTaskNursery.h>
|
||||
#include <exception>
|
||||
#include <functional>
|
||||
|
||||
namespace sscl::co {
|
||||
|
||||
/** Launch a non-viral coroutine on the current ComponentThread io_context and
|
||||
* block until it settles, rethrowing any stored exception.
|
||||
*/
|
||||
template<typename InvokerFactory>
|
||||
void syncAwaitNonViralCoro(InvokerFactory&& _invokerFactory)
|
||||
{
|
||||
std::exception_ptr slotException;
|
||||
|
||||
NonViralTaskNursery nursery;
|
||||
nursery.openAdmission();
|
||||
nursery.launch(
|
||||
[&_invokerFactory](NonViralTaskNursery::Slot::Lease& lease)
|
||||
{
|
||||
return _invokerFactory(lease);
|
||||
},
|
||||
[&slotException](std::exception_ptr& exceptionPtr)
|
||||
{
|
||||
slotException = exceptionPtr;
|
||||
});
|
||||
nursery.closeAdmission();
|
||||
nursery.syncAwaitAllSettlements(
|
||||
sscl::ComponentThread::getSelf()->getIoContext());
|
||||
|
||||
if (slotException) {
|
||||
std::rethrow_exception(slotException);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sscl::co
|
||||
|
||||
#endif // SYNC_AWAIT_NON_VIRAL_CORO_H
|
||||
Reference in New Issue
Block a user