vasi_sync::scchannel

Struct SelfContainedChannel

source
#[repr(C)]
pub struct SelfContainedChannel<T> { /* private fields */ }
Expand description

A fairly minimal channel implementation that implements the vasi::VirtualAddressSpaceIndependent trait.

Breaking the documented API contracts may result both in immediate panics, and panics in subsequent operations on the channel. To avoid this, the user must:

  • Never allow parallel send or receive operations. This is a single-producer, single-consumer channel.
  • Never call send when there is already a message pending.

Loosely inspired by the channel implementation examples in “Rust Atomics and Locks” by Mara Box (O’Reilly). Copyright 2023 Mara Box, 978-1-098-11944-7. (From the preface: “You may use all example code offered with this book for any purpose”).

TODO: Several candidate optimizations have been evaluated and discarded, but are left in the commit history for posterity along with their corresponding microbenchmark results.

One that might be worth revisiting is to remove the internal “Reading” and “Writing” states and either make the interfaces unsafe (since it becomes the caller’s responsibility to avoid parallel reads or writes), or add checked creation of !Sync Reader and Writer objects. This optimization appeared to have a 22% benefit in the “ping pong” microbenchmark on a large simulation machine, but only a 3.5% benefit in the “ping pong pinned” microbenchmark; the latter is expected to be more representative of real large simulation runs (i.e. pinning should be enabled).

Implementations§

source§

impl<T> SelfContainedChannel<T>

source

pub fn new() -> Self

source

pub fn send(&self, message: T)

Sends message through the channel.

Panics if the channel already has an unreceived message.

source

pub fn receive(&self) -> Result<T, SelfContainedChannelError>

Blocks until either the channel contains a message, or the writer has closed the channel.

Returns Ok(T) if a message was received, or Err(SelfContainedMutexError::WriterIsClosed) if the writer is closed.

Panics if another thread is already trying to receive on this channel.

source

pub fn close_writer(&self)

Closes the “write” end of the channel. This will cause any current and subsequent receive operations to fail once the channel is empty.

This method can be called in parallel with other methods on the channel, making it suitable to be called from a separate watchdog thread that detects that the writing thread (or process) has died.

source

pub fn writer_is_closed(&self) -> bool

Whether the write-end of the channel has been closed (via close_writer).

Trait Implementations§

source§

impl<T> Default for SelfContainedChannel<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<T> Drop for SelfContainedChannel<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T: VirtualAddressSpaceIndependent> VirtualAddressSpaceIndependent for SelfContainedChannel<T>

source§

const IGNORE: () = _

Used by the derive macro to validate that fields are Vasi.
source§

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

source§

impl<T> Sync for SelfContainedChannel<T>
where T: Send,

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
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, 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.