pub struct Runtime { /* private fields */ }Expand description
Runtime event loop coordinator.
The Runtime manages the main event loop, coordinating between:
- Event processing (via
PriorityQueueandEventBus) - Task execution (via
WorkQueueandExecutor) - Lifecycle management (boot, run, shutdown)
§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
↓ ↓ ↓
└─────────┴──────────┴→ EmergencyImplementations§
Source§impl Runtime
impl Runtime
Sourcepub fn with_config(config: RuntimeConfig) -> Self
pub fn with_config(config: RuntimeConfig) -> Self
Create a runtime with custom configuration.
Sourcepub fn with_event_bus(self, event_bus: Arc<EventBus>) -> Self
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.
Sourcepub fn boot(&mut self)
pub fn boot(&mut self)
Boot the runtime, transitioning from Booting to Running.
§Panics
Panics if called when not in Booting state.
Sourcepub const fn state(&self) -> RuntimeState
pub const fn state(&self) -> RuntimeState
Get the current runtime state.
Sourcepub fn command_sender(&self) -> Sender<RuntimeCommand>
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.
Sourcepub fn tick(&mut self) -> bool
pub fn tick(&mut self) -> bool
Process one tick of the event loop.
This method:
- Processes runtime commands
- Processes expired timers (schedules their callbacks as tasks)
- Dispatches events from the priority queue
- Executes tasks from the work queue
- Processes queued async events
Returns true if the runtime should continue, false if it should stop.
Sourcepub fn schedule_work<F>(&self, work: F) -> bool
pub fn schedule_work<F>(&self, work: F) -> bool
Schedule a closure for deferred execution.
Returns false if the queue is full or runtime is not accepting work.
Sourcepub fn schedule_work_with_priority<F>(
&self,
priority: Priority,
work: F,
) -> bool
pub fn schedule_work_with_priority<F>( &self, priority: Priority, work: F, ) -> bool
Schedule a closure with specific priority.
Returns false if the queue is full or runtime is not accepting work.
Sourcepub fn schedule_task(&self, task: Task) -> bool
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.
Sourcepub fn schedule_delayed<F>(&self, delay: Duration, work: F) -> TimerHandle
pub fn schedule_delayed<F>(&self, delay: Duration, work: F) -> TimerHandle
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 fireSourcepub fn schedule_periodic<F>(&self, interval: Duration, work: F) -> TimerHandle
pub fn schedule_periodic<F>(&self, interval: Duration, work: F) -> TimerHandle
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 droppedSourcepub fn cancel_timer(&self, id: TimerId) -> bool
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.
Sourcepub const fn timer_wheel(&self) -> &Arc<TimerWheel>
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.
Sourcepub fn queue_event(&self, event: DynEvent) -> bool
pub fn queue_event(&self, event: DynEvent) -> bool
Queue an event for priority-ordered processing.
Returns false if the queue is full.
Sourcepub const fn request_render(&mut self)
pub const fn request_render(&mut self)
Request a render on the next tick.
Sourcepub const fn take_render_pending(&mut self) -> bool
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.
Sourcepub const fn is_render_pending(&self) -> bool
pub const fn is_render_pending(&self) -> bool
Check if a render is pending.
Sourcepub fn set_scope(&mut self, scope: EventScope)
pub fn set_scope(&mut self, scope: EventScope)
Set the current event scope for lifecycle tracking.
Sourcepub fn clear_scope(&mut self)
pub fn clear_scope(&mut self)
Clear the current event scope.
Sourcepub const fn current_scope(&self) -> Option<&EventScope>
pub const fn current_scope(&self) -> Option<&EventScope>
Get a reference to the current scope.
Sourcepub fn shutdown(&mut self)
pub fn shutdown(&mut self)
Request graceful shutdown.
The runtime will finish processing current work before stopping.
Sourcepub const fn emergency_stop(&mut self)
pub const fn emergency_stop(&mut self)
Request emergency stop.
The runtime will stop immediately without draining work.
Sourcepub fn stats(&self) -> RuntimeStats
pub fn stats(&self) -> RuntimeStats
Get runtime statistics.
Sourcepub const fn work_queue(&self) -> &Arc<WorkQueue>
pub const fn work_queue(&self) -> &Arc<WorkQueue>
Get a reference to the work queue.
This allows external code to schedule tasks directly.