virtual_mio/
waker.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![allow(dead_code, unused)]
use std::{
    sync::{Arc, Condvar, Mutex},
    task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
};

use futures::Future;

pub struct InlineWaker {
    lock: Mutex<()>,
    condvar: Condvar,
}
impl InlineWaker {
    pub fn new() -> Arc<Self> {
        Arc::new(Self {
            lock: Mutex::new(()),
            condvar: Condvar::new(),
        })
    }

    fn wake_now(&self) {
        // Note: This guard should be there to prevent race conditions however in the
        // browser it causes a lock up - some strange browser issue. What I suspect
        // is that the Condvar::wait call is not releasing the mutex lock
        #[cfg(not(feature = "js"))]
        let _guard = self.lock.lock().unwrap();

        self.condvar.notify_all();
    }

    pub fn as_waker(self: &Arc<Self>) -> Waker {
        let s: *const Self = Arc::into_raw(Arc::clone(self));
        let raw_waker = RawWaker::new(s as *const (), &VTABLE);
        unsafe { Waker::from_raw(raw_waker) }
    }

    #[cfg(not(feature = "js"))]
    pub fn block_on<'a, A>(task: impl Future<Output = A> + 'a) -> A {
        futures::executor::block_on(task)
    }

    #[cfg(feature = "js")]
    pub fn block_on<'a, A>(task: impl Future<Output = A> + 'a) -> A {
        // Create the waker
        let inline_waker = Self::new();
        let waker = inline_waker.as_waker();
        let mut cx = Context::from_waker(&waker);

        // We loop waiting for the waker to be woken, then we poll again
        let mut task = Box::pin(task);
        loop {
            let lock = inline_waker.lock.lock().unwrap();
            match task.as_mut().poll(&mut cx) {
                Poll::Pending => {
                    inline_waker.condvar.wait(lock).ok();
                }
                Poll::Ready(ret) => {
                    return ret;
                }
            }
        }
    }
}

fn inline_waker_wake(s: &InlineWaker) {
    let waker_arc = unsafe { Arc::from_raw(s) };
    waker_arc.wake_now();
}

fn inline_waker_clone(s: &InlineWaker) -> RawWaker {
    let arc = unsafe { Arc::from_raw(s) };
    std::mem::forget(arc.clone());
    RawWaker::new(Arc::into_raw(arc) as *const (), &VTABLE)
}

const VTABLE: RawWakerVTable = unsafe {
    RawWakerVTable::new(
        |s| inline_waker_clone(&*(s as *const InlineWaker)), // clone
        |s| inline_waker_wake(&*(s as *const InlineWaker)),  // wake
        |s| (*(s as *const InlineWaker)).wake_now(), // wake by ref (don't decrease refcount)
        |s| drop(Arc::from_raw(s as *const InlineWaker)), // decrease refcount
    )
};