forked from elast0ny/shared_memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.rs
36 lines (31 loc) · 1.27 KB
/
event.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use raw_sync::{events::*, Timeout};
use shared_memory::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init();
// Attempt to create a mapping or open if it already exists
println!("Getting the shared memory mapping");
let shmem = match ShmemConf::new().size(4096).flink("event_mapping").create() {
Ok(m) => m,
Err(ShmemError::LinkExists) => ShmemConf::new().flink("event_mapping").open()?,
Err(e) => return Err(Box::new(e)),
};
if shmem.is_owner() {
//Create an event in the shared memory
println!("Creating event in shared memory");
let (evt, used_bytes) = unsafe { Event::new(shmem.as_ptr(), true)? };
println!("\tUsed {used_bytes} bytes");
println!("Launch another instance of this example to signal the event !");
evt.wait(Timeout::Infinite)?;
println!("\tGot signal !");
} else {
// Open existing event
println!("Openning event from shared memory");
let (evt, used_bytes) = unsafe { Event::from_existing(shmem.as_ptr())? };
println!("\tEvent uses {used_bytes} bytes");
println!("Signaling event !");
evt.set(EventState::Signaled)?;
println!("\tSignaled !");
}
println!("Done !");
Ok(())
}