Skip to main content

Kernel

Struct Kernel 

pub struct Kernel { /* private fields */ }
Expand description

The hook kernel: hook points, their listeners, emits and introspection. Send + Sync: share it in an Arc. Listeners run with no lock held, so a listener may emit or listen itself.

Implementations§

§

impl Kernel

pub fn new() -> Kernel

An empty kernel whose listeners get DEFAULT_DEADLINE unless they or their point set another.

pub fn with_default_deadline(deadline: Duration) -> Kernel

An empty kernel with another default deadline.

pub fn with_journal(self, journal: Arc<dyn Journal>) -> Kernel

The kernel with the host’s journal: it offers the contract’s Journal capability (BusJournal::journal), so points may be declared journaled (bus_contract_journal::JOURNALED).

The kernel answers Journal itself: a publish to a point it does not hold declared journaled, or under another type, is refused there (bus_contract_journal::not_journaled, Refused::TypeMismatch); everything else goes to journal. The host’s journal keeps the entries and runs the pump: each entry of a journaled point is delivered by a collect emit on this kernel (Bus::collect_any) from the pump’s thread, never the publisher’s, so local listeners run at their thread’s Kernel::run_pending. It reads a point’s declaration with Kernel::declaration. A journal that needs the kernel to deliver is built with Arc::new_cyclic, holding a std::sync::Weak of it:

ⓘ
let kernel = Arc::new_cyclic(|me: &Weak<Kernel>| {
    let journal = Arc::new(MyJournal::new(me.clone()));
    Kernel::new().with_journal(journal.clone()).with_relay(journal)
});

pub fn with_relay(self, relay: Arc<dyn RelayHost>) -> Kernel

The kernel with the host’s relay: it offers the contract’s Relay capability (BusCrossing::relay) when it has a journal too (Kernel::with_journal), so points may be declared relayable (bus_contract_crossing::relayable): events, and requests another process may ask.

The host keeps and reads entries (RelayHost); the kernel does the rest of the contract’s crossing itself: it holds the authorities (Relay::add_authority) and checks each crossing with them, refuses an entry of a point not relayable here or at another version before the host sees it, writes each point’s version on the entries served, and answers asks (Relay::answer) with an alter emit on itself, once per origin, keeping each answer at least as long as the ask’s deadline (and 30 s).

pub fn declaration(&self, point: &str) -> Option<(PointKey, PointSpec)>

The declaration of the point point: its key and spec (whether it is journaled, its relay codec); None for a point nobody declared. What a host’s Journal and Relay read.

pub fn declare<P>(&self, spec: PointSpec) -> Result<(), Refused>
where P: Point,

Declares the point P: its description and deadline. Listeners may register before or after; declaring twice is refused.

pub fn declare_point( &self, key: PointKey, spec: PointSpec, ) -> Result<(), Refused>

Declares a point by its key (Bus::declare_point). Refused when its id breaks the naming rule (Refused::BadId), when a facet’s contract refuses it (a journaled point that is not an event, a relayable point that cannot cross), and when it has a facet of a contract the kernel does not offer here (no journal or relay from the host), as declare_all would.

