Skip to main content

Scheduler

Struct Scheduler 

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

A handle for spawning tasks on a Pool.

The scheduler determines which processor to execute tasks on based on the processor that called spawn() or spawn_urgent(). This ensures cache locality by keeping data close to the code that processes it.

§Cloning

Schedulers are cheaply cloneable and can be shared across threads. All clones reference the same underlying pool.

§Example

use vicinal::Pool;

let pool = Pool::new();
let scheduler = pool.scheduler();

// Spawn from the main thread.
let handle1 = scheduler.spawn(|| "hello");

// Clone and use in another context.
let scheduler2 = scheduler.clone();
let handle2 = scheduler.spawn(move || scheduler2.spawn(|| "nested"));

assert_eq!(handle1.await, "hello");
assert_eq!(handle2.await.await, "nested");

Implementations§

Source§

impl Scheduler

Source

pub fn spawn<R, F>(&self, task: F) -> JoinHandle<R>
where R: Send + 'static, F: FnOnce() -> R + Send + 'static,

Spawns a task to be executed on the current processor.

The task will be added to the regular (normal-priority) queue and executed by a worker thread associated with the processor that called this method.

Spawned tasks may be executed in any order. Urgent tasks are scheduled preferentially but there is no hard guarantee that they will preempt all regular tasks.

§Returns

A JoinHandle that can be awaited to get the task’s return value.

§Panics

If the task panics, the panic will be captured and re-thrown when the JoinHandle is awaited.

Source

pub fn spawn_urgent<R, F>(&self, task: F) -> JoinHandle<R>
where R: Send + 'static, F: FnOnce() -> R + Send + 'static,

Spawns an urgent (high-priority) task to be executed on the current processor.

Urgent tasks are executed before regular tasks. Use this for time-sensitive operations that should not wait behind a queue of regular work.

Spawned tasks may be executed in any order. Urgent tasks are scheduled preferentially but there is no hard guarantee that they will preempt all regular tasks.

§Returns

A JoinHandle that can be awaited to get the task’s return value.

§Panics

If the task panics, the panic will be captured and re-thrown when the JoinHandle is awaited.

Source

pub fn spawn_and_forget<F>(&self, task: F)
where F: FnOnce() + Send + 'static,

Spawns a fire-and-forget task on the current processor.

Unlike spawn, this method does not return a JoinHandle. The task’s result is discarded, and panics are logged rather than propagated. This reduces overhead by avoiding result channel allocation.

Use this when:

  • The task’s return value is not needed
  • Panic propagation is not required
  • Working with trait objects that cannot support generic return types
§Panics

If the task panics, the panic will be caught and logged via tracing::error! but will not be propagated to the caller.

Source

pub fn spawn_urgent_and_forget<F>(&self, task: F)
where F: FnOnce() + Send + 'static,

Spawns an urgent fire-and-forget task on the current processor.

This is the fire-and-forget equivalent of spawn_urgent. The task is added to the high-priority queue but does not return a JoinHandle.

Use this for time-sensitive operations where the result is not needed.

§Panics

If the task panics, the panic will be caught and logged via tracing::error! but will not be propagated to the caller.

Trait Implementations§

Source§

impl Clone for Scheduler

Source§

fn clone(&self) -> Scheduler

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Scheduler

Source§

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

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more