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
/***************************************************************************************************
 * Copyright (c) 2019 by the authors
 *
 * Author: André Borrmann
 * License: MIT OR Apache License 2.0
 **************************************************************************************************/
//! # Async Multi-Producer-Multi-Consumer Channel
//!
//!

use super::{channel, Receiver, Sender};
use alloc::sync::Arc;
use core::{
  pin::Pin,
  task::{Context, Poll},
};
use futures_util::{stream::Stream, task::AtomicWaker};

/// Create both sides of an asynchronous mpmc channel
pub fn async_channel<T: 'static>() -> (AsyncSender<T>, AsyncReceiver<T>) {
  let (tx, rx) = channel();
  let waker = Arc::new(AtomicWaker::new());
  (
    AsyncSender {
      tx,
      waker: waker.clone(),
    },
    AsyncReceiver { rx, waker },
  )
}

/// The sending part of a mpmc channel. This is used to send data through the channel to an receiver
#[repr(C)]
pub struct AsyncSender<T: 'static> {
  tx: Sender<T>,
  waker: Arc<AtomicWaker>,
}

impl<T: 'static> AsyncSender<T> {
  /// Send data through the channel
  pub fn send(&self, data: T) {
    self.tx.send(data);
    self.waker.wake();
  }
}

/// Enable cloning the Sender so it can be used at different cores, filling up the same queue
impl<T: 'static> Clone for AsyncSender<T> {
  fn clone(&self) -> AsyncSender<T> {
    AsyncSender {
      tx: self.tx.clone(),
      waker: self.waker.clone(),
    }
  }
}

/// The receiving part of the channel. This can be used to retreive data that has been send from the sending part of it
#[repr(C)]
pub struct AsyncReceiver<T: 'static> {
  rx: Receiver<T>,
  waker: Arc<AtomicWaker>,
}

/// Enable cloning the Sender so it can be used at different cores, filling up the same queue
impl<T: 'static> Clone for AsyncReceiver<T> {
  fn clone(&self) -> AsyncReceiver<T> {
    AsyncReceiver {
      rx: self.rx.clone(),
      waker: self.waker.clone(),
    }
  }
}

/// The asyncrounous channel receiving part acts like a stream. When waiting for new data to arrive call `next().await`
/// on the receiver.
impl<T: 'static> Stream for AsyncReceiver<T> {
  type Item = T;

  fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
    if let Ok(v) = self.rx.recv() {
      Poll::Ready(Some(v))
    } else {
      // how get woken? This should be done by the sender
      self.waker.register(cx.waker());
      Poll::Pending
    }
  }
}