9 stable releases

1.4.1 Feb 9, 2024
1.3.0 Mar 13, 2023
1.2.1 Nov 5, 2022
1.1.0 Dec 12, 2021
1.0.0 Nov 5, 2020

#145 in Concurrency

28 downloads per month
Used in supergit

GPL-3.0-or-later

17KB
215 lines

AtomPtr

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.

License

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.

No runtime deps