Skip to main content

Coroutine

Struct Coroutine 

Source
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

Source

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.

Source

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.

Source

pub fn join_handle(&self) -> JoinHandle ⓘ

Create the sole join observer. Multiple observers are rejected to bound waker storage.

Source

pub fn resume(&mut self, cx: &mut Context<'_>) -> Poll<i32>

Poll the suspended operation with the executor’s real waker, then resume native code. A coroutine that was stopped stays pending for good.

Source§

impl Coroutine

Source

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.

Trait Implementations§

Source§

impl Drop for Coroutine

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Future for Coroutine

Source§

type Output = i32

The type of value produced on completion.
Source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<i32>

Attempts to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F> IntoFuture for F
where F: Future,

Source§

type Output = <F as Future>::Output

The output that the future will produce on completion.
Source§

type IntoFuture = F

Which kind of future are we turning this into?
Source§

fn into_future(self) -> <F as IntoFuture>::IntoFuture

Creates a future from a value. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<F> WithTimeout for F
where F: Future,

§

type Output = <F as Future>::Output

Output type of the future.
§

fn with_timeout(self, timeout: Duration) -> TimeoutFuture<F>

Runs a given future with a timeout. Read more
§

fn with_deadline(self, at: Instant) -> TimeoutFuture<F>

Runs a given future with a deadline time. Read more