User Guide
User Guide
Comprehensive guides for using LightOS in your embedded applications.
Threading Model
LightOS provides a lightweight, priority-based preemptive scheduler.
Creating Threads
#include <lightos/thread.hpp>
void worker_task(void* arg) {
while (true) {
// Do work
lightos::thread::yield();
}
}
int main() {
lightos::thread::create(worker_task, nullptr, 1024, 5);
lightos::scheduler::start();
}
Thread Priorities
- Priority 0 (highest) to 255 (lowest)
- Default priority: 128
- Idle thread runs at priority 255
Synchronization Primitives
Mutex
#include <lightos/mutex.hpp>
lightos::mutex mtx;
void critical_section() {
lightos::lock_guard lock(mtx);
// Protected code
}
Semaphore
#include <lightos/semaphore.hpp>
lightos::semaphore sem(0, 10); // Initial: 0, Max: 10
// Producer
sem.release();
// Consumer
sem.acquire();
Queue
#include <lightos/queue.hpp>
lightos::queue<Message, 16> mq;
// Send
mq.send(msg);
// Receive
auto msg = mq.receive();
Memory Management
LightOS provides multiple memory allocation strategies:
- Static pools - Zero-fragmentation, deterministic timing
- Block allocator - Fixed-size block allocation
- Heap allocator - Traditional malloc/free with optional bounds checking
Static Pool Example
#include <lightos/mem/pool.hpp>
lightos::static_pool<Message, 32> msg_pool;
auto* msg = msg_pool.allocate();
msg_pool.deallocate(msg);
Timer Management
One-shot Timer
lightos::timer t;
t.start_oneshot(100_ms, []() {
// Called after 100ms
});
Periodic Timer
t.start_periodic(1_s, []() {
// Called every second
});
Architecture-Specific Guides
POSIX Compatibility Layer
LightOS includes a POSIX compatibility layer for easy porting:
#include <lightos/posix/pthread.hpp>
pthread_t thread;
pthread_create(&thread, nullptr, worker, nullptr);
pthread_join(thread, nullptr);
Supported POSIX APIs:
pthread_*- Thread managementpthread_mutex_*- Mutexessem_*- Semaphorespthread_key_*- Thread-local storagepthread_spin_*- Spinlockstimer_*- POSIX timers
Next Steps
- API Reference - Complete API documentation
- Examples - Working code samples
- Migration Guides - Moving from other RTOSes