Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rust: sync: atomic: Prepare AtomicOps macros for i8/i16 support

Rework the internal AtomicOps macro plumbing to generate per-type
implementations from a mapping list.

Capture the trait definition once and reuse it for both declaration
and per-type impl expansion to reduce duplication and keep future
extensions simple.

This is a preparatory refactor for enabling i8/i16 atomics cleanly.

Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com>
Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
Link: https://patch.msgid.link/20251228120546.1602275-2-fujita.tomonori@gmail.com

authored by

FUJITA Tomonori and committed by
Boqun Feng
2bb8c41e 8de731a6

+66 -19
+66 -19
rust/kernel/sync/atomic/internal.rs
··· 156 156 } 157 157 } 158 158 159 - // Delcares $ops trait with methods and implements the trait for `i32` and `i64`. 160 - macro_rules! declare_and_impl_atomic_methods { 161 - ($(#[$attr:meta])* $pub:vis trait $ops:ident { 162 - $( 163 - $(#[doc=$doc:expr])* 164 - fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? { 165 - $unsafe:tt { bindings::#call($($arg:tt)*) } 166 - } 167 - )* 168 - }) => { 159 + macro_rules! declare_atomic_ops_trait { 160 + ( 161 + $(#[$attr:meta])* $pub:vis trait $ops:ident { 162 + $( 163 + $(#[doc=$doc:expr])* 164 + fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? { 165 + $unsafe:tt { bindings::#call($($arg:tt)*) } 166 + } 167 + )* 168 + } 169 + ) => { 169 170 $(#[$attr])* 170 171 $pub trait $ops: AtomicImpl { 171 172 $( ··· 176 175 ); 177 176 )* 178 177 } 178 + } 179 + } 179 180 180 - impl $ops for i32 { 181 + macro_rules! impl_atomic_ops_for_one { 182 + ( 183 + $ty:ty => $ctype:ident, 184 + $(#[$attr:meta])* $pub:vis trait $ops:ident { 181 185 $( 182 - impl_atomic_method!( 183 - (atomic) $func[$($variant)*]($($arg_sig)*) $(-> $ret)? { 184 - $unsafe { call($($arg)*) } 185 - } 186 - ); 186 + $(#[doc=$doc:expr])* 187 + fn $func:ident [$($variant:ident),*]($($arg_sig:tt)*) $( -> $ret:ty)? { 188 + $unsafe:tt { bindings::#call($($arg:tt)*) } 189 + } 187 190 )* 188 191 } 189 - 190 - impl $ops for i64 { 192 + ) => { 193 + impl $ops for $ty { 191 194 $( 192 195 impl_atomic_method!( 193 - (atomic64) $func[$($variant)*]($($arg_sig)*) $(-> $ret)? { 196 + ($ctype) $func[$($variant)*]($($arg_sig)*) $(-> $ret)? { 194 197 $unsafe { call($($arg)*) } 195 198 } 196 199 ); ··· 203 198 } 204 199 } 205 200 201 + // Declares $ops trait with methods and implements the trait. 202 + macro_rules! declare_and_impl_atomic_methods { 203 + ( 204 + [ $($map:tt)* ] 205 + $(#[$attr:meta])* $pub:vis trait $ops:ident { $($body:tt)* } 206 + ) => { 207 + declare_and_impl_atomic_methods!( 208 + @with_ops_def 209 + [ $($map)* ] 210 + ( $(#[$attr])* $pub trait $ops { $($body)* } ) 211 + ); 212 + }; 213 + 214 + (@with_ops_def [ $($map:tt)* ] ( $($ops_def:tt)* )) => { 215 + declare_atomic_ops_trait!( $($ops_def)* ); 216 + 217 + declare_and_impl_atomic_methods!( 218 + @munch 219 + [ $($map)* ] 220 + ( $($ops_def)* ) 221 + ); 222 + }; 223 + 224 + (@munch [] ( $($ops_def:tt)* )) => {}; 225 + 226 + (@munch [ $ty:ty => $ctype:ident $(, $($rest:tt)*)? ] ( $($ops_def:tt)* )) => { 227 + impl_atomic_ops_for_one!( 228 + $ty => $ctype, 229 + $($ops_def)* 230 + ); 231 + 232 + declare_and_impl_atomic_methods!( 233 + @munch 234 + [ $($($rest)*)? ] 235 + ( $($ops_def)* ) 236 + ); 237 + }; 238 + } 239 + 206 240 declare_and_impl_atomic_methods!( 241 + [ i32 => atomic, i64 => atomic64 ] 207 242 /// Basic atomic operations 208 243 pub trait AtomicBasicOps { 209 244 /// Atomic read (load). ··· 261 216 ); 262 217 263 218 declare_and_impl_atomic_methods!( 219 + [ i32 => atomic, i64 => atomic64 ] 264 220 /// Exchange and compare-and-exchange atomic operations 265 221 pub trait AtomicExchangeOps { 266 222 /// Atomic exchange. ··· 289 243 ); 290 244 291 245 declare_and_impl_atomic_methods!( 246 + [ i32 => atomic, i64 => atomic64 ] 292 247 /// Atomic arithmetic operations 293 248 pub trait AtomicArithmeticOps { 294 249 /// Atomic add (wrapping).