constTASKS_MOST
const TASKS_MOST = 10000
tasks: several jobs at once, taking turns. spawn starts a task, which runs when the one running now lets it: by calling next_turn, wait_seconds, wait_until or wait_all. They all share one processor core, so two tasks never run at the same moment, and nothing needs locks. Good for games, timers, downloads and anything that waits a lot.
use "tasks.jamo"
func count(name: string, times: int)
for i in 0..times
print(f"{name} {i}")
next_turn()
end
end
func main()
spawn(func() => count("a", 3))
spawn(func() => count("b", 3))
wait_all()
end
A run error in a task that nothing catches stops the whole program, the same as anywhere else. A try only catches errors from its own task. When main ends, the program ends, even if tasks are still going.
Each task is a Windows fiber with its own 8 MB stack, like main's.
const TASKS_MOST = 10000
const TASKS_STACK = 8388608
const TASKS_STACK_START = 65536
struct Task
work: func()
fiber: int
wake: float # It may run again from this many seconds since the program started
alive: bool # Spawned and not finished yet
waits_for_all: bool # In wait_all: it may run again once every other task has finished
finished: bool # Finished, with its fiber still to be deleted
endfunc tasks_setup()
func tasks_nothing()
func tasks_entry(place: int): int
Where place's rsp and top are kept while it waits
func tasks_go(place: int)
Goes to the task at place, and comes back here when something goes back to this one
func tasks_tidy()
Deletes the fibers of finished tasks, never the one running
func tasks_start(place: int)
Every task starts here, on its own stack
func tasks_pick(): int
The next task that may run, after the one running now. Waits, without using the processor, while every task is waiting for its time
func tasks_ready(place: int, now: float): bool
func tasks_left_besides(place: int): int
func spawn(work: func())
Starts work as a new task. It first runs when the task running now waits or gives others a turn
func next_turn()
Lets every other task that's ready have a turn, then carries on
func wait_seconds(seconds: float)
Waits this many seconds, while other tasks run
func wait_until(ready: func(): bool)
Waits until ready gives back true, giving other tasks turns in between, so they can make it true
func tasks_left(): int
How many tasks other than this one are still going
func wait_all()
Waits until every other task has finished