Multiprocessing atomics as a spinlock in Rust?

Viewed 249

I would like to use a spinlock in my code which will be used by different processes. Since Rust has atomics for multithreading with swap operations that could be useful for spinlocks, I would like to know:

Can I use atomics from Rust in a shared memory between processes while keeping the provided safety guarantees?

First I need to know how to create an instance inside shared memory, but most important: Is it possible to do something like this to reuse an atomic in a second process?

use std::sync::atomic::{AtomicU8, Ordering};

fn use_atomic(ptr_value: u64) {
    // given: some memory pointer
    // e.g. ptr_value == 0xDEADBEEFu64
    let atomic = unsafe { &*(ptr_value as *mut AtomicU8) };
    let _old_value = atomic.swap(1, Ordering::Relaxed);
}

In case this is a bad idea: is there a better way to do this in Rust? (I'm not used to assembly, but maybe there is some finished code I missed)

0 Answers
Related