1 unstable release

0.1.0 Jan 7, 2024

#1782 in Data structures

Download history 75/week @ 2024-07-20 159/week @ 2024-07-27 101/week @ 2024-08-03 87/week @ 2024-08-10 73/week @ 2024-08-17 78/week @ 2024-08-24 74/week @ 2024-08-31 44/week @ 2024-09-07 47/week @ 2024-09-14 146/week @ 2024-09-21 99/week @ 2024-09-28 101/week @ 2024-10-05 71/week @ 2024-10-12 49/week @ 2024-10-19 27/week @ 2024-10-26 68/week @ 2024-11-02

223 downloads per month

MIT license

9KB
141 lines

Provides an atomic pointer to an [Arc]

The AtomicArc is an AtomicPtr to an [Arc], supporting atomic swap and store operations. The underlying [Arc] reference counts manage deallocation of the underlying memory whereas the AtomicArc manages which underlying [Arc] is loaded by a thread at any point in time. Specifically, load operations on an AtomicArc increment the reference count returning a strong reference to the [Arc] ensuring that the underlying memory is only dropped when all references have been dropped.

Example

It's common to wrap AtomicArc in an [Arc] itself to allow sharing across threads.

# use std::sync::Arc;
# use std::thread;
# use arc_atomic::AtomicArc;
let arc = Arc::new(AtomicArc::new(Arc::new(1)));
let handle = arc.clone();
thread::spawn(move || {
    let val = *handle.load();
    println!("{val}"); // may print '1' or '2'
});
let prev = *arc.swap(Arc::new(2));
println!("{prev}"); // prints '1'

Design

Sequentially consistent ordering is used for all load/swap operations on the underlying AtomicPtr. This ensures that a swap operation will atomically swap a pointer to a new Arc which will be observed by all subsequent loads on any thread. This behaviour is verified using tests with loom and compiles under miri.

This crate provides similar functionality as arc-swap, although the underlying mechanism for providing swappability is simplified by avoiding any attempt at providing a thread-local pointer cache.

Dependencies

~0–24MB
~334K SLoC