1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::io;
15 use std::time::Duration;
16 
17 use crate::{Events, Interest, Selector, Source, Token};
18 
19 /// Registers and deregisters an io's fd
20 pub struct Poll {
21     selector: Selector,
22 }
23 
24 impl Poll {
25     /// Creates a new Poll.
new() -> io::Result<Poll>26     pub fn new() -> io::Result<Poll> {
27         Selector::new().map(|selector| Poll {
28             selector: { selector },
29         })
30     }
31 
32     /// Gets all interested io events within a time limit and writes them into
33     /// `event`.
34     ///
35     /// If `timeout` is none, then it will block the current thread until an
36     /// event arrives.
poll(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()>37     pub fn poll(&self, events: &mut Events, timeout: Option<Duration>) -> io::Result<()> {
38         self.selector.select(events, timeout)
39     }
40 
41     /// Registers the I/O source's fd in order to monitor its io events.
register<S>(&self, source: &mut S, token: Token, interests: Interest) -> io::Result<()> where S: Source + ?Sized,42     pub fn register<S>(&self, source: &mut S, token: Token, interests: Interest) -> io::Result<()>
43     where
44         S: Source + ?Sized,
45     {
46         source.register(&self.selector, token, interests)
47     }
48 
49     /// De-registers the I/O source's fd so the Poll will no longer monitor its
50     /// io events.
deregister<S>(&self, source: &mut S) -> io::Result<()> where S: Source + ?Sized,51     pub fn deregister<S>(&self, source: &mut S) -> io::Result<()>
52     where
53         S: Source + ?Sized,
54     {
55         source.deregister(&self.selector)
56     }
57 
58     /// Returns the selector of the `Poll`
selector(&self) -> &Selector59     pub fn selector(&self) -> &Selector {
60         &self.selector
61     }
62 }
63 
64 impl std::fmt::Debug for Poll {
fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result65     fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66         write!(fmt, "({:?})", self.selector)
67     }
68 }
69 
70 #[cfg(test)]
71 mod test {
72     /// UT cases for debug info of poll
73     ///
74     /// # Brief
75     /// 1. Create a Poll
76     /// 2. Check its fmt debug info
77     #[test]
78     #[cfg(target_os = "linux")]
ut_poll_debug_info()79     fn ut_poll_debug_info() {
80         use crate::Poll;
81         let poll = Poll::new().unwrap();
82         let fmt = format!("{:?}", poll);
83         assert!(fmt.contains("epoll fd:"));
84         assert!(fmt.contains("Select id:"));
85     }
86 }
87