Skip to main content

ntex_util/
task.rs

1//! A synchronization primitive for task wakeup.
2use std::{cell::Cell, fmt, marker::PhantomData, rc, task::Waker};
3
4/// A synchronization primitive for task wakeup.
5///
6/// Sometimes the task interested in a given event will change over time.
7/// An `LocalWaker` can coordinate concurrent notifications with the consumer
8/// potentially "updating" the underlying task to wake up. This is useful in
9/// scenarios where a computation completes in another task and wants to
10/// notify the consumer, but the consumer is in the process of being migrated to
11/// a new logical task.
12///
13/// Consumers should call `register` before checking the result of a computation
14/// and producers should call `wake` after producing the computation (this
15/// differs from the usual `thread::park` pattern). It is also permitted for
16/// `wake` to be called **before** `register`. This results in a no-op.
17///
18/// A single `LocalWaker` may be reused for any number of calls to `register` or
19/// `wake`.
20#[derive(Default)]
21pub struct LocalWaker {
22    waker: Cell<Option<Waker>>,
23    _t: PhantomData<rc::Rc<()>>,
24}
25
26impl LocalWaker {
27    /// Create an `LocalWaker`.
28    pub fn new() -> Self {
29        LocalWaker::with(None)
30    }
31
32    /// Create an `LocalWaker`.
33    pub fn with(waker: Option<Waker>) -> Self {
34        LocalWaker {
35            waker: Cell::new(waker),
36            _t: PhantomData,
37        }
38    }
39
40    #[inline]
41    /// Registers the waker to be notified on calls to `wake`.
42    ///
43    /// Returns `true` if waker was registered before.
44    pub fn register(&self, waker: &Waker) -> bool {
45        self.waker.replace(Some(waker.clone())).is_some()
46    }
47
48    #[inline]
49    /// Calls `wake` on the last `Waker` passed to `register`.
50    ///
51    /// If `register` has not been called yet, then this does nothing.
52    pub fn wake(&self) {
53        if let Some(waker) = self.take() {
54            waker.wake();
55        }
56    }
57
58    #[inline]
59    /// Calls `wake` on the last `Waker` passed to `register`.
60    ///
61    /// If `register` has not been called yet, then this returns `false`.
62    pub fn wake_checked(&self) -> bool {
63        if let Some(waker) = self.take() {
64            waker.wake();
65            true
66        } else {
67            false
68        }
69    }
70
71    /// Returns the last `Waker` passed to `register`, so that the user can wake it.
72    ///
73    /// If a waker has not been registered, this returns `None`.
74    pub fn take(&self) -> Option<Waker> {
75        self.waker.take()
76    }
77}
78
79impl Clone for LocalWaker {
80    fn clone(&self) -> Self {
81        LocalWaker::new()
82    }
83}
84
85impl fmt::Debug for LocalWaker {
86    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87        write!(f, "LocalWaker")
88    }
89}
90
91/// Yields execution back to the current runtime.
92pub async fn yield_to() {
93    use std::{future::Future, pin::Pin, task::Context, task::Poll};
94
95    struct Yield {
96        completed: bool,
97    }
98
99    impl Future for Yield {
100        type Output = ();
101
102        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
103            if self.completed {
104                return Poll::Ready(());
105            }
106
107            self.completed = true;
108            cx.waker().wake_by_ref();
109
110            Poll::Pending
111        }
112    }
113
114    Yield { completed: false }.await;
115}
116
117#[cfg(test)]
118mod test {
119    use super::*;
120
121    #[ntex::test]
122    async fn yield_test() {
123        yield_to().await;
124    }
125}