pub struct Coroutine { /* private fields */ }Expand description
A native coroutine. Neither Send nor Sync: poll it on its creating executor thread.
Implementations§
Source§impl Coroutine
impl Coroutine
Sourcepub unsafe fn new<F: FnOnce() -> i32 + 'static>(stack: Stack, entry: F) -> Self
pub unsafe fn new<F: FnOnce() -> i32 + 'static>(stack: Stack, entry: F) -> Self
Create a coroutine, allocating only its shared control block.
§Safety
The stack must accommodate the entry and all callees, including interrupt frames if the platform uses the same SP. Canary checks cannot prevent memory corruption. Entry must never unwind. On bare metal, all coroutines must run on a single core and must never be entered or suspended from an interrupt. Do not migrate a live coroutine or its handles to another OS thread, including via foreign code.
Dropping the coroutine after it started and before it finished cancels it: the
suspended frames are discarded and the stack is freed, without running a destructor
on them (see Cancelled). The entry and everything it calls must therefore not
leave an object that owns a resource (an open file, an allocation, a lock guard)
only in a frame across a suspension, because nothing would ever release it: keep
it in a place the host owns and releases itself. A future passed to
suspend_on counts as such a place only if it is passed to
suspend_on_cancellable, which the cancellation drops in place. Nothing outside
the coroutine may keep a pointer into its stack beyond the call that is suspended:
the stack is freed and reused once the coroutine is dropped.
On bare metal, the default current-coroutine slot assumes a single core (0).
Multi-core firmware must register a per-core ID hook with set_core_id_provider
before starting coroutines on secondary cores; without one, another core calling
an API that reaches suspend_on (the native application ABI or synchronous VFS
files and directories) while a coroutine is live would see that coroutine as its
own and switch onto its stack.
Sourcepub unsafe fn set_native_context(&mut self, value: *const ())
pub unsafe fn set_native_context(&mut self, value: *const ())
Install the native ABI’s private context for this coroutine.
§Safety
A non-null value must identify a live native ABI context for each poll.
Sourcepub fn join_handle(&self) -> JoinHandle ⓘ
pub fn join_handle(&self) -> JoinHandle ⓘ
Create the sole join observer. Multiple observers are rejected to bound waker storage.
Source§impl Coroutine
impl Coroutine
Sourcepub fn stop(&mut self)
pub fn stop(&mut self)
Cancel a suspended coroutine for good, but keep its stack until it is dropped.
This does what dropping it would do to its pending operation: an operation passed to
suspend_on_cancellable is dropped in place, so it gives back what it holds (a
lock, a file), and the join observer sees Cancelled. The frames stay where they
are and are never resumed: Self::resume stays pending from now on. A host that
must let other code run before it can drop the coroutine (another coroutine of the
same program that still reads its stack, say) stops it first.
Stopping never suspends, even when called from another coroutine: the operation’s
destructor runs as if no coroutine were running, so a wait inside it (closing a file
it opened, say) blocks the caller as [task::block_on] does, and runs to its end.
Nothing can therefore cancel the caller halfway through a destructor and leave what
the operation held, or this coroutine, behind.
Stopping a coroutine that has not started, has finished or is already stopped does nothing. Do not call it from the coroutine itself.