std1.0.0-beta-7 SafeC docs

std :: sync::task

Struct std::TaskScheduler

#include <std/sync/task.h> source

struct TaskScheduler {
    struct Task tasks[TASK_MAX];
    int count;
    int current;
}

Fields

§struct Task tasks[TASK_MAX]source
§int countsource

number of registered tasks

§int currentsource

index of currently running task

Methods

§static struct TaskScheduler new()source · definition

An empty scheduler (no tasks).

§generic<T> int spawn(fn int(?&T, int) func, ?&T arg)source · definition

Spawn a task running func(arg, resume_point):

- func returns >0 to yield, with that value as the next resume_point
- func returns 0 when done

Returns the task index, or -1 if the scheduler is full. static int blink(?&Led led, int step) { ...; return step < 3 ? step + 1 : 0; } sched.spawn(blink, (?&Led)&led);

§int tick()source · definition

Run one scheduling round: every non-blocked, non-done task gets one turn (blocked tasks are skipped without being invoked — see await_fd). Returns the count of still-active (blocked or ready, i.e. not yet TASK_DONE) tasks.

§void run_all()source · definition

Run all tasks in round-robin until all are TASK_DONE. Note: a task that calls await_fd() and is never unblocked (no reactor driving this scheduler) will spin run_all() forever — use std::reactor_run() instead whenever any task may block on I/O.

§int active_count() constsource · definition

Return number of active (non-DONE) tasks.

§void await_fd(int fd, int filter)source · definition

Marks the currently running task (self.current) as blocked until 'fd' becomes ready for 'filter'. Must be called from within a task function's own body during its turn (i.e. before that turn's 'return'), not from outside the scheduler.

§int unblock_fd(int fd, int filter)source · definition

Unblocks every task waiting on the given (fd, filter) pair. Returns how many tasks were unblocked. Called by std::Reactor::poll when the OS reports readiness — not normally called directly.

§int has_blocked() constsource · definition

True if at least one non-done task is currently blocked.

§int has_ready() constsource · definition

True if at least one non-done task is immediately runnable (not blocked). Together with has_blocked(), this is what reactor_run() uses to decide whether to poll with a zero or infinite timeout.