AtomicBitfieldContainer

Struct AtomicBitfieldContainer 

Source
pub struct AtomicBitfieldContainer<T: NoUninit>(/* private fields */);
Expand description

Atomic container for bitfield storage.

Implementations§

Source§

impl<T: NoUninit + FromPrimitive + ToPrimitive + PrimInt> AtomicBitfieldContainer<T>

Source

pub fn new(value: T) -> Self

Creates the new bitfield container.

Examples found in repository?
examples/sample.rs (line 28)
7fn main() {
8    let mut storage;
9
10    storage = A::encode(true);
11    assert!(A::decode(storage));
12    storage = B::update(-1, storage);
13    assert_eq!(B::decode(storage), -1);
14    storage = B::update(2, storage);
15    assert_eq!(B::decode(storage), 2);
16    assert!(A::decode(storage));
17    assert_eq!(C::decode(storage), 0);
18    assert!(B::is_valid(7));
19    assert!(!B::is_valid(8));
20    assert!(B::is_valid(-8));
21    assert!(!B::is_valid(-9));
22
23    println!("Creating atomic bitfield storage");
24
25    type LockBit = BitField<usize, bool, 0, 1, false>;
26    type DataBits = BitField<usize, u32, { LockBit::NEXT_BIT }, 32, false>;
27
28    let container = std::sync::Arc::new(AtomicBitfieldContainer::new(0usize));
29    println!(
30        "lock bit: {}, data bits: {}",
31        container.read::<LockBit>(),
32        container.read::<DataBits>()
33    );
34    let thread = {
35        let container = container.clone();
36
37        std::thread::spawn(move || {
38            println!("thread 1 spawned");
39            while !container.try_acquire::<LockBit>() {}
40            container.update::<DataBits>(42);
41            println!(
42                "thread 1: set data bits to {}",
43                container.read::<DataBits>()
44            );
45            assert!(container.try_release::<LockBit>());
46        })
47    };
48
49    loop {
50        while !container.try_acquire::<LockBit>() {
51            std::thread::yield_now();
52        }
53        if container.read::<DataBits>() != 0 {
54            println!("data bits is not zero, break");
55            assert_eq!(container.read::<DataBits>(), 42);
56            assert!(container.try_release::<LockBit>());
57            break;
58        }
59
60        println!("data bits is zero, loop");
61
62        assert!(container.try_release::<LockBit>());
63    }
64    println!("{}", container.read::<DataBits>());
65    thread.join().unwrap();
66}
Source

pub fn load_ignore_race(&self) -> T

Load underlying value of container ignoring atomic access

Source

pub fn load(&self, order: Ordering) -> T

Load value of container with order ordering.

Source

pub fn store(&self, value: T, order: Ordering)

Store new value to container with order ordering.

Source

pub fn compare_exchange_weak( &self, current: T, new: T, success: Ordering, failure: Ordering, ) -> Result<T, T>

Perform CAS, equivalent to compare_exchange_weak in any std type.

Source

pub fn read<B: BitFieldTrait<T>>(&self) -> B::Type

Use bitfield B to decode the value in container and return the value.

Examples found in repository?
examples/sample.rs (line 31)
7fn main() {
8    let mut storage;
9
10    storage = A::encode(true);
11    assert!(A::decode(storage));
12    storage = B::update(-1, storage);
13    assert_eq!(B::decode(storage), -1);
14    storage = B::update(2, storage);
15    assert_eq!(B::decode(storage), 2);
16    assert!(A::decode(storage));
17    assert_eq!(C::decode(storage), 0);
18    assert!(B::is_valid(7));
19    assert!(!B::is_valid(8));
20    assert!(B::is_valid(-8));
21    assert!(!B::is_valid(-9));
22
23    println!("Creating atomic bitfield storage");
24
25    type LockBit = BitField<usize, bool, 0, 1, false>;
26    type DataBits = BitField<usize, u32, { LockBit::NEXT_BIT }, 32, false>;
27
28    let container = std::sync::Arc::new(AtomicBitfieldContainer::new(0usize));
29    println!(
30        "lock bit: {}, data bits: {}",
31        container.read::<LockBit>(),
32        container.read::<DataBits>()
33    );
34    let thread = {
35        let container = container.clone();
36
37        std::thread::spawn(move || {
38            println!("thread 1 spawned");
39            while !container.try_acquire::<LockBit>() {}
40            container.update::<DataBits>(42);
41            println!(
42                "thread 1: set data bits to {}",
43                container.read::<DataBits>()
44            );
45            assert!(container.try_release::<LockBit>());
46        })
47    };
48
49    loop {
50        while !container.try_acquire::<LockBit>() {
51            std::thread::yield_now();
52        }
53        if container.read::<DataBits>() != 0 {
54            println!("data bits is not zero, break");
55            assert_eq!(container.read::<DataBits>(), 42);
56            assert!(container.try_release::<LockBit>());
57            break;
58        }
59
60        println!("data bits is zero, loop");
61
62        assert!(container.try_release::<LockBit>());
63    }
64    println!("{}", container.read::<DataBits>());
65    thread.join().unwrap();
66}
Source

