18 DEBUG_CHECK(scheduler,
"unexpected: task scheduler is not set!");
19 scheduler->reschedule(std::move(task));
26 auto& promise = task.handle_.promise();
27 if (promise.release_awaiter())
29 promise.add_awaiter();
35 return std::move(task);
38inline bool BasicTask::can_destroy_coroutine(
TaskHandle handle)
const noexcept
41 return handle.promise().is_cancel_on_resume() ||
handle.done();
44inline TaskHandle BasicTask::release_ownership()
48 return std::exchange(handle_, {});
58 auto handle = std::exchange(handle_, {});
60 if (destroy_on_resume)
62 CHECK(this->can_destroy_coroutine(
handle),
"unexpected destruction of an active task without cancellation",
99 handle_.promise().set_exception(error);
104 TRACE_MODULE(ASYNC,
"BasicTask move assignment",
this, this->coroutine_handle(), &other, other.coroutine_handle());
107 if (handle_ && handle_.promise().release_awaiter()) this->destroy_owned_coroutine();
108 handle_ = std::exchange(other.handle_, {});
109 await_exception_ = std::exchange(other.await_exception_, {});
114inline void BasicTask::destroy_owned_coroutine() noexcept
117 CHECK(this->can_destroy_coroutine(
handle),
"unexpected destruction of an active task without cancellation",
this,
127inline void BasicTask::release() noexcept
139 this->destroy_owned_coroutine();
144 if (!handle_)
return false;
146 handle_.
promise().sched_ = scheduler;
152 if (!handle_)
return std::nullopt;
158 if (handle_) handle_.promise().set_preferred_numa_node(node);
161template <TaskPromise ParentPromise>
177 return std::noop_coroutine();
181 auto transfer = this->release_ownership();
183 return transfer.coroutine();
188 if (
auto error = std::exchange(await_exception_, {})) std::rethrow_exception(error);
Move-only promise-neutral ownership claim for a Uni20 coroutine.
Definition async_task.hpp:103
bool set_scheduler(IScheduler *scheduler)
Install the scheduler used for later resumption.
Definition async_task_impl.hpp:142
static void reschedule(BasicTask task)
Reschedule a task when it becomes the sole ownership claimant.
Definition async_task_impl.hpp:11
~BasicTask() noexcept
Release the ownership claim and destroy the frame when appropriate.
Definition async_task_impl.hpp:133
void await_resume()
Definition async_task_impl.hpp:186
std::coroutine_handle coroutine_handle() const noexcept
Return the generic coroutine handle for diagnostics.
Definition async_task.hpp:128
void abandon_leak()
Intentionally leak the coroutine during emergency unwinding.
Definition async_task_impl.hpp:84
void exception_on_resume(std::exception_ptr error) noexcept
Store an exception for delivery on the next resume.
Definition async_task_impl.hpp:97
void resume()
Resume the coroutine and transfer this ownership claim.
Definition async_task_impl.hpp:77
TaskHandle release_handle()
Release this ownership claim to a scheduler activation.
Definition async_task_impl.hpp:51
void set_preferred_numa_node(std::optional< int > node) noexcept
Definition async_task_impl.hpp:156
BasicTask & operator=(BasicTask const &)=delete
static BasicTask make_sole_owner(BasicTask &&task)
Retain a task only when it is the sole ownership claimant.
Definition async_task_impl.hpp:23
void destroy() noexcept
Destroy an exclusively owned coroutine.
Definition async_task_impl.hpp:135
TaskHandle handle() const noexcept
Return the erased task identity.
Definition async_task.hpp:125
void set_cancel_on_resume() noexcept
Request cancellation before the next resume.
Definition async_task_impl.hpp:91
std::optional< int > preferred_numa_node() const noexcept
Definition async_task_impl.hpp:150
std::coroutine_handle await_suspend(std::coroutine_handle< ParentPromise > parent)
Await a nested task from any Uni20 task promise type.
Definition async_task_impl.hpp:162
Internal route for resuming an already-bound coroutine task.
Definition scheduler.hpp:64
Non-owning erased identity for one Uni20 coroutine frame.
Definition async_task.hpp:59
constexpr std::coroutine_handle coroutine() const noexcept
Definition async_task.hpp:67
TaskPromiseBase & promise() const noexcept
Definition async_task_promise.hpp:998
static TaskHandle from(std::coroutine_handle< Promise > handle) noexcept
Definition async_task_promise.hpp:991
IScheduler * scheduler() const noexcept
Return the scheduler currently recorded for this task.
Definition async_task_promise.hpp:357
static void prepare_nested_route(TaskHandle parent, TaskHandle child)
Complete an unbound child's route before nested execution.
Definition async_task_promise.hpp:723
bool is_cancel_on_resume() const noexcept
Reports whether cancellation-on-resume is currently set.
Definition async_task_promise.hpp:302
static void note_running(TaskHandle handle) noexcept
Record that a coroutine has transitioned into a runnable/running state.
Definition async_task_promise.hpp:367
bool release_awaiter() noexcept
Decrease the number of active awaiters by one.
Definition async_task_promise.hpp:206
static bool can_transfer_directly(TaskHandle from, TaskHandle to) noexcept
Report whether symmetric transfer is valid for two prepared task routes.
Definition async_task_promise.hpp:746
void set_cancel_on_resume() noexcept
Mark the coroutine for cancellation at next resume.
Definition async_task_promise.hpp:298
void mark_started() noexcept
Mark the coroutine as having been scheduled to start executing.
Definition async_task_promise.hpp:351
TaskHandle destroy_with_continuation() noexcept
safely destroy this coroutine, returning the continuation_ (which also must now be destroyed)
Definition async_task_promise.hpp:188
std::exception_ptr * continuation_exception_
Destination for an exception returned by a directly awaited task.
Definition async_task_promise.hpp:122
static void validate_scheduler_route(IScheduler *scheduler, TaskRoute route)
Validate a prospective task route without changing promise state.
Definition async_task_promise.hpp:615
static void note_leaked(TaskHandle handle) noexcept
Record that a coroutine has been intentionally leaked.
Definition async_task_promise.hpp:397
static void note_suspended(TaskHandle handle) noexcept
Record that a coroutine has suspended and is waiting for resumption.
Definition async_task_promise.hpp:370
std::optional< int > preferred_numa_node() const noexcept
Retrieve the preferred NUMA node for this coroutine.
Definition async_task_promise.hpp:343
void rethrow_exception()
Throw the current exception, if there is one.
Definition async_task_promise.hpp:292
static void resume_and_track(TaskHandle handle)
Resume a coroutine handle while recording the running transition.
Definition async_task_promise.hpp:400
TaskHandle continuation_
Uni20 task coroutines are nestable.
Definition async_task_promise.hpp:117
cuda::ResourceLease< HandleSlot > handle
Definition cublas/execution.cpp:33
This represents the reverse-mode adjoint for functions of complex variables, assuming a real-valued s...
Definition async/async.hpp:26
TaskRoute task_route(TaskHandle handle) noexcept
Return the current scheduling requirements recorded by a task.
Definition async_task_promise.hpp:715
#define CHECK(cond,...)
Definition trace.hpp:299
#define DEBUG_CHECK(cond,...)
Definition trace.hpp:608
#define TRACE_MODULE(m,...)
Definition trace.hpp:149
#define DEBUG_TRACE_MODULE(m,...)
Definition trace.hpp:511
#define PANIC(...)
Definition trace.hpp:382