Crates.io | switchyard |
lib.rs | switchyard |
version | 0.3.0 |
source | src |
created_at | 2020-11-03 02:28:10.807967 |
updated_at | 2022-06-15 02:18:37.77183 |
description | Real-time compute focused async executor |
homepage | |
repository | https://github.com/BVE-Reborn/switchyard |
max_upload_size | |
id | 308137 |
size | 76,897 |
Real-time compute-focused async executor with job pools, thread-local data, and priorities.
use switchyard::Switchyard;
use switchyard::threads::{thread_info, one_to_one};
// Create a new switchyard without thread local data
let yard = Switchyard::new(one_to_one(thread_info(), Some("thread-name")), ||()).unwrap();
// Spawn a task on priority 10 and get a JoinHandle
let handle = yard.spawn(10, async move { 5 + 5 });
// Spawn a lower priority task
let handle2 = yard.spawn(0, async move { 2 + 2 });
// Wait on the results
assert_eq!(handle.await + handle2.await, 14);
Switchyard is different from other existing async executors, focusing on situations where precise control of threads and execution order is needed. One such situation is using task parallelism to parallelize a compute workload.
Each task has a priority and tasks are ran in order from high priority to low priority.
// Spawn task with lowest priority.
yard.spawn(0, async move { /* ... */ });
// Spawn task with higher priority. If both tasks are waiting, this one will run first.
yard.spawn(10, async move { /* ... */ });
Each yard has some thread local data that can be accessed using spawn_local
.
Both the thread local data and the future generated by the async function passed to spawn_local
may be !Send
and !Sync
. The future will only be resumed on the thread that created it.
If the data is Send
, then you can call access_per_thread_data
to get
a vector of mutable references to all thread's data. See it's documentation for more information.
// Create yard with thread local data. The data is !Sync.
let yard = Switchyard::new(one_to_one(thread_info(), Some("thread-name")), || Cell::new(42)).unwrap();
// Spawn task that uses thread local data. Each running thread will get their own copy.
yard.spawn_local(0, |data| async move { data.set(10) });
1.51
Future MSRV bumps will be breaking changes.
License: MIT OR Apache-2.0 OR Zlib