1 /*
2 * Copyright (c) 2024 Google Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 use core::time::Duration;
25
26 use crate::Error;
27 use crate::INFINITE_TIME;
28
29 pub use crate::sys::handle_set_attach;
30 pub use crate::sys::handle_set_create;
31 pub use crate::sys::handle_set_detach_ref;
32 pub use crate::sys::handle_set_wait;
33
34 use crate::sys::handle;
35 use crate::sys::handle_close;
36 use crate::sys::handle_wait;
37
38 use crate::handle::HandleRef;
39
40 pub struct HandleSet(*mut handle);
41
duration_as_ms(dur: Duration) -> Result<u32, Error>42 fn duration_as_ms(dur: Duration) -> Result<u32, Error> {
43 match dur {
44 Duration::MAX => Ok(INFINITE_TIME),
45 dur => dur.as_millis().try_into().map_err(|_| Error::ERR_OUT_OF_RANGE),
46 }
47 }
48
49 #[allow(clippy::new_without_default)]
50 impl HandleSet {
new() -> Self51 pub fn new() -> Self {
52 // Safety: `handle_set_create` places no preconditions on callers.
53 let handle = unsafe { handle_set_create() };
54 if handle.is_null() {
55 panic!("handle_set_create failed.");
56 }
57 Self(handle)
58 }
59
attach(&self, href: &mut HandleRef) -> Result<(), Error>60 pub fn attach(&self, href: &mut HandleRef) -> Result<(), Error> {
61 if href.attached {
62 panic!("HandleRef is already attached.");
63 }
64 // Safety:
65 // `self` contains a properly initialized handle
66 // `href.inner` is a properly initialized handle_ref that is not attached
67 let ret = unsafe { handle_set_attach(self.0, href.as_mut_ptr()) };
68 Error::from_lk(ret)?;
69
70 href.attached = true;
71 Ok(())
72 }
73
handle_set_wait(&self, href: &mut HandleRef, timeout: Duration) -> Result<(), Error>74 pub fn handle_set_wait(&self, href: &mut HandleRef, timeout: Duration) -> Result<(), Error> {
75 let timeout = duration_as_ms(timeout)?;
76 // Safety:
77 // `self` contains a properly initialized handle
78 // `href` references a valid storage location for a handle_ref
79 let ret = unsafe { handle_set_wait(self.0, href.as_mut_ptr(), timeout) };
80 Error::from_lk(ret)
81 }
82
handle_wait(&self, event_mask: &mut u32, timeout: Duration) -> Result<(), Error>83 pub fn handle_wait(&self, event_mask: &mut u32, timeout: Duration) -> Result<(), Error> {
84 let timeout = duration_as_ms(timeout)?;
85 // Safety:
86 // `self` contains a properly initialized handle
87 // `event_mask` references a valid storage location for a u32
88 let ret = unsafe { handle_wait(self.0, event_mask, timeout) };
89 Error::from_lk(ret)
90 }
91 }
92
93 impl Drop for HandleSet {
drop(&mut self)94 fn drop(&mut self) {
95 // Safety:
96 // `handle_set_create` returned a valid handle that wasn't closed already.
97 unsafe { handle_close(self.0) }
98 }
99 }
100
101 // Safety: the kernel synchronizes operations on handle sets so they can be passed
102 // from one thread to another
103 unsafe impl Send for HandleSet {}
104
105 // Safety: the kernel synchronizes operations on handle sets so it is safe to share
106 // handle sets between threads
107 unsafe impl Sync for HandleSet {}
108