LCOV - code coverage report
Current view: top level - libs/desim/src - chan.rs (source / functions) Coverage Total Hit
Test: 1d18b743246dcf78c27c0bad0234a4c0da6fde89.info Lines: 93.3 % 60 56
Test Date: 2025-02-14 00:11:37 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       576634 :     fn clone(&self) -> Self {
      15       576634 :         Chan {
      16       576634 :             shared: self.shared.clone(),
      17       576634 :         }
      18       576634 :     }
      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        78237 :     pub fn new() -> Chan<T> {
      29        78237 :         Chan {
      30        78237 :             shared: Arc::new(State {
      31        78237 :                 queue: Mutex::new(VecDeque::new()),
      32        78237 :                 waker: Waker::new(),
      33        78237 :             }),
      34        78237 :         }
      35        78237 :     }
      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         1300 :     pub fn recv(&self) -> T {
      40         1300 :         self.shared.recv()
      41         1300 :     }
      42              : 
      43              :     /// Panic if the queue is empty.
      44        62922 :     pub fn must_recv(&self) -> T {
      45        62922 :         self.shared
      46        62922 :             .try_recv()
      47        62922 :             .expect("message should've been ready")
      48        62922 :     }
      49              : 
      50              :     /// Get a message from the front of the queue, return None if the queue is empty.
      51              :     /// Never blocks.
      52        55146 :     pub fn try_recv(&self) -> Option<T> {
      53        55146 :         self.shared.try_recv()
      54        55146 :     }
      55              : 
      56              :     /// Send a message to the back of the queue.
      57       117737 :     pub fn send(&self, t: T) {
      58       117737 :         self.shared.send(t);
      59       117737 :     }
      60              : }
      61              : 
      62              : struct State<T> {
      63              :     queue: Mutex<VecDeque<T>>,
      64              :     waker: Waker,
      65              : }
      66              : 
      67              : impl<T> State<T> {
      68       117737 :     fn send(&self, t: T) {
      69       117737 :         self.queue.lock().push_back(t);
      70       117737 :         self.waker.wake_all();
      71       117737 :     }
      72              : 
      73       118068 :     fn try_recv(&self) -> Option<T> {
      74       118068 :         let mut q = self.queue.lock();
      75       118068 :         q.pop_front()
      76       118068 :     }
      77              : 
      78         1300 :     fn recv(&self) -> T {
      79         1300 :         // interrupt the receiver to prevent consuming everything at once
      80         1300 :         executor::yield_me(0);
      81         1300 : 
      82         1300 :         let mut queue = self.queue.lock();
      83         1300 :         if let Some(t) = queue.pop_front() {
      84            0 :             return t;
      85         1300 :         }
      86              :         loop {
      87         2835 :             self.waker.wake_me_later();
      88         2835 :             if let Some(t) = queue.pop_front() {
      89         1221 :                 return t;
      90         1535 :             }
      91         1535 :             MutexGuard::unlocked(&mut queue, || {
      92         1535 :                 executor::yield_me(-1);
      93         1535 :             });
      94              :         }
      95         1221 :     }
      96              : }
      97              : 
      98              : impl<T> PollSome for Chan<T> {
      99              :     /// Schedules a wakeup for the current thread.
     100       755579 :     fn wake_me(&self) {
     101       755579 :         self.shared.waker.wake_me_later();
     102       755579 :     }
     103              : 
     104              :     /// Checks if chan has any pending messages.
     105       613829 :     fn has_some(&self) -> bool {
     106       613829 :         !self.shared.queue.lock().is_empty()
     107       613829 :     }
     108              : }
        

Generated by: LCOV version 2.1-beta