rtsc 0.4.5

Real-time Synchronization Components
Documentation
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
use std::{
    collections::{BTreeSet, VecDeque},
    future::Future,
    marker::PhantomData,
    mem,
    pin::Pin,
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
    task::{Context, Poll, Waker},
    time::Duration,
};

use crate::{base_channel::ChannelStorage, data_policy::StorageTryPushOutput, Error, Result};
use object_id::UniqueId;
use parking_lot_rt::{Condvar, Mutex};
use pin_project::{pin_project, pinned_drop};

type ClientId = usize;

/// Base async channel
pub struct BaseChannelAsync<T: Sized, S: ChannelStorage<T>>(pub(crate) Arc<ChannelInner<T, S>>);

impl<T: Sized, S: ChannelStorage<T>> BaseChannelAsync<T, S> {
    fn id(&self) -> usize {
        self.0.id.as_usize()
    }
}

impl<T: Sized, S: ChannelStorage<T>> Eq for BaseChannelAsync<T, S> {}

impl<T: Sized, S: ChannelStorage<T>> PartialEq for BaseChannelAsync<T, S> {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

impl<T, S> Clone for BaseChannelAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

pub(crate) struct ChannelInner<T: Sized, S: ChannelStorage<T>> {
    id: UniqueId,
    pub(crate) data: Mutex<InnerData<T, S>>,
    next_op_id: AtomicUsize,
    space_available: Arc<Condvar>,
    data_available: Arc<Condvar>,
}

impl<T: Sized, S: ChannelStorage<T>> BaseChannelAsync<T, S> {
    pub(crate) fn new(capacity: usize, ordering: bool) -> Self {
        let pc = InnerData::new(capacity, ordering);
        let space_available = pc.space_available.clone();
        let data_available = pc.data_available.clone();
        Self(
            ChannelInner {
                id: <_>::default(),
                data: Mutex::new(pc),
                next_op_id: <_>::default(),
                space_available,
                data_available,
            }
            .into(),
        )
    }
    fn op_id(&self) -> usize {
        self.0.next_op_id.fetch_add(1, Ordering::SeqCst)
    }
}

pub(crate) struct InnerData<T: Sized, S: ChannelStorage<T>> {
    queue: S,
    senders: usize,
    receivers: usize,
    pub(crate) send_fut_wakers: VecDeque<Option<(Waker, ClientId)>>,
    pub(crate) send_fut_waker_ids: BTreeSet<ClientId>,
    pub(crate) send_fut_pending: BTreeSet<ClientId>,
    pub(crate) recv_fut_wakers: VecDeque<Option<(Waker, ClientId)>>,
    pub(crate) recv_fut_waker_ids: BTreeSet<ClientId>,
    pub(crate) recv_fut_pending: BTreeSet<ClientId>,
    data_available: Arc<Condvar>,
    space_available: Arc<Condvar>,
    _phatom: PhantomData<T>,
}

impl<T, S> InnerData<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    fn new(capacity: usize, ordering: bool) -> Self {
        assert!(capacity > 0, "channel capacity MUST be > 0");
        Self {
            queue: S::with_capacity_and_ordering(capacity, ordering),
            senders: 1,
            receivers: 1,
            send_fut_wakers: <_>::default(),
            send_fut_waker_ids: <_>::default(),
            send_fut_pending: <_>::default(),
            recv_fut_wakers: <_>::default(),
            recv_fut_waker_ids: <_>::default(),
            recv_fut_pending: <_>::default(),
            data_available: <_>::default(),
            space_available: <_>::default(),
            _phatom: PhantomData,
        }
    }

    // senders

    #[inline]
    fn notify_data_sent(&mut self) {
        self.wake_next_recv();
    }

    #[inline]
    fn wake_next_send(&mut self) {
        if let Some(w) = self.send_fut_wakers.pop_front() {
            if let Some((waker, id)) = w {
                self.send_fut_waker_ids.remove(&id);
                self.send_fut_pending.insert(id);
                waker.wake();
            } else {
                self.space_available.notify_one();
            }
        }
    }
    #[inline]
    fn wake_all_sends(&mut self) {
        self.send_fut_waker_ids.clear();
        for (waker, _) in mem::take(&mut self.send_fut_wakers).into_iter().flatten() {
            waker.wake();
        }
        self.space_available.notify_all();
    }

