1 unstable release

0.1.0 Jan 7, 2024

#824 in Data structures

Download history 1/week @ 2024-01-18 7/week @ 2024-01-25 6/week @ 2024-02-01 13/week @ 2024-02-08 31/week @ 2024-02-15 43/week @ 2024-02-22 31/week @ 2024-02-29 20/week @ 2024-03-07 7/week @ 2024-03-14 17/week @ 2024-03-21 52/week @ 2024-03-28 16/week @ 2024-04-04 1/week @ 2024-04-11

86 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–27MB
~358K SLoC