1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//! Scheduler subsystem.
//!
//! Linux equivalent: `kernel/sched/`
//!
//! This module provides the main runtime loop, task execution, and work queues.
//! It is the heart of the editor's event coordination, managing:
//!
//! - **Runtime**: Central event loop coordinator
//! - **Task**: Deferred work units with priorities
//! - **`WorkQueue`**: Bounded task queue with overflow detection
//! - **`PriorityQueue`**: Priority-ordered event queue
//! - **Executor**: Synchronous task executor with panic handling
//!
//! # Architecture
//!
//! ```text
//! ┌────────────────────────────────────────────────────────────┐
//! │ Runtime │
//! │ ┌──────────────┐ ┌────────────┐ ┌────────────────────┐ │
//! │ │ PriorityQueue│ │ WorkQueue │ │ Executor │ │
//! │ │ (events) │ │ (tasks) │ │ (panic handling) │ │
//! │ └──────┬───────┘ └──────┬─────┘ └──────────┬─────────┘ │
//! │ │ │ │ │
//! │ v v v │
//! │ ┌─────────────────────────────────────────────────────┐ │
//! │ │ EventBus │ │
//! │ │ (type-erased dispatch) │ │
//! │ └─────────────────────────────────────────────────────┘ │
//! └────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Example
//!
//! ```
//! use reovim_kernel::api::v1::*;
//!
//! // Create and boot runtime
//! let mut runtime = Runtime::new();
//! runtime.boot();
//!
//! // Schedule work
//! runtime.schedule_work(|| println!("Hello from deferred task!"));
//!
//! // Schedule priority task
//! runtime.schedule_work_with_priority(Priority::HIGH, || {
//! println!("High priority work!");
//! });
//!
//! // Process all pending work
//! while !runtime.is_idle() {
//! runtime.tick();
//! }
//!
//! // Shutdown
//! runtime.shutdown();
//! assert_eq!(runtime.state(), RuntimeState::Stopping);
//! ```
//!
//! # Panic Safety
//!
//! The executor wraps task execution in `catch_unwind`, ensuring that a
//! panicking task doesn't bring down the entire editor. Failed tasks are
//! marked as such and counted in statistics.
//!
//! ```
//! use reovim_kernel::api::v1::*;
//!
//! let mut runtime = Runtime::new();
//! runtime.boot();
//!
//! // This panic won't crash the runtime
//! runtime.schedule_work(|| panic!("intentional panic"));
//!
//! runtime.tick();
//!
//! let stats = runtime.stats();
//! assert_eq!(stats.tasks_failed, 1);
//! ```
// Re-export all public types
pub use ;