Library: Task(T), a thread given a value with no cast; Guarded(T), a value with its lock Task.spawn copies x to the heap; the thread runs p on its copy and frees it. Guarded.with runs p on the value under a Thread.Mutex. Tested in test/run (a generic module has no main of its own); the Guarded test fails without the lock. Breaks no program.
| @@ -98,7 +98,7 @@ | ||
| 98 | 98 | # its sources, in the order of doc/raz-lib.md, between the page's own |
| 99 | 99 | # head (with its date) and tail |
| 100 | 100 | LIBDOC = Rt Mem Heap Arena Lock Thread.linux-x86_64 Out Sys.linux-x86_64 \ |
| 101 | - Str Bytes.x86_64 Buf Fmt List Map Vec Sort Math Files Opt Width Rand Check Prop C | |
| 101 | + Str Bytes.x86_64 Buf Fmt List Map Vec Task Guarded Sort Math Files Opt Width Rand Check Prop C | |
| 102 | 102 | |
| 103 | 103 | man: build/raz-lib.3 |
| 104 | 104 |
| @@ -211,7 +211,7 @@ | ||
| 211 | 211 | (buffered output), `C`, `Lock`, `Thread` (threads, with C or without), `Mem`, |
| 212 | 212 | `Heap`, `Arena`, `Str`, `Bytes`, `Buf`, `Fmt`, `Math`, `Files`, `Opt` (a command |
| 213 | 213 | line read through one table), `Width` (the columns of a terminal text takes), `List(T)`, `Map(K, V; hash, eq)`, |
| 214 | -`Vec(T)` (a growable array) and `Sort(T; less)`. Volume 2 of the report, `doc/raz-lib.md`, gives each one's interface, | |
| 214 | +`Vec(T)` (a growable array), `Task(T)` and `Guarded(T)` (threads that take a value, and a value with its lock) and `Sort(T; less)`. Volume 2 of the report, `doc/raz-lib.md`, gives each one's interface, | |
| 215 | 215 | and `tools/Doc.raz` prints it from the module's file: |
| 216 | 216 | |
| 217 | 217 | $ ./raz -o Doc tools/Doc.raz && ./Doc lib/Str.raz |
| @@ -21,6 +21,8 @@ | ||
| 21 | 21 | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | |
| 22 | 22 | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | |
| 23 | 23 | | `Thread` | threads, `spawn`, `join` and `signal`, and `Mutex`, in any program; in one that uses C, made by C's `pthread_create` | |
| 24 | +| `Task` | a thread given a value of its own, `Task(T)`: `spawn` copies it, with no cast | | |
| 25 | +| `Guarded` | a value with its lock, `Guarded(T)`: reached only through `with` | | |
| 24 | 26 | | `Mem` | `Allocator`, the interface of every allocator, and pages from the system | |
| 25 | 27 | | `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program | |
| 26 | 28 | | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | |
| @@ -120,7 +122,29 @@ | ||
| 120 | 122 | proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone: 0, or minus an errno *) |
| 121 | 123 | proc lock*(var m: Mutex); |
| 122 | 124 | proc unlock*(var m: Mutex); |
| 125 | + | |
| 126 | +module Task(T); | |
| 127 | +proc spawn*(p: proc(var x: T); x: T): (^Thread.Thread, ERROR); (* p run on a copy of x in a new thread *) | |
| 128 | + | |
| 129 | +module Guarded(T); | |
| 130 | +type Guarded* = record ... end; (* a value and its Thread.Mutex; zero is ready *) | |
| 131 | +proc with*(var g: Guarded; p: proc(var x: T)); (* p run on the value, under the lock *) | |
| 123 | 132 | ``` |
| 133 | + | |
| 134 | +`Task(T)` starts a thread without a cast: `import Jobs := Task(Job); | |
| 135 | +t, e := Jobs.spawn(work, job);` is the safe form of | |
| 136 | +`Thread.spawn(work, ^U8(ADR(job)))`. `spawn` copies `x` into a block | |
| 137 | +from `Heap` before it returns, so the caller may change or drop its `x` | |
| 138 | +at once. The thread owns the copy: `work` changes it freely, and the | |
| 139 | +thread frees it when `work` returns. The thread is joined with | |
| 140 | +`Thread.join`; `Thread.Failed` comes back if there is no memory for the | |
| 141 | +copy or the thread. A result goes back through a pointer in `x`, read | |
| 142 | +after `join`. | |
| 143 | + | |
| 144 | +`Guarded(T)` is a `T` with its `Thread.Mutex`, for sharing a container | |
| 145 | +between threads: the value is reached only through `with`, which holds | |
| 146 | +the lock while `p` runs. A zeroed `Guarded` is ready. `p` must not call | |
| 147 | +`with` on the same `Guarded`, nor keep a pointer into its `x`. | |
| 124 | 148 | |
| 125 | 149 | A `Lock` is for short sections: a thread that finds it taken tries |
| 126 | 150 | again, giving up the processor now and then. A `Mutex` sleeps. Each of |
| @@ -329,9 +329,9 @@ | ||
| 329 | 329 | **Names are flat.** The names of a program's modules are one space, |
| 330 | 330 | with no packages or paths. So the names of the library's modules |
| 331 | 331 | (the library, `doc/raz-lib.md`) are kept for it: a program or another library doesn't |
| 332 | -name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Heap`, | |
| 333 | -`List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`, `Sort`, | |
| 334 | -`Str`, `Sys` or `Thread`, nor any name the library takes later. The | |
| 332 | +name a module of its own `Arena`, `Buf`, `C`, `Files`, `Fmt`, `Guarded`, | |
| 333 | +`Heap`, `List`, `Lock`, `Map`, `Math`, `Mem`, `Opt`, `Out`, `Rt`, | |
| 334 | +`Sort`, `Str`, `Sys`, `Task` or `Thread`, nor any name the library takes later. The | |
| 335 | 335 | modules of any other library share a prefix of their own, as an X11 |
| 336 | 336 | library's `Xauth` and `Xkeys` do, and a program that wants a shorter |
| 337 | 337 | name imports one: `import Auth := Xauth;`. |
| @@ -0,0 +1,33 @@ | ||
| 1 | +(* A value of type T together with the lock that guards it, so that | |
| 2 | + sharing a container between threads is hard to do wrong: the value | |
| 3 | + can be reached only through with, which holds the lock while it | |
| 4 | + runs. | |
| 5 | + | |
| 6 | + A Guarded is ready as it is, with a zero value and the lock free; | |
| 7 | + with can set any other. The lock is a Thread.Mutex, which sleeps | |
| 8 | + while it waits, so p may take its time. p must not call with on the | |
| 9 | + same Guarded, which would wait for itself, and must not keep a | |
| 10 | + pointer into x past its return. | |
| 11 | + | |
| 12 | + It depends on Thread. It leaves out reading the value without the | |
| 13 | + lock, a try-lock and a lock shared by several values: a program | |
| 14 | + that wants those uses Thread.Mutex itself. *) | |
| 15 | +module Guarded(T); | |
| 16 | +import Thread; | |
| 17 | + | |
| 18 | +type | |
| 19 | + Guarded* = record | |
| 20 | + lock: Thread.Mutex; (* held while the value is used *) | |
| 21 | + value: T; (* what the lock guards *) | |
| 22 | + end; | |
| 23 | + | |
| 24 | +(* Runs p on the value, holding the lock, so that no other thread uses | |
| 25 | + it at the same time. *) | |
| 26 | +proc with*(var g: Guarded; p: proc(var x: T)); | |
| 27 | +begin | |
| 28 | + Thread.lock(g.lock); | |
| 29 | + p(g.value); | |
| 30 | + Thread.unlock(g.lock); | |
| 31 | +end; | |
| 32 | + | |
| 33 | +end Guarded. |
| @@ -0,0 +1,53 @@ | ||
| 1 | +(* A thread that is given a value of type T, with no cast: the safe form | |
| 2 | + of Thread.spawn, which takes a pointer to bytes. | |
| 3 | + | |
| 4 | + spawn copies x into a block from the heap before it returns, so the | |
| 5 | + caller may change x, or let it go out of scope, at once. The thread | |
| 6 | + runs p on that copy and frees it when p returns, so the thread owns | |
| 7 | + its copy from first to last and nobody else has to know when it is | |
| 8 | + done with it. p can change its copy; the caller never sees it. What | |
| 9 | + p and x share with others, a pointer in x for one, is the program's | |
| 10 | + to guard: Guarded is for that. | |
| 11 | + | |
| 12 | + It depends on Thread and Heap. It leaves out a way to give p a | |
| 13 | + result: a thread that returns something writes it through a pointer | |
| 14 | + in x, and the caller reads it after Thread.join. *) | |
| 15 | +module Task(T); | |
| 16 | +import Heap, Mem, Thread; | |
| 17 | + | |
| 18 | +type | |
| 19 | + (* What the thread is given: what to run and its copy of x. *) | |
| 20 | + Job = record | |
| 21 | + p: proc(var x: T); | |
| 22 | + x: T; | |
| 23 | + end; | |
| 24 | + | |
| 25 | +(* Where the new thread starts: runs the job's procedure on its copy, | |
| 26 | + then frees the copy. *) | |
| 27 | +proc run(arg: ^U8); | |
| 28 | +var j: ^Job; | |
| 29 | +begin | |
| 30 | + j := ^Job(arg); | |
| 31 | + j^.p(j^.x); | |
| 32 | + Mem.free(Heap.heap, arg); | |
| 33 | +end; | |
| 34 | + | |
| 35 | +(* Starts p(x) in a new thread, on a copy of x; join the thread as | |
| 36 | + Thread.spawn's. Thread.Failed if there is no memory for the copy | |
| 37 | + or for the thread. *) | |
| 38 | +proc spawn*(p: proc(var x: T); x: T): (^Thread.Thread, ERROR); | |
| 39 | +var j: ^Job; t: ^Thread.Thread; err: ERROR; | |
| 40 | +begin | |
| 41 | + NEW(j, Heap.heap); | |
| 42 | + if j = nil then return nil, Thread.Failed; end; | |
| 43 | + j^.p := p; | |
| 44 | + j^.x := x; | |
| 45 | + t, err := Thread.spawn(run, ^U8(j)); | |
| 46 | + if err # nil then | |
| 47 | + Mem.free(Heap.heap, ^U8(j)); | |
| 48 | + return nil, err; | |
| 49 | + end; | |
| 50 | + return t, nil; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +end Task. |
An empty file.
| @@ -0,0 +1,40 @@ | ||
| 1 | +(* Guarded: several threads each add to one counter many times; the total | |
| 2 | + is exact. *) | |
| 3 | +module GuardedTest; | |
| 4 | +import Check, Counter := Guarded(INT), Rt, Thread; | |
| 5 | + | |
| 6 | +const | |
| 7 | + Threads = 8; | |
| 8 | + Times = 20000; | |
| 9 | + | |
| 10 | +var count: Counter.Guarded; | |
| 11 | + | |
| 12 | +proc add(var x: INT); | |
| 13 | +begin | |
| 14 | + INC(x); | |
| 15 | +end; | |
| 16 | + | |
| 17 | +proc check(var x: INT); | |
| 18 | +begin | |
| 19 | + Check.int("Guarded: total", x, Threads * Times); | |
| 20 | +end; | |
| 21 | + | |
| 22 | +(* One thread's share of the work. *) | |
| 23 | +proc work(_arg: ^U8); | |
| 24 | +var i: INT; | |
| 25 | +begin | |
| 26 | + for i := 1 to Times do Counter.with(count, add); end; | |
| 27 | +end; | |
| 28 | + | |
| 29 | +proc main*; | |
| 30 | +var t: [Threads]^Thread.Thread; i: INT; err: ERROR; | |
| 31 | +begin | |
| 32 | + for i := 0 to Threads - 1 do | |
| 33 | + t[i], err := Thread.spawn(work, nil); | |
| 34 | + if err # nil then Rt.stop("Thread.spawn failed"); end; | |
| 35 | + end; | |
| 36 | + for i := 0 to Threads - 1 do Thread.join(t[i]); end; | |
| 37 | + Counter.with(count, check); | |
| 38 | +end; | |
| 39 | + | |
| 40 | +end GuardedTest. |
An empty file.
| @@ -0,0 +1,36 @@ | ||
| 1 | +(* Task: each of several threads is given its own job; the caller changes | |
| 2 | + its job at once after spawn, and the threads' copies are not changed. *) | |
| 3 | +module TaskTest; | |
| 4 | +import Check, Jobs := Task(Job), Rt, Thread; | |
| 5 | + | |
| 6 | +const Threads = 8; | |
| 7 | + | |
| 8 | +(* A thread's job: a number, and where to put its square. *) | |
| 9 | +type Job = record n: INT; out: ^INT; end; | |
| 10 | + | |
| 11 | +var results: [Threads]INT; | |
| 12 | + | |
| 13 | +(* Puts n squared where the job says, then changes its own copy. *) | |
| 14 | +proc work(var j: Job); | |
| 15 | +begin | |
| 16 | + j.out^ := j.n * j.n; | |
| 17 | + j.n := 0; | |
| 18 | +end; | |
| 19 | + | |
| 20 | +proc main*; | |
| 21 | +var t: [Threads]^Thread.Thread; j: Job; i: INT; err: ERROR; | |
| 22 | +begin | |
| 23 | + for i := 0 to Threads - 1 do | |
| 24 | + j := {n: i + 1, out: ADR(results[i])}; | |
| 25 | + t[i], err := Jobs.spawn(work, j); | |
| 26 | + if err # nil then Rt.stop("Task.spawn failed"); end; | |
| 27 | + j.n := 1000; (* the thread must not see this *) | |
| 28 | + j.out := ADR(results[0]); | |
| 29 | + end; | |
| 30 | + for i := 0 to Threads - 1 do Thread.join(t[i]); end; | |
| 31 | + for i := 0 to Threads - 1 do | |
| 32 | + Check.int("Task: result", results[i], (i + 1) * (i + 1)); | |
| 33 | + end; | |
| 34 | +end; | |
| 35 | + | |
| 36 | +end TaskTest. |