    #[inline]
    fn notify_send_fut_drop(&mut self, id: ClientId) {
        if let Some(pos) = self
            .send_fut_wakers
            .iter()
            .position(|w| w.as_ref().is_some_and(|(_, i)| *i == id))
        {
            self.send_fut_wakers.remove(pos);
            self.send_fut_waker_ids.remove(&id);
        }
        if self.send_fut_pending.remove(&id) {
            self.wake_next_send();
        }
    }

    #[inline]
    fn confirm_send_fut_waked(&mut self, id: ClientId) {
        self.send_fut_pending.remove(&id);
    }

    #[inline]
    fn append_send_fut_waker(&mut self, waker: Waker, id: ClientId) {
        if !self.send_fut_waker_ids.insert(id) {
            return;
        }
        self.send_fut_wakers.push_back(Some((waker, id)));
    }

    #[inline]
    fn append_send_sync_waker(&mut self) {
        // use condvar
        self.send_fut_wakers.push_back(None);
    }

    // receivers

    #[inline]
    fn notify_data_received(&mut self) {
        self.wake_next_send();
    }

    #[inline]
    fn wake_next_recv(&mut self) {
        if let Some(w) = self.recv_fut_wakers.pop_front() {
            if let Some((waker, id)) = w {
                self.recv_fut_pending.insert(id);
                self.recv_fut_waker_ids.remove(&id);
                waker.wake();
            } else {
                self.data_available.notify_one();
            }
        }
    }
    #[inline]
    fn wake_all_recvs(&mut self) {
        for (waker, _) in mem::take(&mut self.recv_fut_wakers).into_iter().flatten() {
            waker.wake();
        }
        self.recv_fut_waker_ids.clear();
        self.data_available.notify_all();
    }

    #[inline]
    fn notify_recv_fut_drop(&mut self, id: ClientId) {
        if let Some(pos) = self
            .recv_fut_wakers
            .iter()
            .position(|w| w.as_ref().is_some_and(|(_, i)| *i == id))
        {
            self.recv_fut_wakers.remove(pos);
            self.recv_fut_waker_ids.remove(&id);
        }
        if self.recv_fut_pending.remove(&id) {
            self.wake_next_recv();
        }
    }

    #[inline]
    fn confirm_recv_fut_waked(&mut self, id: ClientId) {
        // the resource is taken, remove from pending
        self.recv_fut_pending.remove(&id);
    }

    #[inline]
    fn append_recv_fut_waker(&mut self, waker: Waker, id: ClientId) {
        if !self.recv_fut_waker_ids.insert(id) {
            return;
        }
        self.recv_fut_wakers.push_back(Some((waker, id)));
    }

    #[inline]
    fn append_recv_sync_waker(&mut self) {
        // use condvar
        self.recv_fut_wakers.push_back(None);
    }
}

#[pin_project(PinnedDrop)]
struct Send<'a, T: Sized, S: ChannelStorage<T>> {
    id: usize,
    channel: &'a BaseChannelAsync<T, S>,
    queued: bool,
    value: Option<T>,
}

#[pinned_drop]
#[allow(clippy::needless_lifetimes)]
impl<'a, T: Sized, S: ChannelStorage<T>> PinnedDrop for Send<'a, T, S> {
    fn drop(self: Pin<&mut Self>) {
        if self.queued {
            self.channel.0.data.lock().notify_send_fut_drop(self.id);
        }
    }
}

impl<T, S> Future for Send<'_, T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    type Output = Result<()>;
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut pc = self.channel.0.data.lock();
        if self.queued {
            pc.confirm_send_fut_waked(self.id);
        }
        if pc.receivers == 0 {
            self.queued = false;
            return Poll::Ready(Err(Error::ChannelClosed));
        }
        if pc.send_fut_wakers.is_empty() || self.queued {
            let push_result = pc.queue.try_push(self.value.take().unwrap());
            if let StorageTryPushOutput::Full(val) = push_result {
                self.value = Some(val);
            } else {
                self.queued = false;
                return Poll::Ready(match push_result {
                    StorageTryPushOutput::Pushed => {
                        pc.notify_data_sent();
                        Ok(())
                    }
                    StorageTryPushOutput::Skipped => Err(Error::ChannelSkipped),
                    StorageTryPushOutput::Full(_) => unreachable!(),
                });
            }
        }
        self.queued = true;
        pc.append_send_fut_waker(cx.waker().clone(), self.id);
        Poll::Pending
    }
}

