1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Multi-producer, multi-consumer FIFO queue communication primitives.
//!
//! This module provides message-based communication over channels, concretely
//! defined among two types:
//!
//! * [`Sender`]
//! * [`Receiver`]
//!
//! A [`Sender`] is used to send data to a [`Receiver`]. Both
//! sender and receiver are clone-able (multi-producer) such that many threads
//! can send simultaneously to multiple receiver (multi-consumer).
//!
//!
//! [`send`]: Sender::send
//!
//! ## Disconnection
//!
//! The send and receive operations on channels will all return a [`Result`]
//! indicating whether the operation succeeded or not. An unsuccessful operation
//! is normally indicative of the other half of a channel having "hung up" by
//! being dropped in its corresponding thread.
//!
//! Once half of a channel has been deallocated, most operations can no longer
//! continue to make progress, so [`Err`] will be returned. Many applications
//! will continue to [`unwrap`] the results returned from this module,
//! instigating a propagation of failure among threads if one unexpectedly dies.
//!
//! [`unwrap`]: Result::unwrap
// Modify from the std::sync::mpsc::channel.

use crate::{
    spin_lock::SpinLock,
    thread::{ParkHandle, Thread},
};
use alloc::{boxed::Box, sync::Arc, vec::Vec};
use core::{
    cell::RefCell,
    fmt,
    sync::atomic::{AtomicBool, AtomicUsize, Ordering},
};
use crossbeam_queue::ArrayQueue;

pub(crate) struct ChannelInner<T> {
    pub q: ArrayQueue<T>,
    pub tx_cnt: AtomicUsize,
    pub rx_cnt: AtomicUsize,
    tx_waiter: SpinLock<Vec<ParkHandle>>,
    rx_waiter: SpinLock<Vec<ParkHandle>>,
}

impl<T> ChannelInner<T> {
    pub fn has_receiver(&self) -> bool {
        self.rx_cnt.load(Ordering::Acquire) != 0
    }

    pub fn has_sender(&self) -> bool {
        self.tx_cnt.load(Ordering::Acquire) != 0
    }

    pub fn capacity(&self) -> usize {
        self.q.capacity()
    }

    pub fn push(
        &self,
        value: T,
        do_unpark: impl Fn(ParkHandle) -> Result<(), ()>,
    ) -> Result<(), T> {
        match self.q.push(value) {
            Ok(_) => {
                if let Some(th) = self.rx_waiter.lock().pop() {
                    do_unpark(th).expect("Failed to unpark channel tx waiter.")
                }
                Ok(())
            }
            Err(e) => Err(e),
        }
    }
    pub fn pop(&self, do_unpark: impl Fn(ParkHandle) -> Result<(), ()>) -> Option<T> {
        match self.q.pop() {
            Some(v) => {
                if let Some(th) = self.tx_waiter.lock().pop() {
                    do_unpark(th).expect("Failed to unpark channel rx waiter.")
                }
                Some(v)
            }
            None => None,
        }
    }
}

/// The receiving half of [`channel`] type.
/// This half can only be owned by one thread, but it can be cloned to receive
/// to other threads.
///
/// Messages sent to the channel can be retrieved using [`recv`].
///
/// [`recv`]: Receiver::recv
pub struct Receiver<T: core::marker::Send + 'static> {
    inner: *mut ChannelInner<T>,
}

// The receiver port can be sent from place to place, so long as it
// is not used to receive non-sendable things.
unsafe impl<T: Send> Send for Receiver<T> {}
unsafe impl<T: Send> Sync for Receiver<T> {}

/// An iterator over messages on a [`Receiver`], created by [`iter`].
///
/// This iterator will block whenever [`next`] is called,
/// waiting for a new message, and [`None`] will be returned
/// when the corresponding channel has hung up.
///
/// [`iter`]: Receiver::iter
/// [`next`]: Iterator::next
#[derive(Debug)]
pub struct Iter<'a, T: core::marker::Send + 'static> {
    rx: &'a Receiver<T>,
}

/// An iterator that attempts to yield all pending values for a [`Receiver`],
/// created by [`try_iter`].
///
/// [`None`] will be returned when there are no pending values remaining or
/// if the corresponding channel has hung up.
///
/// This iterator will never block the caller in order to wait for data to
/// become available. Instead, it will return [`None`].
///
/// [`try_iter`]: Receiver::try_iter
#[derive(Debug)]
pub struct TryIter<'a, T: core::marker::Send + 'static> {
    rx: &'a Receiver<T>,
}

/// An owning iterator over messages on a [`Receiver`],
/// created by **Receiver::into_iter**.
///
/// This iterator will block whenever [`next`]
/// is called, waiting for a new message, and [`None`] will be
/// returned if the corresponding channel has hung up.
///
/// [`next`]: Iterator::next
#[derive(Debug)]
pub struct IntoIter<T: core::marker::Send + 'static> {
    rx: Receiver<T>,
}

