Skip to main content

Runtime

Struct Runtime 

Source
pub struct Runtime { /* private fields */ }
Expand description

The native single-threaded task scheduler.

This is a thread-local runtime and cannot be sent to other threads, use Handle to spawn tasks from other threads. It is available even when the crate-level APIs select Tokio or Compio. A Driver supplies I/O and drives scheduled tasks through block_on.

Dropping the runtime cancels its tasks. If cleanup panics, shutdown finishes before the panic is resumed. A second cleanup panic, or a cleanup panic while the runtime is dropped during an existing unwind, aborts the process.

A process can create at most 65,535 runtimes, and at most 65,535 threads can create one. Neither is reused, and the process aborts when either runs out.

ⓘ
fn assert_send<T: Send>() {}
assert_send::<ntex_rt::rt::Runtime>();

Implementations§

Source§

impl Runtime

Source

pub fn new(handle: Box<dyn Notify>) -> Self

Creates a runtime with default configuration.

handle must wake the driver that will drive this runtime. Creating the scheduler does not start an event loop or install it as the current runtime.

Source

pub fn builder() -> RuntimeBuilder

Creates a runtime builder.

Source

pub fn with_current<T, F: FnOnce(&Self) -> T>(f: F) -> T

Calls f with a reference to the native runtime already active on this thread.

§Panics

Panics if no runtime is active on the current thread.

Source

pub fn handle(&self) -> Handle

Returns a handle to this runtime.

Source

pub fn spawn<F: Future + 'static>(&self, future: F) -> JoinHandle<F::Output> ⓘ

Spawns a new asynchronous task, returning a JoinHandle for it.

Spawning a task enables the task to execute concurrently to other tasks. There is no guarantee that a spawned task will execute to completion. The future and output need not be Send. Unlike crate::rt::spawn(), this low-level method does not apply registered task callbacks.

Source

pub unsafe fn spawn_unchecked<F: Future>( &self, future: F, ) -> JoinHandle<F::Output> ⓘ

Spawns a task whose future may borrow non-'static data.

Like spawn, this returns a local JoinHandle and does not apply registered task callbacks.

§Safety

All data borrowed by the future must remain valid until the future is completed or destroyed, including on unwind. Any borrowed data in the output must remain valid until that output is destroyed, even if its join handle outlives this runtime. Dropping or forgetting a join handle does not cancel its task and is not sufficient to end these obligations.

Source

pub fn spawn_detached<F: Future + 'static>(&self, future: F)

Spawns a new asynchronous task without a JoinHandle.

The future and output need not be Send. The output is dropped on the runtime thread when the task completes. This low-level method does not apply registered task callbacks.

Source

pub fn awake(&self)

Marks the runtime as awake.

Drivers call this after returning from a blocking wait, before handling events that wake tasks. Tasks scheduled from the runtime thread do not notify the driver until poll finds no more tasks to run, the driver must call poll before blocking again.

Starts a busy period, see busy_duration.

Source

pub fn poll(&self) -> PollResult

Polls the runtime and runs scheduled tasks.

Runs bounded batches of ready tasks and remote submissions. Returns PollResult::PollAgain if work remains, PollResult::Pending if the driver may wait for events, or PollResult::Ready after the future supplied to block_on completes.

This does not install the runtime as current; block_on establishes that context before invoking the driver.

Source

pub fn now(&self) -> Instant

Returns the start of the current runtime turn.

While the runtime is busy, returns the time the driver woke up or the previous poll finished, tasks polled in the same turn observe the same instant. Returns Instant::now() while the runtime is idle.

Source

pub fn busy_duration(&self) -> Duration

Returns the total time the runtime has been busy.

The runtime is busy from awake until poll finds no more tasks to run. This includes running tasks and handling driver events, and excludes time the driver is blocked waiting for events. The value is wall-clock time, not CPU time, and includes the current busy period, so it can be read from other threads while a task runs.

Utilization over an interval is the busy duration delta divided by the elapsed time of the interval.

Source

pub fn current_turn(&self) -> Option<Duration>

Returns how long the current runtime turn has been running, None while the runtime is idle.

A turn ends when poll returns, a long turn means a task or a driver event handler keeps the runtime from making progress.

Source

pub fn poll_count(&self) -> u64

Returns the number of scheduler polls, including polls with no ready tasks.

Calls that immediately return PollResult::Ready are not counted.

Source

pub fn poll_again_count(&self) -> u64

Returns the number of times poll returned PollResult::PollAgain.

poll runs a limited number of tasks, and returns PollAgain if scheduled tasks remain. The poll_again_count delta divided by the poll_count delta over an interval is the share of polls that left a backlog; a value close to 1 means tasks are scheduled faster than the runtime runs them.

Source

pub fn self_wake_count(&self) -> u64

Returns the number of times a task woke itself while it was polled.

A task that wakes itself, for example with wake_by_ref() or a yield, is scheduled again without waiting for an event. A steadily growing count points to tasks that poll in a loop, see RuntimeBuilder::self_wake_threshold.

Source

pub fn poll_histogram(&self) -> Histogram

Returns the histogram of poll durations.

Measures time spent inside poll, excluding driver-event processing before the call. Calls returning PollResult::Ready are not recorded. Long polls can delay driver events and increase scheduling latency.

Source

pub fn block_on<F: Future>(&self, future: F, driver: &dyn Driver) -> F::Output

Runs the provided future.

Blocks the current thread until the future completes. The future may borrow local data. Other tasks remain scheduled after this call and may be driven by a later call; dropping the runtime cancels its unfinished tasks. The supplied future is dropped before returning, including if the driver fails or panics.

§Panics

Panics if the driver fails to complete the provided future, or if a runtime is already active on the current thread. Nested calls would block the running task and its runtime until the inner future completes.

Trait Implementations§

Source§

impl Debug for Runtime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Runtime

Source§

fn drop(&mut self)

Cancels outstanding tasks with this runtime active during cleanup.

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

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<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.