Skip to main content

LockFreeRing

Struct LockFreeRing 

Source
pub struct LockFreeRing {
    size: usize,
    frame_count: usize,
    frames: Vec<PhysFrame>,
    header: *mut RingHeader,
    slots: *mut RingSlot,
    capacity: u32,
    slot_size: u32,
}
Expand description

A lock-free single-producer single-consumer ring buffer.

§SPSC guarantee

write must only be called from the producer thread; read must only be called from the consumer thread. No locks are used : only atomic loads/stores with acquire/release ordering.

For multi-queue NIC RSS, create one LockFreeRing per RSS queue rather than sharing a single MPSC ring; this avoids CAS contention and head-of-line blocking.

Fields§

§size: usize

Total size in bytes of the shared memory region.

§frame_count: usize

Number of physical frames backing the ring.

§frames: Vec<PhysFrame>

Physical frames.

§header: *mut RingHeader

Virtual address of the header (first page).

§slots: *mut RingSlot

Virtual address of the first slot (immediately after the header in the first page, or in subsequent pages when slots spill over).

§capacity: u32

Number of slots (derived from capacity in the header).

§slot_size: u32

Slot size in bytes.

Implementations§

Source§

impl LockFreeRing

Source

const DEFAULT_CAPACITY: u32 = 256

Default slot count (power of two).

Source

pub fn new(slot_count: u32, slot_size: usize) -> Result<Arc<Self>, RingError>

Create a new ring with slot_count slots of slot_size bytes.

The ring is allocated from physically-contiguous DMA-accessible frames and zeroed. Returns an error if allocation fails.

Source

pub fn capacity(&self) -> u32

The number of slots in this ring.

Source

pub fn slot_size(&self) -> u32

The size in bytes of each slot.

Source

pub fn frame_phys_addrs(&self) -> Vec<u64>

Physical addresses of the backing frames (for DMA and for mapping into other address spaces via capabilities).

Source

pub fn write(&self, data: &[u8]) -> Result<(), RingError>

Write data into the ring. Returns Full if no slot is available, or MessageTooLarge if the data exceeds the slot capacity.

§Ordering
  1. Non-atomic copy into the slot (copy_nonoverlapping).
  2. len.store(Release) : makes the length visible after data.
  3. tail.store(Release) : publishes the slot.

On ARM/POWER the Release barrier on len prevents the consumer from seeing len != 0 before the data stores are visible.

Source

pub fn read(&self, buf: &mut [u8]) -> Result<usize, RingError>

Read one message from the ring into buf.

Returns Empty if no message is available, or BufferTooSmall if the waiting message is larger than buf.

Source

pub fn try_write(&self, data: &[u8]) -> Result<(), RingError>

Non-blocking write. Equivalent to write.

Source

pub fn write_vectored(&self, bufs: &[&[u8]]) -> Result<(), RingError>

Write multiple buffers in a single slot (scatter-gather). Concatenates bufs and writes them as one message.

Source

pub fn try_read(&self, buf: &mut [u8]) -> Result<Option<usize>, RingError>

Non-blocking read. Returns Ok(None) when the ring is empty.

Source

pub fn notify_consumer_raw(&self)

Notify the consumer that new data is available (futex wake). Increments the consumer notification counter.

Source

pub fn notify_producer_raw(&self)

Notify the producer that space has been freed (futex wake). Increments the producer notification counter.

Source

pub fn has_data(&self) -> bool

Query whether the consumer should wake. Returns true if data is available (tail != head). Used after futex_wait returns.

Source

pub fn has_space(&self) -> bool

Query whether the producer can write without blocking. Returns true if at least one slot is free.

Source

pub fn dma_buffer(&self, slot_index: u32) -> DmaBuffer

Provide a DMA-accessible buffer descriptor for zero-copy NIC operation. The physical address points directly into the slot’s data area so the NIC can DMA into the ring without an intermediate copy.

Source

fn head(&self) -> &AtomicU32

Source

fn tail(&self) -> &AtomicU32

Source

fn slot_at(&self, index: u32) -> *mut RingSlot

Trait Implementations§

Source§

impl Debug for LockFreeRing

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for LockFreeRing

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl IpcConsumer for LockFreeRing

Source§

fn recv(&self, buf: &mut [u8]) -> Result<usize, IpcError>

Receive a message, blocking if the buffer is empty.
Source§

fn try_recv(&self, buf: &mut [u8]) -> Result<Option<usize>, IpcError>

Receive without blocking.
Source§

impl IpcNotification for LockFreeRing

Source§

fn notify_consumer(&self)

Notify the consumer that data is available.
Source§

fn notify_producer(&self)

Notify the producer that space is available.
Source§

fn wait_notification(&self) -> Result<(), IpcError>

Block until a notification arrives.
Source§

impl IpcProducer for LockFreeRing

Source§

fn send(&self, msg: &[u8]) -> Result<(), IpcError>

Send a message, blocking if the buffer is full.
Source§

fn try_send(&self, msg: &[u8]) -> Result<(), IpcError>

Send without blocking.
Source§

impl IpcTransport for LockFreeRing

Source§

fn level(&self) -> TransportLevel

The isolation level of this transport.
Source§

fn capabilities(&self) -> TransportCapabilities

Static capabilities of this transport.
Source§

fn name(&self) -> &'static str

Human-readable name for debugging / profiling.
Source§

impl Send for LockFreeRing

Source§

impl Sync for LockFreeRing

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.