moirai-async 0.6.1

Async/await support for Moirai concurrency library
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
#![expect(
    clippy::unwrap_used,
    reason = "ratchet MOIRAI-UNWRAP-1: pre-existing debt"
)]

//! Bounded async multi-producer single-consumer channel.
//!
//! Waiter bookkeeping is delegated to the shared `WaitQueue`: the same
//! FIFO-by-monotonic-id registration, grant hand-off, and O(log n)
//! cancellation that `Notify`, `Semaphore`, and `RwLock` use. This module
//! keeps only the channel's own admission predicate (buffer capacity) and
//! its two grants — a send frees a receive slot, a receive frees a send slot.

use std::collections::VecDeque;
use std::future::Future;
use std::marker::Unpin;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll};

use super::wait_queue::{WaitQueue, WaiterPoll};

struct SharedState<T> {
    buffer: VecDeque<T>,
    capacity: usize,
    sender_count: usize,
    closed: bool,
    send_waiters: WaitQueue<()>,
    recv_waiters: WaitQueue<()>,
}

/// Sending half of the bounded channel; clone to add producers.
pub struct Sender<T> {
    shared: Arc<Mutex<SharedState<T>>>,
}

impl<T> Clone for Sender<T> {
    fn clone(&self) -> Self {
        let mut shared = self.shared.lock().unwrap();
        shared.sender_count += 1;
        Sender {
            shared: self.shared.clone(),
        }
    }
}

impl<T> Sender<T> {
    /// Send a value, waiting for buffer capacity.
    ///
    /// The returned future resolves `Err(value)` when the channel closes
    /// before the value is accepted.
    pub fn send(&self, value: T) -> SendFuture<'_, T> {
        SendFuture {
            sender: self,
            value: Some(value),
            id: None,
        }
    }

    /// Send without waiting; returns the value when full or closed.
    ///
    /// # Errors
    ///
    /// Returns `Err(value)` when the buffer is at capacity or the channel
    /// is closed.
    pub fn try_send(&self, value: T) -> Result<(), T> {
        let mut shared = self.shared.lock().unwrap();
        if shared.closed {
            return Err(value);
        }
        if shared.buffer.len() >= shared.capacity {
            return Err(value);
        }
        shared.buffer.push_back(value);
        // Grant the oldest parked receiver a slot and wake it outside the
        // lock: a task waker may re-enter the channel, so holding the mutex
        // across `wake` risks a self-deadlock.
        let waker = shared.recv_waiters.grant_oldest(());
        drop(shared);
        if let Some(waker) = waker {
            waker.wake();
        }
        Ok(())
    }

    /// Return whether the channel is closed.
    pub fn is_closed(&self) -> bool {
        self.shared.lock().unwrap().closed
    }

    /// Count of live sender handles.
    pub fn sender_strong_count(&self) -> usize {
        self.shared.lock().unwrap().sender_count
    }
}

impl<T> Drop for Sender<T> {
    fn drop(&mut self) {
        let mut shared = self.shared.lock().unwrap();
        shared.sender_count -= 1;
        if shared.sender_count != 0 {
            return;
        }
        shared.closed = true;
        let wakers = shared.recv_waiters.grant_all(());
        drop(shared);
        for waker in wakers {
            waker.wake();
        }
    }
}

/// Future returned by [`Sender::send`].
pub struct SendFuture<'a, T> {
    sender: &'a Sender<T>,
    value: Option<T>,
    id: Option<u64>,
}

impl<'a, T: Unpin> Future for SendFuture<'a, T> {
    type Output = Result<(), T>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();
        let mut shared = this.sender.shared.lock().unwrap();

        if shared.closed {
            if let Some(id) = this.id.take() {
                shared.send_waiters.deregister(id);
            }
            let value = this.value.take().unwrap();
            return Poll::Ready(Err(value));
        }

        if shared.buffer.len() < shared.capacity {
            if let Some(id) = this.id.take() {
                shared.send_waiters.deregister(id);
            }
            shared.buffer.push_back(this.value.take().unwrap());
            let waker = shared.recv_waiters.grant_oldest(());
            drop(shared);
            if let Some(waker) = waker {
                waker.wake();
            }
            return Poll::Ready(Ok(()));
        }

        // Still full: refresh the existing registration, or join the queue.
        // A stale grant (the slot was taken by another sender) re-registers
        // behind the current waiters rather than losing its place.
        this.id = Some(match this.id {
            Some(id) => match shared.send_waiters.poll_waiter(id, cx.waker()) {
                WaiterPoll::Pending => id,
                WaiterPoll::Granted(()) | WaiterPoll::NotRegistered => {
                    shared.send_waiters.register(cx.waker().clone())
                }
            },
            None => shared.send_waiters.register(cx.waker().clone()),
        });
        Poll::Pending
    }
}

impl<'a, T> Drop for SendFuture<'a, T> {
    fn drop(&mut self) {
        if let Some(id) = self.id
            && let Ok(mut shared) = self.sender.shared.lock()
        {
            shared.send_waiters.deregister(id);
        }
    }
}

