azure_core/sleep/
thread.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use futures::Future;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::task::{Context, Poll};
use std::thread;
use std::time::Duration;

/// Creates a future that resolves after a specified duration of time.
/// Uses a simple thread based implementation for sleep. A more efficient
/// implementation is available by using the `tokio-sleep` crate feature.
pub fn sleep(duration: Duration) -> Sleep {
    Sleep {
        signal: None,
        duration,
    }
}

#[derive(Debug)]
pub struct Sleep {
    signal: Option<Arc<AtomicBool>>,
    duration: Duration,
}

impl Future for Sleep {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if let Some(signal) = &self.signal {
            if signal.load(Ordering::Acquire) {
                Poll::Ready(())
            } else {
                Poll::Pending
            }
        } else {
            let signal = Arc::new(AtomicBool::new(false));
            let waker = cx.waker().clone();
            let duration = self.duration;
            self.get_mut().signal = Some(signal.clone());
            thread::spawn(move || {
                thread::sleep(duration);
                signal.store(true, Ordering::Release);
                waker.wake();
            });
            Poll::Pending
        }
    }
}