tokio_threadpool/park/
default_park.rs

1use tokio_executor::park::{Park, Unpark};
2
3use std::error::Error;
4use std::fmt;
5use std::time::Duration;
6
7use crossbeam_utils::sync::{Parker, Unparker};
8
9/// Parks the thread.
10#[derive(Debug)]
11pub struct DefaultPark {
12    inner: Parker,
13}
14
15/// Unparks threads that were parked by `DefaultPark`.
16#[derive(Debug)]
17pub 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)]
27pub struct ParkError {
28    _p: (),
29}
30
31// ===== impl DefaultPark =====
32
33impl DefaultPark {
34    /// Creates a new `DefaultPark` instance.
35    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.
44    pub(crate) fn notify(&self) {
45        self.inner.unparker().unpark();
46    }
47
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
56impl Park for DefaultPark {
57    type Unpark = DefaultUnpark;
58    type Error = ParkError;
59
60    fn unpark(&self) -> Self::Unpark {
61        DefaultUnpark {
62            inner: self.inner.unparker().clone(),
63        }
64    }
65
66    fn park(&mut self) -> Result<(), Self::Error> {
67        self.inner.park();
68        Ok(())
69    }
70
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
79impl Unpark for DefaultUnpark {
80    fn unpark(&self) {
81        self.inner.unpark();
82    }
83}
84
85// ===== impl ParkError =====
86
87impl fmt::Display for ParkError {
88    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
89        self.description().fmt(fmt)
90    }
91}
92
93impl Error for ParkError {
94    fn description(&self) -> &str {
95        "unknown park error"
96    }
97}