Skip to content

Latest commit

 

History

History
33 lines (21 loc) · 1.17 KB

README.md

File metadata and controls

33 lines (21 loc) · 1.17 KB

Atomic Option Cell

Forked from crossbeam-utils's AtomicCell, this crate provides a tweaked version of that structure called AtomicOptionCell, which has been optimized for atomically-sized Option<NonZero> types.

Where possible, this crate uses lockless operations, and exposes AtomicOptionCell::is_lock_free and atomic_option_is_lock_free to validate when they will be used.

In the interest of interoperability, the FallbackLock trait enables customized fallback locking solutions for non-atomically-sized types.

Example

Create a cell with a per-object Mutex as the fallback.

use atomic_option_cell::{AtomicOptionCell, Mutex};

let a = AtomicOptionCell::new(Some(5), Mutex::new(()));
let five = a.take();

assert_eq!(five, Some(5));
assert_eq!(a.into_inner(), None);

Create a cell with a global Mutex set as the fallback.

use atomic_option_cell::{AtomicOptionCell, Mutex};

let a = AtomicOptionCell::new(Some(5), Mutex::new(()));
let five = a.take();

assert_eq!(five, Some(5));
assert_eq!(a.into_inner(), None);