Skip to main content

psync/
lib.rs

1//! psync: automatic concurrency for async Rust, and a small priority executor.
2//!
3//! - [`graph`] (`#[psync::graph]`) rewrites an async function so that awaits which do not depend
4//!   on each other run concurrently, with results, errors, early exits and writes to files and
5//!   services kept as in the sequential code. The hints [`seq`], [`fresh`], [`independent`] and
6//!   [`speculate`] tell it what tokens cannot show. The generated code needs only `core` and
7//!   `alloc` (loop fission also uses `std`'s `Vec`) and runs on any executor.
8//! - [`Executor`] is a cooperative, single-core, `no_std` executor: larger priorities run first,
9//!   equal priorities rotate FIFO, and sleeping tasks are polled only after a wake.
10//!
11//! The psync book (`book/` in the repository) covers both in depth.
12#![no_std]
13
14extern crate alloc;
15
16#[doc(hidden)]
17#[path = "graph_rt.rs"]
18pub mod __private;
19mod executor;
20#[cfg(feature = "std")]
21mod files;
22mod future;
23#[cfg(feature = "std")]
24mod multi;
25mod os;
26#[cfg(feature = "std")]
27mod par;
28mod park;
29mod pool;
30pub mod profile;
31mod task;
32
33pub use executor::Executor;
34pub use future::{BoxFuture, LocalBoxFuture, YieldNow, yield_now};
35#[cfg(feature = "std")]
36pub use multi::{MultiExecutor, MultiHandle};
37pub use park::{DefaultPark, Park};
38pub use pool::{DefaultPool, Pool};
39pub use psync_macros::{fresh, graph, independent, par, seq, speculate};
40pub use task::{Task, TaskHandle};
41
42/// Limit how many prefetched loop items may be in flight at once across the whole process,
43/// beyond the first item of each loop.
44///
45/// Each `#[psync::graph]` loop keeps at most its `bound` items in flight, and nested loops
46/// multiply: two nested loops with the default bound of 16 may hold 256 requests. The budget caps
47/// the sum over every loop running in the process. The first item of every loop is always free,
48/// so every loop makes progress and nested loops cannot deadlock, even with a budget of 0 (which
49/// runs every loop one item at a time). The default is unlimited (`usize::MAX`).
50///
51/// Call it once at start-up, for example with the number of connections your server or pool
52/// accepts.
53pub fn set_in_flight_budget(limit: usize) {
54    __private::set_budget(limit);
55}
56
57/// How many loop items beyond each loop's first are in flight now: the budget's tokens in use.
58pub fn in_flight_beyond_first() -> usize {
59    __private::extra_in_flight()
60}
61
62#[cfg(all(feature = "rp235x", target_os = "none", target_arch = "arm"))]
63#[global_allocator]
64static HEAP: embedded_alloc::LlffHeap = embedded_alloc::LlffHeap::empty();
65
66/// Initialize the firmware heap before creating an executor.
67///
68/// # Safety
69/// Call exactly once, before any allocation, with no concurrent heap access.
70/// The application's linker script must provide valid heap boundaries.
71#[cfg(all(feature = "rp235x", target_os = "none", target_arch = "arm"))]
72pub unsafe fn init_heap() {
73    unsafe extern "C" {
74        static mut _heap_start: u8;
75        static mut _heap_end: u8;
76    }
77    let start = &raw mut _heap_start as usize;
78    let end = &raw mut _heap_end as usize;
79    unsafe { HEAP.init(start, end - start) };
80}