pub struct SharedQueue<T> { /* private fields */ }Expand description
Lock-free single-producer, single-consumer queue in shared memory.
Each end is exclusive: the first send on a handle claims the queue’s sender
endpoint and the first recv its receiver endpoint, through flags in the
shared header, so two handles – in one process or several – can never both
send or both receive. A handle may hold both ends. Claims release when the
handle drops; a process killed while holding one leaves it held until the
creator drops the segment.
Implementations§
Sourcepub fn create(name: &str, capacity: usize) -> Result<Self, IpcError>
pub fn create(name: &str, capacity: usize) -> Result<Self, IpcError>
Create a new shared queue under a name no live segment holds.
Fails with IpcError::AlreadyExists when the name is taken, so a
second creator can never reinitialise the header under live handles.
T is bounded by bytemuck::Pod: shared-memory contents are written by
one process and read as T by another, so the element type must be valid
for every bit pattern (no bool/char/enum/reference discriminants a
peer could corrupt into an invalid value).
§Errors
Returns IpcError::AlreadyExists for a taken name,
IpcError::InvalidArgument for a zero capacity, an over-aligned T,
or a size overflow, and the OS error otherwise.
Sourcepub fn open(name: &str, capacity: usize) -> Result<Self, IpcError>
pub fn open(name: &str, capacity: usize) -> Result<Self, IpcError>
Open an existing shared queue. Fails with IpcError::InvalidArgument if
the segment was created with a different capacity, which would otherwise
map a view inconsistent with the creator’s and fault on access.
§Errors
Returns IpcError::InvalidArgument for a capacity mismatch (including
a creator that has not finished initialising), and the OS error when the
segment is missing or too small.
Sourcepub fn send(&mut self, value: T) -> Result<(), SendError<T>>
pub fn send(&mut self, value: T) -> Result<(), SendError<T>>
Send a value.
The first call claims the queue’s sender endpoint for this handle; the claim is released when the handle drops.
§Errors
Returns the value inside SendError::Full when the ring is full,
SendError::Closed when the queue is closed, and
SendError::EndpointInUse when another handle already sends on this
queue.
Sourcepub fn recv(&mut self) -> Result<Option<T>, IpcError>
pub fn recv(&mut self) -> Result<Option<T>, IpcError>
Receive a value, or None when the queue is empty.
The first call claims the queue’s receiver endpoint for this handle; the claim is released when the handle drops.
§Errors
Returns IpcError::EndpointInUse when another handle already receives
on this queue.