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


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 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:


Next Steps