Examples

Examples

Working code examples to help you get started with LightOS.


Basic Threading

Simple Thread Creation

#include <lightos/lightos.hpp>

void thread_a(void* arg) {
    int count = 0;
    while (true) {
        printf("Thread A: count = %d\n", count++);
        lightos::thread::sleep(500_ms);
    }
}

void thread_b(void* arg) {
    int count = 0;
    while (true) {
        printf("Thread B: count = %d\n", count++);
        lightos::thread::sleep(750_ms);
    }
}

int main() {
    lightos::thread::create(thread_a, nullptr, 1024, 5);
    lightos::thread::create(thread_b, nullptr, 1024, 5);
    lightos::scheduler::start();
}

Producer/Consumer Pattern

Using Queue for IPC

#include <lightos/lightos.hpp>
#include <lightos/queue.hpp>

struct Message {
    int id;
    int data;
};

lightos::queue<Message, 16> message_queue;

void producer(void* arg) {
    int id = 0;
    while (true) {
        Message msg{id++, rand()};
        message_queue.send(msg);
        lightos::thread::sleep(100_ms);
    }
}

void consumer(void* arg) {
    while (true) {
        auto msg = message_queue.receive();
        printf("Received: id=%d, data=%d\n", msg.id, msg.data);
    }
}

int main() {
    lightos::thread::create(producer, nullptr, 1024, 5);
    lightos::thread::create(consumer, nullptr, 1024, 6);
    lightos::scheduler::start();
}

Mutex Protection

Thread-Safe Counter

#include <lightos/lightos.hpp>
#include <lightos/mutex.hpp>

lightos::mutex counter_mutex;
int shared_counter = 0;

void increment_task(void* arg) {
    for (int i = 0; i < 1000; i++) {
        {
            lightos::lock_guard lock(counter_mutex);
            shared_counter++;
        }
        lightos::thread::yield();
    }
    printf("Task done, counter = %d\n", shared_counter);
}

int main() {
    for (int i = 0; i < 4; i++) {
        lightos::thread::create(increment_task, nullptr, 1024, 5);
    }
    lightos::scheduler::start();
}

Timer Examples

#include <lightos/lightos.hpp>
#include <lightos/timer.hpp>

bool led_state = false;

void blink_callback() {
    led_state = !led_state;
    gpio_write(LED_PIN, led_state);
}

int main() {
    lightos::timer blink_timer;
    blink_timer.start_periodic(500_ms, blink_callback);
    lightos::scheduler::start();
}

Watchdog Refresh Timer

lightos::timer wdt_timer;
wdt_timer.start_periodic(100_ms, []() {
    watchdog_refresh();
});

Semaphore Signaling

ISR to Task Signaling

#include <lightos/lightos.hpp>
#include <lightos/semaphore.hpp>

lightos::semaphore data_ready(0, 1);

// Called from ISR
void uart_rx_isr() {
    // Read data into buffer
    data_ready.release_from_isr();
}

void uart_task(void* arg) {
    while (true) {
        data_ready.acquire();  // Wait for data
        process_uart_data();
    }
}

POSIX Compatibility

pthread Example

#include <lightos/posix/pthread.hpp>
#include <lightos/posix/semaphore.hpp>

sem_t sem;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

void* worker(void* arg) {
    pthread_mutex_lock(&mtx);
    printf("Worker running\n");
    pthread_mutex_unlock(&mtx);
    sem_post(&sem);
    return nullptr;
}

int main() {
    sem_init(&sem, 0, 0);
    
    pthread_t t1, t2;
    pthread_create(&t1, nullptr, worker, nullptr);
    pthread_create(&t2, nullptr, worker, nullptr);
    
    sem_wait(&sem);
    sem_wait(&sem);
    
    printf("All workers done\n");
    return 0;
}

Multi-Core (SMP)

CPU Affinity

#include <lightos/smp.hpp>

void core0_task(void* arg) {
    // Runs only on core 0
    lightos::smp::set_affinity(lightos::thread::get_id(), 0x01);
    while (true) { /* ... */ }
}

void core1_task(void* arg) {
    // Runs only on core 1
    lightos::smp::set_affinity(lightos::thread::get_id(), 0x02);
    while (true) { /* ... */ }
}

Hardware Abstraction

GPIO Example (ARM)

#include <lightos/hal/gpio.hpp>

int main() {
    // Configure LED pin as output
    lightos::gpio::configure(LED_PIN, GPIO_OUTPUT);
    
    while (true) {
        lightos::gpio::toggle(LED_PIN);
        lightos::thread::sleep(500_ms);
    }
}

More Examples

Find more examples in the GitHub repository: