System.Sync
What tasks that share data need when they run at once (System.Task): a
mutex around a value, an integer changed atomically, a value computed once
however many tasks ask for it, a count of permits that bounds how many
tasks do something at once, a flag that tasks wait to see raised, and a
condition on a mutex's value that tasks wait to see hold.
A task waiting for any of them waits as it does in Task.join: it is
suspended, and its worker runs other tasks meanwhile. The program's own
code waiting for a permit, an event or a condition runs no task
meanwhile, since the task that ends its wait may be among them: it sleeps,
and the right to run tasks goes to another thread. So nothing here holds
up a worker.
Each keeps its state word in a Prim.Atomic, a cell of its own whose every
access is atomic.
A value only one task at a time may use.
The state is 0 free, 1 held, or 2 held and perhaps waited for. Taking a free mutex is one compare-and-swap; releasing it is a swap, and wakes the waiting tasks only if the old state was 2. A task that cannot take it marks it 2 and waits, as does one woken from among others, so that the release after it looks for waiters too.
Any task may take a free mutex, including one that just released it:
waiters are woken, not handed the mutex, so one that is never quick enough
can wait for as long as others keep taking it.
value may be read only with the mutex held, so !Debug keeps a derived
Debug from reading it.
f of the value, with the mutex held.
Some of f of the value, with the mutex held, if it is free; None,
without waiting and without calling f, if a task holds it.
Replace the value with f of it, with the mutex held.
An Int that tasks read and change without a mutex. Each operation is one
atomic instruction, ordered with the reads and writes around it.
Adds n, and returns the sum. It wraps on overflow, as the instruction does.
Stores new if the value is expected, and returns whether it was.
A value computed the first time a task asks for it, by that task; the others that ask meanwhile wait for it. The state is 0 not started, 1 being computed, 2 made.
A computation that asks for its own value waits for itself, which is a
deadlock.
made may be read only once state says it is made, so !Debug keeps a
derived Debug from reading it.
Take a permit of s, waiting until one is given back if none is free, and
until deadline if there is one: whether a permit was taken. A deadline
that has passed already takes none, even if one is free.
Give a permit of s back.
f(), holding a permit of s while it runs.
A flag that tasks wait to see raised. setEvent raises it and wakes every
task waiting, and it stays raised, so every later waitEvent returns at
once, until clearEvent lowers it.
let ready = Sync.event()
...
Sync.setEvent(ready) -- in one task
Sync.waitEventUnbounded(ready) -- in another
The flag is read and written under the scheduler's lock.
Its state is read and written only under its protocol, by tasks that may
run in parallel, so !Debug keeps a derived Debug from reading it.
Returns an event, lowered.
Raises e, and wakes every task waiting for it.
Lowers e. A task that waits for it from then on waits for the next
setEvent.
Returns whether e is raised.
Waits until e is raised, or until t runs out. Returns whether it is
raised: at once, even with a deadline that has passed, if it is raised
already.
Waits until e is raised, however long that takes.
A mutex's value that tasks wait on until it is ready for them. A waiter
states what it waits for as a function of the value, run with the mutex
held; a task that changes the value calls notifyAll, and every waiter
runs its function again.
let jobs = Sync.mutex([])
let added = Sync.condition(jobs)
...
Sync.withLock(jobs, Vec.push(_, job)) -- in one task
Sync.notifyAll(added)
...
let job = Sync.waitForUnbounded(added, Vec.pop) -- in another
Several conditions may share one mutex, one for each kind of change that tasks wait for, so that a change wakes only the tasks it may concern.
No notification is lost. A notifyAll made after a waiter's function
returned None, the mutex still held or not, has that waiter run it
again. One made before the function ran finds no waiter, and the function
sees the change.
Its state is read and written only under its protocol, by tasks that may
run in parallel, so !Debug keeps a derived Debug from reading it.
Returns a condition on m's value.
Wakes every task waiting on c, to run its function again.
Runs ready on c's value with the mutex held, and returns the first
Some it returns: at once, or after a notifyAll on c. Waits for each
notifyAll without the mutex. Returns None if ready still returns
None once t has run out; ready runs once more then, so a change
made just before the deadline is not missed.
Runs ready as waitFor does, however long it takes to return Some,
and returns what that holds.
The free permits, and the waiters in the order they came, from head on,
some of them perhaps ended already by their deadlines.
Its state is read and written only under its protocol, by tasks that may
run in parallel, so !Debug keeps a derived Debug from reading it.