Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/colinjfw/arc-atomic
Provides an atomic pointer to an `Arc`
https://github.com/colinjfw/arc-atomic
Last synced: 4 days ago
JSON representation
Provides an atomic pointer to an `Arc`
- Host: GitHub
- URL: https://github.com/colinjfw/arc-atomic
- Owner: colinjfw
- License: mit
- Created: 2024-01-07T18:00:58.000Z (10 months ago)
- Default Branch: mainline
- Last Pushed: 2024-01-07T18:11:08.000Z (10 months ago)
- Last Synced: 2024-11-02T12:42:35.378Z (11 days ago)
- Language: Rust
- Size: 4.88 KB
- Stars: 5
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
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](https://docs.rs/arc-swap),
although the underlying mechanism for providing swappability is simplified by
avoiding any attempt at providing a thread-local pointer cache.