Crates.io | atomptr |
lib.rs | atomptr |
version | 1.4.1 |
source | src |
created_at | 2020-11-05 15:02:11.280461 |
updated_at | 2024-02-09 02:31:27.368664 |
description | A safe, dependency-less abstraction for typed atomic smart pointers |
homepage | |
repository | https://cyberchaos.dev/kookie/atomptr |
max_upload_size | |
id | 308902 |
size | 47,760 |
A safe wrapper around atomic pointers to build datastructures and
lock-free algorithms on top of. Only uses std
as a dependency.
The standard library contains an AtomicPtr
type, which by itself is
very unergonomic to use, because it deals with raw pointers. It is
very easy to misuse this API and get memory leaks or double-free.
This library makes some assumptions about how you would like to use
your data. Furthermore, it is dangerous to use raw pointers without
some other memory management strategy. This crate uses Arc<T>
to
handle reference lifetime management. Data in an AtomPtr<T>
is also
always heap allocated.
Following is a simple example.
use atomptr::AtomPtr;
struct MyData { name: String }
let data = MyData { name: "Kookie".into() };
let a = AtomPtr::new(data);
println!("Name is: {}", a.get_ref().name);
let old_ref = a.swap(MyData { name: "Bob".into() });
println!("Name now is: {}, was {}", a.get_ref().name, old_ref.name);
Because all data is wrapped by Arc<T>
internally, returning a
reference of the underlying data doesn't mean it is removed from the
pointer itself, or de-allocated after Ref<T>
goes out of scope.
Another thread can of course swap the data contained in the pointer,
which is why compare_exchange
requires a previously held data
reference.
This micro-library is free software, and licensed under the GNU General Public License, version 3.0 or (at your choice), any later version.
Additional Permissions: For Submission to the Apple App Store: Provided that you are otherwise in compliance with the GPLv3 for each covered work you convey (including without limitation making the Corresponding Source available in compliance with Section 6 of the GPLv3), the qaul.net developers also grant you the additional permission to convey through the Apple App Store non-source executable versions of the Program as incorporated into each applicable covered work as Executable Versions only under the Mozilla Public License version 2.0.
A copy of both the GPL-3.0 and MPL-2.0 license texts are included in this repository.