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.
5 //! Type-safe bindings for Zircon event pairs.
7 use {AsHandleRef, Cookied, HandleBased, Handle, HandleRef, Peered, Status}
;
8 use {sys, into_result}
;
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).
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 {}
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)
24 pub fn create(options
: EventPairOpts
) -> Result
<(EventPair
, EventPair
), Status
> {
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
))))
34 /// Options for creating an event pair.
36 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
37 pub enum EventPairOpts
{
42 impl Default
for EventPairOpts
{
43 fn default() -> Self {
44 EventPairOpts
::Default
51 use {Duration, ZX_SIGNAL_LAST_HANDLE, ZX_SIGNAL_NONE, ZX_USER_SIGNAL_0}
;
55 fn wait_and_signal_peer() {
56 let (p1
, p2
) = EventPair
::create(EventPairOpts
::Default
).unwrap();
57 let eighty_ms
: Duration
= 80_000_000;
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
));
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
);
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
);
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
));