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
impl Scheduler
Sourcepub fn spawn<R, F>(&self, task: F) -> JoinHandle<R> ⓘ
pub fn spawn<R, F>(&self, task: F) -> JoinHandle<R> ⓘ
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.
Sourcepub fn spawn_urgent<R, F>(&self, task: F) -> JoinHandle<R> ⓘ
pub fn spawn_urgent<R, F>(&self, task: F) -> JoinHandle<R> ⓘ
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.
Sourcepub fn spawn_and_forget<F>(&self, task: F)
pub fn spawn_and_forget<F>(&self, task: F)
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.
Sourcepub fn spawn_urgent_and_forget<F>(&self, task: F)
pub fn spawn_urgent_and_forget<F>(&self, task: F)
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§
Auto Trait Implementations§
impl Freeze for Scheduler
impl !RefUnwindSafe for Scheduler
impl Send for Scheduler
impl Sync for Scheduler
impl Unpin for Scheduler
impl !UnwindSafe for Scheduler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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