Skip to main content

ThreadPerCoreRuntime

Struct ThreadPerCoreRuntime 

Source
pub struct ThreadPerCoreRuntime { /* private fields */ }
Expand description

Thread-per-core runtime for parallel event processing.

Manages multiple reactor threads, routing events by key hash to ensure state locality.

Implementations§

Source§

impl ThreadPerCoreRuntime

Source

pub fn new(config: TpcConfig) -> Result<Self, TpcError>

Creates a new runtime with the given configuration.

§Errors

Returns an error if any core thread cannot be spawned.

Source

pub fn new_with_factory<F>( config: TpcConfig, factory: &F, ) -> Result<Self, TpcError>
where F: OperatorFactory,

Creates a new runtime with operators from a factory.

The factory is called once per core to create that core’s operators.

§Errors

Returns an error if any core thread cannot be spawned.

Source

pub fn num_cores(&self) -> usize

Returns the number of cores.

Source

pub fn is_running(&self) -> bool

Returns true if the runtime is running.

Source

pub fn submit(&self, event: Event) -> Result<(), TpcError>

Submits an event for processing.

The event is routed to a core based on its key.

§Errors

Returns an error if the target core’s queue is full.

Source

pub fn submit_to_core( &self, core_id: usize, event: Event, ) -> Result<(), TpcError>

Submits an event to a specific core.

Use this when you’ve already computed the routing.

§Errors

Returns an error if the core’s queue is full or the core_id is invalid.

Source

pub fn submit_batch(&self, events: Vec<Event>) -> (usize, Option<TpcError>)

Submits a batch of events for processing.

Events are routed to cores based on their keys.

§Errors

Returns the number of successfully submitted events and an error if any event couldn’t be submitted.

Source

pub fn poll(&self) -> Vec<Output>

Polls all cores for outputs.

Returns all available outputs from all cores.

§Note

This method allocates memory. For zero-allocation polling, use poll_into or poll_each instead.

Source

pub fn poll_into(&self, buffer: &mut OutputBuffer, max_per_core: usize) -> usize

Polls all cores for outputs into a pre-allocated buffer (zero-allocation).

Returns the total number of outputs collected across all cores.

§Arguments
  • buffer - Pre-allocated buffer to receive outputs. Use OutputBuffer for optimal performance.
  • max_per_core - Maximum outputs to collect from each core.
§Example
use laminar_core::tpc::OutputBuffer;

// Create buffer once
let mut buffer = OutputBuffer::with_capacity(4096);

// Poll loop - no allocation after warmup
loop {
    let count = runtime.poll_into(&mut buffer, 256);

    for output in buffer.iter() {
        process(output);
    }

    buffer.clear(); // Reuse buffer
}
Source

pub fn poll_each<F>(&self, max_per_core: usize, f: F) -> usize
where F: FnMut(Output) -> bool,

Polls all cores with a callback for each output (zero-allocation).

Processing continues until:

  • All cores have been polled up to max_per_core
  • The callback returns ControlFlow::Break

Returns the total number of outputs processed.

§Arguments
  • max_per_core - Maximum outputs to process from each core.
  • f - Callback function for each output. Return true to continue, false to stop.
§Example
// Process outputs without any allocation
let count = runtime.poll_each(256, |output| {
    match output {
        Output::Event(event) => {
            send_to_sink(event);
        }
        _ => {}
    }
    true // Continue processing
});

// Or stop early on condition
let count = runtime.poll_each(256, |output| {
    if should_stop() {
        false // Stop processing
    } else {
        process(output);
        true
    }
});
Source

pub fn poll_core(&self, core_id: usize) -> Vec<Output>

Polls a specific core for outputs.

§Note

This method allocates memory. For zero-allocation polling, use poll_core_into or poll_core_each instead.

Source

pub fn poll_core_into( &self, core_id: usize, buffer: &mut OutputBuffer, max_count: usize, ) -> usize

Polls a specific core into a pre-allocated buffer (zero-allocation).

Returns the number of outputs collected.

Source

pub fn poll_core_each<F>(&self, core_id: usize, max_count: usize, f: F) -> usize
where F: FnMut(Output) -> bool,

Polls a specific core with a callback for each output (zero-allocation).

Returns the number of outputs processed.

Source

pub fn stats(&self) -> RuntimeStats

Returns statistics for all cores.

Source

pub fn router(&self) -> &KeyRouter

Returns the key router.

Source

pub fn shutdown(self) -> Result<(), TpcError>

Shuts down the runtime gracefully.

Signals all cores to stop and waits for them to finish.

§Errors

Returns an error if any core cannot be joined cleanly.

Source

pub fn run_with_handler<F>(&self, handler: F, shutdown: &AtomicBool)
where F: FnMut(Vec<Output>),

Runs the runtime with a custom output handler.

This is a convenience method that polls outputs and passes them to the handler in a loop until shutdown is signaled.

§Arguments
  • handler - Function called with batches of outputs
  • shutdown - Atomic flag to signal shutdown

Trait Implementations§

Source§

impl Debug for ThreadPerCoreRuntime

Source§

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

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

impl Drop for ThreadPerCoreRuntime

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more