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>
impl<TD: 'static> Switchyard<TD>
Sourcepub fn new<TDFunc>(
thread_allocations: impl IntoIterator<Item = ThreadAllocationOutput>,
thread_local_data_creation: TDFunc,
) -> Result<Self, SwitchyardCreationError>
pub fn new<TDFunc>( thread_allocations: impl IntoIterator<Item = ThreadAllocationOutput>, thread_local_data_creation: TDFunc, ) -> Result<Self, SwitchyardCreationError>
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
.
Sourcepub fn spawn<Fut, T>(&self, priority: Priority, fut: Fut) -> JoinHandle<T> ⓘ
pub fn spawn<Fut, T>(&self, priority: Priority, fut: Fut) -> JoinHandle<T> ⓘ
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.
Sourcepub fn spawn_local<Func, Fut, T>(
&self,
priority: Priority,
async_fn: Func,
) -> JoinHandle<T> ⓘ
pub fn spawn_local<Func, Fut, T>( &self, priority: Priority, async_fn: Func, ) -> JoinHandle<T> ⓘ
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.
Sourcepub async fn wait_for_idle(&self)
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.
Sourcepub fn jobs(&self) -> usize
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.
Sourcepub fn active_threads(&self) -> usize
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.