/// Base async sender
#[derive(Eq, PartialEq)]
pub struct BaseSenderAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    channel: BaseChannelAsync<T, S>,
}

impl<T, S> BaseSenderAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    /// Sends a value to the channel
    #[inline]
    pub fn send(&self, value: T) -> impl Future<Output = Result<()>> + '_ {
        Send {
            id: self.channel.op_id(),
            channel: &self.channel,
            queued: false,
            value: Some(value),
        }
    }
    /// Tries to send a value to the channel
    pub fn try_send(&self, value: T) -> Result<()> {
        let mut pc = self.channel.0.data.lock();
        if pc.receivers == 0 {
            return Err(Error::ChannelClosed);
        }
        match pc.queue.try_push(value) {
            StorageTryPushOutput::Pushed => {
                pc.notify_data_sent();
                Ok(())
            }
            StorageTryPushOutput::Skipped => Err(Error::ChannelSkipped),
            StorageTryPushOutput::Full(_) => Err(Error::ChannelFull),
        }
    }
    /// Sends a value to the channel in a blocking (synchronous) way
    pub fn send_blocking(&self, mut value: T) -> Result<()> {
        let mut pc = self.channel.0.data.lock();
        let pushed = loop {
            if pc.receivers == 0 {
                return Err(Error::ChannelClosed);
            }
            let push_result = pc.queue.try_push(value);
            let StorageTryPushOutput::Full(val) = push_result else {
                break push_result;
            };
            value = val;
            pc.append_send_sync_waker();
            self.channel.0.space_available.wait(&mut pc);
        };
        match pushed {
            StorageTryPushOutput::Pushed => {
                pc.notify_data_sent();
                Ok(())
            }
            StorageTryPushOutput::Skipped => Err(Error::ChannelSkipped),
            StorageTryPushOutput::Full(_) => unreachable!(),
        }
    }
    /// Sends a value to the channel in a blocking (synchronous) way with a given tiemout
    pub fn send_blocking_timeout(&self, mut value: T, timeout: Duration) -> Result<()> {
        let mut pc = self.channel.0.data.lock();
        let pushed = loop {
            if pc.receivers == 0 {
                return Err(Error::ChannelClosed);
            }
            let push_result = pc.queue.try_push(value);
            let StorageTryPushOutput::Full(val) = push_result else {
                break push_result;
            };
            value = val;
            pc.append_send_sync_waker();
            if self
                .channel
                .0
                .space_available
                .wait_for(&mut pc, timeout)
                .timed_out()
            {
                return Err(Error::Timeout);
            }
        };
        pc.notify_data_sent();
        match pushed {
            StorageTryPushOutput::Pushed => Ok(()),
            StorageTryPushOutput::Skipped => Err(Error::ChannelSkipped),
            StorageTryPushOutput::Full(_) => unreachable!(),
        }
    }
    /// Returns the number of items in the channel
    #[inline]
    pub fn len(&self) -> usize {
        self.channel.0.data.lock().queue.len()
    }
    /// Returns true if the channel is full
    #[inline]
    pub fn is_full(&self) -> bool {
        self.channel.0.data.lock().queue.is_full()
    }
    /// Returns true if the channel is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.channel.0.data.lock().queue.is_empty()
    }
    /// Returns true if the channel is still alive
    #[inline]
    pub fn is_alive(&self) -> bool {
        self.channel.0.data.lock().receivers > 0
    }
}

impl<T, S> Clone for BaseSenderAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    fn clone(&self) -> Self {
        self.channel.0.data.lock().senders += 1;
        Self {
            channel: self.channel.clone(),
        }
    }
}

impl<T, S> Drop for BaseSenderAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    fn drop(&mut self) {
        let mut pc = self.channel.0.data.lock();
        pc.senders -= 1;
        if pc.senders == 0 {
            pc.wake_all_recvs();
        }
    }
}

struct Recv<'a, T: Sized, S: ChannelStorage<T>> {
    id: usize,
    channel: &'a BaseChannelAsync<T, S>,
    queued: bool,
}

