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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! Task queue
//!
//! A global task queue for buildnig task executor
//!
//! any task in the queue can be polled from multiple thread

mod deque;
mod local;
mod poll_fn;
mod shared;

use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::Poll;

use crate::poll_fn::poll_fn;

/// Push a task
pub fn push(task: impl Future<Output = ()> + Send + 'static) {
    shared::push(task);
}

/// Push a local task
pub fn push_local(task: impl Future<Output = ()> + 'static) {
    local::push(task);
}

/// Poller
pub struct Poller<'a> {
    local: local::Poller<'a>,
    shared: shared::Poller<'a>,

    // !Send + !Sync
    _marker: PhantomData<*mut ()>,
}

/// Get [`Poller`] for polling task
///
/// [`Poller`]: struct.Poller.html
pub fn poller<'a>() -> Poller<'a> {
    Poller {
        local: local::poller(),
        shared: shared::poller(),
        _marker: PhantomData,
    }
}

impl<'a> Poller<'a> {
    /// Poll one task
    ///
    /// return false if no more task to be polled
    ///
    /// note that, when this function return false, it doesn't mean that is the queue is empty,
    /// some task maybe is in pending state
    ///
    /// use [`wait`] to check if the queue is already empty
    ///
    /// [`wait`]: struct.Poller.html#method.wait
    #[inline(always)]
    pub fn poll_one(&self) -> bool {
        if self.local.poll_one() {
            return true;
        }

        if self.shared.poll_one() {
            return true;
        }

        false
    }

    /// Wait
    ///
    /// return true if there is a task to be polled, or false if the queue becomes empty
    #[inline(always)]
    pub async fn wait(&self) -> bool {
        let mut local_wait = Some(self.local.wait());
        let mut shared_wait = Some(self.shared.wait());
        poll_fn(move |cx| {
            assert!(
                local_wait.is_some() || shared_wait.is_some(),
                "calling poll when future is already done"
            );

            if local_wait.is_some() {
                let f = local_wait.as_mut().unwrap();
                match unsafe { Pin::new_unchecked(f) }.poll(cx) {
                    Poll::Ready(ok) => {
                        local_wait.take();
                        if ok {
                            shared_wait.take();
                            return Poll::Ready(true);
                        }
                    }
                    Poll::Pending => {}
                }
            }

            if shared_wait.is_some() {
                let f = shared_wait.as_mut().unwrap();
                match unsafe { Pin::new_unchecked(f) }.poll(cx) {
                    Poll::Ready(ok) => {
                        shared_wait.take();
                        if ok {
                            local_wait.take();
                            return Poll::Ready(true);
                        }
                    }
                    Poll::Pending => {}
                }
            }

            if local_wait.is_some() || shared_wait.is_some() {
                Poll::Pending
            } else {
                Poll::Ready(false)
            }
        })
        .await
    }
}