Re: [PATCH v6 6/9] rust: sync: atomic: Add the framework of arithmetic operations

Boqun Feng
2025-07-11T21:22:17Z

On Fri, Jul 11, 2025 at 11:03:24PM +0200, Benno Lossin wrote:
[...]
> > Actually at the beginning, I missed the round-trip transmutablity
> > (thanks to you and Gary for bring that up), that's only safe requirement
> > I thought I needed ;-)
>
> So technically we only need round-trip transmutablity & same alignment
> (as size is implied as shown above), but I think it's much more
> understandable if we keep it.
>

Agreed.

[...]
> >
> > Me too! Let's use it then. Combining with your `AtomicAdd<Rhs>`
> > proposal:
> >
> > /// # Safety
> > ///
> > /// Adding any:
> > /// - one being the value of [`Self::Repr`] obtained through transmuting value of type `Self`,
> > /// - the other being the value of [`Self::Delta`] obtained through conversion of `rhs_into_delta()`,
> > /// must yield a value with a bit pattern also valid for `Self`.
>
> I think this will render wrongly in markdown & we shouldn't use a list,
> so how about:
>
> /// Adding any value of type [`Self::Delta`] obtained by [`Self::rhs_into_delta`] to any value of
> /// type [`Self::Repr`] obtained through transmuting a value of type `Self` to must yield a value
> /// with a bit pattern also valid for `Self`.
>

Looks good to me.

> My only gripe with this is that "Adding" isn't really well-defined...
>

I think it's good enough, and I created a GitHub tracking a few things
we decide to postpone:

https://github.com/Rust-for-Linux/linux/issues/1180

> > pub unsafe trait AtomicAdd<Rhs>: AllowAtomic {
> > type Delta = Self::Repr;
> > fn rhs_into_delta(rhs: Rhs) -> Delta;
> > }
> >
> > Note that I have to provide a `Delta` (or better named as `ReprDelta`?)
> > because of when pointer support is added, atomic addition is between
> > a `*mut ()` and a `isize`, not two `*mut()`.
>
> Makes sense, but we don't have default associated types yet :(
>

Oops, we are lucky enough to only have a few types to begin with ;-)
Maybe we can use `#[derive(AtomicAdd)] to select the default delta type
later.

Regards,
Boqun