System.Sync

lib/System/Sync.gob

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.

type Mutex a

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.

fun mutex : fun(a) -> Mutex a
fun withLock : fun(Mutex a, fun(a) -> b) -> b

f of the value, with the mutex held.

fun tryWithLock : fun(Mutex a, fun(a) -> b) -> Option b

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.

fun update : fun(Mutex a, fun(a) -> a) -> Unit

Replace the value with f of it, with the mutex held.

type AtomicInt

An Int that tasks read and change without a mutex. Each operation is one atomic instruction, ordered with the reads and writes around it.

fun atomicInt : fun(Int) -> AtomicInt
fun load : fun(AtomicInt) -> Int
fun store : fun(AtomicInt, Int) -> Unit
fun addAndGet : fun(AtomicInt, Int) -> Int

Adds n, and returns the sum. It wraps on overflow, as the instruction does.

fun compareAndSet : fun(AtomicInt, Int, Int) -> Bool

Stores new if the value is expected, and returns whether it was.

type Once a

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.

fun once : fun(fun() -> a) -> Once a
fun force : fun(Once a) -> a
fun semaphore : fun(Int) -> Semaphore
fun acquire : fun(Semaphore, Option Deadline) -> Bool

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.

fun release : fun(Semaphore) -> Unit

Give a permit of s back.

fun withPermit : fun(Semaphore, fun() -> b) -> b

f(), holding a permit of s while it runs.

type Event

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.

fun event : fun() -> Event

Returns an event, lowered.

fun setEvent : fun(Event) -> Unit

Raises e, and wakes every task waiting for it.

fun clearEvent : fun(Event) -> Unit

Lowers e. A task that waits for it from then on waits for the next setEvent.

fun isEventSet : fun(Event) -> Bool

Returns whether e is raised.

fun waitEvent : [Timeout t] fun(Event, t) -> Bool

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.

fun waitEventUnbounded : fun(Event) -> Unit

Waits until e is raised, however long that takes.

type Condition a

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.

fun condition : fun(Mutex a) -> Condition a

Returns a condition on m's value.

fun notifyAll : fun(Condition a) -> Unit

Wakes every task waiting on c, to run its function again.

fun waitFor : [Timeout t] fun(Condition a, t, fun(a) -> Option b) -> Option b

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.

fun waitForUnbounded : fun(Condition a, fun(a) -> Option b) -> b

Runs ready as waitFor does, however long it takes to return Some, and returns what that holds.

type Semaphore

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.