impl<T: Sized, S: ChannelStorage<T>> Drop for Recv<'_, T, S> {
    fn drop(&mut self) {
        if self.queued {
            self.channel.0.data.lock().notify_recv_fut_drop(self.id);
        }
    }
}

impl<T, S> Future for Recv<'_, T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    type Output = Result<T>;
    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut pc = self.channel.0.data.lock();
        if self.queued {
            pc.confirm_recv_fut_waked(self.id);
        }
        if pc.recv_fut_wakers.is_empty() || self.queued {
            if let Some(val) = pc.queue.get() {
                pc.notify_data_received();
                self.queued = false;
                return Poll::Ready(Ok(val));
            } else if pc.senders == 0 {
                self.queued = false;
                return Poll::Ready(Err(Error::ChannelClosed));
            }
        }
        self.queued = true;
        pc.append_recv_fut_waker(cx.waker().clone(), self.id);
        Poll::Pending
    }
}

/// Base async receiver
#[derive(Eq, PartialEq)]
pub struct BaseReceiverAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    pub(crate) channel: BaseChannelAsync<T, S>,
}

impl<T, S> BaseReceiverAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    /// Receives a value from the channel
    #[inline]
    pub fn recv(&self) -> impl Future<Output = Result<T>> + '_ {
        Recv {
            id: self.channel.op_id(),
            channel: &self.channel,
            queued: false,
        }
    }
    /// Tries to receive a value from the channel
    pub fn try_recv(&self) -> Result<T> {
        let mut pc = self.channel.0.data.lock();
        if let Some(val) = pc.queue.get() {
            pc.notify_data_received();
            Ok(val)
        } else if pc.senders == 0 {
            Err(Error::ChannelClosed)
        } else {
            Err(Error::ChannelEmpty)
        }
    }
    /// Receives a value from the channel in a blocking (synchronous) way
    pub fn recv_blocking(&self) -> Result<T> {
        let mut pc = self.channel.0.data.lock();
        loop {
            if let Some(val) = pc.queue.get() {
                pc.notify_data_received();
                return Ok(val);
            } else if pc.senders == 0 {
                return Err(Error::ChannelClosed);
            }
            pc.append_recv_sync_waker();
            self.channel.0.data_available.wait(&mut pc);
        }
    }
    /// Receives a value from the channel in a blocking (synchronous) way with a given timeout
    pub fn recv_blocking_timeout(&self, timeout: Duration) -> Result<T> {
        let mut pc = self.channel.0.data.lock();
        loop {
            if let Some(val) = pc.queue.get() {
                pc.notify_data_received();
                return Ok(val);
            } else if pc.senders == 0 {
                return Err(Error::ChannelClosed);
            }
            pc.append_recv_sync_waker();
            if self
                .channel
                .0
                .data_available
                .wait_for(&mut pc, timeout)
                .timed_out()
            {
                return Err(Error::Timeout);
            }
        }
    }
    /// Returns the number of items in the channel
    #[inline]
    pub fn len(&self) -> usize {
        self.channel.0.data.lock().queue.len()
    }
    /// Returns true if the channel is full
    #[inline]
    pub fn is_full(&self) -> bool {
        self.channel.0.data.lock().queue.is_full()
    }
    /// Returns true if the channel is empty
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.channel.0.data.lock().queue.is_empty()
    }
    /// Returns true if the channel is still alive
    #[inline]
    pub fn is_alive(&self) -> bool {
        self.channel.0.data.lock().senders > 0
    }
}

impl<T, S> Clone for BaseReceiverAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    fn clone(&self) -> Self {
        self.channel.0.data.lock().receivers += 1;
        Self {
            channel: self.channel.clone(),
        }
    }
}

impl<T, S> Drop for BaseReceiverAsync<T, S>
where
    T: Sized,
    S: ChannelStorage<T>,
{
    fn drop(&mut self) {
        let mut pc = self.channel.0.data.lock();
        pc.receivers -= 1;
        if pc.receivers == 0 {
            pc.wake_all_sends();
        }
    }
}

pub(crate) fn make_channel<T: Sized, S: ChannelStorage<T>>(
    ch: BaseChannelAsync<T, S>,
) -> (BaseSenderAsync<T, S>, BaseReceiverAsync<T, S>) {
    let tx = BaseSenderAsync {
        channel: ch.clone(),
    };
    let rx = BaseReceiverAsync { channel: ch };
    (tx, rx)
}