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
impl Runtime
Sourcepub fn new(handle: Box<dyn Notify>) -> Self
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.
Sourcepub fn builder() -> RuntimeBuilder
pub fn builder() -> RuntimeBuilder
Creates a runtime builder.
Sourcepub fn with_current<T, F: FnOnce(&Self) -> T>(f: F) -> T
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.
Sourcepub fn spawn<F: Future + 'static>(&self, future: F) -> JoinHandle<F::Output> ⓘ
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.
Sourcepub unsafe fn spawn_unchecked<F: Future>(
&self,
future: F,
) -> JoinHandle<F::Output> ⓘ
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.
Sourcepub fn spawn_detached<F: Future + 'static>(&self, future: F)
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.
Sourcepub fn awake(&self)
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.
Sourcepub fn poll(&self) -> PollResult
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.
Sourcepub fn now(&self) -> Instant
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.
Sourcepub fn busy_duration(&self) -> Duration
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.
Sourcepub fn current_turn(&self) -> Option<Duration>
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.
Sourcepub fn poll_count(&self) -> u64
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.
Sourcepub fn poll_again_count(&self) -> u64
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.
Sourcepub fn self_wake_count(&self) -> u64
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.
Sourcepub fn poll_histogram(&self) -> Histogram
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.
Sourcepub fn block_on<F: Future>(&self, future: F, driver: &dyn Driver) -> F::Output
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.