1 //! Zero-capacity channel.
3 //! This kind of channel is also known as *rendezvous* channel.
5 use super::context::Context;
7 use super::select::{Operation, Selected, Token};
8 use super::utils::Backoff;
9 use super::waker::Waker;
11 use crate::cell::UnsafeCell;
12 use crate::marker::PhantomData;
13 use crate::sync::atomic::{AtomicBool, Ordering};
14 use crate::sync::Mutex;
15 use crate::time::Instant;
16 use crate::{fmt, ptr};
18 /// A pointer to a packet.
19 pub(crate) struct ZeroToken(*mut ());
21 impl Default for ZeroToken {
22 fn default() -> Self {
27 impl fmt::Debug for ZeroToken {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 fmt::Debug::fmt(&(self.0 as usize), f)
33 /// A slot for passing one message from a sender to a receiver.
35 /// Equals `true` if the packet is allocated on the stack.
38 /// Equals `true` once the packet is ready for reading or writing.
42 msg: UnsafeCell<Option<T>>,
46 /// Creates an empty packet on the stack.
47 fn empty_on_stack() -> Packet<T> {
48 Packet { on_stack: true, ready: AtomicBool::new(false), msg: UnsafeCell::new(None) }
51 /// Creates a packet on the stack, containing a message.
52 fn message_on_stack(msg: T) -> Packet<T> {
53 Packet { on_stack: true, ready: AtomicBool::new(false), msg: UnsafeCell::new(Some(msg)) }
56 /// Waits until the packet becomes ready for reading or writing.
57 fn wait_ready(&self) {
58 let backoff = Backoff::new();
59 while !self.ready.load(Ordering::Acquire) {
65 /// Inner representation of a zero-capacity channel.
67 /// Senders waiting to pair up with a receive operation.
70 /// Receivers waiting to pair up with a send operation.
73 /// Equals `true` when the channel is disconnected.
74 is_disconnected: bool,
77 /// Zero-capacity channel.
78 pub(crate) struct Channel<T> {
79 /// Inner representation of the channel.
82 /// Indicates that dropping a `Channel<T>` may drop values of type `T`.
83 _marker: PhantomData<T>,
87 /// Constructs a new zero-capacity channel.
88 pub(crate) fn new() -> Self {
90 inner: Mutex::new(Inner {
91 senders: Waker::new(),
92 receivers: Waker::new(),
93 is_disconnected: false,
99 /// Writes a message into the packet.
100 pub(crate) unsafe fn write(&self, token: &mut Token, msg: T) -> Result<(), T> {
101 // If there is no packet, the channel is disconnected.
102 if token.zero.0.is_null() {
106 let packet = &*(token.zero.0 as *const Packet<T>);
107 packet.msg.get().write(Some(msg));
108 packet.ready.store(true, Ordering::Release);
112 /// Reads a message from the packet.
113 pub(crate) unsafe fn read(&self, token: &mut Token) -> Result<T, ()> {
114 // If there is no packet, the channel is disconnected.
115 if token.zero.0.is_null() {
119 let packet = &*(token.zero.0 as *const Packet<T>);
122 // The message has been in the packet from the beginning, so there is no need to wait
123 // for it. However, after reading the message, we need to set `ready` to `true` in
124 // order to signal that the packet can be destroyed.
125 let msg = packet.msg.get().replace(None).unwrap();
126 packet.ready.store(true, Ordering::Release);
129 // Wait until the message becomes available, then read it and destroy the
130 // heap-allocated packet.
132 let msg = packet.msg.get().replace(None).unwrap();
133 drop(Box::from_raw(token.zero.0 as *mut Packet<T>));
138 /// Attempts to send a message into the channel.
139 pub(crate) fn try_send(&self, msg: T) -> Result<(), TrySendError<T>> {
140 let token = &mut Token::default();
141 let mut inner = self.inner.lock().unwrap();
143 // If there's a waiting receiver, pair up with it.
144 if let Some(operation) = inner.receivers.try_select() {
145 token.zero.0 = operation.packet;
148 self.write(token, msg).ok().unwrap();
151 } else if inner.is_disconnected {
152 Err(TrySendError::Disconnected(msg))
154 Err(TrySendError::Full(msg))
158 /// Sends a message into the channel.
162 deadline: Option<Instant>,
163 ) -> Result<(), SendTimeoutError<T>> {
164 let token = &mut Token::default();
165 let mut inner = self.inner.lock().unwrap();
167 // If there's a waiting receiver, pair up with it.
168 if let Some(operation) = inner.receivers.try_select() {
169 token.zero.0 = operation.packet;
172 self.write(token, msg).ok().unwrap();
177 if inner.is_disconnected {
178 return Err(SendTimeoutError::Disconnected(msg));
182 // Prepare for blocking until a receiver wakes us up.
183 let oper = Operation::hook(token);
184 let mut packet = Packet::<T>::message_on_stack(msg);
185 inner.senders.register_with_packet(oper, &mut packet as *mut Packet<T> as *mut (), cx);
186 inner.receivers.notify();
189 // Block the current thread.
190 let sel = cx.wait_until(deadline);
193 Selected::Waiting => unreachable!(),
194 Selected::Aborted => {
195 self.inner.lock().unwrap().senders.unregister(oper).unwrap();
196 let msg = unsafe { packet.msg.get().replace(None).unwrap() };
197 Err(SendTimeoutError::Timeout(msg))
199 Selected::Disconnected => {
200 self.inner.lock().unwrap().senders.unregister(oper).unwrap();
201 let msg = unsafe { packet.msg.get().replace(None).unwrap() };
202 Err(SendTimeoutError::Disconnected(msg))
204 Selected::Operation(_) => {
205 // Wait until the message is read, then drop the packet.
213 /// Attempts to receive a message without blocking.
214 pub(crate) fn try_recv(&self) -> Result<T, TryRecvError> {
215 let token = &mut Token::default();
216 let mut inner = self.inner.lock().unwrap();
218 // If there's a waiting sender, pair up with it.
219 if let Some(operation) = inner.senders.try_select() {
220 token.zero.0 = operation.packet;
222 unsafe { self.read(token).map_err(|_| TryRecvError::Disconnected) }
223 } else if inner.is_disconnected {
224 Err(TryRecvError::Disconnected)
226 Err(TryRecvError::Empty)
230 /// Receives a message from the channel.
231 pub(crate) fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError> {
232 let token = &mut Token::default();
233 let mut inner = self.inner.lock().unwrap();
235 // If there's a waiting sender, pair up with it.
236 if let Some(operation) = inner.senders.try_select() {
237 token.zero.0 = operation.packet;
240 return self.read(token).map_err(|_| RecvTimeoutError::Disconnected);
244 if inner.is_disconnected {
245 return Err(RecvTimeoutError::Disconnected);
249 // Prepare for blocking until a sender wakes us up.
250 let oper = Operation::hook(token);
251 let mut packet = Packet::<T>::empty_on_stack();
252 inner.receivers.register_with_packet(
254 &mut packet as *mut Packet<T> as *mut (),
257 inner.senders.notify();
260 // Block the current thread.
261 let sel = cx.wait_until(deadline);
264 Selected::Waiting => unreachable!(),
265 Selected::Aborted => {
266 self.inner.lock().unwrap().receivers.unregister(oper).unwrap();
267 Err(RecvTimeoutError::Timeout)
269 Selected::Disconnected => {
270 self.inner.lock().unwrap().receivers.unregister(oper).unwrap();
271 Err(RecvTimeoutError::Disconnected)
273 Selected::Operation(_) => {
274 // Wait until the message is provided, then read it.
276 unsafe { Ok(packet.msg.get().replace(None).unwrap()) }
282 /// Disconnects the channel and wakes up all blocked senders and receivers.
284 /// Returns `true` if this call disconnected the channel.
285 pub(crate) fn disconnect(&self) -> bool {
286 let mut inner = self.inner.lock().unwrap();
288 if !inner.is_disconnected {
289 inner.is_disconnected = true;
290 inner.senders.disconnect();
291 inner.receivers.disconnect();
298 /// Returns the current number of messages inside the channel.
299 pub(crate) fn len(&self) -> usize {
303 /// Returns the capacity of the channel.
304 #[allow(clippy::unnecessary_wraps)] // This is intentional.
305 pub(crate) fn capacity(&self) -> Option<usize> {
309 /// Returns `true` if the channel is empty.
310 pub(crate) fn is_empty(&self) -> bool {
314 /// Returns `true` if the channel is full.
315 pub(crate) fn is_full(&self) -> bool {