Files
tokio/tokio-threadpool/src/task/queue.rs
T

35 lines
739 B
Rust
Raw Normal View History

2018-04-05 10:57:05 -07:00
use task::Task;
use std::sync::Arc;
use crossbeam_channel::{unbounded, Receiver, Sender};
2018-04-05 10:57:05 -07:00
#[derive(Debug)]
pub(crate) struct Queue {
// 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 {
chan: unbounded(),
2018-04-05 10:57:05 -07:00
}
}
/// Push a task onto the queue.
#[inline]
2018-04-05 10:57:05 -07:00
pub fn push(&self, task: Arc<Task>) {
self.chan.0.send(task).unwrap();
2018-04-05 10:57:05 -07: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
}
}