Task
Source: std/task.mw.
Stackful coroutines with a single-threaded scheduler. See the async guide for a walkthrough.
Cooperative tasks (stackful coroutines) with a single-threaded scheduler.
fn worker(arg: rawptr) -> i64 { task_sleep_ms(10); ret 7; }var t: *Task = task_spawn(worker, null);task_run(); // runs every task until none can make progressvar r: i64 = task_join(t); // result of `worker`A task gives up the CPU only in task_yield, task_sleep_*, task_wait_fd, task_join and the channel operations. Waiting for I/O uses poll(2); on platforms without it (Windows, for now) sleeping and yielding still work.
Constants
Section titled “Constants”| Name | Type | Value | Notes |
|---|---|---|---|
TASK_STACK_SIZE |
i64 |
262144 |
|
TASK_READY |
i64 |
1 |
|
TASK_RUNNING |
i64 |
2 |
|
TASK_SLEEP |
i64 |
3 |
|
TASK_FDWAIT |
i64 |
4 |
|
TASK_BLOCKED |
i64 |
5 |
|
TASK_DONE |
i64 |
6 |
|
POLL_IN |
i64 |
1 |
|
POLL_OUT |
i64 |
4 |
|
POLL_ERR |
i64 |
8 |
|
POLL_HUP |
i64 |
16 |
struct Task { sp: i64; state: i64; entry: fn(rawptr) -> i64; arg: rawptr; stack: i64; stack_size: i64; next: *Task; wake_ns: i64; fd: i64; events: i64; revents: i64; joiners: *Task; result: i64; detached: i64;}struct Chan { buf: *i64; cap: i64; len: i64; head: i64; closed: i64; recvq: *Task; sendq: *Task;}Functions
Section titled “Functions”task_spawn_sized
Section titled “task_spawn_sized”fn task_spawn_sized(f: fn(rawptr) -> i64, arg: rawptr, size: i64) -> *Tasktask_spawn
Section titled “task_spawn”fn task_spawn(f: fn(rawptr) -> i64, arg: rawptr) -> *Tasktask_detach
Section titled “task_detach”fn task_detach(t: *Task)The task’s memory is released when it ends instead of when it is joined.
task_yield
Section titled “task_yield”fn task_yield()task_sleep_ns
Section titled “task_sleep_ns”fn task_sleep_ns(ns: i64)task_sleep_ms
Section titled “task_sleep_ms”fn task_sleep_ms(ms: i64)task_wait_fd
Section titled “task_wait_fd”fn task_wait_fd(fd: i64, events: i64) -> i64Blocks until fd is ready for events (POLL_IN / POLL_OUT); returns the revents.
task_join
Section titled “task_join”fn task_join(t: *Task) -> i64Waits for the task to end and returns its result. From outside a task this first runs the scheduler until every task has finished or is stuck.
task_run
Section titled “task_run”fn task_run()Runs the scheduler until every task is done or the remaining ones can never wake up (blocked on a channel or join nobody will satisfy).
task_read
Section titled “task_read”fn task_read(fd: i64, buf: *u8, len: u64) -> i64task_write_all
Section titled “task_write_all”fn task_write_all(fd: i64, buf: *u8, len: i64) -> i64Writes the whole buffer. Returns 0 on success, -errno otherwise.
task_accept
Section titled “task_accept”fn task_accept(fd: i64) -> i64task_recv
Section titled “task_recv”fn task_recv(fd: i64, buf: *u8, len: u64) -> i64chan_new
Section titled “chan_new”fn chan_new(cap: i64) -> *Chanchan_free
Section titled “chan_free”fn chan_free(c: *Chan)chan_close
Section titled “chan_close”fn chan_close(c: *Chan)chan_send
Section titled “chan_send”fn chan_send(c: *Chan, v: i64) -> i64Blocks while the channel is full. Returns 0, or -1 when it is closed.
chan_recv
Section titled “chan_recv”fn chan_recv(c: *Chan, out: *i64) -> i64Blocks while the channel is empty. Returns 0, or -1 once it is closed and drained.