Skip to main content

Runtime

Struct Runtime 

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

Runtime event loop coordinator.

The Runtime manages the main event loop, coordinating between:

§Thread Safety

The runtime itself is Send but not Sync - it should be owned by a single thread that drives the event loop. However, the command channel and work queue can be accessed from other threads for scheduling.

§State Transitions

Booting → Running → Stopping
   ↓         ↓          ↓
   └─────────┴──────────┴→ Emergency

Implementations§

Source§

impl Runtime

Source

pub fn new() -> Self

Create a new runtime with default configuration.

Source

pub fn with_config(config: RuntimeConfig) -> Self

Create a runtime with custom configuration.

Source

pub fn with_event_bus(self, event_bus: Arc<EventBus>) -> Self

Create a runtime with a shared event bus.

This is useful when the event bus is shared with other subsystems.

Source

pub fn boot(&mut self)

Boot the runtime, transitioning from Booting to Running.

§Panics

Panics if called when not in Booting state.

Source

pub const fn state(&self) -> RuntimeState

Get the current runtime state.

Source

pub const fn event_bus(&self) -> &Arc<EventBus>

Get a reference to the event bus.

Source

pub fn command_sender(&self) -> Sender<RuntimeCommand>

Get a clone of the command sender.

The sender can be used from other threads to send commands.

Source

pub fn tick(&mut self) -> bool

Process one tick of the event loop.

This method:

  1. Processes runtime commands
  2. Processes expired timers (schedules their callbacks as tasks)
  3. Dispatches events from the priority queue
  4. Executes tasks from the work queue
  5. Processes queued async events

Returns true if the runtime should continue, false if it should stop.

Source

pub fn schedule_work<F>(&self, work: F) -> bool
where F: FnOnce() + Send + 'static,

Schedule a closure for deferred execution.

Returns false if the queue is full or runtime is not accepting work.

Source

pub fn schedule_work_with_priority<F>( &self, priority: Priority, work: F, ) -> bool
where F: FnOnce() + Send + 'static,

Schedule a closure with specific priority.

Returns false if the queue is full or runtime is not accepting work.

Source

pub fn schedule_task(&self, task: Task) -> bool

Schedule a pre-built task for execution.

Returns false if the queue is full or runtime is not accepting work.

Source

pub fn schedule_delayed<F>(&self, delay: Duration, work: F) -> TimerHandle
where F: FnOnce() + Send + 'static,

Schedule work to execute after a delay (one-shot timer).

The callback executes once after the delay passes, on the next tick after the deadline. Returns a handle that cancels the timer when dropped.

§Example
use reovim_kernel::api::v1::*;
use std::time::Duration;

let mut runtime = Runtime::new();
runtime.boot();

// Schedule work for 100ms from now
let handle = runtime.schedule_delayed(Duration::from_millis(100), || {
    println!("Delayed work executed!");
});

// Timer will fire on the tick after 100ms passes
// Drop handle to cancel, or let it fire
Source

pub fn schedule_periodic<F>(&self, interval: Duration, work: F) -> TimerHandle
where F: Fn() + Send + Sync + 'static,

Schedule work to execute periodically.

The callback executes repeatedly at the specified interval. Returns a handle that cancels the timer when dropped.

§Example
use reovim_kernel::api::v1::*;
use std::time::Duration;

let mut runtime = Runtime::new();
runtime.boot();

// Schedule periodic work every 50ms
let handle = runtime.schedule_periodic(Duration::from_millis(50), || {
    println!("Periodic work!");
});

// Timer fires every 50ms until handle is dropped
Source

pub fn cancel_timer(&self, id: TimerId) -> bool

Cancel a timer by ID.

Returns true if the timer was found and cancelled, false if it was already cancelled or has already fired.

Note: Timers are automatically cancelled when their TimerHandle is dropped, so explicit cancellation is rarely needed.

Source

pub const fn timer_wheel(&self) -> &Arc<TimerWheel>

Get a reference to the timer wheel.

This allows external code to schedule timers directly with custom configuration.

Source

pub fn queue_event(&self, event: DynEvent) -> bool

Queue an event for priority-ordered processing.

Returns false if the queue is full.

Source

pub const fn request_render(&mut self)

Request a render on the next tick.

Source

pub const fn take_render_pending(&mut self) -> bool

Check and clear the render pending flag.

Returns true if a render was requested since the last call.

Source

pub const fn is_render_pending(&self) -> bool

Check if a render is pending.

Source

pub fn set_scope(&mut self, scope: EventScope)

Set the current event scope for lifecycle tracking.

Source

pub fn clear_scope(&mut self)

Clear the current event scope.

Source

pub const fn current_scope(&self) -> Option<&EventScope>

Get a reference to the current scope.

Source

pub fn shutdown(&mut self)

Request graceful shutdown.

The runtime will finish processing current work before stopping.

Source

pub const fn emergency_stop(&mut self)

Request emergency stop.

The runtime will stop immediately without draining work.

Source

pub fn is_idle(&self) -> bool

Check if the runtime is idle (no pending work).

Source

pub fn stats(&self) -> RuntimeStats

Get runtime statistics.

Source

pub const fn work_queue(&self) -> &Arc<WorkQueue>

Get a reference to the work queue.

This allows external code to schedule tasks directly.

Source

pub fn run(&mut self)

Run until the runtime stops.

This is a convenience method that calls tick() in a loop. For more control, use tick() directly.

Trait Implementations§

Source§

impl Debug for Runtime

Source§

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

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

impl Default for Runtime

Source§

fn default() -> Self

Returns the “default value” for a 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> 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.