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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
use crate::{
internal::{acquire_internal, Internal},
signal::AsyncSignal,
AsyncReceiver, ReceiveError, SendError,
};
use core::{
cell::UnsafeCell,
fmt::Debug,
marker::PhantomPinned,
mem::{transmute, ManuallyDrop},
pin::Pin,
task::Poll,
};
use branches::{likely, unlikely};
use futures_core::{FusedStream, Future, Stream};
#[repr(u8)]
#[derive(PartialEq, Clone, Copy)]
pub(crate) enum FutureState {
Unregistered,
Pending,
Success,
Failure,
Done,
}
#[cold]
fn mark_branch_unlikely() {}
#[allow(unused)]
impl FutureState {
#[inline(always)]
fn is_pending(&self) -> bool {
*self == FutureState::Pending
}
#[inline(always)]
fn is_done(&self) -> bool {
*self == FutureState::Done
}
#[inline(always)]
fn is_unregistered(&self) -> bool {
*self == FutureState::Unregistered
}
#[inline(always)]
fn is_success(&self) -> bool {
*self == FutureState::Success
}
#[inline(always)]
fn is_failure(&self) -> bool {
*self == FutureState::Failure
}
}
/// SendFuture is a future for sending an object to a channel asynchronously.
/// It must be polled to complete the send operation.
#[must_use = "futures do nothing unless you .await or poll them"]
pub struct SendFuture<'a, T> {
internal: &'a Internal<T>,
sig: AsyncSignal<T>,
_pinned: PhantomPinned,
}
impl<T> Debug for SendFuture<'_, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "SendFuture {{ .. }}")
}
}
impl<T> Drop for SendFuture<'_, T> {
fn drop(&mut self) {
let state = self.sig.state();
if unlikely(!state.is_done()) {
// If we are still pending, try to cancel the send operation.
// Cancellation succeeds → we still own the data and must drop it.
// Otherwise the receiver may already own the payload; we wait for it
// to finish before dropping the future.
let mut need_drop = true;
if state.is_pending()
&& acquire_internal(self.internal).cancel_send_signal(self.sig.as_tagged_ptr())
{
// Cancellation succeeded – we still own the data.
need_drop = true;
} else if !state.is_unregistered() && self.sig.blocking_wait() {
// A receiver has taken ownership; it will drop the data.
need_drop = false;
}
if need_drop {
// SAFETY: the payload has never been moved out of the signal.
unsafe { self.sig.drop_data() };
}
}
}
}
impl<'a, T> SendFuture<'a, T> {
/// Creates a new SendFuture with the given internal channel and data.
#[inline(always)]
pub(crate) fn new(internal: &'a Internal<T>, data: T) -> Self {
SendFuture {
internal,
sig: AsyncSignal::new_send(data),
_pinned: PhantomPinned,
}
}
#[inline(always)]
pub(crate) fn new_finished(internal: &'a Internal<T>) -> Self {
SendFuture {
internal,
sig: AsyncSignal::new_send_finished(),
_pinned: PhantomPinned,
}
}
}
impl<T> Future for SendFuture<'_, T> {
type Output = Result<(), SendError<T>>;
#[inline(always)]
fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
match this.sig.state() {
FutureState::Unregistered => {
let cap = this.internal.capacity();
let mut internal = acquire_internal(this.internal);
if unlikely(internal.recv_count == 0) {
drop(internal);
this.sig.set_state_relaxed(FutureState::Done);
// SAFETY: the data failed to move, we can safely return it to user
unsafe {
return Poll::Ready(Err(SendError(this.sig.assume_init())));
}
}
if let Some(first) = internal.next_recv() {
drop(internal);
this.sig.set_state_relaxed(FutureState::Done);
// SAFETY: data is inited and available from constructor
unsafe { first.send(this.sig.assume_init()) }
return Poll::Ready(Ok(()));
}
if cap > 0 && internal.queue.len() < cap {
this.sig.set_state_relaxed(FutureState::Done);
// SAFETY: data is inited and available from constructor
internal.queue.push_back(unsafe { this.sig.assume_init() });
drop(internal);
return Poll::Ready(Ok(()));
}
this.sig.set_state(FutureState::Pending);
// SAFETY: waker is empty, it is safe to init it here
unsafe {
this.sig.update_waker(cx.waker());
}
// send directly to the waitlist
internal.push_signal(this.sig.dynamic_ptr());
drop(internal);
Poll::Pending
}
FutureState::Success => {
this.sig.set_state_relaxed(FutureState::Done);
Poll::Ready(Ok(()))
}
FutureState::Pending => {
mark_branch_unlikely();
let waker = cx.waker();
// SAFETY: signal waker is valid as we inited it in future pending state
if unlikely(unsafe { !this.sig.will_wake(waker) }) {
// Waker is changed and we need to update waker in the waiting list
let internal = acquire_internal(this.internal);
if internal.send_signal_exists(this.sig.as_tagged_ptr()) {
// SAFETY: signal is not shared with other thread yet so it's safe to
// update waker locally
unsafe {
this.sig.update_waker(waker);
}
drop(internal);
return Poll::Pending;
}
drop(internal);
// signal is already shared, and data will be available shortly, so wait
// synchronously and return the result note:
// it's not possible safely to update waker after the signal is shared,
// but we know data will be ready shortly,
// we can wait synchronously and receive it.
this.sig.set_state(FutureState::Done);
if likely(this.sig.blocking_wait()) {
return Poll::Ready(Ok(()));
}
// the data failed to move, we can safely return it to user
Poll::Ready(Err(SendError(unsafe { this.sig.assume_init() })))
} else {
Poll::Pending
}
}
FutureState::Failure => {
mark_branch_unlikely();
this.sig.set_state_relaxed(FutureState::Done);
// SAFETY: the data failed to move, we can safely return it to user
Poll::Ready(Err(SendError(unsafe { this.sig.assume_init() })))
}
FutureState::Done => {
mark_branch_unlikely();
panic!("polled after result is already returned")
}
}
}
}
/// ReceiveFuture is a future for receiving an object from a channel
/// asynchronously. It must be polled to complete the receive operation.
#[must_use = "futures do nothing unless you .await or poll them"]
pub struct ReceiveFuture<'a, T> {
is_stream: bool,
internal: &'a Internal<T>,
sig: AsyncSignal<T>,
_pinned: PhantomPinned,
}
impl<T> Drop for ReceiveFuture<'_, T> {
fn drop(&mut self) {
let state = self.sig.state();
if unlikely(!state.is_done()) {
// try to cancel the signal if we are still waiting
if state.is_pending()
&& acquire_internal(self.internal).cancel_recv_signal(self.sig.as_tagged_ptr())
{
// signal canceled
return;
}
// we failed to cancel the signal,
// either it is unregistered or a sender got signal ownership, receiver should
// wait until the response
if !state.is_unregistered() && self.sig.blocking_wait() {
// got ownership of data that is not going to be used ever again, so drop it
// this is actually a bug in user code but we should handle it gracefully
// and we warn user in debug mode
// SAFETY: data is not moved it's safe to drop it or put it back to the channel
// queue
unsafe {
if self.internal.capacity() == 0 {
#[cfg(debug_assertions)]
println!(
"warning: ReceiveFuture dropped while send operation is in progress"
);
self.sig.drop_data();
} else {
// fallback: push it back to the channel queue
acquire_internal(self.internal)
.queue
.push_front(self.sig.assume_init())
}
}
}
}
}
}
impl<T> Debug for ReceiveFuture<'_, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "ReceiveFuture {{ .. }}")
}
}
impl<'a, T> ReceiveFuture<'a, T> {
#[inline(always)]
pub(crate) fn new_ref(internal: &'a Internal<T>) -> Self {
Self {
sig: AsyncSignal::new_recv(),
internal,
is_stream: false,
_pinned: PhantomPinned,
}
}
}
impl<T> Future for ReceiveFuture<'_, T> {
type Output = Result<T, ReceiveError>;
#[inline(always)]
fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
loop {
return match this.sig.state() {
FutureState::Unregistered => {
let cap = this.internal.capacity();
let mut internal = acquire_internal(this.internal);
if unlikely(internal.recv_count == 0) {
drop(internal);
this.sig.set_state_relaxed(FutureState::Done);
return Poll::Ready(Err(ReceiveError()));
}
if cap > 0 {
if let Some(v) = internal.queue.pop_front() {
if let Some(t) = internal.next_send() {
// if there is a sender take its data and push it into the queue
unsafe { internal.queue.push_back(t.recv()) }
}
drop(internal);
this.sig.set_state_relaxed(FutureState::Done);
return Poll::Ready(Ok(v));
}
}
if let Some(t) = internal.next_send() {
drop(internal);
this.sig.set_state_relaxed(FutureState::Done);
return Poll::Ready(Ok(unsafe { t.recv() }));
}
if unlikely(internal.send_count == 0) {
this.sig.set_state_relaxed(FutureState::Done);
return Poll::Ready(Err(ReceiveError()));
}
this.sig.set_state(FutureState::Pending);
// SAFETY: waker is NOOP and not shared yet, it is safe to init it here
unsafe {
this.sig.update_waker(cx.waker());
}
// no active waiter so push to the queue
internal.push_signal(this.sig.dynamic_ptr());
drop(internal);
Poll::Pending
}
FutureState::Success => {
this.sig.set_state_relaxed(FutureState::Done);
// SAFETY: data is received and safe to read
Poll::Ready(Ok(unsafe { this.sig.assume_init() }))
}
FutureState::Pending => {
let waker = cx.waker();
// SAFETY: signal waker is valid as we inited it in future pending state
if unsafe { !this.sig.will_wake(waker) } {
// the Waker is changed and we need to update waker in the waiting
// list
let internal = acquire_internal(this.internal);
if internal.recv_signal_exists(this.sig.as_tagged_ptr()) {
// SAFETY: signal is not shared with other thread yet so it's safe to
// update waker locally
unsafe {
this.sig.update_waker(waker);
}
drop(internal);
Poll::Pending
} else {
drop(internal);
// the signal is already shared, and data will be available shortly,
// so wait synchronously and return the result
// note: it's not possible safely to update waker after the signal
// is shared, but we know data will be ready shortly,
// we can wait synchronously and receive it.
this.sig.set_state_relaxed(FutureState::Done);
if likely(this.sig.blocking_wait()) {
// SAFETY: data is received and safe to read
Poll::Ready(Ok(unsafe { this.sig.assume_init() }))
} else {
Poll::Ready(Err(ReceiveError()))
}
}
} else {
Poll::Pending
}
}
FutureState::Failure => {
mark_branch_unlikely();
this.sig.set_state_relaxed(FutureState::Done);
Poll::Ready(Err(ReceiveError()))
}
FutureState::Done => {
mark_branch_unlikely();
if this.is_stream {
this.sig.set_state_relaxed(FutureState::Unregistered);
continue;
}
panic!("polled after result is already returned")
}
};
}
}
}
/// ReceiveStream is a stream for receiving objects from a channel
/// asynchronously.
pub struct ReceiveStream<'a, T: 'a> {
future: Pin<Box<ReceiveFuture<'a, T>>>,
terminated: bool,
receiver: &'a AsyncReceiver<T>,
}
impl<T> Debug for ReceiveStream<'_, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "ReceiveStream {{ .. }}")
}
}
impl<T> Stream for ReceiveStream<'_, T> {
type Item = T;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
if unlikely(self.terminated) {
return Poll::Ready(None);
}
// SAFETY: future is pinned as stream is pinned to a location too
match self.future.as_mut().poll(cx) {
Poll::Ready(res) => match res {
Ok(d) => Poll::Ready(Some(d)),
Err(_) => {
mark_branch_unlikely();
self.terminated = true;
Poll::Ready(None)
}
},
Poll::Pending => Poll::Pending,
}
}
}
impl<T> FusedStream for ReceiveStream<'_, T> {
fn is_terminated(&self) -> bool {
self.receiver.is_terminated()
}
}
impl<'a, T> ReceiveStream<'a, T> {
pub(crate) fn new_borrowed(receiver: &'a AsyncReceiver<T>) -> Self {
let mut future = receiver.recv();
future.is_stream = true;
ReceiveStream {
future: Box::pin(future),
terminated: false,
receiver,
}
}
}
/// ReceiveStreamOwned is a stream for receiving objects from a channel
/// asynchronously and owns the receiver.
pub struct ReceiveStreamOwned<T: 'static> {
future: ManuallyDrop<Pin<Box<ReceiveFuture<'static, T>>>>,
terminated: bool,
receiver: ManuallyDrop<Pin<Box<AsyncReceiver<T>>>>,
}
impl<T: 'static> Debug for ReceiveStreamOwned<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "ReceiveStreamOwned {{ .. }}")
}
}
impl<T: 'static> Stream for ReceiveStreamOwned<T> {
type Item = T;
fn poll_next(
mut self: Pin<&mut Self>,
cx: &mut core::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
if unlikely(self.terminated) {
return Poll::Ready(None);
}
// SAFETY: future is pinned as stream is pinned to a location too
// `future` is initialized and pinned for the lifetime of the stream.
let future = &mut *self.future;
match future.as_mut().poll(cx) {
Poll::Ready(res) => match res {
Ok(d) => Poll::Ready(Some(d)),
Err(_) => {
mark_branch_unlikely();
self.terminated = true;
Poll::Ready(None)
}
},
Poll::Pending => Poll::Pending,
}
}
}
impl<T: 'static> FusedStream for ReceiveStreamOwned<T> {
fn is_terminated(&self) -> bool {
// Receiver is alive while the stream exists.
(&*self.receiver).is_terminated()
}
}
impl<T: 'static> ReceiveStreamOwned<T> {
pub(crate) fn new(receiver: AsyncReceiver<T>) -> Self {
let receiver = Box::pin(receiver);
let mut future = ReceiveFuture::new_ref(&receiver.internal);
future.is_stream = true;
let future = unsafe {
// Safety: `receiver` is pinned and will not be moved for the lifetime
// of `future`, so extending the lifetime is safe here.
transmute::<Pin<Box<ReceiveFuture<'_, T>>>, Pin<Box<ReceiveFuture<'static, T>>>>(
Box::pin(future),
)
};
ReceiveStreamOwned {
future: ManuallyDrop::new(future),
terminated: false,
receiver: ManuallyDrop::new(receiver),
}
}
}
impl<T: 'static> Drop for ReceiveStreamOwned<T> {
fn drop(&mut self) {
// Ensure the future (which borrows the receiver) is dropped before the receiver.
unsafe {
ManuallyDrop::drop(&mut self.future);
ManuallyDrop::drop(&mut self.receiver);
}
}
}
/// SendManyFuture is a future for sending multiple objects to a channel
/// asynchronously. It must be polled to complete the send operation.
#[must_use = "futures do nothing unless you .await or poll them"]
#[derive(Debug)]
pub struct SendManyFuture<'a, 'b, T> {
internal: &'a Internal<T>,
// This is a UnsafeCell, because it can be shared in WaitQueue of the channel
fut: UnsafeCell<SendFuture<'a, T>>,
// Elements that we are writing to the channel
elements: &'b mut std::collections::VecDeque<T>,
// Future is finished and no longer should be polled
finished: bool,
// If set we are in wait queue and fut shall not be used as mutable
in_wait_queue: bool,
_pinned: PhantomPinned,
}
impl<'a, 'b, T> SendManyFuture<'a, 'b, T> {
#[inline(always)]
pub(crate) fn new(
internal: &'a Internal<T>,
elements: &'b mut std::collections::VecDeque<T>,
) -> Self {
SendManyFuture {
internal,
fut: UnsafeCell::new(SendFuture::new_finished(internal)),
elements,
finished: false,
in_wait_queue: false,
_pinned: PhantomPinned,
}
}
}
impl<'a, 'b, T> Future for SendManyFuture<'a, 'b, T> {
type Output = Result<(), SendError<T>>;
#[inline(always)]
fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
loop {
if unlikely(this.finished) {
panic!("polled after completion");
}
if this.in_wait_queue {
// SAFETY: this is pinned therefore it's safe to create a pinned reference
let fut = unsafe { Pin::new_unchecked(this.fut.get_mut()) };
match fut.poll(cx) {
Poll::Ready(res) => {
if this.elements.is_empty() {
this.finished = true;
return Poll::Ready(res);
}
}
Poll::Pending => return Poll::Pending,
}
this.in_wait_queue = false;
}
// If there is nothing to send we are done.
if unlikely(this.elements.is_empty()) {
this.finished = true;
return Poll::Ready(Ok(()));
}
// Channel capacity
let cap = this.internal.capacity();
// Acquire a mutable reference to the internal state.
let mut internal = acquire_internal(this.internal);
// Channel is closed from the other side
if unlikely(internal.recv_count == 0) {
// Return the first element that could not be sent.
let first = this.elements.pop_front().unwrap();
drop(internal);
this.finished = true;
return Poll::Ready(Err(SendError(first)));
}
// -----------------------------------------------------------------
// 1) Deliver directly to waiting receivers.
// -----------------------------------------------------------------
while let Some(waiter) = internal.next_recv() {
let v = this.elements.pop_front().unwrap();
// SAFETY: it is safe to send an owned waiter once
unsafe {
waiter.send(v);
}
if unlikely(this.elements.is_empty()) {
// No more elements to send.
drop(internal);
this.finished = true;
return Poll::Ready(Ok(()));
}
}
if unlikely(this.elements.is_empty()) {
// Nothing left to send.
drop(internal);
this.finished = true;
return Poll::Ready(Ok(()));
}
// -----------------------------------------------------------------
// 2) Fill the channel’s queue (if it has a bounded/unbounded capacity).
// -----------------------------------------------------------------
if cap > 0 {
while internal.queue.len() < cap {
if let Some(v) = this.elements.pop_front() {
internal.queue.push_back(v);
} else {
// All elements have been queued.
drop(internal);
this.finished = true;
return Poll::Ready(Ok(()));
}
}
}
// -----------------------------------------------------------------
// 3) If there are still elements, send the next one via a signal
// -----------------------------------------------------------------
if let Some(v) = this.elements.pop_front() {
// SAFETY: we checked above and we are not in any wait queue as we are not
// registered in the queue yet.
unsafe {
this.fut.get_mut().sig.reset_send(v);
}
// This is pinned therefore this.fut is also pinned.
internal.push_signal(this.fut.get_mut().sig.dynamic_ptr());
this.in_wait_queue = true;
drop(internal);
// go poll the future to register the waker or return early if message already
// arrived
continue;
} else {
// No more elements left.
this.finished = true;
return Poll::Ready(Ok(()));
}
}
}
}
/// DrainIntoBlockingFuture is a future for draining all available messages from a channel
/// into a vector, blocking until at least one message is received.
#[must_use = "futures do nothing unless you .await or poll them"]
pub struct DrainIntoBlockingFuture<'a, 'b, T> {
internal: &'a Internal<T>,
sig: AsyncSignal<T>,
vec: &'b mut Vec<T>,
_pinned: PhantomPinned,
}
impl<T> Drop for DrainIntoBlockingFuture<'_, '_, T> {
fn drop(&mut self) {
let state = self.sig.state();
if unlikely(!state.is_done()) {
// try to cancel the signal if we are still waiting
if state.is_pending()
&& acquire_internal(self.internal).cancel_recv_signal(self.sig.as_tagged_ptr())
{
// signal canceled
return;
}
// we failed to cancel the signal,
// either it is unregistered or a sender got signal ownership, receiver should
// wait until the response
if !state.is_unregistered() && self.sig.blocking_wait() {
// got ownership of data that is not going to be used ever again, so drop it
// SAFETY: data is not moved it's safe to drop it or put it back to the channel queue
unsafe {
if self.internal.capacity() == 0 {
self.sig.drop_data();
} else {
// fallback: push it back to the channel queue
acquire_internal(self.internal)
.queue
.push_front(self.sig.assume_init())
}
}
}
}
}
}
impl<T> Debug for DrainIntoBlockingFuture<'_, '_, T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "DrainIntoBlockingFuture {{ .. }}")
}
}
impl<'a, 'b, T> DrainIntoBlockingFuture<'a, 'b, T> {
#[inline(always)]
pub(crate) fn new(internal: &'a Internal<T>, vec: &'b mut Vec<T>) -> Self {
Self {
sig: AsyncSignal::new_recv(),
internal,
vec,
_pinned: PhantomPinned,
}
}
}
impl<T> Future for DrainIntoBlockingFuture<'_, '_, T> {
type Output = Result<usize, ReceiveError>;
#[inline(always)]
fn poll(self: Pin<&mut Self>, cx: &mut core::task::Context<'_>) -> Poll<Self::Output> {
let this = unsafe { self.get_unchecked_mut() };
match this.sig.state() {
FutureState::Unregistered => {
let vec_initial_length = this.vec.len();
let mut internal = acquire_internal(this.internal);
// Check if channel is closed
if unlikely(internal.recv_count == 0) {
this.sig.set_state_relaxed(FutureState::Done);
return Poll::Ready(Err(ReceiveError()));
}
// Calculate required capacity and reserve
let required_cap = internal.queue.len() + {
if internal.recv_blocking {
0
} else {
internal.wait_list.len()
}
};
let remaining_cap = this.vec.capacity() - vec_initial_length;
if required_cap > remaining_cap {
this.vec
.reserve(vec_initial_length + required_cap - remaining_cap);
}
// Drain queue
this.vec.extend(internal.queue.drain(..));
// Drain wait_list send signals
while let Some(p) = internal.next_send() {
// SAFETY: it's safe to receive from owned signal once
unsafe { this.vec.push(p.recv()) }
}
// If got data, return immediately
let count = this.vec.len() - vec_initial_length;
if count > 0 {
this.sig.set_state_relaxed(FutureState::Done);
return Poll::Ready(Ok(count));
}
// No data, check if there are still senders
if unlikely(internal.send_count == 0) {
this.sig.set_state_relaxed(FutureState::Done);
return Poll::Ready(Err(ReceiveError()));
}
// Register signal and wait
this.sig.set_state(FutureState::Pending);
// SAFETY: waker is NOOP and not shared yet, it is safe to init it here
unsafe {
this.sig.update_waker(cx.waker());
}
internal.push_signal(this.sig.dynamic_ptr());
drop(internal);
Poll::Pending
}
FutureState::Success => {
this.sig.set_state_relaxed(FutureState::Done);
// SAFETY: data is received and safe to read
this.vec.push(unsafe { this.sig.assume_init() });
Poll::Ready(Ok(1))
}
FutureState::Pending => {
let waker = cx.waker();
// SAFETY: signal waker is valid as we inited it in future pending state
if unsafe { !this.sig.will_wake(waker) } {
// the Waker is changed and we need to update waker in the waiting list
let internal = acquire_internal(this.internal);
if internal.recv_signal_exists(this.sig.as_tagged_ptr()) {
// SAFETY: signal is not shared with other thread yet so it's safe to
// update waker locally
unsafe {
this.sig.update_waker(waker);
}
drop(internal);
Poll::Pending
} else {
drop(internal);
// the signal is already shared, and data will be available shortly,
// so wait synchronously and return the result
this.sig.set_state_relaxed(FutureState::Done);
if likely(this.sig.blocking_wait()) {
// SAFETY: data is received and safe to read
this.vec.push(unsafe { this.sig.assume_init() });
Poll::Ready(Ok(1))
} else {
Poll::Ready(Err(ReceiveError()))
}
}
} else {
Poll::Pending
}
}
FutureState::Failure => {
mark_branch_unlikely();
this.sig.set_state_relaxed(FutureState::Done);
Poll::Ready(Err(ReceiveError()))
}
FutureState::Done => {
mark_branch_unlikely();
panic!("polled after result is already returned")
}
}
}
}