preemptive_threads/
lib.rs

1#![no_std]
2#![deny(unsafe_op_in_unsafe_fn)]
3// Temporarily allow missing docs for existing code during refactoring
4#![allow(missing_docs)]
5#![forbid(unreachable_pub)]
6
7//! A `no_std` preemptive multithreading library built from scratch for OS kernels and embedded systems.
8//!
9//! This library provides preemptive multithreading capabilities without requiring the standard library,
10//! making it suitable for embedded systems, OS kernels, and other resource-constrained environments.
11//!
12//! # Features
13//!
14//! - `std-shim`: Enable compatibility layer for standard library
15//! - `x86_64`: Enable x86_64 architecture support  
16//! - `arm64`: Enable ARM64 architecture support
17//! - `riscv64`: Enable RISC-V 64-bit architecture support
18//! - `full-fpu`: Enable full floating point unit save/restore
19//! - `mmu`: Enable memory management unit features like guard pages
20//! - `work-stealing`: Enable work-stealing scheduler implementation
21//! - `hardened`: Enable security hardening features
22//!
23//! # Architecture
24//!
25//! The library is organized around several key abstractions:
26//! - Architecture-specific context switching and interrupt handling
27//! - Pluggable schedulers with different algorithms
28//! - Safe memory management for thread stacks and resources
29//! - Preemptive scheduling with configurable time slices
30
31pub mod arch;
32pub mod atomic_scheduler;
33pub mod context;
34pub mod context_full;
35pub mod error;
36pub mod errors;
37pub mod kernel;
38pub mod mem;
39pub mod observability;
40pub mod perf;
41pub mod platform_timer;
42pub mod preemption;
43pub mod safe_api;
44pub mod sched;
45pub mod scheduler;
46pub mod security;
47pub mod signal_safe;
48pub mod stack_guard;
49pub mod sync;
50pub mod thread;
51pub mod thread_new;
52pub mod time;
53
54#[cfg(all(test, feature = "std"))]
55mod tests;
56
57#[cfg(test)]
58extern crate std;
59
60// Always need alloc for no_std environments
61extern crate alloc;
62
63// Import alloc types and macros
64
65#[cfg(all(not(test), not(feature = "std"), not(feature = "std-shim")))]
66use core::panic::PanicInfo;
67
68#[cfg(all(not(test), not(feature = "std"), not(feature = "std-shim")))]
69#[panic_handler]
70fn panic(_info: &PanicInfo) -> ! {
71    loop {}
72}
73
74pub use arch::{Arch, DefaultArch};
75pub use atomic_scheduler::{AtomicScheduler, ATOMIC_SCHEDULER};
76pub use error::{ThreadError, ThreadResult};  
77pub use kernel::{Kernel, SpawnError};
78pub use mem::{ArcLite, Stack, StackPool, StackSizeClass};
79pub use platform_timer::{init_preemption_timer, stop_preemption_timer, preemption_checkpoint};
80pub use safe_api::{
81    exit_thread as safe_exit, yield_now, Mutex, MutexGuard, ThreadBuilder as OldThreadBuilder, ThreadHandle, ThreadPool,
82};
83pub use scheduler::{Scheduler as OldScheduler, SCHEDULER};
84pub use stack_guard::{ProtectedStack, StackGuard, StackStats, StackStatus};
85pub use sync::{exit_thread, yield_thread};
86pub use thread::{Thread as OldThread, ThreadState as OldThreadState};
87pub use thread_new::{Thread, ThreadId, ThreadState, JoinHandle, ThreadBuilder, ReadyRef, RunningRef};
88pub use time::{Duration, Instant, Timer, TimerConfig, PreemptGuard, IrqGuard};
89pub use observability::{ThreadMetrics, SystemMetrics, ResourceLimiter, ThreadProfiler, HealthMonitor, ObservabilityConfig, init_observability, cleanup_observability};
90
91// Security and hardening exports
92pub use security::{SecurityConfig, SecurityViolation, SecurityStats, SecurityFeature, init_security, get_security_stats, configure_security_feature};
93
94// New lock-free scheduler exports
95pub use sched::{Scheduler as NewScheduler, CpuId, RoundRobinScheduler, DefaultScheduler};
96#[cfg(feature = "work-stealing")]
97pub use sched::WorkStealingScheduler;