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 Magenta event pairs.
7 use {Cookied, HandleBase, Handle, HandleRef, Peered, Status}
;
8 use {sys, into_result}
;
10 /// An object representing a Magenta
11 /// [event pair](https://fuchsia.googlesource.com/magenta/+/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
);
17 impl HandleBase
for EventPair
{
18 fn get_ref(&self) -> HandleRef
{
22 fn from_handle(handle
: Handle
) -> Self {
27 impl Peered
for EventPair
{
30 impl Cookied
for EventPair
{
34 /// Create an event pair, a pair of objects which can signal each other. Wraps the
35 /// [mx_eventpair_create](https://fuchsia.googlesource.com/magenta/+/master/docs/syscalls/eventpair_create.md)
37 pub fn create(options
: EventPairOpts
) -> Result
<(EventPair
, EventPair
), Status
> {
40 let status
= unsafe { sys::mx_eventpair_create(options as u32, &mut out0, &mut out1) }
;
41 into_result(status
, ||
42 (Self::from_handle(Handle(out0
)),
43 Self::from_handle(Handle(out1
))))
47 /// Options for creating an event pair.
49 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
50 pub enum EventPairOpts
{
55 impl Default
for EventPairOpts
{
56 fn default() -> Self {
57 EventPairOpts
::Default
64 use {Duration, MX_SIGNAL_LAST_HANDLE, MX_SIGNAL_NONE, MX_USER_SIGNAL_0}
;
68 fn wait_and_signal_peer() {
69 let (p1
, p2
) = EventPair
::create(EventPairOpts
::Default
).unwrap();
70 let ten_ms
: Duration
= 10_000_000;
72 // Waiting on one without setting any signal should time out.
73 assert_eq
!(p2
.wait(MX_USER_SIGNAL_0
, deadline_after(ten_ms
)), Err(Status
::ErrTimedOut
));
75 // If we set a signal, we should be able to wait for it.
76 assert
!(p1
.signal_peer(MX_SIGNAL_NONE
, MX_USER_SIGNAL_0
).is_ok());
77 assert_eq
!(p2
.wait(MX_USER_SIGNAL_0
, deadline_after(ten_ms
)).unwrap(),
78 MX_USER_SIGNAL_0
| MX_SIGNAL_LAST_HANDLE
);
80 // Should still work, signals aren't automatically cleared.
81 assert_eq
!(p2
.wait(MX_USER_SIGNAL_0
, deadline_after(ten_ms
)).unwrap(),
82 MX_USER_SIGNAL_0
| MX_SIGNAL_LAST_HANDLE
);
84 // Now clear it, and waiting should time out again.
85 assert
!(p1
.signal_peer(MX_USER_SIGNAL_0
, MX_SIGNAL_NONE
).is_ok());
86 assert_eq
!(p2
.wait(MX_USER_SIGNAL_0
, deadline_after(ten_ms
)), Err(Status
::ErrTimedOut
));