Threads
Starting and joining threads, what they share, how they synchronize, and what a racing read sees.
Avin programs run threads in parallel over one shared, collected heap.
Starting and joining
Threads are library code in avin.threads, over runtime entries that start a thread with a context
of its own and wait for it to finish. A thread runs a closure and is joined for its result:
import avin.threads.spawn
main() -> int
val worker = spawn(() -> sum(1 .. 1000000))
val mine = sum(1000001 .. 2000000)
worker.join() + mine
spawn[T: Sendable](@sendable body: () -> T, stack: long = 8388608) -> Thread[T]startsbodyon a new lightweight thread with a stack of its own (8 MiB unless asked otherwise, 64 KiB at the least). The body captures only sendable values, and its result is sendable (what is shared).join() -> Twaits for it and gives back the result; joining a second time gives the same result again. Any thread may join, several at once: one waits for the thread and the others wait for that one, and aThreadis itself sendable. AThreadis collected like any other object once nothing reaches it; a thread that is never joined still gives its stack back when its body returns. Oncejoinanswers, the ended thread keeps nothing alive: its body and what the body captured are collected at the next collection unless something else still reaches them. A thread that overflows its own stack traps withstack overflowlike any other thread’s trap, whatever its stack’s size: one under the least a thread is given, the default, and a stack kept from an ended thread that is larger than the new one asked for, which overflows at its own end.- A trap in any thread stops the program, as it does in a program with one thread: traps report
bugs, and a bug in one thread leaves the state the others share in doubt. Only the first thread to
stop the program reports: what was written is flushed once and one reason is printed, and a
thread that traps after it, or calls
exit, waits for the end. The stopping thread runs the program’s exit hooks after its report, the others still running. - The program ends when
mainreturns, whatever other threads are still running: standard output is flushed, the main thread runs the exit hooks, and the process exits withmain‘s status, taking the other threads with it. AmainansweringErrends it the same way, its error line written first and with status 1 (seemain). - A library scope makes the joins structural:
scope(s -> …)returns its body’s result only once every thread started throughs.spawnhas finished.s.spawntakes the same arguments asspawnand is called by the thread that runs the scope’s body, while the scope is open; a call from another thread, or after the scope has returned, traps. AScopeis not sendable, so a thread the scope started cannot reach it; one that starts more uses a scope of its own.
import avin.threads.{Atomic, scope}
main() -> int
val parts = (0 ..< 4).map(_ -> Atomic(0)).toVector
scope(s ->
for i in 0 ..< 4
s.spawn(() -> parts(i).store((i + 1) * 10)))
parts.map(p -> p.load()).sum
Cancellation
A scope can end its threads early. s.cancel() cancels every thread the scope has started, and every
one it starts from then on; the scope still returns only once all of them have ended, and its body
goes on running after the call. What a program can rely on:
- A cancelled thread stops at its next cancellation point and ends with no result: nothing of its
body runs after that point but its deferred statements,
which run there, innermost first, as its frames are dropped. A deferred statement runs to its end
once begun: its own waits are not cancellation points while it runs, and a cancellation that comes
meanwhile stops the thread at its next cancellation point after. Its
result() -> Option[T]answersNone, and itsjoin()traps, since there is no value to give back; a thread that finished before its cancellation reached it keeps its result, andjoinanswers it as before. A thread started in a cancelled scope runs nothing. - Every wait is a cancellation point: a
sleep, a join, a channel’sreceiveor a full channel’ssend, timed or not, aselect, awithTimeout‘s wait for its body, a future’sawait, a promise’s future, a condition’swait(plain or timed), a wait group’swait(plain or timed), a semaphore’sacquire(plain, timed or inwithPermit), a read-write lock’sreadandwritewaiting to go in, a barrier’swait, a pool‘suseorleasewaiting for a resource, a socket’saccept,connect, read or write that waits for its connection, and the lookup of a host’s name that aconnector abindmakes, however long the resolver takes to answer. A thread parked in one is woken at once, however long the wait. So is every safepoint poll, which compiled code makes on every back edge of a loop and before every call that may recur, so a thread that computes without waiting stops too, within a loop’s turn. - A thread is never stopped where others may be waiting on what it is doing: while it holds a
lock (inside a
Mutex‘swith, for one), while it writes aRwLock, while it runs a barrier’s action, while it initializes alazy val, while it runs what waits for a future it completed. It goes on to the end of that region and stops at its first cancellation point after, so a cancelled thread never leaves a mutex held or a value half made. Waiting for a lock is not a cancellation point; waiting on a condition is, and a thread stopped there takes its mutex again, so the deferred statements of thewithbody run under it, andwithlets it go after them. A wait that left a mark on what it waits for takes it back as it stops: a writer or a reader waiting for aRwLockno longer holds back those behind it, and a party waiting at aBarrierwithdraws its arrival. A thread stopped inside aRwLock‘s read body or a semaphore’swithPermitbody gives its share of the lock, or its permit, back as it stops, as one inside a pool’suseorleasebody gives its resource back; what such a body was doing is its own, as any cancelled code’s is. - Cancellation reaches down through scopes. A cancelled thread that is running a scope of its own, in that scope’s body or waiting for its threads, stops first at that scope: the scope body’s deferred statements run, the inner scope cancels its threads and waits for them to end, each running its own, and only then does the thread go on stopping, running those of the functions around the scope. So every thread below a cancelled scope has ended once that scope returns. Cancelling an inner scope cancels only its own threads. A scope opened while its thread holds a lock is not cancelled with that thread, since nothing stops the thread there: its threads finish, and the thread stops once it has let the lock go.
- Actors are not a scope’s threads. An actor started by a scope’s thread is a top-level actor,
and its turns are threads of their own, so cancelling the scope does not reach it: it runs on and
ends as any actor ends, having handled what the cancelled thread told it, in the order told. A
thread parked in an actor’s
askis parked in a future’sawait, so it stops at once; the question stays in the actor’s mailbox and is handled in its place, and its reply, going to an ask nobody awaits, is dropped, the ask’s timer with it. An actor’s turn is never cancelled by a scope, but the scopes a turn runs are cancelled as any thread’s are. s.isCancelled()answers whether the scope has been cancelled, bycancelor by a cancellation of the thread running it.cancel, likespawn, is called by the thread that runs the scope’s body while the scope is open, and traps otherwise; cancelling twice is cancelling once.- A failure is a value. A trap still stops the whole program, so a scope’s threads report a
failure by returning or sending a
Result, and the body that sees it cancels the scope and returns it. A cancelled thread runs no code of its own as it stops: what it must leave tidy is either under a lock, which it finishes, or its own, which nothing else reaches. The Sysl handles its body reached, a connection for one, are handed back once the scope has waited for it, as a body that returns hands them back itself, so the code after the scope may use or close them.
import avin.threads.{Channel, scope}
fetchAll(urls: Vector[string]) -> Result[Vector[string], string]
val pages = Channel[Result[string, string]](urls.length)
scope(s ->
for u in urls do s.spawn(() -> pages.send(fetch(u)))
var got: Vector[string] = []
var failure: Option[string] = None
while got.length < urls.length && failure.isEmpty
pages.receive() match
Some(Ok(page)) -> got = got :+ page
Some(Err(e)) ->
failure = Some(e)
s.cancel()
None -> ()
failure match
Some(e) -> Err(e)
None -> Ok(got))
Lightweight threads
Every thread a program starts is a lightweight thread: the runtime runs it on one of a few operating system threads of its own, its workers, about one for each processor, started as threads become ready. What a program can rely on:
- Threads are cheap. A thread’s stack is reserved at its full size and the system commits its
pages only as the thread first touches them, so a thread that has not recursed deeply costs a page
or so of memory and a small record. A program may start a hundred thousand threads and join them
all, and a deep recursion still fits in a thread as it does in
main. - A parked thread holds only the stack it is using. A page of a stack stays committed once touched, so the runtime gives pages back: a thread that has stayed parked for about a millisecond (a tick of the runtime’s monitor, with no run between) returns every page below where it parked but the 16 KiB just under it, and the stack of an ended thread kept for the next returns all but its top 16 KiB. A thread that recursed a hundred thousand calls deep and then waits costs, while it waits, what a shallow one does; woken, it recurses as deep again on pages committed afresh. A thread parked only briefly, as one handing values back and forth parks, has run again before its tick and is not trimmed, so a park costs nothing more.
- A wait parks only its thread. A thread that waits for a lock, a condition, a channel, a join, a
future or a promise is set aside, and its worker runs another ready thread meanwhile; the thread
that ends the wait makes it ready again. Ready threads run in the order they became ready, and a
thread may resume on a different worker from the one it waited on. A timed wait parks the same way
and is made ready when its time runs out, and so does a
sleep: ten thousand threads sleeping at once hold no worker between them and all wake in about one sleep. Threads whose times run out together are made ready soonest first, two due at the same moment in the order they began to wait. A timed wait that its event ends first answers once, as woken, and its timer goes with it. A thread waiting on a connection, to accept, connect, read or write, parks the same way on the runtime’s poller, one kernel readiness set for the whole program, and is made ready when the socket is; a connection’s timeout parks it on a timer as well, and whichever comes first ends the wait. So waiting code is written as plain straight-line code: a server may hold one thread per connection, thousands of them waiting at once on a few workers, and a chain of futures each awaiting the one before it resolves however long it is. - Threads are time-sliced. A thread that has run for 10 ms while another thread is ready yields its worker at its next safepoint poll, which compiled code makes on every back edge of a loop and before every call that may recur, and goes to the back of the ready queue. A thread that computes without ever waiting therefore shares its processor rather than holding it, even in a tight loop that calls nothing, and a thread that does not overrun its slice pays nothing for it.
- A call that blocks its operating system thread does not starve the others. A Sysl binding
declared
blockingor a wait for alazy valbeing initialized elsewhere holds the worker it runs on, but once such a call has held its worker for a millisecond or two while other threads are ready, the runtime runs another worker in its place. A worker freed again rejoins the others, or rests until it is needed. - The main thread is not a worker.
mainruns on the program’s first operating system thread, and its own waits block that thread while the workers run the rest. It stays free for work that has to run there, a window above all, which theuipackage runs on it while other threads hand it work.isMainThread()answers whether the calling code runs on it:trueinmainand what it calls, and on a thread pinned to it,falseon every other lightweight thread, a pool’s workers among them.
Pinned to the main thread
Code on the main thread that waits blocks it, and with it a window’s frames. A thread pinned to the
main thread waits without blocking it: spawnOnMain(body) starts body on a lightweight thread
that runs on the main thread only, on a scheduler of the main thread’s own, and parks as any
lightweight thread does. So a handler that has to wait for something hands the waiting to one:
import avin.threads.{Future, spawnOnMain}
import ui.state
val shown = state("loading")
spawnOnMain(() ->
val page = Future(fetch(url)).await()
shown.set(page))
| Function | |
|---|---|
spawnOnMain(body, stack) | Starts body pinned to the main thread, and answers its Thread[T] |
scope.spawnOnMain(body, stack) | The same, as one of the scope’s threads, which it waits for and cancels |
runMainThreads() | Runs each pinned thread ready now, until it waits or ends, and answers how many ran |
isPinnedToMain() | Whether the calling code runs on a pinned thread |
-
It runs on the main thread only, and never beside the main thread’s own code. Only the main thread runs a pinned thread: when it calls
runMainThreads(), which the frame loop does once a frame, and while its own code waits, on a lock, a condition, a join, a channel, a future, a select or a sleep, so the main thread joining a pinned thread runs it. A pinned thread made ready while the main thread computes waits for it. A wait of the main thread on a connection, or in a call that blocks it in the system, runs none. -
It parks, and carries on on the main thread. A pinned thread waiting on anything parks, the main thread goes on with its frame or its own wait, and whatever ends the wait, another thread, a timer or the main thread itself, makes it ready for the main thread again. Ready pinned threads run in the order they became ready;
runMainThreadsruns those ready when it is called, each once, so one that a pinned thread makes ready runs at the next call. Nothing slices a pinned thread: it runs until it waits or ends, as the main thread’s own code does. -
It reaches what the main thread reaches.
bodyis not checked as sendable: a pinned thread may capture aState, aBufferor any other value of the main thread’s, since nothing else runs it, and it uses the Sysl handles the main thread owns as their owner.isMainThread()answerstruein it.Tneed not be sendable; the thread is, whereTis. -
Only the main thread starts one, its own code or a pinned thread; a thread on the workers that calls
spawnOnMainstops the program:avin: trap: a thread pinned to the main thread is started on the main thread, and `spawnOnMain` was called on anotherrunMainThreads()called anywhere but the main thread’s own code, a pinned thread included, runs nothing and answers 0. -
It is cancelled, joined and ended as any thread is. A scope’s
spawnOnMainis cancelled with the scope, and a pinned thread parked in a cancellable wait stops there. Whenmainreturns the program ends, as it always does: a pinned thread still parked, or ready and not yet run, runs no further. One still parked when a window’srunreturns stays parked and runs again whenever the main thread next runs the main scheduler. -
A deadlock names it. The main thread waiting without a limit on what only a parked pinned thread could give is a deadlock like any other, and the report says which threads are pinned:
avin: trap: deadlock: all threads are waiting main: waiting for a thread to end thread 1, pinned to the main thread: waiting to receive from a channel
What is shared
The heap is shared: every object is reachable from every thread that is handed a reference to it.
Nothing is copied when a thread starts; the closure and what it captures are simply reachable from
the new thread too. So what a thread is given is checked: a closure that runs on another thread
captures only sendable values and no var, and the value it gives
back is sendable, and the code it runs reaches no module-level mutable state (below). The threads
then share nothing mutable but through the synchronized types, a Mutex, an Atomic, a
Channel, a Once and a Condition. The error names what is captured and why it cannot be:
error: what is passed for `body` of `spawn` may use only sendable values from around it, but it uses `parts`, an `Array[int]`, which is mutable; an immutable copy (`toVector`) is sendable
A captured var lives in a box on the heap that the closure would share with the code that made
it, so it is never captured for another thread, whatever its type: the closure copies it into a
val first, or the two share an Atomic. A closure is checked by what it captures, the closures
it makes and the local functions it calls included; a method value captures its object, so
spawn(counter.tick) needs a sendable counter; and a by-name body, as Future(body) takes, is a
closure too. A function value held in a val of a plain function type is not sendable, since its
type does not say what it captures: the closure is written where it is passed, or the val is of a
@sendable function type.
A @sendable function type says it. @sendable (A) -> B (function
types) is the type of a function that may run on another thread, and
it is Sendable. A lambda, a function reference or a method value written where one is expected is
checked where it is made exactly as what is passed for a @sendable parameter is: what it captures,
and the module-level state it reaches (below); the error names the type. A value already of a
@sendable type is passed, stored, captured and sent as it is, so a data type or a class whose
fields are @sendable functions is sendable by its structure, and grammars, handlers and tasks held
in module-level values may be used by every thread:
import avin.threads.spawn
data Rule[T](run: @sendable (string) -> Option[T])
val digits: Rule[int] = Rule((s) -> s.toIntOption())
main() = println(spawn(() -> digits.run("42")).join())
error: a `@sendable (int) -> int` may use only sendable values from around it, but it uses `a`, an `Array[int]`, which is mutable; an immutable copy (`toVector`) is sendable
The checks are three marks on a parameter, which any function may use. What is passed for an
@isolated parameter may use only sendable values from around it: a closure captures only
sendable values (a var among them, where its type is sendable), and any other expression reads
only sendable locals and parameters; what it makes itself is its own. Nothing outside therefore
reaches a mutable value it makes or is given. An @isolating parameter asks that, and that
nothing it runs, however indirectly, reaches module-level mutable state (below), so no mutable
object it makes or is given can be stored where code outside reaches it: it is the mark of
isolate‘s body and with‘s function, and of a
mutex’s value and its body. A @sendable
parameter asks both, and that a closure captures no var, since it may run on another thread while
the code that made it goes on.
Inside the function, a parameter so marked that holds a function may be passed on for a parameter
with the same mark or a weaker one; an unmarked one may not, and the error says to mark it. A
method that overrides or implements another marks no parameter more strongly than the method it
overrides does, since a call through that method would not check what is passed.
A function type carries no marks either, so a function, a constructor or a method whose parameter
is marked is a value only where the value’s type gives that parameter a sendable type, a @sendable
function type for one that takes a function: spawn is a (@sendable () -> T, long) -> Thread[T],
and as a (() -> unit, long) -> Thread[unit] it is refused, as Mutex as an (Array[int]) -> Mutex[Array[int]] is. Where no function type is expected, the value takes the @sendable form of
each marked parameter that takes a function, so val sp = spawn[int] holds it and what is passed
through sp is checked where it is written. A by-name parameter so marked has no such type, so
isolate is not a value. A class or a data type that is a function has an apply that marks a
parameter only where its function type makes that parameter sendable likewise.
A parameter marked @sendable is sendable inside its function, whatever its type, since what
is passed for it was checked where it was passed: a @sendable lambda captures it, and it is passed
on for another @sendable parameter as it is. So what is passed for one that does not take a
function is itself sendable: a match function
written in place, whose arms are checked as a lambda’s body is, by what they capture and the
module-level state they reach; a value of a sendable type; a parameter so marked; or f.orElse(g)
of two of these, chains of it included, which holds only the two. A class’s val parameter so
marked is a field whose value is sendable, so it leaves the class sendable by its structure; a var
one does not, since it may be assigned anything later. The mark belongs to the parameter, not to its
type: a marked value copied into a local, a combination held in one, or one stored in a data field
of the trait’s type is a value of that type again. orElse is sendable of sendable parts by the
standard library’s promise, @sendable_result, which says a function’s result holds nothing but its
receiver and its arguments; nothing checks the promise against the body, so only the standard
library makes it, and a function of the program’s own that passes its marked arguments back is not
taken as one.
data Rule[T](run: @sendable (char) -> Option[T])
pick[U](@sendable f: PartialFunction[char, U]) -> Rule[U] = Rule(c -> if f.isDefinedAt(c) then Some(f(c)) else None)
pickAgain[U](@sendable f: PartialFunction[char, U]) -> Rule[U] = pick(f)
val digit: Rule[int] = pickAgain(match c if '0' <= c <= '9' -> int(c) - int('0'))
either[U](@sendable f: PartialFunction[char, U], @sendable g: PartialFunction[char, U]) -> Rule[U] = pick(f.orElse(g))
val sign: Rule[int] = either(match '+' -> 1, match '-' -> -1)
A lambda or a match function passed for a type argument bounded by Sendable, as
take[U: Sendable](value: U) has, is made as a @sendable function and checked as one where it is
made; a plain function type the expected type gives that argument is taken as its @sendable form.
A match function written where a PartialFunction is expected is made of two @sendable
functions there, one testing its patterns and guards and one running its arms, checked as one
function whose fault is reported once. PartialFunction itself does not promise Sendable, so the
bound holds at that call only where every value the call gives of the type argument is such a
match function: an argument of exactly that type for each parameter whose type could give the
callee a value of it (one naming it as what a function passed takes gives none). A written type
argument is held to the bound as written.
take[U: Sendable](value: U) -> U = value
val vowel: PartialFunction[char, int] = take(match
'a' -> 1
'e' -> 2)
import avin.threads.spawn
background[T: Sendable](@sendable body: () -> T) -> T = spawn(body).join()
spawn and s.spawn take their body @sendable, as do Future(body), ex.future(body), a
future’s map, flatMap and onComplete and the orElse of a future of a Result, whose
functions run on a pool thread. A mutex’s value and the body of its with are @isolating
(synchronization).
Module-level state is shared by every thread, and a body reaches it by name, not by capture.
So what is passed for a @sendable parameter, directly or through every function it calls, may
read and write no module-level var, and may read no module-level val, lazy val or object
whose type is not sendable. Avin compiles the whole program, so every function a body can reach is
known: the one a call names, every implementation a call through a trait method or an operator may
reach, every closure made on the way, an extractor a pattern calls, and the initializer of every
lazy val, module-level or a field, that it reads, since its first read runs the initializer on
whichever thread makes it. Which implementations a call may reach follows the types its receiver may
have: a data type’s own, a class’s and its subclasses’, those of the types conforming to a trait or
to a type parameter’s bounds, and, in a generic function called at known type arguments, those of
the types it is called at. So Map[int, string] in a body never reaches a program’s own hash, and
[Money(1), Money(2)].sum reaches Money‘s +. A module-level val of an
Atomic, a Mutex, a Channel or immutable data is the way to share state between threads; a
lazy val of a sendable type is initialized once whichever threads read it. A local lazy val a
body captures brings its initializer along, so what that initializer captures and reaches is held to
these rules too. The error names the
module-level item and the functions it is reached through:
var hits = 0
record() = hits += 1
main()
spawn(() -> record()).join()
error: what is passed for `body` of `spawn` may run on another thread, which shares the program's module-level state, but through `record` it reaches `hits`, a module-level `var`, which every thread would share; share it through an `Atomic` or a `Mutex` held in a `val`
With val hits = Atomic(0) and record() = hits.add(1) the program is legal. Printing is legal from any
thread: print, println and eprintln reach the runtime’s streams, which keep no state of the
program’s, and each call’s output goes out whole: a line one thread prints is never split by
another’s (system-library.md § Standard input). Reading standard input is legal from any thread
too: threads read it one at a time, and a line goes whole to exactly one of them. What is passed for an @isolating parameter is held to it too, though it runs at once on
the calling thread: a module-level value is where it could leave what it isolates for code that
does not take the lock. What is passed for an @isolated one is not.
A Sysl handle is the exception: it belongs to one owner at a time, and a use by any other code traps. The handles a thread’s body or a future’s body reaches move to it when it starts, and are in transit, for the next code to use them, once it returns, before a future’s value is published, so its awaiter may use them at once; an actor will own its handles across every message, whichever thread runs it.
Synchronization
avin.threads offers, all written in Avin over Sysl’s atomics and futex-based locks:
Mutex[T](value), which owns the value it guards, and is sendable whateverTis.m.with(v -> …)takes the lock, runs the body with the value and lets the lock go when the body returns, giving back what it returned; it is the way to a mutable value, such as aBuffer, which the body changes in place. So that nothing reaches the value but under the lock,valueis@isolating, made in the argument (Mutex(Buffer[int]()), not a buffer the caller keeps), and so is the body, whose result is sendable:with[R: Sendable](@isolating body: (T) -> R) -> R. A body may read and assign the caller’svars of sendable types, since it runs at once on the calling thread, and may read module-levelvals of sendable types (another mutex among them), but nothing it runs may reach a module-levelvaror a module-level value of a type that is not sendable: storing the value there, in a module-level collection or in an object’s field would let the program change it later without the lock. A body reads such avarthrough a local read before it. For a sendable value,get()reads it,set(v)replaces it andupdate(f)replaces it withfof it under the lock, giving back the new value. A mutex is not reentrant: a thread that takes one it already holds, from inside a body, would wait for ever, so it traps instead.Condition, made by a mutex’scondition()and waited for inside that mutex’swith; like its mutex, it is sendable.wait()lets the mutex go while it sleeps and holds it again before it returns;notify()wakes one waiting thread andnotifyAll()every one. A wait may end with nothing having changed, so the state is checked again in a loop.waitFor(timeoutMillis: long) -> boolwaits at most that many milliseconds and answersfalsewhere the time ran out first; a time that is not positive answersfalseat once.waitFor(timeout: Duration) -> boolis the same wait for aDuration, a part of a millisecond waited for as a whole one. Waiting from a thread that does not hold the mutex traps.Atomic[T](value), withload,store,swap,compareAndSet(expected, desired) -> boolandupdate(f), and for the integersadd(n)andsub(n), each giving back the new value and trapping on overflow, as+and-do, before anything is stored. Every operation is sequentially consistent; there are no weaker orderings in the library.int,long,booland references are the types it is for, and it holds any type: a value of one word is accessed lock-free, an integer wider than 64 bits under a lock the runtime keeps for them; an atomic reaches another thread only where its value is sendable, as aChanneldoes. A compare-and-set of a reference to an object of a class compares identity, as===does (even for a class that derivesEq), and of a function value likewise, so a reference held in anAtomicis also kept alive by it. A compare-and-set of a scalar compares the value (areal, its bits). A compare-and-set of a value without identity (a data value, anOptionamong them, a tuple, a string or anAny) compares with==: it succeeds where the current value is==toexpected, however the two were made. It loads the value, compares, and compares-and-sets the word it loaded, trying again where another thread changed it in between. The kind is known for eachT, so a scalar or a class reference costs one hardware compare-and-set.update(f)retries by the same rule.Channel[T](capacity = unbounded), a queue between threads: unbounded with no capacity (unboundedis the largestint), bounded with a positive one, wheresendwaits while the channel is full, and a rendezvous with a capacity of zero; a negative capacity traps.receive()waits for a value and givesSomeof it, orNoneonce the channel is closed and every value sent before is received.close()wakes every waiting thread, and a send to a closed channel traps; closing twice does nothing.trySend(x) -> boolandtryReceive() -> Option[T]never wait, andisClosed()asks. A channel isIterable:for x in chreceives until it is closed and drained. The waits have timed forms,receive(timeout)andsend(x, timeout).Once, whoserun(body)runs its body exactly once, whichever threads call it and however many at once: every other call waits until that body has returned, and returns without running its own.isDone()says whether it has run. A body that runs its ownOnceagain traps, as a mutex taken twice does.WaitGroup(), a count of work under way.add(n)countsnmore pieces (or-nfinished, wherenis negative),done()counts one finished, andwait()sleeps until the count is zero, returning at once where it is.waitFor(timeoutMillis: long) -> boolandwaitFor(timeout: Duration) -> boolwait at most that long and answer whether the count reached zero; a time that is not positive only asks.pending()reads the count. It counts work, not threads, so the pieces may be finished by threads started anywhere (a scope, an executor, an actor) or by no new thread at all; threads a body starts and waits for itself are a scope‘s to count, so a wait group has nospawnof its own. A count taken below zero traps, since a piece was finished twice or never added. A wait group is sendable.Semaphore(permits: int, capacity: int = permits), a count of permits.acquire()takes one, sleeping while there is none;tryAcquire() -> booltakes one only where one is free now;acquireFor(timeoutMillis: long) -> boolandacquireFor(timeout: Duration) -> boolwait at most that long;release()gives one back, waking a waiter.withPermit(body)takes a permit for its body and gives it back when the body returns, giving back what it returned; a cancellation that stops the thread inside the body gives the permit back too, and a trap inside it stops the program even in an actor, since the permit would never come back. A semaphore holds at mostcapacitypermits,permitsof them at first, soSemaphore(0, capacity = 1)starts empty and signals. A release that would hold more than the capacity traps, since it gives back a permit nobody took, as does a semaphore made with negative permits, a capacity below one, or more permits than its capacity. Permits belong to no thread: one thread may take a permit another gives back. Waiters take no place in a queue, so a thread that asks just as a permit comes back may take it before one that has waited longer. A semaphore is sendable.RwLock[T](value), a value many threads may read at once and one at a time may write. It owns its value as aMutexdoes, made in the argument, and its bodies are@isolatingand give back a sendable value:read[R: Sendable](@isolating body: (T) -> R) -> Rruns alongside any other readers,write[R: Sendable](@isolating body: (T) -> R) -> Ralone. A read body only reads; one that changes a value other readers are reading races with them, which nothing checks. For a sendable valueget()reads,set(v)writes andupdate(f)replaces the value withfof it, giving back the new value. Writers come first: once a writer waits, a thread that asks to read waits behind it, so a stream of overlapping readers never keeps a writer out; and when a write ends, every reader that was waiting goes in before the next writer, so a stream of writers never keeps those readers out. A write body runs to its end once begun, as a mutex’s does; a read body is stopped by a cancellation like any other code, and its share of the lock is given back as it stops. A trap inside either body stops the program, even in an actor, as one under a mutex does. It is not reentrant: reading or writing inside one’s own write traps, and writing inside one’s own read, or reading inside it while a writer waits, waits for ever. It is sendable whateverTis.Barrier(parties: int, action: () -> unit = () -> ()), a meeting point forpartiesthreads.wait() -> boolsleeps until that many have called it, then all go on and the barrier is ready for the next round; the last to arrive runs the@sendableactionfirst, once a round, with the others still waiting, and itswaitanswerstrue, every otherfalse. The action runs to its end once begun, and one that waits on its own barrier traps. A party whose thread is cancelled while it waits takes its arrival back, so the round still waits forpartiesthreads.partiesreads the number andwaiting()how many wait now. A barrier has at least one party, and is sendable.lazy vals, module-level, fields and locals alike (Initialization), initialized by exactly one thread, as aOnceis run: a thread that reads one while another runs its initializer waits, safe for the collector, and the stored value is published with a release store that every read pairs with an acquire load, so no thread sees it half made. A read by the initializing thread itself traps, as aOncerun again from its body does, and a trap in the initializer stops the program; a read that the initializer’s own code reaches, not through a function it hands on, is a compile error instead.
import avin.threads.{Channel, scope}
main() -> int
val jobs = Channel[int](16)
val results = Channel[int]()
scope(s ->
for w in 0 ..< 4
s.spawn(() ->
for n in jobs do results.send(n * n))
for n in 1 .. 100 do jobs.send(n)
jobs.close())
results.close()
var total = 0
for r in results do total += r
total % 256
Every operation that can wait marks its thread safe for the collector while it waits, so a thread
parked on a lock, a condition, a channel or a join, blocked on standard input, or writing to standard
output or standard error where they lead to a full pipe, its own trap report included, never holds up
a collection. A lightweight thread waiting for standard input from a pipe, a socket or a terminal
parks and leaves its worker to the others; that wait is not a cancellation point.
A Sysl binding declared blocking does the same. A thread
waiting for a mutex takes it in turn: the mutex goes to the thread that has waited longest. A thread
taking a lock it already holds traps at once. A program whose threads, main among them, all wait for
each other stops with a deadlock report; threads that wait for each other while another
still runs sleep for ever.
Rendezvous
A channel made with a capacity of zero holds no value at all: each send hands its value straight to a receiver, so the two threads meet.
import avin.threads.{Channel, spawn}
main() -> int
val handoff = Channel[string](0)
val worker = spawn(() ->
var n = 0
for job in handoff do n += job.charCount
n)
for job in ["parse", "check", "emit"] do handoff.send(job)
handoff.close()
worker.join()
What a program can rely on:
- A send returns only once a receiver has the value.
send(x)waits until a receiver takesx, and a receive waits until a sender hands it one. Everything the receiving thread did before its receive happens before the send returns, as everything the sender did before its send happens before the receive returns, so each side knows the other has reached the meeting point. - The
tryforms need a partner waiting now.trySend(x)handsxover only where a receiver is already waiting, a plainreceiveor aselectwaiting on a receive arm, and answers whether it did;tryReceive()takes a value only from a sender already waiting. Neither waits. A thread that has not yet begun to wait is not waiting. - The timed forms give up cleanly.
send(x, timeout)answersfalsewhere no receiver tookxwithin the time, and thenxis received by nobody;receive(timeout)answersNonewhere no sender came. A timeout of zero or less is thetryform. - A
selectarm hands a value over only when it is paired. A send arm runs once a receiver has taken its value, a receive arm once a sender has handed it one, and the partner may be a plainsendorreceiveor another select’s arm, waiting first or coming while the select waits. Every arm that does not run hands nothing over, so a value meant for a send arm that lost is never received. A select never pairs with itself: a send and a receive arm on one channel wait for other threads. Withelse, a select pairs only with a partner already waiting. - A close ends every hand-off. A sender waiting when the channel is closed traps, as any send to a
closed channel does, its value handed to nobody; a receiver waiting gets
None, and so does every receive after, since a closed rendezvous has nothing to drain. A select’s send arm traps and itsNonearm runs, as on any closed channel. - Its waits are waits like any other. A thread waiting to hand a value over, or to take one, parks only itself, is safe for the collector, and is a cancellation point: cancelled, it leaves nothing on the channel, and the next sender’s value goes to the next receiver. A cancellation that comes after a value was handed over stops a receiver that has it. The wait counts for the deadlock check, whose report names a sender as waiting to hand a value to a receiver. Which of several waiting receivers gets a value is not specified.
- Sendable as any channel. A channel of capacity zero is sendable where its values are, as every channel is.
Pools
A Pool[T, E] holds resources that are costly to make, connections to a database or a server
for one, and lends them again and again. use(f) lends one for the call of f, takes it back when
f returns, and answers Ok of what f returned, or the PoolError[E] that kept the call from
getting one:
class Pool[T, E](
create: @sendable () -> Result[T, E],
maxSize: int,
minIdle: int = 0,
validate: Option[@sendable (T) -> bool] = None,
destroy: Option[@sendable (T) -> unit] = None,
idleTimeout: Option[Duration] = None,
acquireTimeout: Option[Duration] = None,
maxLifetime: Option[Duration] = None,
jitter: Option[Duration] = None,
maxWaiting: Option[int] = None
): Sendable
data PoolError[+E]
AcquireTimedOut
QueueFull
PoolClosed
CreateFailed(error: E)
data PoolStats(size: int, idle: int, lent: int, waiting: int)
data PoolCounters(
acquires: long,
waited: long,
waitTime: Duration,
timeouts: long,
rejected: long,
created: long,
destroyed: long
)
- A resource is made when it is wanted. A call that finds a resource idle takes the one given
back last; one that finds none, with fewer than
maxSizemade, makes one withcreate; one that finds allmaxSizelent waits for one to come back. Acreatethat fails makes the call fail withCreateFailed, carrying the error, and gives up its place, so the pool holds nothing more than before and the next call tries again.warm()makes resources until the pool holdsminIdle, each left idle, answering the first failure ofcreate. - Waiters are served in the order they came. A resource given back goes straight to the call that has waited longest, and so does the place a destroyed resource gives up, where that call then makes a resource of its own; a call that comes while others wait queues behind them.
- A call waits for at most its timeout.
use(timeout, f)waits at mosttimeout,use(f)the pool’sacquireTimeout, or for ever where there is none, and a call whose time runs out fails withAcquireTimedOut, leaving the queue as if it had never come. A timeout of zero or less takes a resource only where one is idle, or can be made, now. - The queue may be bounded. Where
maxWaitingcalls already wait, a call that would join them fails at once withQueueFull, so a server can turn a request away instead of letting it queue; a call that finds a resource idle, or a place to make one, is never turned away, and a zeromaxWaitingmeans no call ever waits. With nomaxWaitingthe queue has no bound. A negativemaxWaitingtraps where the pool is made. - A broken resource is destroyed, not lent again.
lease(f)andlease(timeout, f)lend the resource as aLease[T], whoseresourceis the resource and whosemarkBroken()says it is no good any more: the pool destroys it, withdestroywhere one is given, when the body returns.validatechecks an idle resource before it is lent again, never one just made: one it answersfalsefor is destroyed and the call looks again. - Idle resources expire. A resource idle for longer than
idleTimeoutis destroyed the next time a call looks for one, oldest first, keeping at leastminIdle;evictIdle()does the same at once and answers how many it destroyed. With noidleTimeoutnothing expires. - Old resources retire. A resource older than
maxLifetimeis never lent again: one found idle is destroyed instead of being lent, and the call looks again; one given back is destroyed instead of being kept; andevictIdle()destroys the idle ones however few that leaves,warm()making upminIdleagain.jitterlengthens each resource’smaxLifetimeandidleTimeoutby its own random part ofjitter, from none up to all of it, drawn when the resource is made, so resources made together do not all expire together; it never shortens either limit. - The resource always comes back. It is taken back when
freturns and when the thread runningfis cancelled inside it. A cancellation anywhere else in the call puts back what the call held: its place among the waiters, a resource handed to it there, the place it held whilecreateran, a resourcevalidatewas checking, which goes back as one given back does (destroyed only where the pool is closed by then or the resource has outlivedmaxLifetime) and is checked again before it is next lent, so a deadline passing during a check never destroys a good resource. A trap insidef,create,validateordestroystops the program, even in an actor, as one inside a semaphore’swithPermitdoes, since what the pool counts would otherwise never come right. close()ends the pool. Every call waiting fails withPoolClosed, as does every call after, the idle resources are destroyed, andclosewaits for the lent ones to come back, destroying each as it does; a resource whose make finishes after the close is destroyed too. Closing a closed pool waits for the same, and a thread that closes a pool while a resource is lent to it waits for ever.isClosed()says whether it was closed.stats()answers what the pool holds now:sizeresources in all, of whichidlewait to be lent andlentare lent, andwaitingcalls waiting for one; the rest are being made or checked.maxSizebelow one, andminIdlebelow zero or abovemaxSize, trap where the pool is made.counters()answers running totals since the pool was made:acquirescalls lent a resource,waitedof them after waiting in the queue, andwaitTimeall of them took from the call to the lending, making or checking a resource included;timeoutscalls whose time ran out;rejectedcalls turned away withQueueFull; andcreatedresources made anddestroyedlet go, for whatever reason. Acreatethat fails makes nothing.- A resource belongs to one call at a time, so a pool of any type is sendable, as a
Mutexis, and its resources may be mutable connections. So that nothing else reaches one,create,validateanddestroyare@sendable, and a body is@isolatingand gives back only a sendable value: it may not keep the resource, or its lease, or hand it to the body of another pool’s call. Each resource given back has its Sysl handles put in transit, so the thread or actor it is lent to next uses them as its own.
import avin.threads.{Atomic, Pool, scope}
class Conn(val id: int): Sendable
main()
val made = Atomic(0)
val db = Pool[Conn, string](() -> Ok(Conn(made.add(1))), maxSize = 2)
val total = Atomic(0)
scope(s ->
for n in 1 .. 10 do s.spawn(() -> _ = db.use(c -> total.add(n))))
println(s"total ${total.load()}, made at most 2: ${made.load() <= 2}")
val st = db.stats()
println(s"lent ${st.lent}, all idle: ${st.idle == st.size}")
db.close()
println(db.use(c -> c.id))
total 55, made at most 2: true
lent 0, all idle: true
Err(the pool is closed)
select
A select block waits on several channels at once, and on connections, and runs exactly one of its
arms: the first whose channel operation can complete or whose connection has bytes to read.
select ending its line opens the block of its arms, written as a match’s are; elsewhere select
is an ordinary name. As with a match, inside brackets whose commas separate items a comma after a
one-line arm’s body ends the arm, the select and the item, so in f(select …
ch.receive() as v -> v, "a") the select is the first argument and "a" the second; a tuple
body there is written in parentheses.
import avin.threads.Channel
import avin.time.millis
step(jobs: Channel[int], results: Channel[int], r: int) -> string = select
jobs.receive() as Some(j) -> s"job $j"
jobs.receive() as None -> "no more jobs"
results.send(r) -> "sent"
after 100.millis -> "idle"
What a program can rely on:
- The arms.
ch.receive()receives, with an optionalaspattern:Some(p)runs for a value and binds it byp, any pattern avaltakes;Noneruns once the channel is closed and every value sent before is received; a name runs for either and binds theOptionreceived;_, or no pattern, runs for either and binds nothing.ch.send(x)sends, and binds nothing.stream.readable()runs once a read ofstreamwould answer without waiting, and binds nothing: its body reads.sink.writable()runs once a write tosinkwould send something without waiting, and binds nothing: its body writes, withwriteSometo send only what goes at once.after druns where no other arm became ready within theDurationd;elseruns where no arm is ready now, and is written as a match’selseis, with no arrow. A select has at most oneafterand oneelse, and not both, since a select withelsenever waits. Any other arm, anaspattern on areadable()orwritable()arm among them, is an error. - Guards. Any arm but
elsemay end in anifguard before its arrow, as a match arm does:local.writable() if pending.length > 0 -> …,ch.receive() as Some(x) if open -> …,after d if waiting -> …. Where the guard isfalsethe arm is absent from this select: its channel or stream is not evaluated, its value is not evaluated or sent, nothing of it is watched, and it never runs, as if it were not written. A guard is evaluated before anything is received, so unlike a match guard it cannot name what its arm’s pattern binds. An absentafterleaves a select that waits until an arm runs.elsetakes no guard: what follows it is its body, soelse if c then a else bis anif. A select whose every arm is guarded off, with noafterorelseleft, traps when it is reached, since nothing but a cancellation could end its wait; with anelseit runs theelse, and with anafterit waits its time. - Evaluation. The arms are evaluated once, in the order they are written, before any arm is
tried: an arm’s guard first, then, where it is
trueor there is none, its channel or stream, its value sent and theafter‘s duration. Only the arm that runs receives or sends: every other channel keeps its values, and a value meant for an arm that does not run is never sent. Areadable()arm reads nothing and awritable()arm writes nothing, whether it runs or not. - Streams. A
readable()arm’s stream is anyReadiness(avin.threads), which every connection, aByteStream, is: TCP, Unix and TLS. It is ready where the stream’sreadable()answerstruewhen the arm is tried: bytes already buffered, those areadLineread beyond its line or plaintext TLS has decrypted and not yet handed over, or bytes arrived on the socket, and also the other end having finished sending or reset the connection, or the connection closed, each of which its read then answers. A TLS stream counts only whole records of plaintext, so bytes of a record still arriving do not make it ready. Where it is not ready, a select that waits watches the stream’s descriptor on the runtime’s poller under the same one wait as its channel arms, and whichever comes first, bytes, a channel’s value, theafter‘s time or a cancellation, ends it. The arm uses its stream as a read does, so only the thread a connection belongs to may name it in a select; any other traps. This is how a thread that owns a connection waits both for its peer and for work other threads send it: it does all of the connection’s reads and writes, which only it may. A connection split into a reading and a writing half gives each half to a thread of its own, the reader’s select naming its half’sreadable()and the writer’s its half’swritable(). Standard input,io.stdin(), is aReadinesstoo: ready where bytes are in the buffer every reader of it shares, those areadLineread past its line among them, or have arrived, or the input has ended or failed, or it is a file. It belongs to no thread, so any thread may name it, and several at once; a select waits on it while another thread reads it, and bytes that read leaves in the buffer end the wait as arriving bytes do. - Sinks. A
writable()arm’s sink is anyWritability(avin.threads): every connection, and the writing half of a split one. It is ready where the sink’swritable()answerstruewhen the arm is tried: the socket has room, or the connection has failed or is closed, which its write then answers. A TLS sink is ready once the record an earlierwriteSomebegan has gone,writable()sending what of it there is room for; the writing half of a split TLS connection sends each record whole before itswriteSomeanswers, so the loop below leaves nothing behind when it ends. Where it is not ready, a select that waits watches the descriptor for room on the poller as areadable()arm watches it for bytes, and a reading and a writing half of one connection may each wait on it at once, each woken by its own direction. - Choosing. Of several arms ready at once, each is as likely to run as any other: the arms are
tried in an order drawn at random each time. Where none is ready, a select with
elseruns it, and one without waits, parking only its thread, until an arm can complete and runs it, or until itsafter‘s time runs out. Anafterof zero or less tries the arms once, aselsedoes. A select ofafteralone waits its duration; one ofelsealone runs it. - Closed channels behave as
receiveandsenddo. The values sent before a close are received first; then aNonearm, a name and_run, every time after.Some(p)is never ready on a closed and drained channel, so a select whose only arms want values from closed channels waits for itsafter, runs itselse, or waits for ever. A send arm on a closed channel traps when it is tried, assenddoes. - Its value. A select is an expression whose value is the arm that ran, the arms’ types unified as
a match’s are. Its bodies are code of the function it is in:
return,breakandcontinueleave an arm as they leave a match’s, and a select may be nested in another’s arm.end selectmay close one. - Waiting. A select that waits is a wait like a channel’s own: safe for the collector, and a
cancellation point, so a scope’s
cancelstops a thread parked in it at once. Nothing of a select that has ended, by an arm, its time or a cancellation, is left on the channels or the poller it waited on, and a notification a select was woken by and could not use is passed on to the next thread waiting on that channel, so no other waiter, a plainreceiveor another select, misses it.
import avin.threads.Channel
// Workers take jobs until told to stop; each result is sent where there is room for it.
work(jobs: Channel[int], stop: Channel[unit], results: Channel[int])
var going = true
while going
select
jobs.receive() as Some(j) -> results.send(j * j)
jobs.receive() as None -> going = false
stop.receive() as None -> going = false
import avin.io.IoError
import avin.net.TcpStream
import avin.threads.Channel
// The thread that owns a connection echoes each line its peer sends and forwards each message other
// threads put on `pushed`, until the peer finishes sending.
serve(conn: TcpStream, pushed: Channel[string]) -> Result[unit, IoError]
var open = true
while open
select
conn.readable() -> conn.readLine()? match
Some(line) -> conn.writeText(s"echo $line\n")?
None -> open = false
pushed.receive() as Some(m) -> conn.writeText(s"push $m\n")?
conn.close()
import avin.io.IoError
import avin.net.StreamWriter
import avin.threads.Channel
// The writing half of a split connection sends `data` a part at a time, as room comes, and stops
// early where `stop` is closed. Answers how much went.
send(w: StreamWriter, data: Array[byte], stop: Channel[unit]) -> Result[int, IoError]
var at = 0
while at < data.length
select
w.writable() -> at += w.writeSome(data.slice(at, data.length))?
stop.receive() as None -> return Ok(at)
Ok(at)
import avin.io.IoError
import avin.net.TcpStream
// Relays bytes both ways between two connections on one thread. Each side is read only while
// nothing it gave is still waiting to be written, and watched for room only while something is.
relayPair(a: TcpStream, b: TcpStream) -> Result[unit, IoError]
var toA: Array[byte] = Array.empty()
var toB: Array[byte] = Array.empty()
var aOpen = true
var bOpen = true
while aOpen || bOpen || toA.length > 0 || toB.length > 0
select
a.readable() if aOpen && toB.length == 0 ->
toB = a.read(4096)?
if toB.length == 0
aOpen = false
b.shutdownOutput()?
b.readable() if bOpen && toA.length == 0 ->
toA = b.read(4096)?
if toA.length == 0
bOpen = false
a.shutdownOutput()?
a.writable() if toA.length > 0 -> toA = toA.slice(a.writeSome(toA)?, toA.length)
b.writable() if toB.length > 0 -> toB = toB.slice(b.writeSome(toB)?, toB.length)
Ok(())
Selectors
A Selector[K] (avin.threads) is a select whose arms are made at run time, for a set of sources
whose size is known only then: N connections, N tunnels, N workers’ channels. Each entry is added
under a key of the program’s choosing, and a wait answers the key of the entry that ran.
import avin.io.IoError
import avin.net.TcpStream
import avin.threads.Selector
// One direction of a relayed connection pair, and what it has read and not yet written.
class Leg(val from: TcpStream, val to: TcpStream)
var pending: Array[byte] = Array.empty()
var open = true
// Relays bytes both ways between each pair of connections, all on one thread, until every pair has
// finished: each leg's reading side is watched while it has nothing pending, and its writing side
// while it has.
relay(pairs: Vector[(TcpStream, TcpStream)]) -> Result[unit, IoError]
val legs = Buffer[Leg]()
for (a, b) in pairs
legs.append(Leg(a, b))
legs.append(Leg(b, a))
val sel = Selector[int]()
while legs.exists(l -> l.open || l.pending.length > 0)
sel.clear()
for i in 0 ..< legs.length
val leg = legs(i)
if leg.pending.length > 0 then sel.writable(leg.to, i)
elif leg.open then sel.readable(leg.from, i)
val leg = legs(sel.wait())
if leg.pending.length > 0
leg.pending = leg.pending.slice(leg.to.writeSome(leg.pending)?, leg.pending.length)
else
val bytes = leg.from.read(4096)?
if bytes.length == 0
leg.open = false
leg.to.shutdownOutput()?
else leg.pending = bytes
Ok(())
What a program can rely on:
- Entries.
receive(channel, key) -> SelectReceive[T]adds a receive, ready for a value or the channel closed and drained, and answers the entry, whosereceived()is theOptionreceived once a wait has answered its key.send(channel, x, key)adds a send ofx, which a wait sends only where it answers the key; the entry is then taken out, so a value goes once.readable(source, key)andwritable(sink, key)add a stream or a sink, as a select’sreadable()andwritable()arms take them. Several entries may share a key. - The set. The entries stay from one wait to the next, so a loop may keep them and change only
what changed, or
clear()them and add afresh.remove(key)andremoveWhere(p)take out every entry under a key, or whose key satisfiesp, and answer how many;sizeandisEmptycount them. The set is changed only between waits, by the thread that waits on it. - Waiting.
wait() -> Kwaits until an entry can complete, does its one operation and answers its key.within(timeout) -> Option[K]waits at mosttimeout,Nonewhere the time runs out first;until(deadline)waits until anInstant;poll() -> Option[K]tries the entries once, aselsedoes, and a timeout of zero or less does the same. Awaiton an empty selector traps, as a select with every arm guarded off does;withinanduntilon one wait out their time, andpollanswersNone. - The same wait as
select. A selector’s wait is a select’s own: entries tried in an order drawn at random each time, so of several ready at once each is as likely to run, and none is starved; one pass and one park on the runtime’s poller however many entries there are; at most one operation done, no other entry receiving, sending, reading or writing; a closed channel, a send to a closed channel, a stream used by a thread it does not belong to, cancellation and collection all as a select’s arms meet them, above.
Timers and deadlines
Time reaches threads as channels and as limits on their waits, all in avin.threads, each taking a
Duration. Every one of them waits as the other waits do: parking its lightweight
thread on the runtime’s timers, holding no worker, safe for the collector, and a cancellation point.
import avin.threads.{Channel, Ticker, after}
import avin.time.seconds
// Reports progress every second, and gives up on a result after five.
watch(results: Channel[int]) -> Option[int]
val progress = Ticker(seconds(1))
val deadline = after(seconds(5))
var got: Option[int] = None
var waiting = true
while waiting
select
results.receive() as r ->
got = r
waiting = false
progress.ticks.receive() -> println("still waiting")
deadline.receive() -> waiting = false
progress.stop()
got
What a program can rely on:
after(d) -> Channel[Instant]gives a channel that delivers one value oncedhas passed from the call, at once for a duration of zero or less: theInstantat which the time fell due, however late it is received. It is never delivered sooner. Only that one value ever comes; a receive after it waits for ever, as on any channel nobody sends to, unless the channel is closed, which ends it: nothing is delivered after a close. Of several threads receiving from one, one gets the value. It is an ordinary channel to receive from, alone, with a timeout, or as aselectarm (after(d).receive() -> …, beside theafter darm, which it does not replace).- An
afternobody receives from costs nothing but itself. The channel holds the time its value falls due, a number, and no timer, thread or registration anywhere: the value is delivered by the first receive or select that looks once the time has come, and a thread waiting on the channel waits on its condition until that time at the most, its timer going with its wait. So anafterthat is dropped, lost in a select to another arm, or waited on by a thread that is cancelled leaves nothing behind and is collected like any object once nothing reaches it, however long its duration. Ticker(period)delivers a tick on its channeltickseveryperiod, the first a period after it is made, each tick the instant it fell due, so each later than the one before. The channel holds one tick at the most: a tick that falls due while one is waiting to be received is dropped, so a slow receiver gets the oldest tick it has not received and then the ticks from its next period on, never a backlog to catch up on.stop()ends the ticks: none is received after it, not even one that fell due before and was waiting; stopping twice is stopping once, and a thread waiting onticksgoes on waiting. A ticker, likeafter, holds no timer or thread, so one dropped withoutstopis collected as any object is. A period of zero or less traps. ATickeris sendable.- Nothing sends to these channels but their clocks. A
send,trySend, timed send orselectsend arm on a channelafteror aTickermade traps. receive(timeout: Duration) -> Option[T]waits at mosttimeoutfor a value:Someof one, as soon as it comes, orNonewhere the time ran out first or once the channel is closed and every value sent before is received;isClosed()tells the two apart where it matters.send(x, timeout: Duration) -> boolwaits at mosttimeoutfor room in a bounded channel, or for a receiver to takexfrom a rendezvous, and answers whether it sentx, at once on an unbounded one; a value whose time ran out is not sent. A send to a closed channel traps, assenddoes, rather than answeringfalse. A timeout of zero or less is thetryform,tryReceiveortrySend. Neither waits less than its timeout where nothing comes, a part of a millisecond waited for as a whole one.withTimeout(timeout, s -> …) -> Option[T]runs its body with a new scope for at mosttimeout:Someof what the body returns where it returns in time,Nonewhere the time runs out first. Then the body is cancelled where it is, waiting or computing, with every thread its scope started and everything below them, as a scope’scancelcancels them, andwithTimeoutanswers once they have all ended. A body that has returned keeps its result, however close to the time it did. A timeout of zero or less runs nothing and answersNone. Timeouts nest: an inner one ends at its own time or when an outer one’s runs out, whichever is sooner, and awithTimeouton a thread a scope cancels stops with that thread, its body first.- The body of a
withTimeoutruns on a lightweight thread of its own, so that it can be stopped wherever it is, even whenwithTimeoutis called frommain, whose own thread no cancellation reaches. The calling thread waits for it, so the body reads and assigns the caller’s variables, uses the Sysl handles the caller owns and those it makes are the caller’s after it, as a closure the caller ran itself would. What tells the threads apart still does:isMainThread()answersfalsein the body, and a lock the caller holds is not the body’s, so a body that takes it waits for ever, and one that waits on a condition of it traps.
Request context
A piece of work, a request a server handles above all, carries a deadline and values that every call
it makes may need: the time left for a query, the request’s id for a log line. They live in the
running thread’s request context, in avin.threads, which code reads where it is rather than
being handed it through every signature on the way. A scope sets it for the code it runs:
import avin.threads.{Context, Key, withDeadline, withValue}
import avin.time.{now, seconds}
val requestId = Key[string]("request id")
handle(id: string, request: Request) -> Reply
withValue(requestId, id, () ->
withDeadline(now() + seconds(5), s -> respond(request)) match
Ok(reply) -> reply
Err(e) -> Reply.unavailable(e.message))
// Far below, with no parameter for either.
log(line: string) = println(s"[${Context.current().value(requestId).getOrElse("-")}] $line")
Context.current() -> Contextis the calling thread’s context: what the scopes around the running code set, or one with no deadline and no values. AContextis an immutable snapshot, and sendable: the scopes the code opens after reading it change the thread’s context, not the snapshot. It answersdeadline -> Option[Instant], the instant the work must be done by, andremaining() -> Option[Duration], the time left on the monotonic clock, never less than zero; bothNonewhere no scope set a deadline.mainand every thread begin with an empty context.Key[T](name)names a value of typeT. Keys are told apart by identity, not by name, so two keys of one name are two keys: a module makes each of its keys once, as a module-level value, and what it sets is reached by that key alone.key.nameis for showing.withValue[K: Sendable, T](key: Key[K], value: K, body: () -> T) -> Trunsbodyon the calling thread withvalueunderkey, and puts the context back however the body ends, by returning or by a cancellation. Inside,value[T](key: Key[T]) -> Option[T]answersSome(value), in the body, everything it calls and the threads it starts, until a scope inside sets the key again, which shadows it for its own body. A value of another type is not set under a key, and a lookup answers the key’s own type, so nothing is cast; the value is sendable, since other threads read it.withDeadline[T](deadline: Instant, body: (Scope) -> T) -> Result[T, DeadlineExceeded]iswithTimeoutat an instant:Okof what the body returns beforedeadline, andErr(DeadlineExceeded(deadline))where the deadline passes first. Then the body is cancelled where it is, with every thread its scope started and everything below them, andwithDeadlineanswers once they have all ended. A deadline already past runs nothing. The wall clock is read once, as the scope opens; from then on the deadline runs on the monotonic clock, so setting the wall clock moves nothing.DeadlineExceeded(deadline: Instant)is anErrorwhose message isdeadline exceeded.withTimeoutsets the deadline too,timeoutfrom the call, so code below either reads the time it has left the same way.- A nested deadline tightens and never loosens. The context keeps the sooner of a scope’s deadline and the one around it, so the code inside a later deadline still reads the earlier one, and the outer scope’s cancellation ends the inner one with its body: no scope can buy its body more time than the scope around it has.
- Blocking calls observe the deadline through cancellation. Since a deadline that passes cancels
its body, every cancellation point below it ends at once: a socket’s
accept,connect, read or write, a pool‘suseorleasewaiting for a resource, a channel’sreceiveorsend, aselect, asleep, a join, a future’sawait, and the safepoint poll of code that computes without waiting. Nothing inside sees an error value: the body stops, its deferred statements run, and the error iswithDeadline‘s answer. Code that must tidy up when it is stopped, a database driver telling its server to drop a query, does it in adefer, whose waits run to their end. - What a new thread takes follows what can cancel it. A thread a scope starts, through
s.spawnor as the body of awithTimeoutorwithDeadline, takes the starter’s whole context, its deadline with its values, since a cancellation of the scope reaches it. A plainspawn, the code of a future (Future(…),executor.future,map,flatMap,onComplete) and an executor’sexecutetask take the values with no deadline, since nothing cancels them when it passes; a future’s callback takes the context of the code that attached it, not of the thread that completed the future. An actor’s turn takes nothing, neither from the code that told the actor nor from the worker it runs on: an actor outlives any one request, and its turns are not a scope’s threads. A value that should cross atellgoes in the message. - Siblings share nothing. A value a thread sets is its own and its children’s: the thread that started it, and the threads beside it, do not see it.
Futures
A Future[T] is a value some thread is computing. Future(body) runs body on a shared pool of
worker threads and gives back the future of its result at once; the body is a by-name argument, so
it is written in place or as a trailing block. Its value is sendable, as a thread’s result is, and
the body, like the functions of map, flatMap and onComplete, is @sendable:
apply[T: Sendable](@sendable body: -> T) -> Future[T].
import avin.threads.Future
main() -> int
val page = Future(fetch(url))
val counted = Future
val words = load(path).split(" ")
words.length
val both = for p in page, n in counted yield s"${p.charCount} characters, $n words"
println(both.await())
0
await() -> Twaits for the value and gives it back, the same value at every call, safe for the collector while it waits.await(timeoutMillis: long) -> Option[T]waits at most that many milliseconds and givesNonewhere the time ran out first; a time that is not positive asks without waiting;await(timeout: Duration) -> Option[T]is the same wait for aDuration, a part of a millisecond waited for as a whole one.isCompleted()asks andvalue()givesSomeof a value already there; neither waits. Every timed wait takes a count of milliseconds as alongor aDuration.- The combinators never wait.
map(f)andflatMap(f)give the future offof the value,frunning on a pool thread once the value is there, so afor … yieldover futures is a future too;zip(other)pairs two values, andonComplete(f)runsfwith the value on a pool thread, at once where it is already there.Future.successful(x)is complete from the start.Future.all(fs)is the future of every value of anIterableof futures, as aVectorin the order given, andFuture.first(fs)of the value of whichever completes first; the first of no futures traps.Future.traverse(xs, f)isFuture.alloffof each element:fis called for the elements in turn on the calling thread, so their futures start in that order, and the values come back in it however the futures finish. - A future carries no failure. A computation that can fail returns
Future[Result[T, E]], and such a future also hasmapOk(f)andflatMapOk(f), which go on from anOkand pass anErrthrough unchanged,mapErr(f), which turns one failure into another, andorElse(other), which goes on to the futureothergives only where this one ended inErr:otheris by-name, so it is not even evaluated after anOk, and its value may widen to a supertype asResult.orElse‘s does. A trap inside a body stops the program, as it does in any thread. - A
Promise[T](), of a sendableT, is completed by another thread: itsfuturefield is the future it completes,complete(x)completes it and traps where it is already complete, andtryComplete(x) -> boolcompletes it only where nothing has yet, answering whether it did, for threads that race to give the value. Every thread waiting on the future wakes with the value.
The pool is started on first use, one worker at a time as work arrives with no idle worker, up
to one for each processor the machine offers. Its workers are never joined, so the program ends when
main returns, whatever futures are pending. A body may wait for another future. A worker that
waits in await for a value not yet there counts as not working, and where work is queued with no
worker free, the pool starts another in its place; that one stays afterwards and is idle while there
is nothing to do. So waits nested in bodies never leave the pool without a thread to run the future
they wait for, however deep they go, and the pool grows only by as many workers as wait at once.
blocking(body) says a body waits on something other than a future: a channel, a lock, a
blocking binding, a sleep. On a worker of a pool, the pool counts it as waiting while body runs,
exactly as it counts one in await, so a full pool whose workers all wait on a channel still runs
the task that fills it. A blocking inside another, or an await inside one, is counted once; on a
thread of no pool, body simply runs. sleep(d: Duration) pauses the calling thread for at
least d (from avin.time), safe for the collector while it
sleeps; a duration of zero or less returns at once, and one longer than about thirty years sleeps
about thirty years. A thread that sleeps parks, freeing the operating system thread it ran on, and
main blocks its own. A worker of a pool that sleeps keeps its place in the pool, and
blocking(sleep(d)) gives it up.
import avin.threads.{blocking, Channel, Future}
main() -> int
val jobs = Channel[int]()
val workers = (0 ..< 64).map(i -> Future(blocking(jobs.receive().getOrElse(0)))).toVector
Future
for n in 1 .. 64 do jobs.send(n)
println(Future.all(workers).await().sum)
0
Executors
An Executor is a pool of threads of one’s own, chosen explicitly. Executor.fixed(n) keeps at
most n tasks working at once, n at least one, and starts its workers as work comes, as the shared
pool does; a worker waiting in await or blocking leaves its place to another there too.
ex.future(body) runs body on it, and a future runs its map, flatMap and onComplete on the
executor it was made on, so a chain begun on an executor stays there. Executor.shared() is the
pool Future(body) uses, the same executor every time it is asked (Executor.shared() === Executor.shared()); Future.successful, Future.all, Future.first, Future.traverse and a
promise’s future belong to it.
An executor is sendable, so a body on one thread may give work to an executor another made.
ex.shutdown() lets the workers end once the queued work is done, and isShutdown() asks. A new
body given to an executor after its shutdown traps, but the functions of futures already made on it
still run there, on a worker started for them where none is left, so no future made before is
stranded. The shared executor is never shut down: isShutdown() answers false, and
shutdown() on it traps.
ex.execute(task) runs a @sendable task on the executor’s workers with no future of it, the
way to give a pool work that queues itself again and again, as an actor’s
turns do. The task runs as an owner of its own and nothing is handed to it: a Sysl handle it reaches
that the giving code owns traps there, and one in transit is taken by its first use. Work given
after shutdown still runs, as the functions of futures do.
import avin.threads.{Executor, Future}
main() -> int
val io = Executor.fixed(4)
val sizes = (1 .. 100).map(n -> io.future(n * n)).toVector
val total = Future.all(sizes).await().sum
io.shutdown()
println(total)
0
The memory model
Starting a thread happens before its body; its end happens before join returns; releasing a
Mutex happens before the next thread takes it; a send happens before the receive that gets the
value; an Atomic store happens before every load that reads it; and completing a future happens
before every await that gives its value and before every combinator’s function runs with it.
In the rest of the kit: a wait group’s add and done happen before every wait they let return;
a semaphore’s release happens before the acquire that takes the permit it gave back; the end of a
RwLock‘s write happens before every read and write after it, and the end of a read before the
write after it; and every party’s arrival at a barrier happens before that round’s action, which
happens before every party’s wait of the round returns. A program whose shared
mutable state is reached only under those edges sees sequentially consistent behaviour.
A data race on a plain field or var is not undefined behaviour. A racing read returns some
value that was written to that location, never a mixture of two writes and never a value nothing
wrote. The compiler gives each access to a heap field, an array element or a module-level variable
LLVM’s unordered atomic ordering: it costs nothing on the machines Avin targets, and it is exactly
the promise that a word is never torn and a racing load is never undefined. A reference is one word,
and so is every built-in scalar.
An integer wider than 64 bits and a union with a scalar member are the exceptions. An i128 or
an i257 is several words, and so is a union such as int | string (its tag, its bits and its
reference); a field or variable of one is read and written as untorn 64-bit words, so a racing read
of it may combine words of different writes. Each word is still one some write stored, a union read
so is still a value of one of its members (never a reference no write stored), and a program that
shares such a value under the edges above, or through an Atomic or a Mutex, never sees a mixture.
An object is complete before any thread can see it. A constructor ends with a release fence, so
a thread that reads a reference to an object, however it got it, sees the object’s header and every
field its constructor set. Without it, a racing reader could see a reference field still zero,
which no Avin type allows. The fence is no instruction at all on x86-64 and one dmb ish barrier
on arm64, paid once per object made.
Collection
A collection stops the world. The collecting thread raises a flag, waits until every other thread is parked at a safepoint or is in a safe region (waiting, or blocked in a call), collects alone, and lets them go. A lightweight thread that is not running is always safe, and its frames stay roots of every collection however long it waits. Compiled code polls the flag on every back edge of its loops and once per level of every recursion, one load and a branch that is not taken; allocation is itself a safepoint. A program with one thread never stops anything.
Actors
Actors handle their messages in turns, each turn a lightweight thread of its own (scheduling), so a turn that waits parks as any thread does. An actor handles one message at a time, so its own state needs no lock; its mailbox is a runtime queue on the shared heap, and a message is a reference to a heap value. A message holds only values no other actor can change, or hands over the ownership of what it carries, and the compiler checks every message type for it (what may be sent).
Diagnostics
The runtime says what a program’s threads are doing in two cases: when every one of them waits for something nothing can end, and when the program is asked for a thread dump.
Deadlock
A program whose threads all wait for each other stops, as a trap does, with a list of its threads and what each waits on:
avin: trap: deadlock: all threads are waiting
main: waiting for a thread to end
thread 1: waiting for a lock
thread 2: waiting for a lock
The program is deadlocked when main waits without a limit (for a lock, a condition, a join, a
channel, a future, a select with no after, or anything built on them) and at that
moment
- no lightweight thread is running or ready, so every one waits too, and
- nothing outside the threads can end a wait: no timer is set (a sleep, a wait with a limit, an
ask‘s limit, an actor’s timer, a signal watch’swait), no thread waits on a socket, no thread the program did not start is attached, and no callback that such a thread may call (@threaded) is held.
A thread in a blocking call (a blocking binding, reading
input, waiting for a command to end) is running, so a program with one is never deadlocked, and
neither is a main that runs its own work, a window’s frame loop among it. An ask that nobody
answers is not a deadlock either: its limit ends it with TimedOut. An actor waiting for mail
nobody can send has no thread at all, so main awaiting its end is deadlocked once everything else
waits.
The report lists main first, then each lightweight thread still there in the order the threads
started, numbered from 1 by that order; a thread that has ended is not listed. A pool’s idle workers,
which wait for work by design and are as many as the machine has processors, are left out. What a
thread waits on is one of: a lock (a mutex’s), a condition, a thread to end (a join), receiving from
a channel, sending to a full channel, a future, or a select, waiting in a select on 2 channels,
which counts its arms that name a channel. A select with an after is a wait with a limit, whose
timer can always end it, so it is never part of a deadlock. The report goes to standard error after what the
program had printed is flushed, the exit hooks run after it, and the program ends as a trap ends it.
Only a deadlock of the whole program is found. Threads that wait for each other while another
thread still runs, or while main computes or sleeps, are not reported: the program goes on, and
ends when main returns.
Thread dump
The quit signal (Ctrl-\ at a terminal, kill -QUIT) writes every thread’s state to standard error,
and the program goes on as it was:
avin: thread dump
main: running
thread 1: waiting to receive from a channel, 250 ms
thread 2: sleeping, 1200 ms
thread 3: waiting on a condition with a time limit, 3 ms
thread 4: running
thread 5: idle, waiting for work, 40 ms
Each thread is running (running, in a blocking call while it is in one), ready for a worker,
ending, or waiting, with what it waits on as in the deadlock report, sleeping, waiting on a socket, or, for a pool’s worker with no work, idle, waiting for work; a wait with a limit says so,
and every wait says how long it has lasted, in milliseconds. Idle workers are listed here. A dump
names no functions: a thread’s frames do not record which function they belong to.
A program that watches the quit signal hears of it instead, and no dump is written while the watch lasts. A program started with the signal ignored keeps it ignored.