hawkw/mycelium

bitfield: some kind of `AtomicBitfield`?

hawkw opened this issue · 0 comments

hawkw commented

it would be cool to have a way to simplify storing mycelium-bitfield bitfield types in atomics.

i'm not sure whether the best design is something like this, where the T-typed bitfield types are treated as "snapshots" that can be loaded/stored in the atomic:

// `T` is a `mycelium-bitfield` generated type
pub struct AtomicBitfield<T> { 
  // ...
}

impl<T> AtomicBitfield<T> {
   pub fn load(&self, ordering: Ordering) -> T { 
       // ...
   }

   pub fn compare_exchange(&self, curr: T, next: T, success: Ordering, failure: Ordering) -> Result<T, T> {
      // ...
   }

   // and so on...
}  

or, something like this:

mycelium_bitfield::bitfield! {
   pub struct MyBitfield<AtomicUsize> {
      const FOO = 1;
      const BAR = 1;
      // ...
  }
}

and having it generate versions of the get and set methods that take &self when the bits type is an atomic.

I think the first one is actually better, as it allows more complicated CAS patterns that toggle multiple bits in one atomic op...