Runtime

Struct Runtime 

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

A non-blocking runtime, which will use a given ThreadSelector to assign Tasks to threads.

§Threads

The ThreadSelector will assign a spawned Task to a thread.

§Run Flag

The RuntimeBuilder::runflag will be used to determine if underlying tasks should continue to be polled. One the runflag is set to false, all tasks will immediately stop being polled and threads will gracefully terminate and Drop all active tasks.

Implementations§

Source§

impl Runtime

Source

pub fn builder() -> RuntimeBuilder

Create a RuntimeBuilder to start building a new Runtime

Source

pub fn set_threadlocal<I: Into<Arc<Runtime>>>(runtime: &Runtime)

Set the thread-local Runtime as the current instance.

Threads started by Runtime::spawn will automatically assign the thread-local runtime instance immediately upon being spawend.

Source

pub fn unset_threadlocal()

Set the thread-local Runtime to None

Source

pub fn get_threadlocal() -> Option<Runtime>

Get the thread-local Runtime, returning None if it did not exist.

Source

pub fn get() -> Runtime

Get the current thread-local Runtime, panicing if it did not exist.

Source

pub fn active_task_count(&self) -> usize

Get the total number of active tasks across all threads, including dedicated threads.

Note: The active_task_count is incremented and decremented by the task thread, so pending tasks will not increment the count until discovered by the selected thread.

Source

pub fn thread_task_count(&self, thread_id: u64) -> usize

Get the number of active tasks on the given shared thread id

Note: The active_task_count is incremented and decremented by the task thread, so pending tasks will not increment the count until discovered by the selected thread.

Source

pub fn runflag<'a>(&'a self) -> &'a Arc<AtomicBool>

Get the flag that is used to check if the runtime should keep running.

Setting this atomic flag to false anywhere in the codebase will cause this runtime to stop running. There are no guarantees a Task will have Task::drive called again after this flag is set to false.

Source

pub fn spawn<T>( &self, task_name: &str, task: T, ) -> JoinHandle<<T::Task as Task>::Output>
where T: IntoTask + Send + 'static,

Spawn the given task on a nonblocking thread.

§Threading

The underlying ThreadSelector will determine which thread the task runs on. Multiple non-blocking tasks can run concurrently on the same threa where they will be serviced in a round-robin fashion. A single nonblocking thread will not attempt to idle unless all active tasks report task::Nonblock::Idle.

§Task + Send

For tasks that are Send, you may pass || task into this function to spawn it.

§Task + !Send

Since a Task will always run on a single thread, it is possible to avoid requiring it to be Send. Instead IntoTask will be sent to the target nonblocking thread, where it will instantiate the Task there. This allows a user to create non-Send tasks by transferring the Send responsibility to the IntoTask.

§IntoTask

Users are free to implement IntoTask for their custom types, which reduces boilerplate code needed to spawn nonblocking tasks through this function.

Source

pub fn shutdown<I: Idle>( &self, idle: I, timeout: Option<Duration>, ) -> Result<(), TimedOut>

Shutdown the runtime by setting the runflag to false. Then wait for it to terminate, optionally timing-out after the given timeout Duration.

This function blocks, which means it is not appropriate to call from within a non-blocking Task.

Source

pub fn join<I: Idle>(&self, idle: I)

Join the runtime, returning after a graceful exit.

This function blocks, which means it is not appropriate to call from within a non-blocking Task.

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 = 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.