pub fn update<B: BitFieldTrait<T>>(&self, value: B::Type)

Update container according to bitfield B, performs CAS under the hood.

Examples found in repository?
examples/sample.rs (line 40)
7fn main() {
8    let mut storage;
9
10    storage = A::encode(true);
11    assert!(A::decode(storage));
12    storage = B::update(-1, storage);
13    assert_eq!(B::decode(storage), -1);
14    storage = B::update(2, storage);
15    assert_eq!(B::decode(storage), 2);
16    assert!(A::decode(storage));
17    assert_eq!(C::decode(storage), 0);
18    assert!(B::is_valid(7));
19    assert!(!B::is_valid(8));
20    assert!(B::is_valid(-8));
21    assert!(!B::is_valid(-9));
22
23    println!("Creating atomic bitfield storage");
24
25    type LockBit = BitField<usize, bool, 0, 1, false>;
26    type DataBits = BitField<usize, u32, { LockBit::NEXT_BIT }, 32, false>;
27
28    let container = std::sync::Arc::new(AtomicBitfieldContainer::new(0usize));
29    println!(
30        "lock bit: {}, data bits: {}",
31        container.read::<LockBit>(),
32        container.read::<DataBits>()
33    );
34    let thread = {
35        let container = container.clone();
36
37        std::thread::spawn(move || {
38            println!("thread 1 spawned");
39            while !container.try_acquire::<LockBit>() {}
40            container.update::<DataBits>(42);
41            println!(
42                "thread 1: set data bits to {}",
43                container.read::<DataBits>()
44            );
45            assert!(container.try_release::<LockBit>());
46        })
47    };
48
49    loop {
50        while !container.try_acquire::<LockBit>() {
51            std::thread::yield_now();
52        }
53        if container.read::<DataBits>() != 0 {
54            println!("data bits is not zero, break");
55            assert_eq!(container.read::<DataBits>(), 42);
56            assert!(container.try_release::<LockBit>());
57            break;
58        }
59
60        println!("data bits is zero, loop");
61
62        assert!(container.try_release::<LockBit>());
63    }
64    println!("{}", container.read::<DataBits>());
65    thread.join().unwrap();
66}
Source

pub fn update_synchronized<B: BitFieldTrait<T>>(&self, value: B::Type)

Update container according to bitfield B. Does atomic store under the hood

Source

pub fn update_conditional<B: BitFieldTrait<T>>( &self, value_to_be_set: B::Type, conditional_old_value: B::Type, ) -> B::Type

Conditional update of storage container according to bitfield B.

This is equivalent of CAS for bitfields.

Source§

impl AtomicBitfieldContainer<u8>

Source

pub fn fetch_or<B: BitFieldTrait<u8>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<u8>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<u8>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<u8>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<u8>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<u8>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<u16>

Source

pub fn fetch_or<B: BitFieldTrait<u16>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<u16>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<u16>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<u16>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<u16>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<u16>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<u32>

Source

pub fn fetch_or<B: BitFieldTrait<u32>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<u32>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<u32>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<u32>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<u32>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<u32>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<u64>

Source

pub fn fetch_or<B: BitFieldTrait<u64>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<u64>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<u64>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<u64>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<u64>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<u64>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<i8>

Source

pub fn fetch_or<B: BitFieldTrait<i8>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<i8>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<i8>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<i8>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<i8>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<i8>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<i16>

Source

pub fn fetch_or<B: BitFieldTrait<i16>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<i16>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<i16>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<i16>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<i16>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<i16>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<i32>

Source

pub fn fetch_or<B: BitFieldTrait<i32>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<i32>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<i32>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<i32>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<i32>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<i32>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<i64>

Source

pub fn fetch_or<B: BitFieldTrait<i64>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<i64>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<i64>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<i64>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<i64>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<i64>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<usize>

Source