/// The sending-half of [`channel`] type. This half can only be owned by one
/// thread, but it can be cloned to send to other threads.
///
/// Messages can be sent through this channel with [`send`].
///
/// [`send`]: Sender::send
pub struct Sender<T: core::marker::Send + 'static> {
    inner: *mut ChannelInner<T>,
}

// The send port can be sent from place to place, so long as it
// is not used to send non-sendable things.
unsafe impl<T: Send> Send for Sender<T> {}
unsafe impl<T: Send> Sync for Sender<T> {}

/// An error returned from the [`Sender::send`] function on **channel**s.
///
/// A **send** operation can only fail if the receiving end of a channel is
/// disconnected, implying that the data could never be received. The error
/// contains the data being sent as a payload so it can be recovered.
///
/// [`Sender::send`]: Sender::send
#[derive(PartialEq, Eq, Clone, Copy)]
pub struct SendError<T>(pub T);

/// An error returned from the [`recv`] function on a [`Receiver`].
///
/// The [`recv`] operation can only fail if the sending half of a
/// [`channel`] is disconnected, implying that no further
/// messages will ever be received.
///
/// [`recv`]: Receiver::recv
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct RecvError;

/// This enumeration is the list of the possible error outcomes for the
/// [`try_send`] method.
///
/// [`try_send`]: Sender::try_send
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum TrySendError<T> {
    /// The data could not be sent on the [`channel`] because it would
    /// require that the callee block to send the data.
    ///
    /// If this is a buffered channel, then the buffer is full at this time. If
    /// this is not a buffered channel, then there is no [`Receiver`] available
    /// to acquire the data.
    Full(T),

    /// This [`channel`]'s receiving half has disconnected, so the data
    /// could not be sent. The data is returned back to the callee in this
    /// case.
    Disconnected(T),
}

/// This enumeration is the list of the possible reasons that [`try_recv`] could
/// not return data when called. This can occur with a [`channel`].
///
/// [`try_recv`]: Receiver::try_recv
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum TryRecvError {
    /// This **channel** is currently empty, but the **Sender**(s) have not yet
    /// disconnected, so data may yet become available.
    Empty,

    /// The **channel**'s sending half has become disconnected, and there will
    /// never be any more data received on it.
    Disconnected,
}

/// Creates a new bounded channel.
/// All data sent on the [`Sender`] will become available on the [`Receiver`]
/// in the same order as it was sent. Like asynchronous [`channel`]s, the
/// [`Receiver`] will block until a message becomes available.
///
/// This channel has an internal buffer on which messages will be queued.
/// `bound` specifies the buffer size. When the internal buffer becomes full,
/// future sends will *block* waiting for the buffer to open up. Note that a
/// buffer size of 0 is valid, in which case this becomes "rendezvous channel"
/// where each [`send`] will not return until a [`recv`] is paired with it.
///
/// Both [`Sender`] and [`Receiver`] can be cloned to [`send`] or [`recv`] to
/// the same channel multiple times.
///
/// If the [`Receiver`] is disconnected while trying to [`send`] with the
/// [`Sender`], the [`send`] method will return a [`SendError`]. Similarly, If
/// the [`Sender`] is disconnected while trying to [`recv`], the [`recv`] method
/// will return a [`RecvError`].
///
/// [`send`]: Sender::send
/// [`recv`]: Receiver::recv
pub fn channel<T: core::marker::Send + 'static>(bound: usize) -> (Sender<T>, Receiver<T>) {
    let chan = Box::into_raw(Box::new(ChannelInner {
        q: ArrayQueue::new(bound),
        tx_cnt: AtomicUsize::new(1),
        rx_cnt: AtomicUsize::new(1),
        tx_waiter: SpinLock::new(Vec::new()),
        rx_waiter: SpinLock::new(Vec::new()),
    }));
    (Sender { inner: chan }, Receiver { inner: chan })
}

