Skip to contentSkip to conversation

rust: atomic: add fetch_sub

PATCH Superseded Andreas Hindborg 3 participants 4 messages Jan 28

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH 1/1 rust: atomic: add fetch_sub

Add Atomic::fetch_sub with implementation and documentation in line with existing Atomic::fetch_add implementation.

---
 rust/kernel/sync/atomic.rs          | 45 +++++++++++++++++++++++++++++++++++++
 rust/kernel/sync/atomic/internal.rs |  5 +++++
 2 files changed, 50 insertions(+)
rust/kernel/sync/atomic.rs +45 -0
index 4aebeacb961a2..aa0b672cef848 100644
@@ -559,4 +559,49 @@ pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering)
         // SAFETY: `ret` comes from reading `self.0`, which is a valid `T` per type invariants.
         unsafe { from_repr(ret) }
     }
+
+    /// Atomic fetch and subtract.
+    ///
+    /// Atomically updates `*self` to `(*self).wrapping_sub(v)`, and returns the value of `*self`
+    /// before the update.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use kernel::sync::atomic::{Atomic, Acquire, Full, Relaxed};
+    ///
+    /// let x = Atomic::new(42);
+    ///
+    /// assert_eq!(42, x.load(Relaxed));
+    ///
+    /// assert_eq!(30, { x.fetch_sub(12, Acquire); x.load(Relaxed) });
+    ///
+    /// let x = Atomic::new(42);
+    ///
+    /// assert_eq!(42, x.load(Relaxed));
+    ///
+    /// assert_eq!(30, { x.fetch_sub(12, Full); x.load(Relaxed) });
+    /// ```
+    #[inline(always)]
+    pub fn fetch_sub<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) -> T
+    where
+        // Types that support addition also support subtraction.
+        T: AtomicAdd<Rhs>,
+    {
+        let v = T::rhs_into_delta(v);
+
+        // INVARIANT: `self.0` is a valid `T` after `atomic_fetch_sub*()` due to safety requirement
+        // of `AtomicAdd`.
+        let ret = {
+            match Ordering::TYPE {
+                OrderingType::Full => T::Repr::atomic_fetch_sub(&self.0, v),
+                OrderingType::Acquire => T::Repr::atomic_fetch_sub_acquire(&self.0, v),
+                OrderingType::Release => T::Repr::atomic_fetch_sub_release(&self.0, v),
+                OrderingType::Relaxed => T::Repr::atomic_fetch_sub_relaxed(&self.0, v),
+            }
+        };
+
+        // SAFETY: `ret` comes from reading `self.0`, which is a valid `T` per type invariants.
+        unsafe { from_repr(ret) }
+    }
 }
rust/kernel/sync/atomic/internal.rs +5 -0
index 6fdd8e59f45be..7eed8c66efff8 100644
@@ -261,5 +261,10 @@ fn fetch_add[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) ->
             // SAFETY: `a.as_ptr()` is valid and properly aligned.
             unsafe { bindings::#call(v, a.as_ptr().cast()) }
         }
+
+        fn fetch_sub[acquire, release, relaxed](a: &AtomicRepr<Self>, v: Self::Delta) -> Self {
+            // SAFETY: `a.as_ptr()` is valid and properly aligned.
+            unsafe { bindings::#call(v, a.as_ptr().cast()) }
+        }
     }
 );

---
base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377
change-id: 20260128-atomic-sub-2ff15962df12

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>
maintainer Reviewed-by on v1 1/1
#1 lore raw

rust/kernel/sync/atomic.rs pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) · line 577

Quoted 10 lines · Andreas Hindborg

Add Atomic::fetch_sub with implementation and documentation in line with existing Atomic::fetch_add implementation.

rust/kernel/sync/atomic.rs +5 -0
+    /// let x = Atomic::new(42);
+    ///
+    /// assert_eq!(42, x.load(Relaxed));
+    ///
+    /// assert_eq!(30, { x.fetch_sub(12, Acquire); x.load(Relaxed) });

These asserts are a bit confusing to read. After all, the fetch_sub call also returns a value, so how about this?

let x = Atomic::new(42);
assert_eq!(42, x.load(Relaxed));
assert_eq!(42, x.fetch_sub(12, Acquire));
assert_eq!(30, x.load(Relaxed));

Otherwise LGTM.

Alice

author reviewer
#2 lore raw
Quoted 12 lines · Alice Ryhl

rust/kernel/sync/atomic.rs pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) · line 577

Quoted 10 lines · Andreas Hindborg

Add Atomic::fetch_sub with implementation and documentation in line with existing Atomic::fetch_add implementation.

rust/kernel/sync/atomic.rs +5 -0
+    /// let x = Atomic::new(42);
+    ///
+    /// assert_eq!(42, x.load(Relaxed));
+    ///
+    /// assert_eq!(30, { x.fetch_sub(12, Acquire); x.load(Relaxed) });
Quoted 7 lines

These asserts are a bit confusing to read. After all, the fetch_sub call also returns a value, so how about this?

let x = Atomic::new(42);
assert_eq!(42, x.load(Relaxed));
assert_eq!(42, x.fetch_sub(12, Acquire));
assert_eq!(30, x.load(Relaxed));

We can do that. I basically copied the docs from add. Should I change those as well?

Best regards,
Andreas Hindborg

maintainer
#3 lore raw
Quoted 17 lines · Andreas Hindborg
Quoted 15 lines · Alice Ryhl

rust/kernel/sync/atomic.rs pub fn fetch_add<Rhs, Ordering: ordering::Ordering>(&self, v: Rhs, _: Ordering) · line 577

Quoted 10 lines · Andreas Hindborg

Add Atomic::fetch_sub with implementation and documentation in line with existing Atomic::fetch_add implementation.

rust/kernel/sync/atomic.rs +5 -0
+    /// let x = Atomic::new(42);
+    ///
+    /// assert_eq!(42, x.load(Relaxed));
+    ///
+    /// assert_eq!(30, { x.fetch_sub(12, Acquire); x.load(Relaxed) });

These asserts are a bit confusing to read. After all, the fetch_sub call also returns a value, so how about this?

Quoted 7 lines
Quoted 4 lines

let x = Atomic::new(42);
assert_eq!(42, x.load(Relaxed));
assert_eq!(42, x.fetch_sub(12, Acquire));
assert_eq!(30, x.load(Relaxed));

We can do that. I basically copied the docs from add. Should I change those as well?

Please do. I also find that the existing example has too many line breaks :) Something compact as what Alice suggested would be nice.

Best,
Gary

Quoted 3 lines

Best regards,
Andreas Hindborg