/// Receiving half of the bounded channel.
pub struct Receiver<T> {
    shared: Arc<Mutex<SharedState<T>>>,
}

impl<T> Receiver<T> {
    /// Receive the next value, waiting for one to arrive.
    ///
    /// The returned future resolves `Err(())` when the channel is closed
    /// and drained.
    pub fn recv(&mut self) -> RecvFuture<'_, T> {
        RecvFuture {
            receiver: self,
            id: None,
        }
    }

    /// Receive without waiting; `None` when the buffer is empty.
    pub fn try_recv(&mut self) -> Option<T> {
        let mut shared = self.shared.lock().unwrap();
        let value = shared.buffer.pop_front();
        let waker = if value.is_some() {
            shared.send_waiters.grant_oldest(())
        } else {
            None
        };
        drop(shared);
        if let Some(waker) = waker {
            waker.wake();
        }
        value
    }

    /// Close the channel, waking every parked sender and receiver.
    pub fn close(&mut self) {
        let mut shared = self.shared.lock().unwrap();
        shared.closed = true;
        let mut wakers = shared.send_waiters.grant_all(());
        wakers.extend(shared.recv_waiters.grant_all(()));
        drop(shared);
        for waker in wakers {
            waker.wake();
        }
    }
}

impl<T> Drop for Receiver<T> {
    fn drop(&mut self) {
        let mut shared = self.shared.lock().unwrap();
        shared.closed = true;
        let wakers = shared.send_waiters.grant_all(());
        drop(shared);
        for waker in wakers {
            waker.wake();
        }
    }
}

/// Future returned by [`Receiver::recv`].
pub struct RecvFuture<'a, T> {
    receiver: &'a mut Receiver<T>,
    id: Option<u64>,
}

impl<'a, T> Future for RecvFuture<'a, T> {
    type Output = Result<T, ()>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();
        let mut shared = this.receiver.shared.lock().unwrap();
        if let Some(value) = shared.buffer.pop_front() {
            if let Some(id) = this.id.take() {
                shared.recv_waiters.deregister(id);
            }
            let waker = shared.send_waiters.grant_oldest(());
            drop(shared);
            if let Some(waker) = waker {
                waker.wake();
            }
            return Poll::Ready(Ok(value));
        }
        if shared.closed {
            if let Some(id) = this.id.take() {
                shared.recv_waiters.deregister(id);
            }
            return Poll::Ready(Err(()));
        }

        this.id = Some(match this.id {
            Some(id) => match shared.recv_waiters.poll_waiter(id, cx.waker()) {
                WaiterPoll::Pending => id,
                WaiterPoll::Granted(()) | WaiterPoll::NotRegistered => {
                    shared.recv_waiters.register(cx.waker().clone())
                }
            },
            None => shared.recv_waiters.register(cx.waker().clone()),
        });
        Poll::Pending
    }
}

impl<'a, T> Drop for RecvFuture<'a, T> {
    fn drop(&mut self) {
        if let Some(id) = self.id
            && let Ok(mut shared) = self.receiver.shared.lock()
        {
            shared.recv_waiters.deregister(id);
        }
    }
}

