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.
ThreadSelect::Dedicatedwill assign the task to its own, new, dedicated thread, which will terminate as soon as the task is shutdown.ThreadSelect::Sharedwill assign the task to a shared thread by ID. Shared threads will terminate after all of its tasks have completed plus a givenRuntimeBuilder::thread_wind_down_duration.
§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
impl Runtime
Sourcepub fn builder() -> RuntimeBuilder
pub fn builder() -> RuntimeBuilder
Create a RuntimeBuilder to start building a new Runtime
Sourcepub fn set_threadlocal<I: Into<Arc<Runtime>>>(runtime: &Runtime)
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.
Sourcepub fn unset_threadlocal()
pub fn unset_threadlocal()
Sourcepub fn get_threadlocal() -> Option<Runtime>
pub fn get_threadlocal() -> Option<Runtime>
Sourcepub fn active_task_count(&self) -> usize
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.
Sourcepub fn thread_task_count(&self, thread_id: u64) -> usize
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.
Sourcepub fn runflag<'a>(&'a self) -> &'a Arc<AtomicBool>
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.
Sourcepub fn spawn<T>(
&self,
task_name: &str,
task: T,
) -> JoinHandle<<T::Task as Task>::Output>
pub fn spawn<T>( &self, task_name: &str, task: T, ) -> JoinHandle<<T::Task as Task>::Output>
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.