Line data Source code
1 : use std::{collections::VecDeque, sync::Arc};
2 :
3 : use parking_lot::{Mutex, MutexGuard};
4 :
5 : use crate::executor::{self, PollSome, Waker};
6 :
7 : /// FIFO channel with blocking send and receive. Can be cloned and shared between threads.
8 : /// Blocking functions should be used only from threads that are managed by the executor.
9 : pub struct Chan<T> {
10 : shared: Arc<State<T>>,
11 : }
12 :
13 : impl<T> Clone for Chan<T> {
14 4851697 : fn clone(&self) -> Self {
15 4851697 : Chan {
16 4851697 : shared: self.shared.clone(),
17 4851697 : }
18 4851697 : }
19 : }
20 :
21 : impl<T> Default for Chan<T> {
22 0 : fn default() -> Self {
23 0 : Self::new()
24 0 : }
25 : }
26 :
27 : impl<T> Chan<T> {
28 632832 : pub fn new() -> Chan<T> {
29 632832 : Chan {
30 632832 : shared: Arc::new(State {
31 632832 : queue: Mutex::new(VecDeque::new()),
32 632832 : waker: Waker::new(),
33 632832 : }),
34 632832 : }
35 632832 : }
36 :
37 : /// Get a message from the front of the queue, block if the queue is empty.
38 : /// If not called from the executor thread, it can block forever.
39 4173 : pub fn recv(&self) -> T {
40 4173 : self.shared.recv()
41 4173 : }
42 :
43 : /// Panic if the queue is empty.
44 517717 : pub fn must_recv(&self) -> T {
45 517717 : self.shared
46 517717 : .try_recv()
47 517717 : .expect("message should've been ready")
48 517717 : }
49 :
50 : /// Get a message from the front of the queue, return None if the queue is empty.
51 : /// Never blocks.
52 454449 : pub fn try_recv(&self) -> Option<T> {
53 454449 : self.shared.try_recv()
54 454449 : }
55 :
56 : /// Send a message to the back of the queue.
57 947620 : pub fn send(&self, t: T) {
58 947620 : self.shared.send(t);
59 947620 : }
60 : }
61 :
62 : struct State<T> {
63 : queue: Mutex<VecDeque<T>>,
64 : waker: Waker,
65 : }
66 :
67 : impl<T> State<T> {
68 947620 : fn send(&self, t: T) {
69 947620 : self.queue.lock().push_back(t);
70 947620 : self.waker.wake_all();
71 947620 : }
72 :
73 972166 : fn try_recv(&self) -> Option<T> {
74 972166 : let mut q = self.queue.lock();
75 972166 : q.pop_front()
76 972166 : }
77 :
78 4173 : fn recv(&self) -> T {
79 4173 : // interrupt the receiver to prevent consuming everything at once
80 4173 : executor::yield_me(0);
81 4173 :
82 4173 : let mut queue = self.queue.lock();
83 4173 : if let Some(t) = queue.pop_front() {
84 0 : return t;
85 4173 : }
86 : loop {
87 10213 : self.waker.wake_me_later();
88 10213 : if let Some(t) = queue.pop_front() {
89 3655 : return t;
90 6040 : }
91 6040 : MutexGuard::unlocked(&mut queue, || {
92 6040 : executor::yield_me(-1);
93 6040 : });
94 : }
95 3655 : }
96 : }
97 :
98 : impl<T> PollSome for Chan<T> {
99 : /// Schedules a wakeup for the current thread.
100 6074198 : fn wake_me(&self) {
101 6074198 : self.shared.waker.wake_me_later();
102 6074198 : }
103 :
104 : /// Checks if chan has any pending messages.
105 4895078 : fn has_some(&self) -> bool {
106 4895078 : !self.shared.queue.lock().is_empty()
107 4895078 : }
108 : }
|