rust/kernel/sync/atomic/predefine.rs

Source file repositories/reference/linux-study-clean/rust/kernel/sync/atomic/predefine.rs

File Facts

System
Linux kernel
Corpus path
rust/kernel/sync/atomic/predefine.rs
Extension
.rs
Size
10334 bytes
Lines
336
Domain
Rust Kernel Layer
Bucket
Rust API Membrane
Inferred role
Rust Kernel Layer: implementation source
Status
source implementation candidate

Why This File Exists

Rust-side wrappers and abstractions around kernel C APIs, ownership contracts, allocation, synchronization, and module integration.

Dependency Surface

Detected Declarations

Annotated Snippet

fn atomic_basic_tests() {
        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(v);

            assert_eq!(v, x.load(Relaxed));
        });

        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(v);
            let ptr = x.as_ptr();

            // SAFETY: `ptr` is a valid pointer and no concurrent access.
            assert_eq!(v, unsafe { atomic_load(ptr, Relaxed) });
        });
    }

    #[test]
    fn atomic_acquire_release_tests() {
        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(0);

            x.store(v, Release);
            assert_eq!(v, x.load(Acquire));
        });

        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(0);
            let ptr = x.as_ptr();

            // SAFETY: `ptr` is a valid pointer and no concurrent access.
            unsafe { atomic_store(ptr, v, Release) };

            // SAFETY: `ptr` is a valid pointer and no concurrent access.
            assert_eq!(v, unsafe { atomic_load(ptr, Acquire) });
        });
    }

    #[test]
    fn atomic_xchg_tests() {
        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(v);

            let old = v;
            let new = v + 1;

            assert_eq!(old, x.xchg(new, Full));
            assert_eq!(new, x.load(Relaxed));
        });

        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(v);
            let ptr = x.as_ptr();

            let old = v;
            let new = v + 1;

            // SAFETY: `ptr` is a valid pointer and no concurrent access.
            assert_eq!(old, unsafe { xchg(ptr, new, Full) });
            assert_eq!(new, x.load(Relaxed));
        });
    }

    #[test]
    fn atomic_cmpxchg_tests() {
        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(v);

            let old = v;
            let new = v + 1;

            assert_eq!(Err(old), x.cmpxchg(new, new, Full));
            assert_eq!(old, x.load(Relaxed));
            assert_eq!(Ok(old), x.cmpxchg(old, new, Relaxed));
            assert_eq!(new, x.load(Relaxed));
        });

        for_each_type!(42 in [i8, i16, i32, i64, u32, u64, isize, usize] |v| {
            let x = Atomic::new(v);
            let ptr = x.as_ptr();

            let old = v;
            let new = v + 1;

            // SAFETY: `ptr` is a valid pointer and no concurrent access.
            assert_eq!(Err(old), unsafe { cmpxchg(ptr, new, new, Full) });
            assert_eq!(old, x.load(Relaxed));
            // SAFETY: `ptr` is a valid pointer and no concurrent access.
            assert_eq!(Ok(old), unsafe { cmpxchg(ptr, old, new, Relaxed) });
            assert_eq!(new, x.load(Relaxed));
        });

Annotation

Implementation Notes