Skip to content

File syn_sched.h

FileList > sched > syn_sched.h

Go to the source code of this file

Cooperative scheduler for protothread tasks. More...

  • #include "../common/syn_compiler.h"
  • #include "syn_task.h"
  • #include <stdbool.h>

Classes

Type Name
struct SYN_Sched
Scheduler control block.

Public Functions

Type Name
size_t syn_sched_alive_count (const SYN_Sched * sched)
Get the number of tasks that are still alive in the scheduler.
void syn_sched_init (SYN_Sched * sched, SYN_Task * tasks, size_t count)
Initialize the scheduler with a task array.
bool syn_sched_run (SYN_Sched * sched)
Run one scheduler tick.
SYN_NORETURN void syn_sched_run_forever (SYN_Sched * sched)
Run the scheduler forever.
void syn_task_create (SYN_Task * task, const char * name, SYN_TaskFunc func, uint8_t priority, void * user_data)
Initialize a single task descriptor.
void syn_task_restart (SYN_Task * task)
Restart a task from the beginning of its protothread.
void syn_task_resume (SYN_Task * task)
Resume a suspended task, making it eligible to run again.
void syn_task_suspend (SYN_Task * task)
Suspend a task. It will be skipped by the scheduler until resumed.

Public Static Functions

Type Name
bool syn_task_is_alive (const SYN_Task * task)
Check if a task is still alive (not DEAD).

Macros

Type Name
define SYN_SCHED_PRIO_LEVELS 8
Maximum number of priority levels (0 to N-1). Override in syn_config.h if more are needed.

Detailed Description

The scheduler manages an array of SYN_Task descriptors. On each tick it selects the highest-priority ready task and calls its protothread function. Equal-priority tasks are served round-robin.

** **

static SYN_Task tasks[3];
static SYN_Sched sched;

syn_task_create(&tasks[0], "blink",   blink_fn,   1, NULL);
syn_task_create(&tasks[1], "serial",  serial_fn,  0, NULL);
syn_task_create(&tasks[2], "monitor", monitor_fn, 2, NULL);

syn_sched_init(&sched, tasks, 3);
syn_sched_run_forever(&sched);

Public Functions Documentation

function syn_sched_alive_count

Get the number of tasks that are still alive in the scheduler.

size_t syn_sched_alive_count (
    const SYN_Sched * sched
) 

Parameters:

  • sched Scheduler.

Returns:

Alive task count.


function syn_sched_init

Initialize the scheduler with a task array.

void syn_sched_init (
    SYN_Sched * sched,
    SYN_Task * tasks,
    size_t count
) 

Parameters:

  • sched Scheduler to initialize.
  • tasks Pointer to an array of SYN_Task structs. Each task should already be initialized via syn_task_create().
  • count Number of tasks in the array.

function syn_sched_run

Run one scheduler tick.

bool syn_sched_run (
    SYN_Sched * sched
) 

Scans all tasks and runs the single highest-priority ready task (lowest priority value first, round-robin among equal priorities).

Parameters:

  • sched Scheduler to run.

Returns:

true if at least one task is still alive (not DEAD).


function syn_sched_run_forever

Run the scheduler forever.

SYN_NORETURN void syn_sched_run_forever (
    SYN_Sched * sched
) 

Equivalent to while (1) { syn_sched_run(sched); }. This function never returns.

Parameters:

  • sched Scheduler to run.

function syn_task_create

Initialize a single task descriptor.

void syn_task_create (
    SYN_Task * task,
    const char * name,
    SYN_TaskFunc func,
    uint8_t priority,
    void * user_data
) 

Sets the task to READY state with the protothread reset to the top.

Parameters:

  • task Task to initialize.
  • name Human-readable name (stored by pointer, not copied).
  • func Protothread function.
  • priority Priority level (0 = highest).
  • user_data Optional pointer to task-private data (or NULL).

function syn_task_restart

Restart a task from the beginning of its protothread.

void syn_task_restart (
    SYN_Task * task
) 

Resets the protothread continuation and sets the task to READY.

Parameters:

  • task Task to restart.

function syn_task_resume

Resume a suspended task, making it eligible to run again.

void syn_task_resume (
    SYN_Task * task
) 

Parameters:

  • task Task to resume.

function syn_task_suspend

Suspend a task. It will be skipped by the scheduler until resumed.

void syn_task_suspend (
    SYN_Task * task
) 

Parameters:

  • task Task to suspend.

Public Static Functions Documentation

function syn_task_is_alive

Check if a task is still alive (not DEAD).

static inline bool syn_task_is_alive (
    const SYN_Task * task
) 

Parameters:

  • task Task to check.

Returns:

true if alive.


Macro Definition Documentation

define SYN_SCHED_PRIO_LEVELS

Maximum number of priority levels (0 to N-1). Override in syn_config.h if more are needed.

#define SYN_SCHED_PRIO_LEVELS `8`



The documentation for this class was generated from the following file src/syntropic/sched/syn_sched.h