Skip to main content

Crate ntex_rt

Crate ntex_rt 

Source
Expand description

Single-threaded asynchronous runtime infrastructure for ntex.

A System owns the runtime configuration and coordinates one or more Arbiter execution threads. Each arbiter runs local futures on its own thread, allowing tasks to use types that are not Send.

Use System::build() to configure a system, spawn() to start a local task, and spawn_blocking() for blocking work.

Local JoinHandles are neither Send nor Sync, even for Send outputs.

Handle::spawn() submits a Send future to the handle’s runtime and returns a RemoteJoinHandle for the calling thread. On the runtime’s own thread the task is spawned directly. From other threads the future and later its Send output share one allocation, which is passed to the runtime thread:

let task = handle.spawn(async { 42 });
assert_eq!(task.await.unwrap(), 42);

Dropping the handle detaches the task, RemoteJoinHandle::cancel() cancels it. A submitted future is polled and dropped on the runtime thread; shutdown or cancellation before producing a result yields a join error. No task is guaranteed to finish. Rejected submissions are never polled and their futures may be dropped on the submitting thread.

Use Handle::spawn_detached() for fire-and-forget work. Its output need not be Send and is discarded on the owner.

Remote join handles also remain on their calling thread, even when the submission uses the runtime’s local fast path. Both the submitted future and its output must be Send:

ⓘ
handle.spawn(async { std::rc::Rc::new(42) });

LocalWaker owns a thread-confined wake handle. It specializes native task wakes on their owner thread and preserves standard waker behavior for other runtimes and wrapped wakers.

Runtime backends are selected with Cargo features:

  • no runtime feature uses the native ntex runtime
  • tokio uses the Tokio-based runtime adapter
  • compio uses the Compio-based runtime adapter

The tokio and compio features are mutually exclusive. The rt module always exposes the native scheduler, including Runtime and RuntimeBuilder, independently of which adapter supplies the crate-level spawning functions and Handle.

A Runner supplies the event loop used by SystemRunner and arbiter threads. Building a system only stores its configuration; execution begins when the returned system runner is driven.

Native event-loop integrations implement Driver and use PollResult from Runtime::poll to decide whether to wait, poll again, or stop. Runtime, arbiter, and blocking pool metric types are grouped in metrics.

Re-exports§

pub use self::arbiter::Arbiter;
pub use self::arbiter::get_item;
pub use self::arbiter::set_item;
pub use self::arbiter::with_item;
pub use self::rt::Runtime;
pub use self::rt::RuntimeBuilder;
pub use self::rt::Handle;
pub use self::rt::JoinError;
pub use self::rt::JoinHandle;
pub use self::rt::RemoteJoinHandle;
pub use self::rt::now;
pub use self::rt::spawn;
pub use self::rt::spawn_detached;

Modules§

metrics
Runtime, arbiter, and blocking thread pool metrics.
rt
Native single-threaded runtime, task spawning, handles, and clock access.
signals
Process-signal subscriptions and optional application-panic notifications.

Macros§

syscall
Executes a Unix system call and converts a -1 result to the last I/O error.

Structs§

Arbiter
An asynchronous execution environment running on one OS thread.
BlockingError
Error returned when blocking work cannot produce a result.
BlockingResult
Future resolving to the result of blocking work.
Builder
Builder for an ntex runtime system.
Id
Identifier assigned to a System or Arbiter.
LocalWaker
An owned wake handle that can only be used on the thread that acquired it.
System
Runtime manager for a group of arbiter threads.
SystemRunner
A configured system that has not yet started its event loop.
ThreadPool
A thread pool for executing blocking operations.

Enums§

DriverType
Operating-system I/O mechanism used by a driver.
PollResult
Outcome of one call to Runtime::poll.

Traits§

Driver
Drives I/O events and scheduled tasks for the native Runtime.
Notify
Wakes a runtime driver, retaining the wakeup if it has not blocked yet.
Runner
Creates and drives an event loop for a system or arbiter thread.

Functions§

get_item
Returns a cloned value from the current arbiter’s thread-local storage.
set_item
Inserts a value into the current arbiter’s thread-local storage.
spawn_blocking
Submits blocking work and returns a future for its result.
task_callbacks⚠
Installs process-wide callbacks for instrumenting spawned tasks.
task_opt_callbacks⚠
Attempts to install process-wide task callbacks.
with_item
Provides access to a value in the current arbiter’s thread-local storage.

Type Aliases§

BlockFuture
A boxed, pinned future passed to a Runner.