Preemptive Multithreading Rust Library
A production-ready no_std
preemptive multithreading library built from scratch for OS kernels and embedded systems.
Features
- Zero Standard Library Dependencies: Built with
#![no_std]
for maximum compatibility - True Preemptive Scheduling: Hardware timer-driven preemption with configurable time slices
- Lock-Free Architecture: High-performance lock-free data structures throughout
- Multi-Architecture Support: x86_64, ARM64, and RISC-V support
- Memory Safety: RAII-based resource management with epoch-based reclamation
- Security Hardening: Stack canaries, CFI, ASLR, thread isolation, and comprehensive audit logging
- Performance Optimized: NUMA-aware scheduling, CPU cache optimization, and SIMD acceleration
- Comprehensive Observability: Built-in metrics, profiling, and health monitoring
Architecture Support
Architecture | Context Switch | Timer Interrupts | Security Features | Status |
---|---|---|---|---|
x86_64 | ✅ | ✅ (APIC) | ✅ Full | Stable |
ARM64 | ✅ | ✅ (Generic Timer) | ✅ Full | Stable |
RISC-V 64 | ✅ | ✅ (SBI Timer) | ✅ Full | Stable |
Quick Start
Add to your Cargo.toml
:
[]
= "1.0"
# Enable features as needed
[]
= ["x86_64", "hardened"]
= [] # x86_64 architecture support
= [] # ARM64 architecture support
= [] # RISC-V 64-bit support
= [] # Security hardening features
= [] # Memory management unit features
= [] # Work-stealing scheduler
= [] # Standard library compatibility
Basic Threading Example
use ;
pub extern "C" !
Advanced Scheduler Configuration
use ;
// Configure scheduler for specific CPU
let mut scheduler = new;
scheduler.set_time_slice;
// Create CPU-affine thread
let handle = new
.cpu_affinity // CPU 2 only
.priority
.spawn
.expect;
Security-Hardened Threading
use ;
// Enable all security features
configure_security_feature;
configure_security_feature;
configure_security_feature;
// Create isolated thread
let handle = new
.name
.security_level
.spawn
.expect;
Performance Characteristics
Context Switch Performance
Architecture | Typical Time | Optimized Time | Notes |
---|---|---|---|
x86_64 | ~500ns | ~300ns | With minimal register saves |
ARM64 | ~400ns | ~250ns | Using pointer authentication |
RISC-V | ~600ns | ~400ns | RISC architecture benefits |
Memory Usage
- Base overhead: ~8KB per thread
- Stack size: Configurable (default 64KB)
- Scheduler state: ~64 bytes per thread
- Global state: ~4KB total
Scalability
- Threads: Tested up to 10,000 concurrent threads
- CPUs: Scales linearly up to 64 cores
- Memory: Constant overhead per thread
Security Features
Stack Protection
- Stack canaries: Detect buffer overflows
- Guard pages: Prevent stack overflow (requires MMU)
- Stack randomization: ASLR for thread stacks
Control Flow Integrity (CFI)
- Indirect call protection: Verify call targets
- Return address protection: Shadow stack
- Function pointer verification: Label-based CFI
Thread Isolation
- Domain-based isolation: Separate thread security domains
- Memory access control: Restrict cross-thread access
- Resource limits: Per-domain resource quotas
Audit Logging
- Security events: Comprehensive violation tracking
- Performance monitoring: Threshold-based alerts
- Export formats: JSON, CSV, plain text
API Reference
Core Types
ThreadBuilder
Primary interface for creating and configuring threads.
JoinHandle
Handle to a spawned thread that can be used to wait for completion.
SecurityConfig
Configuration for security and hardening features.
Scheduler Types
RoundRobinScheduler
Simple round-robin scheduling with configurable time slices.
WorkStealingScheduler
Advanced work-stealing scheduler with NUMA awareness (requires work-stealing
feature).
Synchronization Primitives
Mutex
Lock-free mutex implementation with fast paths.
let mutex = new;
// Automatically unlocked
Memory Management
Stack
RAII stack management with automatic cleanup.
StackPool
High-performance stack allocation pool.
ArcLite
Lightweight atomic reference counting for no_std
environments.
Examples
See the examples/
directory for complete working examples:
- basic_threading: Simple multi-threading example
- scheduler_config: Custom scheduler configuration
- security_hardened: Security features demonstration
- performance_test: Performance benchmarking
- numa_aware: NUMA-aware threading
- embedded_kernel: Integration with embedded kernel
Platform Support
Operating Systems
- Freestanding: Primary target (no OS)
- Linux: Testing and development
- Embedded RTOS: Various RTOS integrations
Hardware Requirements
Minimum
- Memory: 64KB RAM minimum
- Timer: Hardware timer for preemption
- Architecture: x86_64, ARM64, or RISC-V
Recommended
- Memory: 1MB+ for optimal performance
- Cores: Multi-core for true parallelism
- MMU: Memory management unit for security features
Building and Testing
Prerequisites
# Install Rust (nightly required for some features)
|
# Install target architectures
Build
# Basic build
# With all features
# Architecture-specific
Testing
# Unit tests (requires std)
# Integration tests
# Performance tests
# Security tests
# Fuzz testing (requires cargo-fuzz)
Contributing
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Development Setup
- Fork the repository
- Create a feature branch
- Implement your changes with tests
- Run the test suite
- Submit a pull request
Code Style
- Use
rustfmt
for formatting - Run
clippy
for linting - Follow Rust API guidelines
- Document all public APIs
License
This project is dual-licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
Acknowledgments
- Inspired by research in lock-free data structures
- Built on principles from modern OS kernels
- Security design influenced by CFI and Intel CET
- Performance optimization techniques from high-frequency trading systems
Changelog
See CHANGELOG.md for version history and breaking changes.
Roadmap
v1.1 (Planned)
- WASM support for browser environments
- Real-time scheduling policies
- Hardware transactional memory support
- Advanced profiling integration
v1.2 (Future)
- Distributed threading across network
- GPU compute thread integration
- Machine learning-based scheduling
- Formal verification of core algorithms
For detailed documentation, visit docs.rs/preemptive-threads.