1use crate::{ok, AsHandleRef, Handle, HandleBased, HandleRef, Peered};
8
9#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
14#[repr(transparent)]
15pub struct EventPair(Handle);
16impl_handle_based!(EventPair);
17impl Peered for EventPair {}
18
19impl EventPair {
20 pub fn create() -> (Self, Self) {
29 let mut out0 = 0;
30 let mut out1 = 0;
31 let options = 0;
32 let status = unsafe { crate::sys::zx_eventpair_create(options, &mut out0, &mut out1) };
33 ok(status).expect(
34 "eventpair creation always succeeds except with OOM or when job policy denies it",
35 );
36 unsafe { (Self::from(Handle::from_raw(out0)), Self::from(Handle::from_raw(out1))) }
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43 use crate::{Duration, MonotonicInstant, Signals, Status};
44
45 #[test]
46 fn wait_and_signal_peer() {
47 let (p1, p2) = EventPair::create();
48 let eighty_ms = Duration::from_millis(80);
49
50 assert_eq!(
52 p2.wait_handle(Signals::USER_0, MonotonicInstant::after(eighty_ms)),
53 Err(Status::TIMED_OUT)
54 );
55
56 assert!(p1.signal_peer(Signals::NONE, Signals::USER_0).is_ok());
58 assert_eq!(
59 p2.wait_handle(Signals::USER_0, MonotonicInstant::after(eighty_ms)).unwrap(),
60 Signals::USER_0
61 );
62
63 assert_eq!(
65 p2.wait_handle(Signals::USER_0, MonotonicInstant::after(eighty_ms)).unwrap(),
66 Signals::USER_0
67 );
68
69 assert!(p1.signal_peer(Signals::USER_0, Signals::NONE).is_ok());
71 assert_eq!(
72 p2.wait_handle(Signals::USER_0, MonotonicInstant::after(eighty_ms)),
73 Err(Status::TIMED_OUT)
74 );
75 }
76}