LCOV - code coverage report
Current view: top level - libs/desim/src - chan.rs (source / functions) Coverage Total Hit
Test: 02e8c57acd6e2b986849f552ca30280d54699b79.info Lines: 93.3 % 60 56
Test Date: 2024-06-26 17:13:54 Functions: 69.0 % 29 20

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

Generated by: LCOV version 2.1-beta