]> git.proxmox.com Git - rustc.git/blob - src/vendor/fuchsia-zircon/src/eventpair.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / vendor / fuchsia-zircon / src / eventpair.rs
1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 //! Type-safe bindings for Zircon event pairs.
6
7 use {AsHandleRef, Cookied, HandleBased, Handle, HandleRef, Peered, Status};
8 use {sys, into_result};
9
10 /// An object representing a Zircon
11 /// [event pair](https://fuchsia.googlesource.com/zircon/+/master/docs/concepts.md#Other-IPC_Events_Event-Pairs_and-User-Signals).
12 ///
13 /// As essentially a subtype of `Handle`, it can be freely interconverted.
14 #[derive(Debug, Eq, PartialEq)]
15 pub struct EventPair(Handle);
16 impl_handle_based!(EventPair);
17 impl Peered for EventPair {}
18 impl Cookied for EventPair {}
19
20 impl EventPair {
21 /// Create an event pair, a pair of objects which can signal each other. Wraps the
22 /// [zx_eventpair_create](https://fuchsia.googlesource.com/zircon/+/master/docs/syscalls/eventpair_create.md)
23 /// syscall.
24 pub fn create(options: EventPairOpts) -> Result<(EventPair, EventPair), Status> {
25 let mut out0 = 0;
26 let mut out1 = 0;
27 let status = unsafe { sys::zx_eventpair_create(options as u32, &mut out0, &mut out1) };
28 into_result(status, ||
29 (Self::from(Handle(out0)),
30 Self::from(Handle(out1))))
31 }
32 }
33
34 /// Options for creating an event pair.
35 #[repr(u32)]
36 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
37 pub enum EventPairOpts {
38 /// Default options.
39 Default = 0,
40 }
41
42 impl Default for EventPairOpts {
43 fn default() -> Self {
44 EventPairOpts::Default
45 }
46 }
47
48 #[cfg(test)]
49 mod tests {
50 use super::*;
51 use {Duration, ZX_SIGNAL_LAST_HANDLE, ZX_SIGNAL_NONE, ZX_USER_SIGNAL_0};
52 use deadline_after;
53
54 #[test]
55 fn wait_and_signal_peer() {
56 let (p1, p2) = EventPair::create(EventPairOpts::Default).unwrap();
57 let eighty_ms: Duration = 80_000_000;
58
59 // Waiting on one without setting any signal should time out.
60 assert_eq!(p2.wait_handle(ZX_USER_SIGNAL_0, deadline_after(eighty_ms)), Err(Status::ErrTimedOut));
61
62 // If we set a signal, we should be able to wait for it.
63 assert!(p1.signal_peer(ZX_SIGNAL_NONE, ZX_USER_SIGNAL_0).is_ok());
64 assert_eq!(p2.wait_handle(ZX_USER_SIGNAL_0, deadline_after(eighty_ms)).unwrap(),
65 ZX_USER_SIGNAL_0 | ZX_SIGNAL_LAST_HANDLE);
66
67 // Should still work, signals aren't automatically cleared.
68 assert_eq!(p2.wait_handle(ZX_USER_SIGNAL_0, deadline_after(eighty_ms)).unwrap(),
69 ZX_USER_SIGNAL_0 | ZX_SIGNAL_LAST_HANDLE);
70
71 // Now clear it, and waiting should time out again.
72 assert!(p1.signal_peer(ZX_USER_SIGNAL_0, ZX_SIGNAL_NONE).is_ok());
73 assert_eq!(p2.wait_handle(ZX_USER_SIGNAL_0, deadline_after(eighty_ms)), Err(Status::ErrTimedOut));
74 }
75 }