Step 13: a thread-safe heap with block markers, native threads Heap takes a lock, refuses bad sizes and alignments, and marks each block it gives out, so that freeing one twice, or one it never gave out, stops the program through Rt.stop. Big blocks now honour their alignment. Out locks its buffer. Lock is a spinlock that yields. Thread, for programs without C (link static), spawns threads with clone on mmaped stacks with a guard page, joins them through the kernel's cleared tid, and has a futex Mutex. Examples with pthread_create and with Thread.
| @@ -38,8 +38,8 @@ | ||
| 38 | 38 | ## Where things stand |
| 39 | 39 | |
| 40 | 40 | Stages 1 and 2 are done (steps 1-10). Stage 3 (steps 11-14) is under |
| 41 | -way: its report changes are written and approved, and steps 11 and 12 | |
| 42 | -are done. Next is step 13, threads and a thread-safe heap. | |
| 41 | +way: its report changes are written and approved, and steps 11 to 13 | |
| 42 | +are done. Next is step 14, line numbers for gdb. | |
| 43 | 43 | |
| 44 | 44 | `test/libc` runs its programs over `lib/Sys.jed`, the C one, through |
| 45 | 45 | a link in its `mods`. A test or example without `.expect` is only |
| @@ -80,6 +80,8 @@ | ||
| 80 | 80 | - A real constant is held as an F64; one typed `F32` holds a value an |
| 81 | 81 | F32 can hold. An `F32` in a register is its low 32 bits; the upper |
| 82 | 82 | ones are left as they fall. |
| 83 | +- `Heap` and `Out` lock with `Lock`, a spinlock that yields, since they | |
| 84 | + serve C's threads and `Thread`'s alike; `Thread.Mutex` is a futex. | |
| 83 | 85 | - Every failed check says "index out of range"; some errors point at |
| 84 | 86 | the symbol after the offending expression. Error messages are to be |
| 85 | 87 | improved later, all together. |
| @@ -114,7 +114,7 @@ | ||
| 114 | 114 | with C's `stdio`: flush one before using the other. |
| 115 | 115 | - [x] 12. `F32`: arithmetic, conversions, calls; real literals become |
| 116 | 116 | untyped constants that take `F32` or `F64`. |
| 117 | -- [ ] 13. A thread-safe `lib/Heap.jed`: its bad sizes refused, and a | |
| 117 | +- [x] 13. A thread-safe `lib/Heap.jed`: its bad sizes refused, and a | |
| 118 | 118 | marker in each block's header so that freeing a block twice, or |
| 119 | 119 | one it never gave out, stops the program; `Out` locks its buffer. |
| 120 | 120 | An example with threads from `pthread_create`. |
| @@ -811,12 +811,14 @@ | ||
| 811 | 811 | |
| 812 | 812 | | Module | What | |
| 813 | 813 | |---|---| |
| 814 | -| `Rt` | ending the program, `exit` and `atExit`; what the compiler's checks call when they fail; string comparison | | |
| 815 | -| `Sys` | system calls: `write`, `mmap`, `munmap`, `exit`; one file per target, and `Sys.jed` over the C library for the others | | |
| 816 | -| `Out` | writing text and numbers to standard output, buffered | | |
| 817 | -| `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`; a program that imports it uses C | | |
| 814 | +| `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; what the compiler's checks call when they fail; string comparison | | |
| 815 | +| `Sys` | system calls: `write`, `mmap`, `munmap`, `yield`, `exit`; one file per target, and `Sys.jed` over the C library for the others | | |
| 816 | +| `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | |
| 817 | +| `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | |
| 818 | +| `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | | |
| 819 | +| `Thread` | threads for a program without C: `spawn`, `join`, and `Mutex`; it says `link static` | | |
| 818 | 820 | | `Mem` | `Allocator`, the interface of every allocator, and pages from the system | |
| 819 | -| `Heap` | a general allocator, `Heap.heap` | | |
| 821 | +| `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 | | |
| 820 | 822 | | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | |
| 821 | 823 | | `Str` | operations on `[]U8` strings | |
| 822 | 824 | | `Buf` | a growable byte buffer | |
| @@ -0,0 +1,5 @@ | ||
| 1 | +a part summed | |
| 2 | +a part summed | |
| 3 | +a part summed | |
| 4 | +a part summed | |
| 5 | +total 500000500000 |
| @@ -0,0 +1,52 @@ | ||
| 1 | +(* Threads from C's pthread_create, each summing its part of an array | |
| 2 | + and using the heap and Out, which lock themselves. *) | |
| 3 | +module Pthreads; | |
| 4 | +import Out, C, Heap, Mem; | |
| 5 | + | |
| 6 | +const | |
| 7 | + Workers = 4; | |
| 8 | + N = 1000000; | |
| 9 | + | |
| 10 | +type | |
| 11 | + Part = record lo, hi, sum: I64; end; | |
| 12 | + Numbers = [N]I64; | |
| 13 | + | |
| 14 | +var numbers: ^Numbers; | |
| 15 | + | |
| 16 | +proc work(arg: ^U8): ^U8; | |
| 17 | +var p: ^Part; i: I64; scratch: ^U8; | |
| 18 | +begin | |
| 19 | + p := ^Part(arg); | |
| 20 | + for i := p^.lo to p^.hi - 1 do | |
| 21 | + p^.sum := p^.sum + numbers^[i]; | |
| 22 | + if i mod 1000 = 0 then | |
| 23 | + scratch := Mem.alloc(Heap.heap, 100, 8); | |
| 24 | + Mem.free(Heap.heap, scratch); | |
| 25 | + end; | |
| 26 | + end; | |
| 27 | + Out.str("a part summed\n"); | |
| 28 | + return nil; | |
| 29 | +end; | |
| 30 | + | |
| 31 | +proc main*: I64; | |
| 32 | +var t: [Workers]U64; parts: [Workers]Part; i, total: I64; | |
| 33 | +begin | |
| 34 | + NEW(numbers, Heap.heap); | |
| 35 | + for i := 0 to N - 1 do numbers^[i] := i + 1; end; | |
| 36 | + for i := 0 to Workers - 1 do | |
| 37 | + parts[i] := {lo: i * N div Workers, hi: (i + 1) * N div Workers}; | |
| 38 | + if C.pthread_create(ADR(t[i]), nil, work, ^U8(ADR(parts[i]))) # 0 then | |
| 39 | + Out.str("can't start a thread\n"); | |
| 40 | + return 1; | |
| 41 | + end; | |
| 42 | + end; | |
| 43 | + total := 0; | |
| 44 | + for i := 0 to Workers - 1 do | |
| 45 | + C.pthread_join(t[i], nil); | |
| 46 | + total := total + parts[i].sum; | |
| 47 | + end; | |
| 48 | + Out.str("total "); Out.int(total); Out.ln; | |
| 49 | + return 0; | |
| 50 | +end; | |
| 51 | + | |
| 52 | +end Pthreads. |
| @@ -0,0 +1 @@ | ||
| 1 | +total 8167664175825000 |
| @@ -0,0 +1,48 @@ | ||
| 1 | +(* Native threads, for a program without C: workers that take jobs | |
| 2 | + from a shared counter and add up their results under a Mutex. *) | |
| 3 | +module Workers; | |
| 4 | +import Out, Thread; | |
| 5 | + | |
| 6 | +const | |
| 7 | + Workers = 4; | |
| 8 | + Jobs = 100; | |
| 9 | + | |
| 10 | +var | |
| 11 | + next: I64; (* the next job, taken with XADD *) | |
| 12 | + m: Thread.Mutex; | |
| 13 | + total: I64; | |
| 14 | + | |
| 15 | +(* job n: the sum of the squares up to n *) | |
| 16 | +proc job(n: I64): I64; | |
| 17 | +var i, s: I64; | |
| 18 | +begin | |
| 19 | + s := 0; | |
| 20 | + for i := 1 to n do s := s + i * i; end; | |
| 21 | + return s; | |
| 22 | +end; | |
| 23 | + | |
| 24 | +proc work(arg: ^U8); | |
| 25 | +var n, r: I64; | |
| 26 | +begin | |
| 27 | + n := XADD(ADR(next), 1); | |
| 28 | + while n < Jobs do | |
| 29 | + r := job(n * 1000); | |
| 30 | + Thread.lock(ADR(m)); | |
| 31 | + total := total + r; | |
| 32 | + Thread.unlock(ADR(m)); | |
| 33 | + n := XADD(ADR(next), 1); | |
| 34 | + end; | |
| 35 | +end; | |
| 36 | + | |
| 37 | +proc main*; | |
| 38 | +var t: [Workers]^Thread.Thread; i: I64; err: ERROR; | |
| 39 | +begin | |
| 40 | + for i := 0 to Workers - 1 do | |
| 41 | + t[i], err := Thread.spawn(work, nil); | |
| 42 | + if err # nil then Out.str(MSG(err)); Out.ln; return; end; | |
| 43 | + end; | |
| 44 | + for i := 0 to Workers - 1 do Thread.join(t[i]); end; | |
| 45 | + Out.str("total "); Out.int(total); Out.ln; | |
| 46 | +end; | |
| 47 | + | |
| 48 | +end Workers. |
| @@ -10,6 +10,9 @@ | ||
| 10 | 10 | extern proc exit*(status: I32); |
| 11 | 11 | extern proc _exit*(status: I32); |
| 12 | 12 | extern proc __errno_location*: ^I32; (* errno is this thread's: through here *) |
| 13 | +extern proc sched_yield*: I32; | |
| 14 | +extern proc pthread_create*(t: ^U64; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; | |
| 15 | +extern proc pthread_join*(t: U64; result: ^^U8): I32; | |
| 13 | 16 | extern proc printf*(format: ^U8; ...): I32; |
| 14 | 17 | extern proc fflush*(f: ^U8): I32; |
| 15 | 18 | extern var stdout*: ^U8; (* FILE * *) |
| @@ -1,41 +1,65 @@ | ||
| 1 | 1 | (* A general allocator: Heap.heap. Small blocks come in nine sizes, |
| 2 | 2 | 16 to 4096 bytes with their header, each with a list of the free |
| 3 | 3 | ones; a block bigger than that gets pages of its own. Every block |
| 4 | - has a 16-byte header before it: its size class, or minus its size | |
| 5 | - in bytes for a big one. *) | |
| 4 | + has a 16-byte header before it: its size class, or minus the size | |
| 5 | + of its pages for a big one, which start less than a page before it; | |
| 6 | + then a marker, which says the block is out, | |
| 7 | + so that freeing it twice, or freeing what the heap never gave out, | |
| 8 | + stops the program. One lock keeps it whole across threads. *) | |
| 6 | 9 | module Heap; |
| 7 | -import Mem; | |
| 10 | +import Mem, Lock, Rt; | |
| 8 | 11 | |
| 9 | 12 | const |
| 10 | 13 | Classes = 9; |
| 11 | 14 | Header = 16; |
| 12 | 15 | Refill = 65536; |
| 16 | + Largest = 1 shl 46; (* bigger sizes are refused *) | |
| 17 | + Out = 5A6564426C6F636BH; (* the marker, xor the header's address *) | |
| 13 | 18 | |
| 14 | 19 | type |
| 15 | 20 | Block = record next: ^Block; end; |
| 21 | + Head = record size, mark: I64; end; | |
| 16 | 22 | |
| 17 | 23 | var |
| 18 | 24 | lists: [Classes]^Block; |
| 19 | 25 | cur: ^U8; (* where the next new small block comes from *) |
| 20 | 26 | left: I64; |
| 27 | + lock: Lock.Lock; | |
| 21 | 28 | std: Mem.Allocator; |
| 22 | 29 | heap*: ^Mem.Allocator; |
| 23 | 30 | |
| 31 | +(* The block p, with size for its header, given out. *) | |
| 32 | +proc give(p: ^U8; size: I64): ^U8; | |
| 33 | +var h: ^Head; | |
| 34 | +begin | |
| 35 | + h := ^Head(p); | |
| 36 | + h^.size := size; | |
| 37 | + h^.mark := Out xor I64(h); | |
| 38 | + return p + Header; | |
| 39 | +end; | |
| 40 | + | |
| 41 | +(* size bytes aligned to align, or nil: also for a size below zero or | |
| 42 | + too big, and an alignment that isn't a power of two up to a page. *) | |
| 24 | 43 | proc alloc(a: ^Mem.Allocator; size, align: I64): ^U8; |
| 25 | -var c, n: I64; b: ^Block; p: ^U8; h: ^I64; | |
| 44 | +var c, n: I64; b: ^Block; p: ^U8; | |
| 26 | 45 | begin |
| 46 | + if (size < 0) or (size > Largest) or (align < 1) or (align > Mem.Page) | |
| 47 | + or (align and (align - 1) # 0) then | |
| 48 | + return nil; | |
| 49 | + end; | |
| 27 | 50 | n := size + Header; |
| 28 | - if (size < 0) or (align > Header) and (align > Mem.Page) then return nil; end; | |
| 29 | 51 | c := 0; |
| 30 | 52 | while (c < Classes) and (16 shl c < n) do INC(c); end; |
| 31 | 53 | if (c = Classes) or (align > Header) then |
| 32 | - n := (n + Mem.Page - 1) div Mem.Page * Mem.Page; | |
| 54 | + (* pages of its own, the block at align from their start *) | |
| 55 | + if align < Header then align := Header; end; | |
| 56 | + n := (size + align + Mem.Page - 1) div Mem.Page * Mem.Page; | |
| 33 | 57 | p := Mem.pages(n); |
| 34 | 58 | if p = nil then return nil; end; |
| 35 | - h := ^I64(p); | |
| 36 | - h^ := -n; | |
| 37 | - return p + Header; | |
| 59 | + return give(p + align - Header, -n); | |
| 38 | 60 | end; |
| 61 | + Lock.take(ADR(lock)); | |
| 62 | + defer Lock.give(ADR(lock)); | |
| 39 | 63 | b := lists[c]; |
| 40 | 64 | if b # nil then |
| 41 | 65 | lists[c] := b^.next; |
| @@ -50,22 +74,27 @@ | ||
| 50 | 74 | cur := cur + (16 shl c); |
| 51 | 75 | left := left - (16 shl c); |
| 52 | 76 | end; |
| 53 | - h := ^I64(p); | |
| 54 | - h^ := c; | |
| 55 | - return p + Header; | |
| 77 | + return give(p, c); | |
| 56 | 78 | end; |
| 57 | 79 | |
| 58 | 80 | proc free(a: ^Mem.Allocator; p: ^U8); |
| 59 | -var h: ^I64; b: ^Block; | |
| 81 | +var h: ^Head; b: ^Block; | |
| 60 | 82 | begin |
| 61 | 83 | if p = nil then return; end; |
| 62 | - h := ^I64(p - Header); | |
| 63 | - if h^ < 0 then | |
| 64 | - Mem.unmap(^U8(h), -h^); | |
| 84 | + h := ^Head(p - Header); | |
| 85 | + Lock.take(ADR(lock)); | |
| 86 | + if h^.mark # Out xor I64(h) then | |
| 87 | + Rt.stop("Heap: a block freed twice, or never given out"); | |
| 88 | + end; | |
| 89 | + h^.mark := 0; | |
| 90 | + if h^.size < 0 then | |
| 91 | + Lock.give(ADR(lock)); | |
| 92 | + Mem.unmap(^U8(I64(h) - I64(h) mod Mem.Page), -h^.size); | |
| 65 | 93 | else |
| 66 | 94 | b := ^Block(h); |
| 67 | - b^.next := lists[h^]; | |
| 68 | - lists[h^] := b; | |
| 95 | + b^.next := lists[h^.size]; | |
| 96 | + lists[h^.size] := b; | |
| 97 | + Lock.give(ADR(lock)); | |
| 69 | 98 | end; |
| 70 | 99 | end; |
| 71 | 100 |
| @@ -0,0 +1,25 @@ | ||
| 1 | +(* A lock for short sections of code: a word set to 1, atomically, by | |
| 2 | + the thread that takes it. A thread that finds it taken tries again, | |
| 3 | + giving up the processor now and then. It works with C's threads and | |
| 4 | + with Thread's alike; Thread.Mutex is the one that sleeps. *) | |
| 5 | +module Lock; | |
| 6 | +import Sys; | |
| 7 | + | |
| 8 | +type Lock* = record held: I64; end; | |
| 9 | + | |
| 10 | +proc take*(l: ^Lock); | |
| 11 | +var tries: I64; | |
| 12 | +begin | |
| 13 | + tries := 0; | |
| 14 | + while not CAS(ADR(l^.held), 0, 1) do | |
| 15 | + INC(tries); | |
| 16 | + if tries = 64 then Sys.yield; tries := 0; end; | |
| 17 | + end; | |
| 18 | +end; | |
| 19 | + | |
| 20 | +proc give*(l: ^Lock); | |
| 21 | +begin | |
| 22 | + XCHG(ADR(l^.held), 0); | |
| 23 | +end; | |
| 24 | + | |
| 25 | +end Lock. |
| @@ -2,17 +2,20 @@ | ||
| 2 | 2 | 4 KB. The buffer is written out when it is full, at each line feed |
| 3 | 3 | when standard output is a terminal, by flush, and when the program |
| 4 | 4 | ends by Rt.exit. C's stdio keeps a buffer of its own: a program that |
| 5 | - writes with both flushes one before writing with the other. *) | |
| 5 | + writes with both flushes one before writing with the other. A lock | |
| 6 | + keeps what each call writes together when threads write at once. *) | |
| 6 | 7 | module Out; |
| 7 | -import Rt, Sys; | |
| 8 | +import Rt, Sys, Lock; | |
| 8 | 9 | |
| 9 | 10 | var |
| 10 | 11 | buf: [4096]U8; |
| 11 | 12 | n: I64; (* bytes in buf *) |
| 12 | 13 | tty: BOOL; (* standard output is a terminal *) |
| 14 | + lock: Lock.Lock; | |
| 13 | 15 | |
| 14 | -(* Writes out the buffer. What can't be written is dropped. *) | |
| 15 | -proc flush*; | |
| 16 | +(* Writes out the buffer, with the lock held. What can't be written is | |
| 17 | + dropped. *) | |
| 18 | +proc out; | |
| 16 | 19 | var i, k: I64; |
| 17 | 20 | begin |
| 18 | 21 | i := 0; |
| @@ -25,18 +28,31 @@ | ||
| 25 | 28 | n := 0; |
| 26 | 29 | end; |
| 27 | 30 | |
| 28 | -proc char*(c: U8); | |
| 31 | +proc flush*; | |
| 29 | 32 | begin |
| 30 | - if n = LEN(buf) then flush; end; | |
| 31 | - buf[n] := c; | |
| 32 | - INC(n); | |
| 33 | - if (c = '\n') and tty then flush; end; | |
| 33 | + Lock.take(ADR(lock)); | |
| 34 | + out; | |
| 35 | + Lock.give(ADR(lock)); | |
| 34 | 36 | end; |
| 35 | 37 | |
| 36 | 38 | proc str*(s: []U8); |
| 37 | 39 | var i: I64; |
| 38 | 40 | begin |
| 39 | - for i := 0 to LEN(s) - 1 do char(s[i]); end; | |
| 41 | + Lock.take(ADR(lock)); | |
| 42 | + for i := 0 to LEN(s) - 1 do | |
| 43 | + if n = LEN(buf) then out; end; | |
| 44 | + buf[n] := s[i]; | |
| 45 | + INC(n); | |
| 46 | + if (s[i] = '\n') and tty then out; end; | |
| 47 | + end; | |
| 48 | + Lock.give(ADR(lock)); | |
| 49 | +end; | |
| 50 | + | |
| 51 | +proc char*(c: U8); | |
| 52 | +var b: [1]U8; | |
| 53 | +begin | |
| 54 | + b[0] := c; | |
| 55 | + str(b); | |
| 40 | 56 | end; |
| 41 | 57 | |
| 42 | 58 | proc ln*; |
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | (* The run time: ending the program, and what the compiler's code |
| 2 | - calls: fail when a check fails, eq when = or # compares two strings. | |
| 2 | + calls: fail when a check fails, eq when = or # compares two strings; | |
| 3 | + and stop, for the library's own failures. | |
| 3 | 4 | The compiler finds fail, eq, exit and cExit by these names. *) |
| 4 | 5 | module Rt; |
| 5 | 6 | import Sys; |
| @@ -37,6 +38,15 @@ | ||
| 37 | 38 | end; |
| 38 | 39 | if cExit # nil then cExit(I32(status)); end; |
| 39 | 40 | Sys.exit(status); |
| 41 | +end; | |
| 42 | + | |
| 43 | +(* Stops the program: says msg on standard error, then ends it by exit | |
| 44 | + with status 2, as a failed check does. *) | |
| 45 | +proc stop*(msg: []U8); | |
| 46 | +begin | |
| 47 | + Sys.write(Sys.stderr, msg); | |
| 48 | + Sys.write(Sys.stderr, "\n"); | |
| 49 | + exit(2); | |
| 40 | 50 | end; |
| 41 | 51 | |
| 42 | 52 | (* A check failed at line of file: says so on standard error and ends |
| @@ -48,6 +48,11 @@ | ||
| 48 | 48 | return C.isatty(I32(fd)) = 1; |
| 49 | 49 | end; |
| 50 | 50 | |
| 51 | +proc yield*; | |
| 52 | +begin | |
| 53 | + C.sched_yield(); | |
| 54 | +end; | |
| 55 | + | |
| 51 | 56 | (* Ends the program, at once, with status. *) |
| 52 | 57 | proc exit*(status: I64); |
| 53 | 58 | begin |
| @@ -38,6 +38,12 @@ | ||
| 38 | 38 | return SYSCALL(16, fd, 5401H, ADR(t)) = 0; |
| 39 | 39 | end; |
| 40 | 40 | |
| 41 | +(* Gives the processor to another thread. *) | |
| 42 | +proc yield*; | |
| 43 | +begin | |
| 44 | + SYSCALL(24); | |
| 45 | +end; | |
| 46 | + | |
| 41 | 47 | (* Ends the program, at once, with status. *) |
| 42 | 48 | proc exit*(status: I64); |
| 43 | 49 | begin |
| @@ -0,0 +1,113 @@ | ||
| 1 | +(* Threads for programs without C, made with the clone system call. | |
| 2 | + Each runs on a stack of its own, mmaped, with a page below it that | |
| 3 | + can't be touched, so that running off the stack stops the program | |
| 4 | + rather than spoiling what lies below. join waits on the thread's id, | |
| 5 | + which the kernel clears, and wakes it for, when the thread ends; | |
| 6 | + a Mutex sleeps on a futex. A program with C threads uses C's, | |
| 7 | + through pthread_create, and so can't use these. *) | |
| 8 | +module Thread; | |
| 9 | +import Sys; | |
| 10 | +link static; | |
| 11 | + | |
| 12 | +const | |
| 13 | + Failed*: ERROR = "can't start a thread"; | |
| 14 | + Stack = 1 shl 20; | |
| 15 | + Page = 4096; | |
| 16 | + (* clone: share memory, files, signal handlers, the thread group; | |
| 17 | + write the id to tid, and clear it at the end *) | |
| 18 | + Flags = 350F00H; | |
| 19 | + FutexWait = 0; | |
| 20 | + FutexWake = 1; | |
| 21 | + Private = 128; | |
| 22 | + | |
| 23 | +type | |
| 24 | + Thread* = record | |
| 25 | + tid: I64; (* the kernel's id, until the thread ends *) | |
| 26 | + base: ^U8; (* its stack's mapping, the guard page first *) | |
| 27 | + size: I64; | |
| 28 | + end; | |
| 29 | + (* 0 free, 1 taken, 2 taken with a thread waiting *) | |
| 30 | + Mutex* = record state: I64; end; | |
| 31 | + | |
| 32 | +(* clone(flags, stack, tid, tid), the new thread starting with fn(arg) | |
| 33 | + on stack and ending, when fn returns, with the exit system call. | |
| 34 | + fn and arg go on the new stack first, since the new thread has no | |
| 35 | + registers but rsp of its own to find them by. Returns the new | |
| 36 | + thread's id, or minus an errno. *) | |
| 37 | +proc start(flags: I64; stack: ^U8; ptid, ctid: ^I64; fn: proc(arg: ^U8); arg: ^U8): I64; | |
| 38 | +code 48H, 83H, 0EEH, 10H, (* sub rsi, 16 *) | |
| 39 | + 4CH, 89H, 06H, (* mov [rsi], r8 *) | |
| 40 | + 4CH, 89H, 4EH, 08H, (* mov [rsi + 8], r9 *) | |
| 41 | + 49H, 89H, 0CAH, (* mov r10, rcx *) | |
| 42 | + 45H, 31H, 0C0H, (* xor r8d, r8d: no tls *) | |
| 43 | + 0B8H, 38H, 0, 0, 0, (* mov eax, 56: clone *) | |
| 44 | + 0FH, 05H, (* syscall *) | |
| 45 | + 48H, 85H, 0C0H, (* test rax, rax *) | |
| 46 | + 75H, 0DH, (* jnz parent *) | |
| 47 | + 58H, (* pop rax: fn *) | |
| 48 | + 5FH, (* pop rdi: arg *) | |
| 49 | + 0FFH, 0D0H, (* call rax *) | |
| 50 | + 31H, 0FFH, (* xor edi, edi *) | |
| 51 | + 0B8H, 3CH, 0, 0, 0, (* mov eax, 60: exit, this thread only *) | |
| 52 | + 0FH, 05H, (* syscall *) | |
| 53 | + 0C3H; (* parent: ret *) | |
| 54 | + | |
| 55 | +proc futex(word: ^I64; op, val: I64): I64; | |
| 56 | +begin | |
| 57 | + return SYSCALL(202, word, op, val, 0); | |
| 58 | +end; | |
| 59 | + | |
| 60 | +(* Starts p(arg) in a new thread. *) | |
| 61 | +proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); | |
| 62 | +var base: ^U8; t: ^Thread; size: I64; | |
| 63 | +begin | |
| 64 | + size := Stack + Page; | |
| 65 | + base := Sys.mmap(size); | |
| 66 | + if base = nil then return nil, Failed; end; | |
| 67 | + SYSCALL(10, base, Page, 0); (* mprotect: the guard page, no access *) | |
| 68 | + (* the Thread at the top, the stack below it, aligned to 16 *) | |
| 69 | + t := ^Thread(base + size - 32); | |
| 70 | + t^.base := base; | |
| 71 | + t^.size := size; | |
| 72 | + if start(Flags, base + size - 32, ADR(t^.tid), ADR(t^.tid), p, arg) < 0 then | |
| 73 | + Sys.munmap(base, size); | |
| 74 | + return nil, Failed; | |
| 75 | + end; | |
| 76 | + return t, nil; | |
| 77 | +end; | |
| 78 | + | |
| 79 | +(* Waits for t to end, then frees its stack. *) | |
| 80 | +proc join*(t: ^Thread); | |
| 81 | +var tid: ^VOLATILE I64; v: I64; base: ^U8; size: I64; | |
| 82 | +begin | |
| 83 | + tid := ADR(t^.tid); | |
| 84 | + v := tid^; | |
| 85 | + while v # 0 do | |
| 86 | + futex(ADR(t^.tid), FutexWait, v); | |
| 87 | + v := tid^; | |
| 88 | + end; | |
| 89 | + base := t^.base; | |
| 90 | + size := t^.size; | |
| 91 | + Sys.munmap(base, size); | |
| 92 | +end; | |
| 93 | + | |
| 94 | +proc lock*(m: ^Mutex); | |
| 95 | +var c: I64; | |
| 96 | +begin | |
| 97 | + if CAS(ADR(m^.state), 0, 1) then return; end; | |
| 98 | + c := XCHG(ADR(m^.state), 2); | |
| 99 | + while c # 0 do | |
| 100 | + futex(ADR(m^.state), FutexWait + Private, 2); | |
| 101 | + c := XCHG(ADR(m^.state), 2); | |
| 102 | + end; | |
| 103 | +end; | |
| 104 | + | |
| 105 | +proc unlock*(m: ^Mutex); | |
| 106 | +begin | |
| 107 | + if XADD(ADR(m^.state), -1) # 1 then | |
| 108 | + XCHG(ADR(m^.state), 0); | |
| 109 | + futex(ADR(m^.state), FutexWake + Private, 1); | |
| 110 | + end; | |
| 111 | +end; | |
| 112 | + | |
| 113 | +end Thread. |
| @@ -0,0 +1 @@ | ||
| 1 | +Thread says link static, but |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module ThreadC; | |
| 2 | +import Thread, C; | |
| 3 | + | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | +end; | |
| 7 | + | |
| 8 | +end ThreadC. |
| @@ -0,0 +1,10 @@ | ||
| 1 | +nil | |
| 2 | +nil | |
| 3 | +nil | |
| 4 | +nil | |
| 5 | +nil | |
| 6 | +nil | |
| 7 | +a block | |
| 8 | +aligned | |
| 9 | +a block | |
| 10 | +freeing it again |
| @@ -0,0 +1,34 @@ | ||
| 1 | +(* The heap refuses bad sizes and alignments, and stops the program | |
| 2 | + when a block is freed twice. *) | |
| 3 | +module HeapCheck; | |
| 4 | +import Out, Heap, Mem; | |
| 5 | + | |
| 6 | +proc show(p: ^U8); | |
| 7 | +begin | |
| 8 | + if p = nil then Out.str("nil\n"); else Out.str("a block\n"); end; | |
| 9 | +end; | |
| 10 | + | |
| 11 | +proc main*; | |
| 12 | +var p, q: ^U8; | |
| 13 | +begin | |
| 14 | + show(Mem.alloc(Heap.heap, -1, 8)); | |
| 15 | + show(Mem.alloc(Heap.heap, 1 shl 60, 8)); | |
| 16 | + show(Mem.alloc(Heap.heap, 7FFFFFFFFFFFFFF0H, 8)); | |
| 17 | + show(Mem.alloc(Heap.heap, 10, 3)); | |
| 18 | + show(Mem.alloc(Heap.heap, 10, 0)); | |
| 19 | + show(Mem.alloc(Heap.heap, 10, 8192)); | |
| 20 | + p := Mem.alloc(Heap.heap, 10, 64); | |
| 21 | + show(p); | |
| 22 | + if I64(p) mod 64 = 0 then Out.str("aligned\n"); end; | |
| 23 | + Mem.free(Heap.heap, p); | |
| 24 | + q := Mem.alloc(Heap.heap, 100000, 8); | |
| 25 | + show(q); | |
| 26 | + Mem.free(Heap.heap, q); | |
| 27 | + p := Mem.alloc(Heap.heap, 24, 8); | |
| 28 | + Mem.free(Heap.heap, p); | |
| 29 | + Out.str("freeing it again\n"); | |
| 30 | + Mem.free(Heap.heap, p); | |
| 31 | + Out.str("not reached\n"); | |
| 32 | +end; | |
| 33 | + | |
| 34 | +end HeapCheck. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1 @@ | ||
| 1 | +freeing a stray pointer |
| @@ -0,0 +1,14 @@ | ||
| 1 | +(* Freeing what the heap never gave out stops the program. *) | |
| 2 | +module HeapStray; | |
| 3 | +import Out, Heap, Mem; | |
| 4 | + | |
| 5 | +var a: [4]I64; | |
| 6 | + | |
| 7 | +proc main*; | |
| 8 | +begin | |
| 9 | + Out.str("freeing a stray pointer\n"); | |
| 10 | + Mem.free(Heap.heap, ^U8(ADR(a[2]))); | |
| 11 | + Out.str("not reached\n"); | |
| 12 | +end; | |
| 13 | + | |
| 14 | +end HeapStray. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1,201 @@ | ||
| 1 | +the same line from every thread | |
| 2 | +the same line from every thread | |
| 3 | +the same line from every thread | |
| 4 | +the same line from every thread | |
| 5 | +the same line from every thread | |
| 6 | +the same line from every thread | |
| 7 | +the same line from every thread | |
| 8 | +the same line from every thread | |
| 9 | +the same line from every thread | |
| 10 | +the same line from every thread | |
| 11 | +the same line from every thread | |
| 12 | +the same line from every thread | |
| 13 | +the same line from every thread | |
| 14 | +the same line from every thread | |
| 15 | +the same line from every thread | |
| 16 | +the same line from every thread | |
| 17 | +the same line from every thread | |
| 18 | +the same line from every thread | |
| 19 | +the same line from every thread | |
| 20 | +the same line from every thread | |
| 21 | +the same line from every thread | |
| 22 | +the same line from every thread | |
| 23 | +the same line from every thread | |
| 24 | +the same line from every thread | |
| 25 | +the same line from every thread | |
| 26 | +the same line from every thread | |
| 27 | +the same line from every thread | |
| 28 | +the same line from every thread | |
| 29 | +the same line from every thread | |
| 30 | +the same line from every thread | |
| 31 | +the same line from every thread | |
| 32 | +the same line from every thread | |
| 33 | +the same line from every thread | |
| 34 | +the same line from every thread | |
| 35 | +the same line from every thread | |
| 36 | +the same line from every thread | |
| 37 | +the same line from every thread | |
| 38 | +the same line from every thread | |
| 39 | +the same line from every thread | |
| 40 | +the same line from every thread | |
| 41 | +the same line from every thread | |
| 42 | +the same line from every thread | |
| 43 | +the same line from every thread | |
| 44 | +the same line from every thread | |
| 45 | +the same line from every thread | |
| 46 | +the same line from every thread | |
| 47 | +the same line from every thread | |
| 48 | +the same line from every thread | |
| 49 | +the same line from every thread | |
| 50 | +the same line from every thread | |
| 51 | +the same line from every thread | |
| 52 | +the same line from every thread | |
| 53 | +the same line from every thread | |
| 54 | +the same line from every thread | |
| 55 | +the same line from every thread | |
| 56 | +the same line from every thread | |
| 57 | +the same line from every thread | |
| 58 | +the same line from every thread | |
| 59 | +the same line from every thread | |
| 60 | +the same line from every thread | |
| 61 | +the same line from every thread | |
| 62 | +the same line from every thread | |
| 63 | +the same line from every thread | |
| 64 | +the same line from every thread | |
| 65 | +the same line from every thread | |
| 66 | +the same line from every thread | |
| 67 | +the same line from every thread | |
| 68 | +the same line from every thread | |
| 69 | +the same line from every thread | |
| 70 | +the same line from every thread | |
| 71 | +the same line from every thread | |
| 72 | +the same line from every thread | |
| 73 | +the same line from every thread | |
| 74 | +the same line from every thread | |
| 75 | +the same line from every thread | |
| 76 | +the same line from every thread | |
| 77 | +the same line from every thread | |
| 78 | +the same line from every thread | |
| 79 | +the same line from every thread | |
| 80 | +the same line from every thread | |
| 81 | +the same line from every thread | |
| 82 | +the same line from every thread | |
| 83 | +the same line from every thread | |
| 84 | +the same line from every thread | |
| 85 | +the same line from every thread | |
| 86 | +the same line from every thread | |
| 87 | +the same line from every thread | |
| 88 | +the same line from every thread | |
| 89 | +the same line from every thread | |
| 90 | +the same line from every thread | |
| 91 | +the same line from every thread | |
| 92 | +the same line from every thread | |
| 93 | +the same line from every thread | |
| 94 | +the same line from every thread | |
| 95 | +the same line from every thread | |
| 96 | +the same line from every thread | |
| 97 | +the same line from every thread | |
| 98 | +the same line from every thread | |
| 99 | +the same line from every thread | |
| 100 | +the same line from every thread | |
| 101 | +the same line from every thread | |
| 102 | +the same line from every thread | |
| 103 | +the same line from every thread | |
| 104 | +the same line from every thread | |
| 105 | +the same line from every thread | |
| 106 | +the same line from every thread | |
| 107 | +the same line from every thread | |
| 108 | +the same line from every thread | |
| 109 | +the same line from every thread | |
| 110 | +the same line from every thread | |
| 111 | +the same line from every thread | |
| 112 | +the same line from every thread | |
| 113 | +the same line from every thread | |
| 114 | +the same line from every thread | |
| 115 | +the same line from every thread | |
| 116 | +the same line from every thread | |
| 117 | +the same line from every thread | |
| 118 | +the same line from every thread | |
| 119 | +the same line from every thread | |
| 120 | +the same line from every thread | |
| 121 | +the same line from every thread | |
| 122 | +the same line from every thread | |
| 123 | +the same line from every thread | |
| 124 | +the same line from every thread | |
| 125 | +the same line from every thread | |
| 126 | +the same line from every thread | |
| 127 | +the same line from every thread | |
| 128 | +the same line from every thread | |
| 129 | +the same line from every thread | |
| 130 | +the same line from every thread | |
| 131 | +the same line from every thread | |
| 132 | +the same line from every thread | |
| 133 | +the same line from every thread | |
| 134 | +the same line from every thread | |
| 135 | +the same line from every thread | |
| 136 | +the same line from every thread | |
| 137 | +the same line from every thread | |
| 138 | +the same line from every thread | |
| 139 | +the same line from every thread | |
| 140 | +the same line from every thread | |
| 141 | +the same line from every thread | |
| 142 | +the same line from every thread | |
| 143 | +the same line from every thread | |
| 144 | +the same line from every thread | |
| 145 | +the same line from every thread | |
| 146 | +the same line from every thread | |
| 147 | +the same line from every thread | |
| 148 | +the same line from every thread | |
| 149 | +the same line from every thread | |
| 150 | +the same line from every thread | |
| 151 | +the same line from every thread | |
| 152 | +the same line from every thread | |
| 153 | +the same line from every thread | |
| 154 | +the same line from every thread | |
| 155 | +the same line from every thread | |
| 156 | +the same line from every thread | |
| 157 | +the same line from every thread | |
| 158 | +the same line from every thread | |
| 159 | +the same line from every thread | |
| 160 | +the same line from every thread | |
| 161 | +the same line from every thread | |
| 162 | +the same line from every thread | |
| 163 | +the same line from every thread | |
| 164 | +the same line from every thread | |
| 165 | +the same line from every thread | |
| 166 | +the same line from every thread | |
| 167 | +the same line from every thread | |
| 168 | +the same line from every thread | |
| 169 | +the same line from every thread | |
| 170 | +the same line from every thread | |
| 171 | +the same line from every thread | |
| 172 | +the same line from every thread | |
| 173 | +the same line from every thread | |
| 174 | +the same line from every thread | |
| 175 | +the same line from every thread | |
| 176 | +the same line from every thread | |
| 177 | +the same line from every thread | |
| 178 | +the same line from every thread | |
| 179 | +the same line from every thread | |
| 180 | +the same line from every thread | |
| 181 | +the same line from every thread | |
| 182 | +the same line from every thread | |
| 183 | +the same line from every thread | |
| 184 | +the same line from every thread | |
| 185 | +the same line from every thread | |
| 186 | +the same line from every thread | |
| 187 | +the same line from every thread | |
| 188 | +the same line from every thread | |
| 189 | +the same line from every thread | |
| 190 | +the same line from every thread | |
| 191 | +the same line from every thread | |
| 192 | +the same line from every thread | |
| 193 | +the same line from every thread | |
| 194 | +the same line from every thread | |
| 195 | +the same line from every thread | |
| 196 | +the same line from every thread | |
| 197 | +the same line from every thread | |
| 198 | +the same line from every thread | |
| 199 | +the same line from every thread | |
| 200 | +the same line from every thread | |
| 201 | +160000 160000 |
| @@ -0,0 +1,50 @@ | ||
| 1 | +(* Native threads: a counter under a Mutex, the heap and Out used from | |
| 2 | + every thread at once, and join. *) | |
| 3 | +module Spawn; | |
| 4 | +import Out, Thread, Heap, Mem; | |
| 5 | + | |
| 6 | +const | |
| 7 | + Workers = 8; | |
| 8 | + Rounds = 20000; | |
| 9 | + | |
| 10 | +type Node = record next: ^Node; n: I64; end; | |
| 11 | + | |
| 12 | +var | |
| 13 | + m: Thread.Mutex; | |
| 14 | + count, atomic: I64; | |
| 15 | + | |
| 16 | +proc work(arg: ^U8); | |
| 17 | +var i: I64; p, list: ^Node; | |
| 18 | +begin | |
| 19 | + list := nil; | |
| 20 | + for i := 1 to Rounds do | |
| 21 | + Thread.lock(ADR(m)); | |
| 22 | + INC(count); | |
| 23 | + Thread.unlock(ADR(m)); | |
| 24 | + XADD(ADR(atomic), 1); | |
| 25 | + NEW(p, Heap.heap); | |
| 26 | + p^.next := list; | |
| 27 | + list := p; | |
| 28 | + if i mod 4 = 0 then | |
| 29 | + while list # nil do | |
| 30 | + p := list^.next; | |
| 31 | + Mem.free(Heap.heap, ^U8(list)); | |
| 32 | + list := p; | |
| 33 | + end; | |
| 34 | + end; | |
| 35 | + end; | |
| 36 | + for i := 1 to 25 do Out.str("the same line from every thread\n"); end; | |
| 37 | +end; | |
| 38 | + | |
| 39 | +proc main*; | |
| 40 | +var t: [Workers]^Thread.Thread; i: I64; err: ERROR; | |
| 41 | +begin | |
| 42 | + for i := 0 to Workers - 1 do | |
| 43 | + t[i], err := Thread.spawn(work, nil); | |
| 44 | + if err # nil then Out.str(MSG(err)); Out.ln; return; end; | |
| 45 | + end; | |
| 46 | + for i := 0 to Workers - 1 do Thread.join(t[i]); end; | |
| 47 | + Out.int(count); Out.char(' '); Out.int(atomic); Out.ln; | |
| 48 | +end; | |
| 49 | + | |
| 50 | +end Spawn. |