crossbeam icon indicating copy to clipboard operation
crossbeam copied to clipboard

AtomicCell for struct's those size is not the power of two

Open feelingnothing opened this issue 2 years ago • 1 comments

Is it possible to use AtomicCell for struct where underlying atomic type is rounded up to the nearest power of two? For example this would use AtomicU32

struct Foo {
    foo: u16,
    bar: u8,
}

This would use AtomicU64

struct Foo {
    foo: u16,
    bar: u8,
    bar1: u8,
    bar2: u8,
}

I don't know the internal workings of AtomicCell, but I think if this is possible, the usage cases will increase

feelingnothing avatar May 17 '22 16:05 feelingnothing

You can use AtomicCell, but it won't be lock-free.

fprasx avatar Jul 05 '22 00:07 fprasx

One of the conditions for AtomicCell<T> to use the atomic type is that T has at least the same alignment as the size.

So you can use the repr attribute to adjust the alignment.

#[repr(align(4))]
struct Foo {
    foo: u16,
    bar: u8,
    _padding: u8,
}

taiki-e avatar Apr 28 '23 09:04 taiki-e