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>
impl<T> Block<T>
unsafe fn addr_of_header(me: NonNull<Self>) -> NonNull<BlockHeader<T>>
unsafe fn addr_of_values(me: NonNull<Self>) -> NonNull<Values<T>>
source§impl<T> Block<T>
impl<T> Block<T>
pub(crate) fn new(start_index: usize) -> Box<Block<T>>
sourcepub(crate) fn is_at_index(&self, index: usize) -> bool
pub(crate) fn is_at_index(&self, index: usize) -> bool
Returns true
if the block matches the given index.
sourcepub(crate) fn distance(&self, other_index: usize) -> usize
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
.
sourcepub(crate) unsafe fn read(&self, slot_index: usize) -> Option<Read<T>>
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.
sourcepub(crate) fn has_value(&self, slot_index: usize) -> bool
pub(crate) fn has_value(&self, slot_index: usize) -> bool
Returns true if this block has a value in the given slot.
Always returns false when given an index from a different block.
sourcepub(crate) unsafe fn write(&self, slot_index: usize, value: T)
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.
sourcepub(crate) unsafe fn tx_close(&self)
pub(crate) unsafe fn tx_close(&self)
Signal to the receiver that the sender half of the list is closed.
pub(crate) unsafe fn is_closed(&self) -> bool
sourcepub(crate) unsafe fn reclaim(&mut self)
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.
sourcepub(crate) unsafe fn tx_release(&self, tail_position: usize)
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.
sourcepub(crate) fn is_final(&self) -> bool
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.
sourcepub(crate) fn observed_tail_position(&self) -> Option<usize>
pub(crate) fn observed_tail_position(&self) -> Option<usize>
Returns the observed_tail_position
value, if set
sourcepub(crate) fn load_next(&self, ordering: Ordering) -> Option<NonNull<Block<T>>>
pub(crate) fn load_next(&self, ordering: Ordering) -> Option<NonNull<Block<T>>>
Loads the next block
sourcepub(crate) unsafe fn try_push(
&self,
block: &mut NonNull<Block<T>>,
success: Ordering,
failure: Ordering,
) -> Result<(), NonNull<Block<T>>>
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.
sourcepub(crate) fn grow(&self) -> NonNull<Block<T>>
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.