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 2483999 : fn clone(&self) -> Self {
15 2483999 : Chan {
16 2483999 : shared: self.shared.clone(),
17 2483999 : }
18 2483999 : }
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 314568 : pub fn new() -> Chan<T> {
29 314568 : Chan {
30 314568 : shared: Arc::new(State {
31 314568 : queue: Mutex::new(VecDeque::new()),
32 314568 : waker: Waker::new(),
33 314568 : }),
34 314568 : }
35 314568 : }
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 2245 : pub fn recv(&self) -> T {
40 2245 : self.shared.recv()
41 2245 : }
42 :
43 : /// Panic if the queue is empty.
44 262748 : pub fn must_recv(&self) -> T {
45 262748 : self.shared
46 262748 : .try_recv()
47 262748 : .expect("message should've been ready")
48 262748 : }
49 :
50 : /// Get a message from the front of the queue, return None if the queue is empty.
51 : /// Never blocks.
52 235958 : pub fn try_recv(&self) -> Option<T> {
53 235958 : self.shared.try_recv()
54 235958 : }
55 :
56 : /// Send a message to the back of the queue.
57 484770 : pub fn send(&self, t: T) {
58 484770 : self.shared.send(t);
59 484770 : }
60 : }
61 :
62 : struct State<T> {
63 : queue: Mutex<VecDeque<T>>,
64 : waker: Waker,
65 : }
66 :
67 : impl<T> State<T> {
68 484770 : fn send(&self, t: T) {
69 484770 : self.queue.lock().push_back(t);
70 484770 : self.waker.wake_all();
71 484770 : }
72 :
73 498706 : fn try_recv(&self) -> Option<T> {
74 498706 : let mut q = self.queue.lock();
75 498706 : q.pop_front()
76 498706 : }
77 :
78 2245 : fn recv(&self) -> T {
79 2245 : // interrupt the receiver to prevent consuming everything at once
80 2245 : executor::yield_me(0);
81 2245 :
82 2245 : let mut queue = self.queue.lock();
83 2245 : if let Some(t) = queue.pop_front() {
84 0 : return t;
85 2245 : }
86 : loop {
87 5517 : self.waker.wake_me_later();
88 5517 : if let Some(t) = queue.pop_front() {
89 1954 : return t;
90 3272 : }
91 3272 : MutexGuard::unlocked(&mut queue, || {
92 3272 : executor::yield_me(-1);
93 3272 : });
94 : }
95 1954 : }
96 : }
97 :
98 : impl<T> PollSome for Chan<T> {
99 : /// Schedules a wakeup for the current thread.
100 3152589 : fn wake_me(&self) {
101 3152589 : self.shared.waker.wake_me_later();
102 3152589 : }
103 :
104 : /// Checks if chan has any pending messages.
105 2537869 : fn has_some(&self) -> bool {
106 2537869 : !self.shared.queue.lock().is_empty()
107 2537869 : }
108 : }
|