Switchyard

Struct Switchyard 

Source
pub struct Switchyard<TD: 'static> { /* private fields */ }
Expand description

Compute focused async executor.

See crate documentation for more details.

Implementations§

Source§

impl<TD: 'static> Switchyard<TD>

Source

pub fn new<TDFunc>( thread_allocations: impl IntoIterator<Item = ThreadAllocationOutput>, thread_local_data_creation: TDFunc, ) -> Result<Self, SwitchyardCreationError>
where TDFunc: Fn() -> TD + Send + Sync + 'static,

Create a new switchyard.

For each element in the provided thread_allocations iterator, the yard will spawn a worker thread with the given settings. Helper functions in threads can generate these iterators for common situations.

thread_local_data_creation will be called on each thread to create the thread local data accessible by spawn_local.

Source

pub fn spawn<Fut, T>(&self, priority: Priority, fut: Fut) -> JoinHandle<T>
where Fut: Future<Output = T> + Send + 'static, T: Send + 'static,

Spawn a future which can migrate between threads during executionat the given priority.

A higher priority will cause the task to be run sooner.

§Example
use switchyard::{Switchyard, threads::single_thread};

// Create a yard with a single pool
let yard: Switchyard<()> = Switchyard::new(single_thread(None, None), || ()).unwrap();

// Spawn a task with priority 0 and get a handle to the result.
let handle = yard.spawn(0, async move { 2 * 2 });

// Await result
assert_eq!(handle.await, 4);
§Panics
  • finish has been called on the pool.
Source

pub fn spawn_local<Func, Fut, T>( &self, priority: Priority, async_fn: Func, ) -> JoinHandle<T>
where Func: FnOnce(Arc<TD>) -> Fut + Send + 'static, Fut: Future<Output = T>, T: Send + 'static,

Spawns an async function which is tied to a single thread during execution.

Spawns to the given job pool at the given priority.

The given async function will be provided an Arc to the thread-local data to create its future with.

A higher priority will cause the task to be run sooner.

The function must be Send, but the future returned by that function may be !Send.

§Example
use std::{cell::Cell, sync::Arc};
use switchyard::{Switchyard, threads::single_thread};

// Create a yard with thread local data.
let yard: Switchyard<Cell<u64>> = Switchyard::new(
    single_thread(None, None),
    || Cell::new(42)
).unwrap();

// Spawn an async function using the data.
yard.spawn_local(0, |data: Arc<Cell<u64>>| async move {data.set(12);});

async fn some_async(data: Arc<Cell<u64>>) -> u64 {
    data.set(15);
    2 * 2
}

// Works with normal async functions too
let handle = yard.spawn_local(0, some_async);
assert_eq!(handle.await, 4);
§Panics
  • Panics is pool refers to a non-existent job pool.
Source

pub async fn wait_for_idle(&self)

Wait until all working threads are starved of work due to lack of jobs or all jobs waiting.

§Safety
  • This function provides no safety guarantees.
  • Jobs may be added while the future returns.
  • Jobs may be woken while the future returns.
Source

pub fn jobs(&self) -> usize

Current amount of jobs in flight.

§Safety
  • This function provides no safety guarantees.
  • Jobs may be added after the value is received and before it is returned.
Source

pub fn active_threads(&self) -> usize

Count of threads currently processing jobs.

§Safety
  • This function provides no safety guarantees.
  • Jobs may be added after the value is received and before it is returned re-activating threads.
Source

pub fn finish(&mut self)

Kill all threads as soon as they finish their jobs. All calls to spawn and spawn_local will panic after this function is called.

This is equivalent to calling drop. Calling this function twice will be a no-op the second time.

Trait Implementations§

Source§

impl<TD: 'static> Drop for Switchyard<TD>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<TD> Send for Switchyard<TD>

Source§

impl<TD> Sync for Switchyard<TD>

Auto Trait Implementations§

§

impl<TD> Freeze for Switchyard<TD>

§

impl<TD> !RefUnwindSafe for Switchyard<TD>

§

impl<TD> Unpin for Switchyard<TD>

§

impl<TD> !UnwindSafe for Switchyard<TD>

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.