Struct tokio::sync::mpsc::block::Block

source ·
pub(crate) struct Block<T> {
    header: BlockHeader<T>,
    values: Values<T>,
}
Expand description

A block in a linked list.

Each block in the list can hold up to BLOCK_CAP messages.

Fields§

§header: BlockHeader<T>

The header fields.

§values: Values<T>

Array containing values pushed into the block. Values are stored in a continuous array in order to improve cache line behavior when reading. The values must be manually dropped.

Implementations§

source§

impl<T> Block<T>

source

unsafe fn addr_of_header(me: NonNull<Self>) -> NonNull<BlockHeader<T>>

source

unsafe fn addr_of_values(me: NonNull<Self>) -> NonNull<Values<T>>

source§

impl<T> Block<T>

source

pub(crate) fn new(start_index: usize) -> Box<Block<T>>

source

pub(crate) fn is_at_index(&self, index: usize) -> bool

Returns true if the block matches the given index.

source

pub(crate) fn distance(&self, other_index: usize) -> usize

Returns the number of blocks between self and the block at the specified index.

start_index must represent a block after self.

source

pub(crate) unsafe fn read(&self, slot_index: usize) -> Option<Read<T>>

Reads the value at the given offset.

Returns None if the slot is empty.

Safety

To maintain safety, the caller must ensure:

  • No concurrent access to the slot.
source

pub(crate) fn has_value(&self, slot_index: usize) -> bool

Returns true if there is a value in the slot to be consumed

Safety

To maintain safety, the caller must ensure:

  • No concurrent access to the slot.
source

pub(crate) unsafe fn write(&self, slot_index: usize, value: T)

Writes a value to the block at the given offset.

Safety

To maintain safety, the caller must ensure:

  • The slot is empty.
  • No concurrent access to the slot.
source

pub(crate) unsafe fn tx_close(&self)

Signal to the receiver that the sender half of the list is closed.

source

pub(crate) unsafe fn is_closed(&self) -> bool

source

pub(crate) unsafe fn reclaim(&mut self)

Resets the block to a blank state. This enables reusing blocks in the channel.

Safety

To maintain safety, the caller must ensure:

  • All slots are empty.
  • The caller holds a unique pointer to the block.
source

pub(crate) unsafe fn tx_release(&self, tail_position: usize)

Releases the block to the rx half for freeing.

This function is called by the tx half once it can be guaranteed that no more senders will attempt to access the block.

Safety

To maintain safety, the caller must ensure:

  • The block will no longer be accessed by any sender.
source

fn set_ready(&self, slot: usize)

Mark a slot as ready

source

pub(crate) fn is_final(&self) -> bool

Returns true when all slots have their ready bits set.

This indicates that the block is in its final state and will no longer be mutated.

source

pub(crate) fn observed_tail_position(&self) -> Option<usize>

Returns the observed_tail_position value, if set

source

pub(crate) fn load_next(&self, ordering: Ordering) -> Option<NonNull<Block<T>>>

Loads the next block

source

pub(crate) unsafe fn try_push( &self, block: &mut NonNull<Block<T>>, success: Ordering, failure: Ordering ) -> Result<(), NonNull<Block<T>>>

Pushes block as the next block in the link.

Returns Ok if successful, otherwise, a pointer to the next block in the list is returned.

This requires that the next pointer is null.

Ordering

This performs a compare-and-swap on next using AcqRel ordering.

Safety

To maintain safety, the caller must ensure:

  • block is not freed until it has been removed from the list.
source

pub(crate) fn grow(&self) -> NonNull<Block<T>>

Grows the Block linked list by allocating and appending a new block.

The next block in the linked list is returned. This may or may not be the one allocated by the function call.

Implementation

It is assumed that self.next is null. A new block is allocated with start_index set to be the next block. A compare-and-swap is performed with AcqRel memory ordering. If the compare-and-swap is successful, the newly allocated block is released to other threads walking the block linked list. If the compare-and-swap fails, the current thread acquires the next block in the linked list, allowing the current thread to access the slots.

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Block<T>

§

impl<T> Send for Block<T>where T: Send,

§

impl<T> !Sync for Block<T>

§

impl<T> Unpin for Block<T>where T: Unpin,

§

impl<T> !UnwindSafe for Block<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.