////////////////////////////////////////////////////////////////////////////////
// Sender
////////////////////////////////////////////////////////////////////////////////
impl<T: core::marker::Send + 'static> Sender<T> {
    #[inline]
    fn inner<'a>(&self) -> &'a ChannelInner<T> {
        unsafe { &*self.inner }
    }

    /// Can send a value through this channel.
    pub fn can_send(&self) -> bool {
        let inner = self.inner();
        inner.q.is_empty() && inner.has_receiver()
    }

    /// Does anyone can receive a value through this channel.
    pub fn has_receiver(&self) -> bool {
        let inner = self.inner();
        inner.has_receiver()
    }

    /// Sends a value on this channel.
    ///
    /// This function will *block* until space in the internal buffer becomes
    /// available or a receiver is available to hand off the message to.
    ///
    /// Note that a successful send does *not* guarantee that the receiver will
    /// ever see the data if there is a buffer on this channel. Items may be
    /// enqueued in the internal buffer for the receiver to receive at a later
    /// time. If the buffer size is 0, however, the channel becomes a rendezvous
    /// channel and it guarantees that the receiver has indeed received
    /// the data if this function returns success.
    ///
    /// This function will never panic, but it may return [`Err`] if the
    /// [`Receiver`] has disconnected and is no longer able to receive
    /// information.
    pub fn send(&self, t: T) -> Result<(), SendError<T>> {
        let inner = self.inner();
        let mut t_ = t;
        loop {
            if !inner.has_receiver() {
                break Err(SendError(t_));
            } else if let Err(e) = inner.push(t_, |th| Ok(th.unpark())) {
                t_ = e;
                if inner.q.is_full() {
                    let mut guard = inner.tx_waiter.lock();
                    if inner.q.is_full() {
                        Thread::park_current_and(move |th| {
                            guard.push(th);
                            drop(guard)
                        });
                    }
                }
            } else {
                break Ok(());
            }
        }
    }

    /// Attempts to send a value on this channel without blocking.
    ///
    /// This method differs from [`send`] by returning immediately if the
    /// channel's buffer is full or no receiver is waiting to acquire some
    /// data. Compared with [`send`], this function has two failure cases
    /// instead of one (one for disconnection, one for a full buffer).
    ///
    /// See [`send`] for notes about guarantees of whether the
    /// receiver has received the data or not if this function is successful.
    ///
    /// [`send`]: Self::send
    pub fn try_send(&self, t: T) -> Result<(), TrySendError<T>> {
        let inner = self.inner();
        if !inner.has_receiver() {
            Err(TrySendError::Disconnected(t))
        } else if let Err(t) = inner.push(t, |th| Ok(th.unpark())) {
            Err(TrySendError::Full(t))
        } else {
            Ok(())
        }
    }

    /// Get capacity of the channel.
    pub fn capacity(&self) -> usize {
        self.inner().capacity()
    }
}

impl<T: core::marker::Send + 'static> Clone for Sender<T> {
    fn clone(&self) -> Sender<T> {
        if self.inner().tx_cnt.fetch_add(1, Ordering::Relaxed) > isize::MAX as usize {
            panic!("sender count overflowed.");
        }
        Sender { inner: self.inner }
    }
}

impl<T: core::marker::Send + 'static> Drop for Sender<T> {
    fn drop(&mut self) {
        let inner = self.inner();
        if inner.tx_cnt.fetch_sub(1, Ordering::AcqRel) == 1
            && inner.rx_cnt.load(Ordering::Relaxed) == 0
        {
            unsafe { drop(Box::from_raw(self.inner)) }
        }
    }
}

impl<T: core::marker::Send + 'static> fmt::Debug for Sender<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Sender").finish()
    }
}

////////////////////////////////////////////////////////////////////////////////
// Receiver
////////////////////////////////////////////////////////////////////////////////

