2018-04-05 10:57:05 -07:00
|
|
|
use task::Task;
|
|
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
2018-12-28 20:34:54 +01:00
|
|
|
use crossbeam_channel::{unbounded, Receiver, Sender};
|
2018-07-16 23:22:48 +02:00
|
|
|
|
2018-04-05 10:57:05 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) struct Queue {
|
2018-12-28 20:34:54 +01:00
|
|
|
// TODO(stjepang): Use a custom, faster MPMC queue implementation that supports `steal_many()`.
|
|
|
|
|
chan: (Sender<Arc<Task>>, Receiver<Arc<Task>>),
|
2018-04-05 10:57:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ===== impl Queue =====
|
|
|
|
|
|
|
|
|
|
impl Queue {
|
|
|
|
|
/// Create a new, empty, `Queue`.
|
|
|
|
|
pub fn new() -> Queue {
|
|
|
|
|
Queue {
|
2018-12-28 20:34:54 +01:00
|
|
|
chan: unbounded(),
|
2018-04-05 10:57:05 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Push a task onto the queue.
|
2018-12-28 20:34:54 +01:00
|
|
|
#[inline]
|
2018-04-05 10:57:05 -07:00
|
|
|
pub fn push(&self, task: Arc<Task>) {
|
2018-12-28 20:34:54 +01:00
|
|
|
self.chan.0.send(task).unwrap();
|
2018-04-05 10:57:05 -07:00
|
|
|
}
|
|
|
|
|
|
2018-12-28 20:34:54 +01:00
|
|
|
/// Pop a task from the queue.
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn pop(&self) -> Option<Arc<Task>> {
|
|
|
|
|
self.chan.1.try_recv().ok()
|
2018-09-21 19:20:41 +02:00
|
|
|
}
|
|
|
|
|
}
|