elast0ny / shared_memory

A Rust wrapper around native shared memory for Linux and Windows
382 stars 51 forks source link

Added support for serde serializable handles #39

Closed mitsuhiko closed 4 years ago

mitsuhiko commented 4 years ago

This changes the locking to work on &self instead of &mut self (Fixes #37) and adds serde serializable handles. These can be exchanged between two processes trivially.

This is useful with procspawn:

use procspawn::{self, spawn};
use shared_memory::{SharedMemCast, Handle};

#[derive(SharedMemCast)]
struct ShmemStructExample {
    value: u32,
}

fn main() {
    procspawn::init();

    let handle = Handle::new(ShmemStructExample { value: 10 }).unwrap();

    spawn(handle.clone(), |handle| {
        let mut state = handle.wlock().unwrap();
        state.value = 99;
    }).join().unwrap();

    let state = handle.rlock().unwrap();
    assert_eq!(state.value, 99);
}
mitsuhiko commented 4 years ago

Not sure yet about the lock situation. Right now it uses lock id 0 but I guess this could use per handle locks easily.

mitsuhiko commented 4 years ago

Force pushed to reflect changes from master.

elast0ny commented 4 years ago

There has been major changes to how the crate works and locks are now implemented in another crate.

Closing as this is probably irrelevant now.

Thanks !