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::sync::{Arc, Mutex};
15 use std::time::Duration;
16 
17 pub(crate) use crate::executor::driver_handle::Handle;
18 
19 cfg_time! {
20     use crate::time::TimeDriver;
21 }
22 
23 cfg_net! {
24     use crate::net::IoDriver;
25 }
26 
27 #[cfg(target_family = "unix")]
28 cfg_signal! {
29     use crate::signal::unix::SignalDriver;
30 }
31 
32 // Flag used to identify whether to park on condvar.
33 pub(crate) enum ParkFlag {
34     NotPark,
35     Park,
36     ParkTimeout(Duration),
37 }
38 
39 pub(crate) struct Driver {
40     #[cfg(feature = "net")]
41     io: IoDriver,
42     #[cfg(all(feature = "signal", target_family = "unix"))]
43     signal: SignalDriver,
44     #[cfg(feature = "time")]
45     time: Arc<TimeDriver>,
46 }
47 
48 impl Driver {
initialize() -> (Arc<Handle>, Arc<Mutex<Driver>>)49     pub(crate) fn initialize() -> (Arc<Handle>, Arc<Mutex<Driver>>) {
50         #[cfg(feature = "net")]
51         let (io_handle, io_driver) = IoDriver::initialize();
52         #[cfg(feature = "time")]
53         let (time_handle, time_driver) = TimeDriver::initialize();
54         let handle = Handle {
55             #[cfg(feature = "net")]
56             io: io_handle,
57             #[cfg(feature = "time")]
58             time: time_handle,
59         };
60         #[cfg(all(feature = "signal", target_family = "unix"))]
61         let signal_driver = SignalDriver::initialize(&handle);
62         let driver = Driver {
63             #[cfg(feature = "net")]
64             io: io_driver,
65             #[cfg(all(feature = "signal", target_family = "unix"))]
66             signal: signal_driver,
67             #[cfg(feature = "time")]
68             time: time_driver,
69         };
70         (Arc::new(handle), Arc::new(Mutex::new(driver)))
71     }
72 
run(&mut self) -> ParkFlag73     pub(crate) fn run(&mut self) -> ParkFlag {
74         let _duration: Option<Duration> = None;
75         #[cfg(feature = "time")]
76         let _duration = self.time.run();
77         #[cfg(feature = "net")]
78         self.io
79             .drive(_duration)
80             .unwrap_or_else(|e| panic!("io driver running failed, error: {e}"));
81         #[cfg(all(feature = "signal", target_family = "unix"))]
82         if self.io.process_signal() {
83             self.signal.broadcast();
84         }
85         #[cfg(all(target_os = "linux", feature = "process"))]
86         crate::process::GlobalZombieChild::get_instance().release_zombie();
87         if cfg!(feature = "net") {
88             ParkFlag::NotPark
89         } else {
90             match _duration {
91                 None => ParkFlag::Park,
92                 Some(duration) if duration.is_zero() => ParkFlag::NotPark,
93                 Some(duration) => ParkFlag::ParkTimeout(duration),
94             }
95         }
96     }
97 
run_once(&mut self)98     pub(crate) fn run_once(&mut self) {
99         #[cfg(feature = "time")]
100         self.time.run();
101         #[cfg(feature = "net")]
102         self.io
103             .drive(Some(Duration::from_millis(0)))
104             .unwrap_or_else(|e| panic!("io driver running failed, error: {e}"));
105         #[cfg(all(feature = "signal", target_family = "unix"))]
106         if self.io.process_signal() {
107             self.signal.broadcast();
108         }
109         #[cfg(all(target_os = "linux", feature = "process"))]
110         crate::process::GlobalZombieChild::get_instance().release_zombie();
111     }
112 }
113