Skip to main content

MpPublisher

Struct MpPublisher 

Source
pub struct MpPublisher<T: Pod> { /* private fields */ }
Expand description

The write side of a Photon MPMC channel.

Unlike Publisher, MpPublisher is Clone + Send + Sync — multiple threads can publish concurrently. Sequence numbers are claimed atomically via fetch_add on a shared counter, and the cursor is advanced with a single best-effort CAS (no spin loop). Consumers use stamp-based reading, so the cursor only needs to be eventually consistent for subscribe(), latest(), and pending().

Created via channel_mpmc().

Implementations§

Source§

impl<T: Pod> MpPublisher<T>

Source

pub fn publish(&self, value: T)

Publish a single value. Zero-allocation, O(1) amortised.

Multiple threads may call this concurrently. Each call atomically claims a sequence number, writes the slot using the seqlock protocol, then advances the shared cursor.

Instead of spinning on the cursor CAS (which serializes all producers on one cache line), this implementation waits for the predecessor’s slot stamp to become committed. Stamp checks distribute contention across per-slot cache lines, avoiding the single-point serialization bottleneck. Once the predecessor is confirmed done, a single CAS advances the cursor, followed by a catch-up loop to absorb any successors that are also done.

Source

pub fn publish_with(&self, f: impl FnOnce(&mut MaybeUninit<T>))

Publish by writing directly into the slot via a closure.

Like publish, but the closure receives a &mut MaybeUninit<T> for in-place construction, potentially eliminating a write-side memcpy.

§Example
use std::mem::MaybeUninit;
let (p, subs) = photon_ring::channel_mpmc::<u64>(64);
let mut sub = subs.subscribe();
p.publish_with(|slot| { slot.write(42u64); });
assert_eq!(sub.try_recv(), Ok(42));
Source

pub fn published(&self) -> u64

Number of messages claimed so far (across all clones).

This reads the shared atomic counter — the value may be slightly ahead of the cursor if some producers haven’t committed yet.

Source

pub fn capacity(&self) -> u64

Ring capacity (power of two).

Trait Implementations§

Source§

impl<T: Pod> Clone for MpPublisher<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Pod> Send for MpPublisher<T>

Source§

impl<T: Pod> Sync for MpPublisher<T>

Auto Trait Implementations§

§

impl<T> Freeze for MpPublisher<T>

§

impl<T> !RefUnwindSafe for MpPublisher<T>

§

impl<T> Unpin for MpPublisher<T>

§

impl<T> UnsafeUnpin for MpPublisher<T>

§

impl<T> !UnwindSafe for MpPublisher<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.