• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         assert!(!href.is_attached(), "HandleRef is already attached.");
62         // Safety:
63         // `self` contains a properly initialized handle
64         // `href.inner` is a properly initialized handle_ref that is not attached
65         let ret = unsafe { handle_set_attach(self.0, href.as_mut_ptr()) };
66         Error::from_lk(ret)?;
67 
68         Ok(())
69     }
70 
handle_set_wait(&self, href: &mut HandleRef, timeout: Duration) -> Result<(), Error>71     pub fn handle_set_wait(&self, href: &mut HandleRef, timeout: Duration) -> Result<(), Error> {
72         let timeout = duration_as_ms(timeout)?;
73         // Safety:
74         // `self` contains a properly initialized handle
75         // `href` references a valid storage location for a handle_ref
76         let ret = unsafe { handle_set_wait(self.0, href.as_mut_ptr(), timeout) };
77         Error::from_lk(ret)
78     }
79 
handle_wait(&self, event_mask: &mut u32, timeout: Duration) -> Result<(), Error>80     pub fn handle_wait(&self, event_mask: &mut u32, timeout: Duration) -> Result<(), Error> {
81         let timeout = duration_as_ms(timeout)?;
82         // Safety:
83         // `self` contains a properly initialized handle
84         // `event_mask` references a valid storage location for a u32
85         let ret = unsafe { handle_wait(self.0, event_mask, timeout) };
86         Error::from_lk(ret)
87     }
88 }
89 
90 impl Drop for HandleSet {
drop(&mut self)91     fn drop(&mut self) {
92         // Safety:
93         // `handle_set_create` returned a valid handle that wasn't closed already.
94         unsafe { handle_close(self.0) }
95     }
96 }
97 
98 // Safety: the kernel synchronizes operations on handle sets so they can be passed
99 // from one thread to another
100 unsafe impl Send for HandleSet {}
101 
102 // Safety: the kernel synchronizes operations on handle sets so it is safe to share
103 // handle sets between threads
104 unsafe impl Sync for HandleSet {}
105