/// Create a bounded channel with the given buffer capacity.
#[must_use]
pub fn channel<T>(capacity: usize) -> (Sender<T>, Receiver<T>) {
    let shared = Arc::new(Mutex::new(SharedState {
        buffer: VecDeque::with_capacity(capacity),
        capacity,
        sender_count: 1,
        closed: false,
        send_waiters: WaitQueue::new(),
        recv_waiters: WaitQueue::new(),
    }));
    (
        Sender {
            shared: shared.clone(),
        },
        Receiver { shared },
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::future::Future;
    use std::pin::Pin;
    use std::sync::{
        Arc,
        atomic::{AtomicUsize, Ordering},
    };
    use std::task::{Context, Poll, Wake, Waker};

    fn poll_future<F: Future + Unpin>(future: &mut F) -> Poll<F::Output> {
        let mut context = Context::from_waker(Waker::noop());
        Pin::new(future).poll(&mut context)
    }

    fn poll_future_with_waker<F: Future + Unpin>(future: &mut F, waker: &Waker) -> Poll<F::Output> {
        let mut context = Context::from_waker(waker);
        Pin::new(future).poll(&mut context)
    }

    struct CountingWake(Arc<AtomicUsize>);

    impl Wake for CountingWake {
        fn wake(self: Arc<Self>) {
            self.0.fetch_add(1, Ordering::Release);
        }

        fn wake_by_ref(self: &Arc<Self>) {
            self.0.fetch_add(1, Ordering::Release);
        }
    }

    #[test]
    fn test_mpsc_send_recv() {
        let (tx, mut rx) = channel(10);
        tx.try_send(1).unwrap();
        tx.try_send(2).unwrap();
        tx.try_send(3).unwrap();
        assert_eq!(rx.try_recv(), Some(1));
        assert_eq!(rx.try_recv(), Some(2));
        assert_eq!(rx.try_recv(), Some(3));
        assert!(rx.try_recv().is_none());
    }

    #[test]
    fn test_mpsc_closed_sender() {
        let (tx, mut rx) = channel::<i32>(10);
        tx.try_send(1).unwrap();
        drop(tx);
        assert_eq!(rx.try_recv(), Some(1));
        assert!(rx.try_recv().is_none());
    }

    #[test]
    fn test_mpsc_closed_receiver() {
        let (tx, rx) = channel::<i32>(10);
        drop(rx);
        assert!(tx.try_send(1).is_err());
    }

    #[test]
    fn test_mpsc_capacity() {
        let (tx, mut rx) = channel(2);
        assert!(tx.try_send(1).is_ok());
        assert!(tx.try_send(2).is_ok());
        assert!(tx.try_send(3).is_err());
        let _ = rx.try_recv();
        assert!(tx.try_send(3).is_ok());
    }

    #[test]
    fn test_mpsc_sender_clone() {
        let (tx1, mut rx) = channel(10);
        let tx2 = tx1.clone();
        tx1.try_send(1).unwrap();
        tx2.try_send(2).unwrap();
        drop(tx1);
        drop(tx2);
        assert_eq!(rx.try_recv(), Some(1));
        assert_eq!(rx.try_recv(), Some(2));
        assert!(rx.try_recv().is_none());
    }

    #[test]
    fn test_mpsc_sender_strong_count() {
        let (tx1, _) = channel::<i32>(10);
        assert_eq!(tx1.sender_strong_count(), 1);
        let tx2 = tx1.clone();
        assert_eq!(tx1.sender_strong_count(), 2);
        drop(tx2);
        assert_eq!(tx1.sender_strong_count(), 1);
    }

    #[test]
    fn test_mpsc_send_pending_then_recv() {
        let (tx, mut rx) = channel(1);
        tx.try_send(1).unwrap();
        let mut send = tx.send(2);
        assert!(matches!(poll_future(&mut send), Poll::Pending));
        let _ = rx.try_recv();
        assert!(matches!(poll_future(&mut send), Poll::Ready(Ok(()))));
    }

    #[test]
    fn test_mpsc_async_recv_pending_then_send() {
        let (tx, mut rx) = channel(1);
        let mut recv = rx.recv();
        assert!(matches!(poll_future(&mut recv), Poll::Pending));
        tx.try_send(42).unwrap();
        assert!(matches!(poll_future(&mut recv), Poll::Ready(Ok(42))));
    }

    #[test]
    fn test_mpsc_send_future_dropped_cancels_waiter() {
        let (tx, _rx) = channel(1);
        tx.try_send(1).unwrap();
        // Send future goes pending, then is dropped without completing
        let mut send = tx.send(2);
        assert!(matches!(poll_future(&mut send), Poll::Pending));
        drop(send);
        // The full send_waiters queue must be empty after the drop
        assert!(tx.shared.lock().unwrap().send_waiters.is_empty());
    }

    #[test]
    fn test_mpsc_recv_future_dropped_cancels_waiter() {
        let (tx, mut rx) = channel(1);
        tx.try_send(1).unwrap();
        // Consume the item, then recv goes pending waiting for next item
        let _ = rx.try_recv();
        let mut recv = rx.recv();
        assert!(matches!(poll_future(&mut recv), Poll::Pending));
        drop(recv);
        // The full recv_waiters queue must be empty after the drop
        assert!(tx.shared.lock().unwrap().recv_waiters.is_empty());
    }

    #[test]
    fn oldest_pending_sender_is_woken_first() {
        let (tx, mut rx) = channel(1);
        tx.try_send(1).expect("initial send must fill the channel");

        let first_wakes = Arc::new(AtomicUsize::new(0));
        let second_wakes = Arc::new(AtomicUsize::new(0));
        let first_waker = Waker::from(Arc::new(CountingWake(Arc::clone(&first_wakes))));
        let second_waker = Waker::from(Arc::new(CountingWake(Arc::clone(&second_wakes))));
        let mut first = tx.send(2);
        let mut second = tx.send(3);

        assert!(poll_future_with_waker(&mut first, &first_waker).is_pending());
        assert!(poll_future_with_waker(&mut second, &second_waker).is_pending());

        assert_eq!(rx.try_recv(), Some(1));
        assert_eq!(first_wakes.load(Ordering::Acquire), 1);
        assert_eq!(second_wakes.load(Ordering::Acquire), 0);

        assert!(matches!(
            poll_future_with_waker(&mut first, &first_waker),
            Poll::Ready(Ok(()))
        ));
        assert_eq!(rx.try_recv(), Some(2));
        assert_eq!(second_wakes.load(Ordering::Acquire), 1);
        assert!(matches!(
            poll_future_with_waker(&mut second, &second_waker),
            Poll::Ready(Ok(()))
        ));
        assert_eq!(rx.try_recv(), Some(3));
    }
}