1 use tokio_executor::park::{Park, Unpark};
2 
3 use std::error::Error;
4 use std::fmt;
5 use std::time::Duration;
6 
7 use crossbeam_utils::sync::{Parker, Unparker};
8 
9 /// Parks the thread.
10 #[derive(Debug)]
11 pub struct DefaultPark {
12     inner: Parker,
13 }
14 
15 /// Unparks threads that were parked by `DefaultPark`.
16 #[derive(Debug)]
17 pub struct DefaultUnpark {
18     inner: Unparker,
19 }
20 
21 /// Error returned by [`ParkThread`]
22 ///
23 /// This currently is never returned, but might at some point in the future.
24 ///
25 /// [`ParkThread`]: struct.ParkThread.html
26 #[derive(Debug)]
27 pub struct ParkError {
28     _p: (),
29 }
30 
31 // ===== impl DefaultPark =====
32 
33 impl DefaultPark {
34     /// Creates a new `DefaultPark` instance.
new() -> DefaultPark35     pub fn new() -> DefaultPark {
36         DefaultPark {
37             inner: Parker::new(),
38         }
39     }
40 
41     /// Unpark the thread without having to clone the unpark handle.
42     ///
43     /// Named `notify` to avoid conflicting with the `unpark` fn.
notify(&self)44     pub(crate) fn notify(&self) {
45         self.inner.unparker().unpark();
46     }
47 
park_sync(&self, duration: Option<Duration>)48     pub(crate) fn park_sync(&self, duration: Option<Duration>) {
49         match duration {
50             None => self.inner.park(),
51             Some(duration) => self.inner.park_timeout(duration),
52         }
53     }
54 }
55 
56 impl Park for DefaultPark {
57     type Unpark = DefaultUnpark;
58     type Error = ParkError;
59 
unpark(&self) -> Self::Unpark60     fn unpark(&self) -> Self::Unpark {
61         DefaultUnpark {
62             inner: self.inner.unparker().clone(),
63         }
64     }
65 
park(&mut self) -> Result<(), Self::Error>66     fn park(&mut self) -> Result<(), Self::Error> {
67         self.inner.park();
68         Ok(())
69     }
70 
park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error>71     fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> {
72         self.inner.park_timeout(duration);
73         Ok(())
74     }
75 }
76 
77 // ===== impl DefaultUnpark =====
78 
79 impl Unpark for DefaultUnpark {
unpark(&self)80     fn unpark(&self) {
81         self.inner.unpark();
82     }
83 }
84 
85 // ===== impl ParkError =====
86 
87 impl fmt::Display for ParkError {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result88     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
89         self.description().fmt(fmt)
90     }
91 }
92 
93 impl Error for ParkError {
description(&self) -> &str94     fn description(&self) -> &str {
95         "unknown park error"
96     }
97 }
98