pub fn fetch_or<B: BitFieldTrait<usize>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<usize>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<usize>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<usize>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Examples found in repository?
examples/sample.rs (line 39)
7fn main() {
8    let mut storage;
9
10    storage = A::encode(true);
11    assert!(A::decode(storage));
12    storage = B::update(-1, storage);
13    assert_eq!(B::decode(storage), -1);
14    storage = B::update(2, storage);
15    assert_eq!(B::decode(storage), 2);
16    assert!(A::decode(storage));
17    assert_eq!(C::decode(storage), 0);
18    assert!(B::is_valid(7));
19    assert!(!B::is_valid(8));
20    assert!(B::is_valid(-8));
21    assert!(!B::is_valid(-9));
22
23    println!("Creating atomic bitfield storage");
24
25    type LockBit = BitField<usize, bool, 0, 1, false>;
26    type DataBits = BitField<usize, u32, { LockBit::NEXT_BIT }, 32, false>;
27
28    let container = std::sync::Arc::new(AtomicBitfieldContainer::new(0usize));
29    println!(
30        "lock bit: {}, data bits: {}",
31        container.read::<LockBit>(),
32        container.read::<DataBits>()
33    );
34    let thread = {
35        let container = container.clone();
36
37        std::thread::spawn(move || {
38            println!("thread 1 spawned");
39            while !container.try_acquire::<LockBit>() {}
40            container.update::<DataBits>(42);
41            println!(
42                "thread 1: set data bits to {}",
43                container.read::<DataBits>()
44            );
45            assert!(container.try_release::<LockBit>());
46        })
47    };
48
49    loop {
50        while !container.try_acquire::<LockBit>() {
51            std::thread::yield_now();
52        }
53        if container.read::<DataBits>() != 0 {
54            println!("data bits is not zero, break");
55            assert_eq!(container.read::<DataBits>(), 42);
56            assert!(container.try_release::<LockBit>());
57            break;
58        }
59
60        println!("data bits is zero, loop");
61
62        assert!(container.try_release::<LockBit>());
63    }
64    println!("{}", container.read::<DataBits>());
65    thread.join().unwrap();
66}
Source

pub fn try_release<B: BitFieldTrait<usize>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Examples found in repository?
examples/sample.rs (line 45)
7fn main() {
8    let mut storage;
9
10    storage = A::encode(true);
11    assert!(A::decode(storage));
12    storage = B::update(-1, storage);
13    assert_eq!(B::decode(storage), -1);
14    storage = B::update(2, storage);
15    assert_eq!(B::decode(storage), 2);
16    assert!(A::decode(storage));
17    assert_eq!(C::decode(storage), 0);
18    assert!(B::is_valid(7));
19    assert!(!B::is_valid(8));
20    assert!(B::is_valid(-8));
21    assert!(!B::is_valid(-9));
22
23    println!("Creating atomic bitfield storage");
24
25    type LockBit = BitField<usize, bool, 0, 1, false>;
26    type DataBits = BitField<usize, u32, { LockBit::NEXT_BIT }, 32, false>;
27
28    let container = std::sync::Arc::new(AtomicBitfieldContainer::new(0usize));
29    println!(
30        "lock bit: {}, data bits: {}",
31        container.read::<LockBit>(),
32        container.read::<DataBits>()
33    );
34    let thread = {
35        let container = container.clone();
36
37        std::thread::spawn(move || {
38            println!("thread 1 spawned");
39            while !container.try_acquire::<LockBit>() {}
40            container.update::<DataBits>(42);
41            println!(
42                "thread 1: set data bits to {}",
43                container.read::<DataBits>()
44            );
45            assert!(container.try_release::<LockBit>());
46        })
47    };
48
49    loop {
50        while !container.try_acquire::<LockBit>() {
51            std::thread::yield_now();
52        }
53        if container.read::<DataBits>() != 0 {
54            println!("data bits is not zero, break");
55            assert_eq!(container.read::<DataBits>(), 42);
56            assert!(container.try_release::<LockBit>());
57            break;
58        }
59
60        println!("data bits is zero, loop");
61
62        assert!(container.try_release::<LockBit>());
63    }
64    println!("{}", container.read::<DataBits>());
65    thread.join().unwrap();
66}
Source

pub fn update_bool<B: BitFieldTrait<usize>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Source§

impl AtomicBitfieldContainer<isize>

Source

pub fn fetch_or<B: BitFieldTrait<isize>>(&self, value: B::Type)

fetch_or operation for bitfield B

Source

pub fn fetch_and<B: BitFieldTrait<isize>>(&self, value: B::Type)

fetch_and operation for bitfield B

Source

pub fn fetch_xor<B: BitFieldTrait<isize>>(&self, value: B::Type)

fetch_xor operation for bitfield B

Source

pub fn try_acquire<B: BitFieldTrait<isize>>(&self) -> bool

Try to acquire bitfield B, can be used to implement atomic locks.

Source

pub fn try_release<B: BitFieldTrait<isize>>(&self) -> bool

Try to release bitfield B, can be used to implement atomic locks.

Source

pub fn update_bool<B: BitFieldTrait<isize>>(&self, value: bool, order: Ordering)
where B::Type: From<bool>,

Update storage container according to bitfield B based on boolean value, B::Type must implement From<bool>.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.