API Reference / Process & Scheduler
Process & Scheduler
TikuOS uses cooperative, event-driven processes built on protothreads — stackless lightweight threads that occupy only a few bytes of state. Processes communicate via events (posted to a single global queue) and typed channels. The scheduler dispatches one event per iteration and drops into a user-provided idle hook when there is nothing to do.
Headers: kernel/process/tiku_process.h, kernel/process/tiku_proto.h, kernel/process/tiku_lc.h, kernel/scheduler/tiku_sched.h.
Process Management
Header: kernel/process/tiku_process.h
Types
typedef struct tiku_process {
struct tiku_process *next; /* Next process in linked list */
const char *name; /* Human-readable process name */
PT_THREAD((*thread)(...)); /* Process thread function */
struct pt pt; /* Protothread control state */
uint8_t is_running; /* Non-zero if process is active */
void *local; /* Per-process local storage (or NULL) */
} tiku_process_t;
typedef struct tiku_channel {
uint8_t *buf; /* Pointer to message storage */
uint8_t msg_size; /* Size of each message in bytes */
uint8_t capacity; /* Maximum number of messages */
volatile uint8_t head; /* Index of oldest message */
volatile uint8_t count; /* Number of messages in channel */
} tiku_channel_t;
System Events
| Constant | Value | Description |
|---|---|---|
TIKU_EVENT_INIT | 0x01 | Sent when a process is started |
TIKU_EVENT_EXIT | 0x02 | Sent when a process is exiting |
TIKU_EVENT_CONTINUE | 0x03 | Continue processing |
TIKU_EVENT_POLL | 0x04 | Process poll request |
TIKU_EVENT_EXITED | 0x05 | Broadcast when a process has exited |
TIKU_EVENT_FORCE_EXIT | 0x06 | Force-exit a process |
TIKU_EVENT_USER | 0x10 | Base for user-defined events |
TIKU_EVENT_TIMER | 0x88 | Timer expiration event |
Process Declaration Macros
TIKU_PROCESS(name, strname)
Declare and define a process with no local storage.
TIKU_PROCESS(blink_process, "Blink");
TIKU_PROCESS_WITH_LOCAL(name, strname, local_type)
Declare a process with typed local storage. A static instance of local_type is allocated and wired into the process struct at compile time.
struct my_state { uint16_t counter; uint8_t flag; };
TIKU_PROCESS_WITH_LOCAL(my_proc, "my process", struct my_state);
TIKU_PROCESS_TYPED(name, strname, local_type)
Like TIKU_PROCESS_WITH_LOCAL but also generates a type-safe accessor function name_local().
struct sensor_state { uint16_t reading; uint8_t count; };
TIKU_PROCESS_TYPED(sensor_proc, "sensor", struct sensor_state);
TIKU_PROCESS_THREAD(sensor_proc, ev, data) {
struct sensor_state *s = sensor_proc_local(); /* fully type-safe */
...
}
TIKU_LOCAL(type)
Access the current process's local storage with a cast. Place above TIKU_PROCESS_BEGIN().
TIKU_PROCESS_THREAD(my_proc, ev, data) {
struct my_state *s = TIKU_LOCAL(struct my_state);
TIKU_PROCESS_BEGIN();
...
}
Thread Control Macros
| Macro | Description |
|---|---|
TIKU_PROCESS_THREAD(name, ev, data) | Declare a process thread function |
TIKU_PROCESS_BEGIN() | Start the protothread block |
TIKU_PROCESS_END() | End the protothread block |
TIKU_PROCESS_YIELD() | Yield to other processes |
TIKU_PROCESS_YIELD_UNTIL(cond) | Yield until condition is true |
TIKU_PROCESS_WAIT_EVENT() | Wait for any event |
TIKU_PROCESS_WAIT_EVENT_UNTIL(cond) | Wait for a specific event condition |
TIKU_PROCESS_EXIT() | Exit the current process |
TIKU_PROCESS_CURRENT() | Pointer to the currently running process |
TIKU_THIS() | Alias for TIKU_PROCESS_CURRENT() |
TIKU_AUTOSTART_PROCESSES(...)
Register processes for automatic startup at boot.
TIKU_AUTOSTART_PROCESSES(&proc_a, &proc_b);
Channel Macros
TIKU_CHANNEL_DECLARE(name, type, depth)
Declare a typed, fixed-size message queue with generated inline accessors: name_init(), name_put(&msg), name_get(&msg).
TIKU_CHANNEL_DECLARE(sensor_ch, struct sensor_msg, 4);
sensor_ch_init();
sensor_ch_put(&msg);
sensor_ch_get(&msg);
Functions
| Function | Description |
|---|---|
tiku_process_init() | Initialize the process scheduler. Resets the process list and event queue. Call once at startup. |
tiku_process_start(p, data) | Start a process. Adds p to the active list and posts TIKU_EVENT_INIT. No-op if already running. |
tiku_process_exit(p) | Exit a process. Marks it stopped and removes it from the active list. |
tiku_process_post(p, ev, data) | Post an event. Use TIKU_PROCESS_BROADCAST to target all processes. Returns 1 on success, 0 if queue full. |
tiku_process_run() | Dequeue and dispatch one event. Returns 0 when the queue is empty. |
tiku_process_poll(p) | Request a process to receive TIKU_EVENT_POLL on the next scheduler run. |
tiku_process_is_running(p) | Check if a process is currently running. |
tiku_process_queue_space() | Free slots in the event queue. |
tiku_process_queue_full() | Non-zero if the event queue is full. |
tiku_process_queue_empty() | Non-zero if the event queue is empty. |
tiku_process_queue_length() | Number of pending events in the queue. |
tiku_autostart_start(processes) | Start all processes in a NULL-terminated array. |
Channel Functions
| Function | Description |
|---|---|
tiku_channel_init(ch, buf, msg_size, capacity) | Initialize a channel with caller-provided storage. |
tiku_channel_put(ch, msg) | Put a message into the channel. Returns 1 on success, 0 if full. |
tiku_channel_get(ch, out) | Pop a message. Returns 1 on success, 0 if empty. |
tiku_channel_is_empty(ch) | Non-zero if the channel is empty. |
tiku_channel_free(ch) | Number of free message slots remaining. |
Protothreads
Header: kernel/process/tiku_proto.h
Stackless lightweight threads using only 2 bytes per thread, built on local continuations. Protothreads let you write blocking-style code without a dedicated stack.
Types
struct pt {
lc_t lc; /* Local continuation state */
};
Return Codes
| Constant | Value | Description |
|---|---|---|
PT_WAITING | 0 | Thread is waiting for a condition |
PT_YIELDED | 1 | Thread has yielded |
PT_EXITED | 2 | Thread has exited |
PT_ENDED | 3 | Thread has ended normally |
Macros
| Macro | Description |
|---|---|
PT_INIT(pt) | Initialize a protothread control structure. Call before first use. |
PT_THREAD(name_args) | Declare a protothread function. |
PT_BEGIN(pt) | Mark the beginning of a protothread. Must be the first statement. |
PT_END(pt) | Mark the end of a protothread. Must be the last statement. |
PT_WAIT_UNTIL(pt, condition) | Block until condition becomes true. |
PT_WAIT_WHILE(pt, cond) | Block while condition is true. |
PT_WAIT_THREAD(pt, thread) | Wait for a child protothread to complete. |
PT_SPAWN(pt, child, thread) | Initialize and wait for a child protothread. |
PT_RESTART(pt) | Restart the protothread from the beginning. |
PT_EXIT(pt) | Exit the protothread immediately. |
PT_YIELD(pt) | Voluntarily yield execution. |
PT_YIELD_UNTIL(pt, cond) | Yield until a condition is met. |
PT_SCHEDULE(f) | Non-zero if the protothread is still running. |
PT_THREAD(my_thread(struct pt *pt, int data))
{
PT_BEGIN(pt);
/* thread code */
PT_END(pt);
}
Local Continuations
Header: kernel/process/tiku_lc.h
Low-level mechanism that captures and restores a function's execution state. Foundation for protothreads — most code should use the protothread API instead of touching LCs directly.
Types
typedef unsigned short lc_t;
Macros
| Macro | Description |
|---|---|
LC_INIT(s) | Initialize a local continuation variable. |
LC_RESUME(s) | Resume from a saved continuation point. |
LC_SET(s) | Save the current execution position. |
LC_END(s) | End the local continuation block. |
LC_RESET(s) | Reset the continuation (alias for LC_INIT). |
LC_IS_RESUMED(s) | Non-zero if the continuation has been set. |
Note: LC_SET cannot be used inside another switch statement. Each LC_SET in a function must be on a different line.
Scheduler
Header: kernel/scheduler/tiku_sched.h
Top-level event loop. Initializes the process system, software timers, and the hardware timer, then dispatches events until stopped. Provides an idle hook for entering low-power modes when the event queue is empty.
Types
typedef void (*tiku_sched_idle_hook_t)(void);
Constants
| Constant | Value | Description |
|---|---|---|
TIKU_SCHED_RUNNING | 0 | Scheduler is running normally |
TIKU_SCHED_STOPPED | 1 | Scheduler has been asked to stop |
Functions
| Function | Description |
|---|---|
tiku_sched_init() | Initialize the scheduler and all managed subsystems (process system, software timers, hardware timer). Call once at startup. |
tiku_sched_start(p, data) | Start a process through the scheduler. Wrapper around tiku_process_start(). |
tiku_sched_run_once() | Run one scheduler iteration. Returns 1 if work was done, 0 if idle. |
tiku_sched_loop() | Enter the main loop (never returns). Dispatches events, checks timers, and calls the idle hook when idle. |
tiku_sched_stop() | Stop the scheduler loop. Causes tiku_sched_loop() to return on the next iteration. |
tiku_sched_has_pending() | Non-zero if the event queue is non-empty or any timer is due. |
tiku_sched_set_idle_hook(hook) | Register an idle hook called when no work is pending. Pass NULL to clear. |
tiku_sched_notify() | Call from any ISR that generates work (e.g. clock tick). Requests the timer process to poll. |