Skip to main content

tokio/sync/mpsc/
block.rs

1use crate::loom::cell::UnsafeCell;
2use crate::loom::sync::atomic::{AtomicPtr, AtomicUsize};
3
4use std::alloc::Layout;
5use std::mem::MaybeUninit;
6use std::ops;
7use std::ptr::{self, NonNull};
8use std::sync::atomic::Ordering::{self, AcqRel, Acquire, Release};
9
10/// A block in a linked list.
11///
12/// Each block in the list can hold up to `BLOCK_CAP` messages.
13pub(crate) struct Block<T> {
14    /// The header fields.
15    header: BlockHeader<T>,
16
17    /// Array containing values pushed into the block. Values are stored in a
18    /// continuous array in order to improve cache line behavior when reading.
19    /// The values must be manually dropped.
20    values: Values<T>,
21}
22
23/// Extra fields for a `Block<T>`.
24struct BlockHeader<T> {
25    /// The start index of this block.
26    ///
27    /// Slots in this block have indices in `start_index .. start_index + BLOCK_CAP`.
28    start_index: usize,
29
30    /// The next block in the linked list.
31    next: AtomicPtr<Block<T>>,
32
33    /// Bitfield tracking slots that are ready to have their values consumed.
34    ready_slots: AtomicUsize,
35
36    /// The observed `tail_position` value *after* the block has been passed by
37    /// `block_tail`.
38    observed_tail_position: UnsafeCell<usize>,
39}
40
41pub(crate) enum Read<T> {
42    Value(T),
43    Closed,
44}
45
46#[repr(transparent)]
47struct Values<T>([UnsafeCell<MaybeUninit<T>>; BLOCK_CAP]);
48
49use super::BLOCK_CAP;
50
51/// Masks an index to get the block identifier.
52const BLOCK_MASK: usize = !(BLOCK_CAP - 1);
53
54/// Masks an index to get the value offset in a block.
55const SLOT_MASK: usize = BLOCK_CAP - 1;
56
57/// Flag tracking that a block has gone through the sender's release routine.
58///
59/// When this is set, the receiver may consider freeing the block.
60const RELEASED: usize = 1 << BLOCK_CAP;
61
62/// Flag tracking all senders dropped.
63///
64/// When this flag is set, the send half of the channel has closed.
65const TX_CLOSED: usize = RELEASED << 1;
66
67/// Mask covering all bits used to track slot readiness.
68const READY_MASK: usize = RELEASED - 1;
69
70/// Returns the index of the first slot in the block referenced by `slot_index`.
71#[inline(always)]
72pub(crate) fn start_index(slot_index: usize) -> usize {
73    BLOCK_MASK & slot_index
74}
75
76/// Returns the offset into the block referenced by `slot_index`.
77#[inline(always)]
78pub(crate) fn offset(slot_index: usize) -> usize {
79    SLOT_MASK & slot_index
80}
81
82generate_addr_of_methods! {
83    impl<T> Block<T> {
84        unsafe fn addr_of_header(self: NonNull<Self>) -> NonNull<BlockHeader<T>> {
85            &self.header
86        }
87
88        unsafe fn addr_of_values(self: NonNull<Self>) -> NonNull<Values<T>> {
89            &self.values
90        }
91    }
92}
93
94impl<T> Block<T> {
95    pub(crate) fn new(start_index: usize) -> Box<Block<T>> {
96        unsafe {
97            // Allocate the block on the heap.
98            // SAFETY: The size of the Block<T> is non-zero, since it is at least the size of the header.
99            let block = std::alloc::alloc(Layout::new::<Block<T>>()) as *mut Block<T>;
100            let block = match NonNull::new(block) {
101                Some(block) => block,
102                None => std::alloc::handle_alloc_error(Layout::new::<Block<T>>()),
103            };
104
105            // Write the header to the block.
106            Block::addr_of_header(block).as_ptr().write(BlockHeader {
107                // The absolute index in the channel of the first slot in the block.
108                start_index,
109
110                // Pointer to the next block in the linked list.
111                next: AtomicPtr::new(ptr::null_mut()),
112
113                ready_slots: AtomicUsize::new(0),
114
115                observed_tail_position: UnsafeCell::new(0),
116            });
117
118            // Initialize the values array.
119            Values::initialize(Block::addr_of_values(block));
120
121            // Convert the pointer to a `Box`.
122            // Safety: The raw pointer was allocated using the global allocator, and with
123            // the layout for a `Block<T>`, so it's valid to convert it to box.
124            Box::from_raw(block.as_ptr())
125        }
126    }
127
128    /// Returns `true` if the block matches the given index.
129    pub(crate) fn is_at_index(&self, index: usize) -> bool {
130        debug_assert!(offset(index) == 0);
131        self.header.start_index == index
132    }
133
134    /// Returns the number of blocks between `self` and the block at the
135    /// specified index.
136    ///
137    /// `start_index` must represent a block *after* `self`.
138    pub(crate) fn distance(&self, other_index: usize) -> usize {
139        debug_assert!(offset(other_index) == 0);
140        other_index.wrapping_sub(self.header.start_index) / BLOCK_CAP
141    }
142
143    /// Reads the value at the given offset.
144    ///
145    /// Returns `None` if the slot is empty.
146    ///
147    /// # Safety
148    ///
149    /// To maintain safety, the caller must ensure:
150    ///
151    /// * No concurrent access to the slot.
152    pub(crate) unsafe fn read(&self, slot_index: usize) -> Option<Read<T>> {
153        let offset = offset(slot_index);
154
155        let ready_bits = self.header.ready_slots.load(Acquire);
156
157        if !is_ready(ready_bits, offset) {
158            if is_tx_closed(ready_bits) {
159                return Some(Read::Closed);
160            }
161
162            return None;
163        }
164
165        // Get the value
166        //
167        // Safety:
168        //
169        // 1. The caller guarantees that there is no concurrent access to the slot.
170        // 2. The `UnsafeCell` always give us a valid pointer to the value.
171        let value = self.values[offset].with(|ptr| unsafe { ptr::read(ptr) });
172
173        // Safety: the ready bit is set, so the value has been initialized.
174        Some(Read::Value(unsafe { value.assume_init() }))
175    }
176
177    /// Returns true if *this* block has a value in the given slot.
178    ///
179    /// Always returns false when given an index from a different block.
180    pub(crate) fn has_value(&self, slot_index: usize) -> bool {
181        if slot_index < self.header.start_index {
182            return false;
183        }
184        if slot_index >= self.header.start_index + super::BLOCK_CAP {
185            return false;
186        }
187
188        let offset = offset(slot_index);
189        let ready_bits = self.header.ready_slots.load(Acquire);
190        is_ready(ready_bits, offset)
191    }
192
193    /// Writes a value to the block at the given offset.
194    ///
195    /// # Safety
196    ///
197    /// To maintain safety, the caller must ensure:
198    ///
199    /// * The slot is empty.
200    /// * No concurrent access to the slot.
201    pub(crate) unsafe fn write(&self, slot_index: usize, value: T) {
202        // Get the offset into the block
203        let slot_offset = offset(slot_index);
204
205        self.values[slot_offset].with_mut(|ptr| {
206            // Safety: the caller guarantees that there is no concurrent access to the slot
207            unsafe {
208                ptr::write(ptr, MaybeUninit::new(value));
209            }
210        });
211
212        // Release the value. After this point, the slot ref may no longer
213        // be used. It is possible for the receiver to free the memory at
214        // any point.
215        self.set_ready(slot_offset);
216    }
217
218    /// Signal to the receiver that the sender half of the list is closed.
219    pub(crate) unsafe fn tx_close(&self) {
220        self.header.ready_slots.fetch_or(TX_CLOSED, Release);
221    }
222
223    /// Resets the block to a blank state. This enables reusing blocks in the
224    /// channel.
225    ///
226    /// # Safety
227    ///
228    /// To maintain safety, the caller must ensure:
229    ///
230    /// * All slots are empty.
231    /// * The caller holds a unique pointer to the block.
232    pub(crate) unsafe fn reclaim(&mut self) {
233        self.header.start_index = 0;
234        self.header.next = AtomicPtr::new(ptr::null_mut());
235        self.header.ready_slots = AtomicUsize::new(0);
236    }
237
238    /// Releases the block to the rx half for freeing.
239    ///
240    /// This function is called by the tx half once it can be guaranteed that no
241    /// more senders will attempt to access the block.
242    ///
243    /// # Safety
244    ///
245    /// To maintain safety, the caller must ensure:
246    ///
247    /// * The block will no longer be accessed by any sender.
248    pub(crate) unsafe fn tx_release(&self, tail_position: usize) {
249        // Track the observed tail_position. Any sender targeting a greater
250        // tail_position is guaranteed to not access this block.
251        self.header
252            .observed_tail_position
253            // Safety:
254            //
255            // 1. The caller guarantees unique access to the block.
256            // 2. The `UnsafeCell` always gives us a valid pointer.
257            .with_mut(|ptr| unsafe { *ptr = tail_position });
258
259        // Set the released bit, signalling to the receiver that it is safe to
260        // free the block's memory as soon as all slots **prior** to
261        // `observed_tail_position` have been filled.
262        self.header.ready_slots.fetch_or(RELEASED, Release);
263    }
264
265    /// Mark a slot as ready
266    fn set_ready(&self, slot: usize) {
267        let mask = 1 << slot;
268        self.header.ready_slots.fetch_or(mask, Release);
269    }
270
271    /// Returns `true` when all slots have their `ready` bits set.
272    ///
273    /// This indicates that the block is in its final state and will no longer
274    /// be mutated.
275    pub(crate) fn is_final(&self) -> bool {
276        self.header.ready_slots.load(Acquire) & READY_MASK == READY_MASK
277    }
278
279    /// Returns the `observed_tail_position` value, if set
280    pub(crate) fn observed_tail_position(&self) -> Option<usize> {
281        if 0 == RELEASED & self.header.ready_slots.load(Acquire) {
282            None
283        } else {
284            Some(
285                self.header
286                    .observed_tail_position
287                    .with(|ptr| unsafe { *ptr }),
288            )
289        }
290    }
291
292    /// Loads the next block
293    pub(crate) fn load_next(&self, ordering: Ordering) -> Option<NonNull<Block<T>>> {
294        let ret = NonNull::new(self.header.next.load(ordering));
295
296        debug_assert!(unsafe {
297            ret.map_or(true, |block| {
298                block.as_ref().header.start_index == self.header.start_index.wrapping_add(BLOCK_CAP)
299            })
300        });
301
302        ret
303    }
304
305    /// Pushes `block` as the next block in the link.
306    ///
307    /// Returns Ok if successful, otherwise, a pointer to the next block in
308    /// the list is returned.
309    ///
310    /// This requires that the next pointer is null.
311    ///
312    /// # Ordering
313    ///
314    /// This performs a compare-and-swap on `next` using `AcqRel` ordering.
315    ///
316    /// # Safety
317    ///
318    /// To maintain safety, the caller must ensure:
319    ///
320    /// * `block` is not freed until it has been removed from the list.
321    pub(crate) unsafe fn try_push(
322        &self,
323        block: &mut NonNull<Block<T>>,
324        success: Ordering,
325        failure: Ordering,
326    ) -> Result<(), NonNull<Block<T>>> {
327        // Safety: caller guarantees that `block` is valid.
328        unsafe { block.as_mut() }.header.start_index =
329            self.header.start_index.wrapping_add(BLOCK_CAP);
330
331        let next_ptr = self
332            .header
333            .next
334            .compare_exchange(ptr::null_mut(), block.as_ptr(), success, failure)
335            .unwrap_or_else(|x| x);
336
337        match NonNull::new(next_ptr) {
338            Some(next_ptr) => Err(next_ptr),
339            None => Ok(()),
340        }
341    }
342
343    /// Grows the `Block` linked list by allocating and appending a new block.
344    ///
345    /// The next block in the linked list is returned. This may or may not be
346    /// the one allocated by the function call.
347    ///
348    /// # Implementation
349    ///
350    /// It is assumed that `self.next` is null. A new block is allocated with
351    /// `start_index` set to be the next block. A compare-and-swap is performed
352    /// with `AcqRel` memory ordering. If the compare-and-swap is successful, the
353    /// newly allocated block is released to other threads walking the block
354    /// linked list. If the compare-and-swap fails, the current thread acquires
355    /// the next block in the linked list, allowing the current thread to access
356    /// the slots.
357    pub(crate) fn grow(&self) -> NonNull<Block<T>> {
358        // Create the new block. It is assumed that the block will become the
359        // next one after `&self`. If this turns out to not be the case,
360        // `start_index` is updated accordingly.
361        let new_block = Block::new(self.header.start_index + BLOCK_CAP);
362
363        let mut new_block = unsafe { NonNull::new_unchecked(Box::into_raw(new_block)) };
364
365        // Attempt to store the block. The first compare-and-swap attempt is
366        // "unrolled" due to minor differences in logic
367        //
368        // `AcqRel` is used as the ordering **only** when attempting the
369        // compare-and-swap on self.next.
370        //
371        // If the compare-and-swap fails, then the actual value of the cell is
372        // returned from this function and accessed by the caller. Given this,
373        // the memory must be acquired.
374        //
375        // `Release` ensures that the newly allocated block is available to
376        // other threads acquiring the next pointer.
377        let next = NonNull::new(
378            self.header
379                .next
380                .compare_exchange(ptr::null_mut(), new_block.as_ptr(), AcqRel, Acquire)
381                .unwrap_or_else(|x| x),
382        );
383
384        let next = match next {
385            Some(next) => next,
386            None => {
387                // The compare-and-swap succeeded and the newly allocated block
388                // is successfully pushed.
389                return new_block;
390            }
391        };
392
393        // There already is a next block in the linked list. The newly allocated
394        // block could be dropped and the discovered next block returned;
395        // however, that would be wasteful. Instead, the linked list is walked
396        // by repeatedly attempting to compare-and-swap the pointer into the
397        // `next` register until the compare-and-swap succeed.
398        //
399        // Care is taken to update new_block's start_index field as appropriate.
400
401        let mut curr = next;
402
403        // TODO: Should this iteration be capped?
404        loop {
405            let actual = unsafe { curr.as_ref().try_push(&mut new_block, AcqRel, Acquire) };
406
407            curr = match actual {
408                Ok(()) => {
409                    return next;
410                }
411                Err(curr) => curr,
412            };
413
414            crate::loom::thread::yield_now();
415        }
416    }
417}
418
419/// Returns `true` if the specified slot has a value ready to be consumed.
420fn is_ready(bits: usize, slot: usize) -> bool {
421    let mask = 1 << slot;
422    mask == mask & bits
423}
424
425/// Returns `true` if the closed flag has been set.
426fn is_tx_closed(bits: usize) -> bool {
427    TX_CLOSED == bits & TX_CLOSED
428}
429
430impl<T> Values<T> {
431    /// Initialize a `Values` struct from a pointer.
432    ///
433    /// # Safety
434    ///
435    /// The raw pointer must be valid for writing a `Values<T>`.
436    unsafe fn initialize(_value: NonNull<Values<T>>) {
437        // When fuzzing, `UnsafeCell` needs to be initialized.
438        if_loom! {
439            let p = _value.as_ptr() as *mut UnsafeCell<MaybeUninit<T>>;
440            for i in 0..BLOCK_CAP {
441                unsafe {
442                    p.add(i).write(UnsafeCell::new(MaybeUninit::uninit()));
443                }
444            }
445        }
446    }
447}
448
449impl<T> ops::Index<usize> for Values<T> {
450    type Output = UnsafeCell<MaybeUninit<T>>;
451
452    fn index(&self, index: usize) -> &Self::Output {
453        self.0.index(index)
454    }
455}
456
457#[cfg(all(test, not(loom)))]
458#[test]
459fn assert_no_stack_overflow() {
460    // https://github.com/tokio-rs/tokio/issues/5293
461
462    struct Foo {
463        _a: [u8; 2_000_000],
464    }
465
466    assert_eq!(
467        Layout::new::<MaybeUninit<Block<Foo>>>(),
468        Layout::new::<Block<Foo>>()
469    );
470
471    let _block = Block::<Foo>::new(0);
472}