#local #async #context #async-trait #blocking

async-local

For safely extending the lifetime of thread locals

15 releases (6 stable)

2.0.0 Oct 19, 2023
1.2.0 Mar 27, 2023
0.15.4 Feb 20, 2023
0.6.0 Dec 15, 2022
0.3.0 Nov 30, 2022
Download history 54/week @ 2023-08-06 66/week @ 2023-08-13 12/week @ 2023-08-20 82/week @ 2023-08-27 59/week @ 2023-09-03 15/week @ 2023-09-10 13/week @ 2023-09-17 18/week @ 2023-09-24 20/week @ 2023-10-01 13/week @ 2023-10-08 40/week @ 2023-10-15 21/week @ 2023-10-22 57/week @ 2023-10-29 24/week @ 2023-11-05 20/week @ 2023-11-12 61/week @ 2023-11-19

166 downloads per month
Used in 4 crates (3 directly)

MIT license

19KB
334 lines

Async Local

License Cargo Documentation

Unlocking the potential of thread-locals in an async context

By using a barrier to rendezvous worker threads during runtime shutdown, it can be gauranteed that no task will outlive thread local data belonging to worker threads. With this, pointers to thread locals constrained by invariant lifetimes are guaranteed to be of a valid lifetime suitable for use accross await points.

Runtime Configuration (optional)

For best performance, use the Tokio runtime as configured via the tokio::main or tokio::test macro with the crate attribute set to async_local while the barrier-protected-runtime feature is enabled on async-local. Doing so configures the Tokio runtime with a barrier that rendezvous runtime worker threads during shutdown in a way that ensures tasks never outlive thread local data owned by runtime worker threads and obviates the need for Box::leak as a means of lifetime extension.

#[cfg(test)]*
mod tests {
  use std::sync::atomic::{AtomicUsize, Ordering};

  use async_local::{AsyncLocal, Context};
  use generativity::make_guard;
  use tokio::task::yield_now;

  thread_local! {
      static COUNTER: Context<AtomicUsize> = Context::new(AtomicUsize::new(0));
  }

  #[tokio::test(crate = "async_local", flavor = "multi_thread")]
  async fn it_increments() {
    make_guard!(guard);
    let counter = COUNTER.local_ref(guard);
    yield_now().await;
    counter.fetch_add(1, Ordering::SeqCst);
  }
}

Dependencies

~0.4–32MB
~448K SLoC