impl<T: core::marker::Send + 'static> Receiver<T> {
    #[inline]
    fn inner<'a>(&self) -> &'a ChannelInner<T> {
        unsafe { &*self.inner }
    }

    /// Can receive a value through this channel.
    pub fn can_recv(&self) -> bool {
        let inner = self.inner();
        !inner.q.is_empty() && inner.has_sender()
    }

    /// Does anyone can send a value through this channel.
    pub fn has_sender(&self) -> bool {
        let inner = self.inner();
        inner.has_sender()
    }

    /// Attempts to wait for a value on this receiver, returning an error if the
    /// corresponding channel has hung up.
    ///
    /// This function will always block the current thread if there is no data
    /// available and it's possible for more data to be sent. Once a message is
    /// sent to the corresponding [`Sender`] (or [`Sender`]), then this
    /// receiver will wake up and return that message.
    ///
    /// If the corresponding [`Sender`] has disconnected, or it disconnects
    /// while this call is blocking, this call will wake up and return
    /// [`Err`] to indicate that no more messages can ever be received on
    /// this channel. However, since channels are buffered, messages sent
    /// before the disconnect will still be properly received.
    pub fn recv(&self) -> Result<T, RecvError> {
        let inner = self.inner();
        loop {
            match inner.pop(|th| Ok(th.unpark())) {
                Some(n) => break Ok(n),
                None if !inner.has_sender() => {
                    break inner.pop(|th| Ok(th.unpark())).ok_or(RecvError)
                }
                None => {
                    let mut guard = inner.rx_waiter.lock();
                    if let Some(n) = inner.pop(|th| Ok(th.unpark())) {
                        break Ok(n);
                    } else {
                        Thread::park_current_and(|th| {
                            guard.push(th);
                            drop(guard);
                        });
                    }
                }
            }
        }
    }

    /// Attempts to return a pending value on this receiver without blocking.
    ///
    /// This method will never block the caller in order to wait for data to
    /// become available. Instead, this will always return immediately with a
    /// possible option of pending data on the channel.
    ///
    /// This is useful for a flavor of "optimistic check" before deciding to
    /// block on a receiver.
    ///
    /// Compared with [`recv`], this function has two failure cases instead of
    /// one (one for disconnection, one for an empty buffer).
    ///
    /// [`recv`]: Self::recv
    pub fn try_recv(&self) -> Result<T, TryRecvError> {
        let inner = self.inner();
        match inner.pop(|th| Ok(th.unpark())) {
            Some(n) => Ok(n),
            None if !inner.has_sender() => inner
                .pop(|th| Ok(th.unpark()))
                .ok_or(TryRecvError::Disconnected),
            None => Err(TryRecvError::Empty),
        }
    }

    /// Returns an iterator that will block waiting for messages, but never
    /// [`panic!`]. It will return [`None`] when the channel has hung up.
    pub fn iter(&self) -> Iter<'_, T> {
        Iter { rx: self }
    }

    /// Returns an iterator that will attempt to yield all pending values.
    /// It will return `None` if there are no more pending values or if the
    /// channel has hung up. The iterator will never [`panic!`] or block the
    /// user by waiting for values.
    pub fn try_iter(&self) -> TryIter<'_, T> {
        TryIter { rx: self }
    }

    /// Get capacity of the channel.
    pub fn capacity(&self) -> usize {
        self.inner().capacity()
    }
}

impl<'a, T: core::marker::Send + 'static> Iterator for Iter<'a, T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        self.rx.recv().ok()
    }
}

impl<'a, T: core::marker::Send + 'static> Iterator for TryIter<'a, T> {
    type Item = T;

    fn next(&mut self) -> Option<T> {
        self.rx.try_recv().ok()
    }
}

impl<'a, T: core::marker::Send + 'static> IntoIterator for &'a Receiver<T> {
    type Item = T;
    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Iter<'a, T> {
        self.iter()
    }
}

impl<T: core::marker::Send + 'static> Iterator for IntoIter<T> {
    type Item = T;
    fn next(&mut self) -> Option<T> {
        self.rx.recv().ok()
    }
}

impl<T: core::marker::Send + 'static> IntoIterator for Receiver<T> {
    type Item = T;
    type IntoIter = IntoIter<T>;

    fn into_iter(self) -> IntoIter<T> {
        IntoIter { rx: self }
    }
}

impl<T: core::marker::Send + 'static> Clone for Receiver<T> {
    fn clone(&self) -> Receiver<T> {
        if self.inner().rx_cnt.fetch_add(1, Ordering::Relaxed) > isize::MAX as usize {
            panic!("receiver count overflowed.");
        }
        Receiver { inner: self.inner }
    }
}

impl<T: core::marker::Send + 'static> Drop for Receiver<T> {
    fn drop(&mut self) {
        let inner = self.inner();
        if inner.rx_cnt.fetch_sub(1, Ordering::AcqRel) == 1
            && inner.tx_cnt.load(Ordering::Relaxed) == 0
        {
            unsafe { drop(Box::from_raw(self.inner)) }
        }
    }
}

impl<T: core::marker::Send + 'static> fmt::Debug for Receiver<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Receiver").finish()
    }
}

struct PollingResponseInner<T> {
    finished: AtomicBool,
    t: RefCell<Option<T>>,
}

/// Helper structure to get response directly by polling this object.
pub struct PollingResponse<T> {
    inner: Arc<PollingResponseInner<T>>,
}

impl<T> Default for PollingResponse<T> {
    fn default() -> Self {
        Self {
            inner: Arc::new(PollingResponseInner {
                finished: AtomicBool::new(false),
                t: RefCell::new(None),
            }),
        }
    }
}

impl<T> PollingResponse<T> {
    /// Poll and get the response.
    pub fn poll(self) -> T {
        while !self.inner.finished.load(Ordering::Acquire) {
            core::hint::spin_loop();
        }
        self.inner.t.borrow_mut().take().unwrap()
    }

    /// Put the response into the cell.
    pub fn put(self, t: T) {
        *self.inner.t.borrow_mut() = Some(t);
        self.inner.finished.store(true, Ordering::Release)
    }
}

impl<T> Clone for PollingResponse<T> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

unsafe impl<T: Send> Sync for PollingResponseInner<T> {}