pub fn listen<P>( &self, id: impl Into<String>, opts: ListenOpts, run: impl Fn(&<P as Point>::Input, &mut <P as Point>::Value) -> Result<(), HookError> + Send + Sync + 'static, ) -> Result<(), Refused>
where P: Point,

Registers a listener on P under id. It reads the emit’s input and edits the value; its edit counts only when it returns Ok within its deadline without panicking.

Refused, with nothing changed, when the id is taken on the point or when its after/before close a cycle. A constraint naming a listener not (yet) registered is accepted, reported by each emit and by introspection, and ignored.

pub fn listen_any( &self, key: PointKey, id: String, opts: ListenOpts, run: Arc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError> + Sync + Send>, ) -> Result<(), Refused>

Registers an erased listener by its point’s key (Bus::listen_any).

pub fn listen_local<P>( &self, id: impl Into<String>, opts: ListenOpts, run: impl Fn(&<P as Point>::Input, &mut <P as Point>::Value) -> Result<(), HookError> + 'static, ) -> Result<(), Refused>
where P: Point,

Registers a local listener on P under id, bound to the calling thread: it may hold state that is not Send. It takes its place in the point’s order; an emit from another thread queues it for this thread’s Kernel::run_pending.

pub fn listen_local_any( &self, key: PointKey, id: String, opts: ListenOpts, run: Rc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError>>, ) -> Result<(), Refused>

Registers an erased local listener by its point’s key (Bus::listen_local_any).

pub fn unlisten(&self, point: &str, id: &str) -> bool

Removes the listener id of the point point; whether it was there.

pub fn forget(&self, owner: &str) -> usize

Removes every listener of owner, on every point (a plugin uninstalled, a module switched off); how many were removed.

pub fn on_pending(&self, wake: impl Fn(ThreadId) + Send + Sync + 'static)

Adds a waker called, with the thread’s id, when an emit queues local listeners for that thread. A waker that panics is ignored.

pub fn run_pending(&self) -> Vec<PendingRun>

Runs the local listeners queued for the calling thread by emits on other threads, emit by emit in the order they were queued (Bus::run_pending). A listener removed since is skipped.

pub fn ask<P>( &self, input: <P as Point>::Input, value: <P as Point>::Value, ) -> u64
where P: Point, <P as Point>::Value: Clone,

Asks the request P now: its listeners run at the calling thread’s next Kernel::drain (Defer). The ask’s number.

pub fn ask_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, value: Box<dyn Any + Send>, clone: fn(&(dyn Any + Send + 'static)) -> Box<dyn Any + Send>, ) -> u64

Queues an alter emit of point for the calling thread’s next Kernel::drain and calls the wakers (Defer::ask_any).

pub fn drain(&self) -> Vec<PendingRun>

Runs the requests asked on the calling thread, in ask order, each as an alter emit with the listeners registered now; what each did (Defer::drain). An ask made while draining waits for the next drain.

pub fn waiting(&self) -> usize

How many requests asked on the calling thread wait for its drain.

pub fn on_failure(&self, sink: impl Fn(&Failure) + Send + Sync + 'static)

Adds a sink called with each failure that dropped a contribution (Failure::dropped), as it happens: where the host publishes its FAILED_FACT fact or logs. A sink that panics is ignored.

pub fn alter<P>( &self, input: <P as Point>::Input, value: <P as Point>::Value, ) -> Altered<<P as Point>::Value>
where P: Point, <P as Point>::Value: Clone,

Passes value through P’s listeners in order: each edits the value the previous ones left. A listener that fails leaves it as it found it.

pub fn collect<P>( &self, input: <P as Point>::Input, ) -> Collected<<P as Point>::Value>
where P: Point, <P as Point>::Value: Default,

Gives each of P’s listeners, in order, a fresh default value to fill, and gathers the values of those that succeed.

pub fn points(&self) -> Vec<PointInfo>

Every point, in id order, with its listeners in the order they run.

pub fn listeners(&self, point: &str) -> Option<Vec<ListenerInfo>>

The listeners of the point point, in the order they run; None for a point nobody declared or listened to.

§

impl Kernel

pub fn set_timing(&self, on: bool)

Times listener runs (the default) or stops: when off, nothing is counted and no pass is reported over budget; the deadlines still hold. Turning it off keeps the times so far.

pub fn timing(&self) -> bool

Whether listener runs are timed (Kernel::set_timing).

pub fn timings(&self) -> Vec<PointTimings>

Every point that ran a pass since the last reset, in id order, its listeners in the order they run (Timings::points).

pub fn point_timings(&self, point: &str) -> Option<PointTimings>

The timings of the point point (Timings::point).

pub fn reset_timings(&self)

Starts a new window of times (Timings::reset).

pub fn on_over_budget(&self, sink: impl Fn(&OverBudget) + Send + Sync + 'static)

Adds a sink called with each pass over its point’s budget, on the thread that ran it (Timings::add_budget_sink). A sink that panics is ignored.

Trait Implementations§

§

impl Bus for Kernel

§

fn capability(&self, contract: TypeId) -> Option<Provided<'_>>

Defer, Timings and Trace always; Journal with a journal (Kernel::with_journal); Relay with a journal and a relay. The contract crates’ extension traits (BusDefer::defer, …) go through this lookup, so the two always agree.

§

fn declare_point(&self, point: PointKey, spec: PointSpec) -> Result<(), Refused>

Declares a point: its description and deadline. Listeners may register before or after; declaring twice is refused.
§

fn listen_any( &self, point: PointKey, id: String, opts: ListenOpts, run: Arc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError> + Sync + Send>, ) -> Result<(), Refused>

Registers a listener on a point under id. Refused, with nothing changed, when the id is taken on the point, when its constraints close a cycle, or when the point is registered with another type.
§

fn listen_local_any( &self, point: PointKey, id: String, opts: ListenOpts, run: Rc<dyn Fn(&(dyn Any + Sync + Send + 'static), &mut (dyn Any + Send + 'static)) -> Result<(), HookError>>, ) -> Result<(), Refused>

Registers a local listener on a point under id, bound to the calling thread: it runs only on that thread. Refused as Bus::listen_any is, and when opts asks for a thread of its own (Refused::Threaded).
§

fn run_pending(&self) -> Vec<PendingRun>

Runs the local listeners queued for the calling thread by emits on other threads, emit by emit in the order they were queued; what each emit’s deferred part did. Empty when nothing waits.
§

fn add_pending_waker(&self, wake: Arc<dyn Fn(ThreadId) + Sync + Send>)

Adds a waker called when an emit queues local listeners for a thread, with that thread’s id: where the host asks that thread to call Bus::run_pending (a repaint request, a message to its loop). A waker that panics is ignored.
§

fn unlisten(&self, point: &str, id: &str) -> bool

Removes the listener id of the point point; whether it was there.
§

fn forget(&self, owner: &str) -> usize

Removes every listener of owner, on every point; how many.
§

fn alter_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, value: Box<dyn Any + Send>, clone: fn(&(dyn Any + Send + 'static)) -> Box<dyn Any + Send>, ) -> Altered<Box<dyn Any + Send>>

Passes value through the point’s listeners in order, each editing what the previous ones left; clone copies it so a failing listener leaves it as it found it.
§

fn collect_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, fresh: fn() -> Box<dyn Any + Send>, ) -> Collected<Box<dyn Any + Send>>

Gives each of the point’s listeners, in order, a value from fresh to fill, and gathers those that succeed.
§

fn add_failure_sink(&self, sink: Arc<dyn Fn(&Failure) + Sync + Send>)

Adds a sink called with each failure that dropped a contribution, as it happens.
§

fn points(&self) -> Vec<PointInfo>

Every point, in id order, with its listeners in the order they run.
§

fn listeners(&self, point: &str) -> Option<Vec<ListenerInfo>>

The listeners of the point point, in the order they run; None for a point nobody declared or listened to.
§

impl Debug for Kernel

§

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

Formats the value using the given formatter. Read more
§

impl Default for Kernel

§

fn default() -> Kernel

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

impl Defer for Kernel

§

fn ask_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, value: Box<dyn Any + Send>, clone: fn(&(dyn Any + Send + 'static)) -> Box<dyn Any + Send>, ) -> u64

Queues an alter emit of point with input and value for the calling thread’s next Defer::drain; the ask’s number.
§

fn drain(&self) -> Vec<PendingRun>

Runs the asks queued for the calling thread, in ask order; what each did. Empty when nothing waits.
§

fn waiting(&self) -> usize

How many asks wait for the calling thread’s drain (zero before a clean exit).
§

impl Drop for Kernel

§

fn drop(&mut self)

Drops this thread’s local listeners of the kernel; another thread’s go when that thread ends.

§

impl Journal for Kernel

The host’s journal (Kernel::with_journal), behind the kernel’s own check of what may be published.

§

fn last_seq(&self) -> u64

The number of the last entry; 0 before the first.
§

fn since(&self, seq: u64, limit: usize) -> Since

The entries after seq, oldest first, at most limit; a gap when some were dropped before this read.
§

fn wait( &self, patterns: &[&str], seq: u64, timeout: Duration, ) -> Option<JournalEntry>

The first entry after seq whose kind matches one of patterns (kind_matches; empty matches every kind), waiting for it up to timeout; None when none came. An entry already in the journal is returned at once.
§

fn publish_any( &self, point: PointKey, input: Arc<dyn Any + Sync + Send>, fresh: fn() -> Box<dyn Any + Send>, cause: Option<u64>, ) -> Result<u64, Refused>

Records an event of the journaled point point, caused by the entry cause; its number. fresh makes the value each listener fills at delivery. Refused, with nothing recorded, when the point is not declared journaled on this bus (not_journaled) or is declared with another type (Refused::TypeMismatch).
§

fn flush(&self, timeout: Duration) -> bool

Waits until every entry recorded before the call has been delivered to its listeners, up to timeout; whether it was. For a host before it exits and for tests.
§

impl Relay for Kernel

The host’s relay (Kernel::with_relay) behind the kernel’s own crossing: authorities, versions and answered asks.

§

fn cursor(&self) -> Cursor

Where a reader that starts now stands: this journal’s run and its last number.
§

fn resume(&self, from: Cursor, limit: usize) -> Resumed

The relayable entries after from, at most limit.
§

fn ingest( &self, origin: &Origin, entry: RelayedEntry, ) -> Result<u64, IngestError>

Takes in an entry relayed from origin; its number in this journal.
§

fn answer(&self, origin: &Origin, ask: RelayedAsk) -> RelayedAnswer

Answers an ask relayed from origin: runs the request’s listeners here and says what they did. Blocks while they run.
§

fn add_authority( &self, authority: Arc<dyn Fn(&Crossing<'_>) -> Result<(), String> + Sync + Send>, )

Adds an authority shown everything that crosses in before it is taken: an event to ingest, an ask to answer. An authority that panics refuses.
§

impl Timings for Kernel

§

fn points(&self) -> Vec<PointTimings>

Every point that ran a pass since the last reset, in id order.
§

fn point(&self, point: &str) -> Option<PointTimings>

The timings of the point point; None when it ran no pass since the last reset.
§

fn reset(&self)

Starts a new window: every count back to zero (after a start-up phase, before a measured run).
§

fn add_budget_sink(&self, sink: Arc<dyn Fn(&OverBudget) + Sync + Send>)

Adds a sink called with each pass over its point’s budget, on the thread that ran it: where the host publishes its OVER_BUDGET_FACT fact or logs. A sink that panics is ignored.
§

impl Trace for Kernel

§

fn add_trace_sink( &self, sink: Arc<dyn Fn(&TraceEvent) + Sync + Send>, ) -> Observing

Adds a sink; it observes until the guard is dropped.
§

fn observers(&self) -> usize

How many sinks observe now: zero when the tap is off.

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<B> BusCrossing for B
where B: Bus + ?Sized,

§

fn relay(&self) -> Option<&dyn Relay>

The Relay capability of this bus; None when the bus does not offer it.
§

impl<B> BusDefer for B
where B: Bus + ?Sized,

§

fn defer(&self) -> Option<&dyn Defer>

The Defer capability of this bus; None when the bus does not offer it.
§

impl<B> BusJournal for B
where B: Bus + ?Sized,

§

fn journal(&self) -> Option<&dyn Journal>

The Journal capability of this bus; None when the bus does not offer it.
§

impl<B> BusTimings for B
where B: Bus + ?Sized,

§

fn timings(&self) -> Option<&dyn Timings>

The Timings capability of this bus; None when the bus does not offer it.
§

impl<B> BusTrace for B
where B: Bus + ?Sized,

§

fn trace(&self) -> Option<&dyn Trace>

The Trace capability of this bus; None when the bus does not offer it.
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.