Thread in a program with C, by C's pthread_create The entry point of a program that uses C now stores a list of C's symbols in the library's variables, found by name: exit in Rt.cExit as before, and, when the program has Thread, pthread_create and pthread_join in Thread.cCreate and Thread.cJoin. Thread makes C's threads when they are set, clone's when not; it drops link static, and uses no C itself. Its threads keep a stack for Rt's handlers either way, so an overflow in a C one is told too. Tests: SpawnC, ThreadOverflowC; ThreadC, the error, goes.
| @@ -163,7 +163,10 @@ | ||
| 163 | 163 | - C symbols are reached through quadwords the loader fills in |
| 164 | 164 | (GLOB_DAT), with no PLT; the executable exports nothing. In a C |
| 165 | 165 | program the entry point stores C's `exit` in `Rt.cExit`, found by |
| 166 | - name like `Rt.exit`, before any module body runs. | |
| 166 | + name like `Rt.exit`, before any module body runs; and, if the | |
| 167 | + program has `Thread`, `pthread_create` and `pthread_join` in | |
| 168 | + `Thread.cCreate` and `Thread.cJoin`, so that `Thread` makes C's | |
| 169 | + threads there and serves programs with C and without. | |
| 167 | 170 | - Past an extern's parameters, a string goes as a C string and an |
| 168 | 171 | integer constant as an `INT`. |
| 169 | 172 | - A real constant is held as an F64; one typed `F32` holds a value an |
| @@ -177,8 +180,9 @@ | ||
| 177 | 180 | its prologue, and the program ends by `Rt.exit(2)`, after its |
| 178 | 181 | `atExit` procedures, as a failed check does (report 2026-10.22; a |
| 179 | 182 | fault inside `Out`'s or `Heap`'s lock would hang there). The handlers run on a stack of their own: `Rt`'s for |
| 180 | - the main thread, and one in each `Thread` thread's mapping. C's | |
| 181 | - threads have none, so a stack overflow in one still ends the program | |
| 183 | + the main thread, and one in each `Thread` thread's mapping, made | |
| 184 | + by clone or by C. Threads a program makes with `pthread_create` | |
| 185 | + itself have none, so a stack overflow in one still ends the program | |
| 182 | 186 | by the signal with no message. |
| 183 | 187 | - The smallest value of a signed type divided by -1 stops the program |
| 184 | 188 | (SIGFPE), as 6.1 says, `mod` as `div`; for a type narrower than 64 |
| @@ -18,12 +18,12 @@ | ||
| 18 | 18 | | `src/Sym.raz` | 684 | 555 | Types, objects, scopes, layout | |
| 19 | 19 | | `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes | |
| 20 | 20 | | `src/X64.raz` | 1,493 | 1,237 | The instruction encoder, jumps within a procedure, the assembler | |
| 21 | -| `src/Gen.raz` | 2,471 | 1,778 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 21 | +| `src/Gen.raz` | 2,478 | 1,780 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 22 | 22 | | `src/Item.raz` | 1,763 | 1,458 | Items: types, constants, variables, operators, conversions, calls | |
| 23 | 23 | | `src/Parse.raz` | 3,846 | 3,355 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 24 | 24 | | `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places | |
| 25 | 25 | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | |
| 26 | -| `src/Raz.raz` | 273 | 214 | The command line | | |
| 26 | +| `src/Raz.raz` | 305 | 238 | The command line | | |
| 27 | 27 | |
| 28 | 28 | Every file opens with a comment that says what it is, what it depends |
| 29 | 29 | on and what it leaves out. Read those first; they are the map. |
| @@ -1395,7 +1395,11 @@ | ||
| 1395 | 1395 | `Raz.raz` parses the arguments, calls `Parse.compileMain`, and works out what |
| 1396 | 1396 | the program uses: `use` marks the modules imported from the main module |
| 1397 | 1397 | and `Rt`; `usesC` decides whether the program is dynamic, refusing one |
| 1398 | -that both uses C and has a module saying `link static`. Then it has | |
| 1398 | +that both uses C and has a module saying `link static`. In a program | |
| 1399 | +that uses C, it gives the entry point the C symbols the library needs, | |
| 1400 | +each with the variable it goes in, found by name (`addCLink`): `exit` | |
| 1401 | +for `Rt.cExit`, and, if the program has `Thread`, `pthread_create` and | |
| 1402 | +`pthread_join` for `Thread.cCreate` and `Thread.cJoin`. Then it has | |
| 1399 | 1403 | gen's `entryPoint` write the entry point, and `Link.markLive` mark what it |
| 1400 | 1404 | reaches. The program is `proc main*: INT`; where raz is, for the library |
| 1401 | 1405 | beside it, it reads from `/proc/self/exe`, and the version it prints is |
| @@ -1487,16 +1491,20 @@ | ||
| 1487 | 1491 | ends the program with status 2, after the procedures registered with |
| 1488 | 1492 | `Rt.atExit`, as for a failed check. A fault inside a section that holds the lock of `Out` or `Heap` (a stack overflow in `Out`, say) would leave `atExit`'s flush waiting for it: the program hangs instead of ending. This is rare, and a failed check has the same exposure. The handlers run on a stack |
| 1489 | 1493 | of their own: `Rt`'s for the main thread, one in each `Thread` |
| 1490 | - thread's mapping. C's threads have none, so a stack overflow in one | |
| 1491 | - ends the program by the signal with no message. | |
| 1494 | + thread's mapping, whether clone or C made the thread. Threads a | |
| 1495 | + program makes with C's `pthread_create` itself have none, so a stack | |
| 1496 | + overflow in one ends the program by the signal with no message. | |
| 1492 | 1497 | - **Locks.** `Heap` and `Out` lock with `Lock`, a spinlock that yields, |
| 1493 | 1498 | since they serve C's threads and `Thread`'s alike; `Thread.Mutex` is |
| 1494 | 1499 | a futex. |
| 1495 | 1500 | - **C.** A program uses C if a module it uses has an `extern` or a |
| 1496 | 1501 | `link` of a library. C's symbols are reached through quadwords the |
| 1497 | 1502 | loader fills in (GLOB_DAT), with no PLT; the executable exports |
| 1498 | - nothing. Its entry point stores C's `exit` in `Rt.cExit` before any | |
| 1499 | - module body runs. | |
| 1503 | + nothing. Its entry point stores C's `exit` in `Rt.cExit`, and, if | |
| 1504 | + the program has `Thread`, `pthread_create` and `pthread_join` in | |
| 1505 | + `Thread.cCreate` and `Thread.cJoin`, before any module body runs: | |
| 1506 | + `Thread` then makes C's threads, which C's library knows of, and | |
| 1507 | + uses no C itself. | |
| 1500 | 1508 | - **Frames.** A deferred statement's slots stay reserved for the rest |
| 1501 | 1509 | of its procedure. A generic instance first used in a procedure, or in |
| 1502 | 1510 | a module's body, is made before that code begins (`instancesFor`). |
| @@ -1,14 +1,21 @@ | ||
| 1 | -(* Threads for programs without C, made with the clone system call. | |
| 1 | +(* Threads, made with the clone system call in a program without C. | |
| 2 | 2 | Each runs on a stack of its own, mmaped, with a page below it that |
| 3 | 3 | can't be touched, so that running off the stack stops the program |
| 4 | 4 | rather than spoiling what lies below; above it, a little stack for |
| 5 | 5 | Rt's signal handlers, so that they can say so. join waits on the |
| 6 | 6 | thread's id, which the kernel clears, and wakes it for, when the |
| 7 | - thread ends; a Mutex sleeps on a futex. A program with C threads | |
| 8 | - uses C's, through pthread_create, and so can't use these. *) | |
| 7 | + thread ends; a Mutex sleeps on a futex. | |
| 8 | + | |
| 9 | + A program that uses C makes them with C's pthread_create instead, | |
| 10 | + since C keeps things of each thread's own, errno and its buffers, | |
| 11 | + that a thread clone makes wouldn't have. The compiler's entry point | |
| 12 | + finds cCreate and cJoin by name and stores C's calls in them, as it | |
| 13 | + stores C's exit in Rt.cExit; so this module uses no C itself, and | |
| 14 | + serves both kinds of program. C's thread has a stack and a guard | |
| 15 | + page of C's; the mapping here holds only the handlers' stack and | |
| 16 | + the Thread. *) | |
| 9 | 17 | module Thread; |
| 10 | 18 | import Sys, Rt; |
| 11 | -link static; | |
| 12 | 19 | |
| 13 | 20 | const |
| 14 | 21 | Failed*: ERROR = "can't start a thread"; |
| @@ -25,7 +32,7 @@ | ||
| 25 | 32 | type |
| 26 | 33 | (* A thread, as spawn gives it, until join. *) |
| 27 | 34 | Thread* = record |
| 28 | - tid: INT; (* the kernel's id, until the thread ends *) | |
| 35 | + tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *) | |
| 29 | 36 | base: ^U8; (* its stack's mapping, the guard page first *) |
| 30 | 37 | size: INT; |
| 31 | 38 | fn: proc(arg: ^U8); (* what it runs, and with what *) |
| @@ -37,6 +44,12 @@ | ||
| 37 | 44 | free, 1 taken, 2 taken with a thread waiting. *) |
| 38 | 45 | Mutex* = record state: INT; end; |
| 39 | 46 | |
| 47 | +var | |
| 48 | + (* C's pthread_create and pthread_join, in a program that uses C; | |
| 49 | + nil in one that doesn't *) | |
| 50 | + cCreate: proc(t: ^UINT; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; | |
| 51 | + cJoin: proc(t: UINT; result: ^^U8): I32; | |
| 52 | + | |
| 40 | 53 | (* clone(flags, stack, tid, tid), the new thread starting with fn(arg) |
| 41 | 54 | on stack and ending, when fn returns, with the exit system call. |
| 42 | 55 | fn and arg go on the new stack first, since the new thread has no |
| @@ -80,19 +93,21 @@ | ||
| 80 | 93 | t^.fn(t^.arg); |
| 81 | 94 | end; |
| 82 | 95 | |
| 96 | +(* Where a thread C makes starts, as pthread_create calls it *) | |
| 97 | +proc cRun(arg: ^U8): ^U8; | |
| 98 | +begin | |
| 99 | + run(arg); | |
| 100 | + return nil; | |
| 101 | +end; | |
| 102 | + | |
| 83 | 103 | (* Starts p(arg) in a new thread. *) |
| 84 | 104 | proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); |
| 85 | -var base: ^U8; t: ^Thread; size: INT; | |
| 105 | +var base: ^U8; t: ^Thread; size: INT; tid: UINT; | |
| 86 | 106 | begin |
| 87 | 107 | size := Page + Stack + Alt + Page; |
| 108 | + if cCreate # nil then size := Alt + Page; end; (* C gives the stack *) | |
| 88 | 109 | base := Sys.mmap(size); |
| 89 | 110 | if base = nil then return nil, Failed; end; |
| 90 | - (* mprotect: the guard page, no access; it fails when the process | |
| 91 | - has as many mappings as it may *) | |
| 92 | - if SYSCALL(10, base, Page, 0) < 0 then | |
| 93 | - Sys.munmap(base, size); | |
| 94 | - return nil, Failed; | |
| 95 | - end; | |
| 96 | 111 | (* from the top: the Thread in the last page, the handlers' stack, |
| 97 | 112 | then the thread's, starting on a page *) |
| 98 | 113 | t := ^Thread(base + size - Page); |
| @@ -100,7 +115,18 @@ | ||
| 100 | 115 | t^.size := size; |
| 101 | 116 | t^.fn := p; |
| 102 | 117 | t^.arg := arg; |
| 103 | - if start(Flags, base + Page + Stack, ADR(t^.tid), ADR(t^.tid), run, ^U8(t)) < 0 then | |
| 118 | + if cCreate # nil then | |
| 119 | + if cCreate(ADR(tid), nil, cRun, ^U8(t)) # 0 then | |
| 120 | + Sys.munmap(base, size); | |
| 121 | + return nil, Failed; | |
| 122 | + end; | |
| 123 | + t^.tid := INT(tid); | |
| 124 | + return t, nil; | |
| 125 | + end; | |
| 126 | + (* mprotect: the guard page, no access; it fails when the process | |
| 127 | + has as many mappings as it may *) | |
| 128 | + if (SYSCALL(10, base, Page, 0) < 0) | |
| 129 | + or (start(Flags, base + Page + Stack, ADR(t^.tid), ADR(t^.tid), run, ^U8(t)) < 0) then | |
| 104 | 130 | Sys.munmap(base, size); |
| 105 | 131 | return nil, Failed; |
| 106 | 132 | end; |
| @@ -111,11 +137,15 @@ | ||
| 111 | 137 | proc join*(t: ^Thread); |
| 112 | 138 | var tid: ^VOLATILE INT; v: INT; base: ^U8; size: INT; |
| 113 | 139 | begin |
| 114 | - tid := ADR(t^.tid); | |
| 115 | - v := tid^; | |
| 116 | - while v # 0 do | |
| 117 | - _ := futex(ADR(t^.tid), FutexWait, v); | |
| 140 | + if cJoin # nil then | |
| 141 | + _ := cJoin(UINT(t^.tid), nil); | |
| 142 | + else | |
| 143 | + tid := ADR(t^.tid); | |
| 118 | 144 | v := tid^; |
| 145 | + while v # 0 do | |
| 146 | + _ := futex(ADR(t^.tid), FutexWait, v); | |
| 147 | + v := tid^; | |
| 148 | + end; | |
| 119 | 149 | end; |
| 120 | 150 | base := t^.base; |
| 121 | 151 | size := t^.size; |
| @@ -89,7 +89,8 @@ | ||
| 89 | 89 | CodeMark* = record pc*, fixups*, lines*, said*, fails*: INT; end; |
| 90 | 90 | |
| 91 | 91 | (* For entryPoint: the address of chunk, put in the variable whose |
| 92 | - chunk is into before the program starts *) | |
| 92 | + chunk is into before the program starts; or, for C's symbols, the | |
| 93 | + address in chunk, the quadword the loader fills in *) | |
| 93 | 94 | Link* = record chunk*, into*: INT; end; |
| 94 | 95 | |
| 95 | 96 | CallInfo* = record |
| @@ -2365,15 +2366,18 @@ | ||
| 2365 | 2366 | main; and ends with Rt.exit, its status main's or 0. A dynamic program |
| 2366 | 2367 | starts as C's crt1 does, through __libc_start_main(main, argc, argv, |
| 2367 | 2368 | nil, nil, rtld_fini, stack_end), with rsp aligned; its main first |
| 2368 | - gives Rt C's exit, in cExit. | |
| 2369 | + gives the library what it needs of C: Rt C's exit, in cExit, and | |
| 2370 | + Thread pthread_create and pthread_join. | |
| 2369 | 2371 | |
| 2370 | 2372 | dynamic: the program uses C. first: the first module, whose chain |
| 2371 | 2373 | gives the order of the bodies. mainObj: the program's main procedure, or |
| 2372 | - nil. cExit and exitProc: the chunks of Rt's cExit and exit. The | |
| 2374 | + nil. The first numCLinks of cLinks: C's symbols, each loaded from the | |
| 2375 | + quadword the loader fills in and stored in a variable of the | |
| 2376 | + library's, in a program that uses C. exitProc: the chunk of Rt's exit. The | |
| 2373 | 2377 | first numLinks of links: the addresses Rt is given, of what the program |
| 2374 | 2378 | has: the places, and the profiler and calls; what it isn't given, |
| 2375 | 2379 | nothing reaches, and the linker leaves out. *) |
| 2376 | -proc entryPoint*(dynamic: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cExit, exitProc: INT; links: []Link; numLinks: INT): INT; | |
| 2380 | +proc entryPoint*(dynamic: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT; | |
| 2377 | 2381 | var entry, start, i: INT; m: ^Sym.Module; |
| 2378 | 2382 | begin |
| 2379 | 2383 | entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start")); |
| @@ -2399,8 +2403,10 @@ | ||
| 2399 | 2403 | X.push(RBP); (* aligns the stack again *) |
| 2400 | 2404 | X.store(X.memRip(argcChunk, 0), RDI, 8); |
| 2401 | 2405 | X.store(X.memRip(argvChunk, 0), RSI, 8); |
| 2402 | - X.load(RAX, X.memRip(Chunk.externChunk(internStr("exit")), 0), 8, false); | |
| 2403 | - X.store(X.memRip(cExit, 0), RAX, 8); | |
| 2406 | + for i := 0 to numCLinks - 1 do | |
| 2407 | + X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false); | |
| 2408 | + X.store(X.memRip(cLinks[i].into, 0), RAX, 8); | |
| 2409 | + end; | |
| 2404 | 2410 | else |
| 2405 | 2411 | start := entry; |
| 2406 | 2412 | Chunk.beginChunk(entry); |
| @@ -37,6 +37,8 @@ | ||
| 37 | 37 | var |
| 38 | 38 | links: [4]Gen.Link; |
| 39 | 39 | numLinks: INT; |
| 40 | + cLinks: [3]Gen.Link; (* C's symbols the entry point gives the library, in a program that uses C *) | |
| 41 | + numCLinks: INT; | |
| 40 | 42 | |
| 41 | 43 | proc say1(f, a: []U8): []U8; |
| 42 | 44 | begin |
| @@ -108,11 +110,21 @@ | ||
| 108 | 110 | Out.flush(); |
| 109 | 111 | end; |
| 110 | 112 | |
| 111 | -proc rtObj(rt: ^Sym.Module; n: []U8): ^Sym.Obj; | |
| 113 | +(* The module named n, or nil if the program has none *) | |
| 114 | +proc moduleNamed(n: []U8): ^Sym.Module; | |
| 115 | +var m: ^Sym.Module; | |
| 116 | +begin | |
| 117 | + m := Parse.firstModule; | |
| 118 | + while (m # nil) and (Scan.nameOf(m^.name) # n) do m := m^.next; end; | |
| 119 | + return m; | |
| 120 | +end; | |
| 121 | + | |
| 122 | +(* What the library module m, found by name, declares as n *) | |
| 123 | +proc objIn(m: ^Sym.Module; n: []U8): ^Sym.Obj; | |
| 112 | 124 | var o: ^Sym.Obj; |
| 113 | 125 | begin |
| 114 | - o := Sym.find(rt^.scope, internStr(n)); | |
| 115 | - if o = nil then Diag.fatal(say1("Rt has no {}", n)); end; | |
| 126 | + o := Sym.find(m^.scope, internStr(n)); | |
| 127 | + if o = nil then Diag.fatal(Diag.say("{} has no {}", [Fmt.str(Scan.nameOf(m^.name)), Fmt.str(n)])); end; | |
| 116 | 128 | return o; |
| 117 | 129 | end; |
| 118 | 130 | |
| @@ -124,6 +136,14 @@ | ||
| 124 | 136 | INC(numLinks); |
| 125 | 137 | end; |
| 126 | 138 | |
| 139 | +(* The library is given C's symbol sym, in its variable v *) | |
| 140 | +proc addCLink(sym: []U8; v: ^Sym.Obj); | |
| 141 | +begin | |
| 142 | + cLinks[numCLinks].chunk := Chunk.externChunk(internStr(sym)); | |
| 143 | + cLinks[numCLinks].into := v^.chunk; | |
| 144 | + INC(numCLinks); | |
| 145 | +end; | |
| 146 | + | |
| 127 | 147 | (* s as a C string, in memory that is never freed *) |
| 128 | 148 | proc cstr(s: []U8): ^U8; |
| 129 | 149 | var p: ^U8; |
| @@ -138,8 +158,8 @@ | ||
| 138 | 158 | var |
| 139 | 159 | path, outPath, s: []U8; dir: []U8; |
| 140 | 160 | i, first, entry, fd, last, errno: INT; |
| 141 | - m, rt: ^Sym.Module; mainObj: ^Sym.Obj; | |
| 142 | - run, noProfile, listing: BOOL; err: ERROR; | |
| 161 | + m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj; | |
| 162 | + run, noProfile, listing, dynamic: BOOL; err: ERROR; | |
| 143 | 163 | begin |
| 144 | 164 | path := ""; |
| 145 | 165 | outPath := ""; |
| @@ -210,8 +230,7 @@ | ||
| 210 | 230 | if Diag.errors > 0 then Diag.finish(); end; |
| 211 | 231 | |
| 212 | 232 | use(m); |
| 213 | - rt := Parse.firstModule; | |
| 214 | - while (rt # nil) and (Scan.nameOf(rt^.name) # "Rt") do rt := rt^.next; end; | |
| 233 | + rt := moduleNamed("Rt"); | |
| 215 | 234 | if rt # nil then use(rt); end; |
| 216 | 235 | |
| 217 | 236 | mainObj := Sym.find(m^.scope, internStr("main")); |
| @@ -234,13 +253,26 @@ | ||
| 234 | 253 | if not Elf.stripped then |
| 235 | 254 | Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); |
| 236 | 255 | Chunk.setData(Link.placesChunk, nil, 0, 8); |
| 237 | - addLink(Link.placesChunk, rtObj(rt, "places")); | |
| 238 | - addLink(rtObj(rt, "calls")^.chunk, rtObj(rt, "teller")); | |
| 239 | - addLink(rtObj(rt, "place")^.chunk, rtObj(rt, "placer")); | |
| 240 | - if not noProfile then addLink(rtObj(rt, "startProfile")^.chunk, rtObj(rt, "profiler")); end; | |
| 256 | + addLink(Link.placesChunk, objIn(rt, "places")); | |
| 257 | + addLink(objIn(rt, "calls")^.chunk, objIn(rt, "teller")); | |
| 258 | + addLink(objIn(rt, "place")^.chunk, objIn(rt, "placer")); | |
| 259 | + if not noProfile then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end; | |
| 260 | + end; | |
| 261 | + (* what a program that uses C is given of C: Rt its exit, and | |
| 262 | + Thread, if the program has it, the calls that make and join C's | |
| 263 | + threads, so that the C library knows of them *) | |
| 264 | + dynamic := usesC(); | |
| 265 | + numCLinks := 0; | |
| 266 | + if dynamic then | |
| 267 | + addCLink("exit", objIn(rt, "cExit")); | |
| 268 | + thread := moduleNamed("Thread"); | |
| 269 | + if (thread # nil) and thread^.used then | |
| 270 | + addCLink("pthread_create", objIn(thread, "cCreate")); | |
| 271 | + addCLink("pthread_join", objIn(thread, "cJoin")); | |
| 272 | + end; | |
| 241 | 273 | end; |
| 242 | - entry := Gen.entryPoint(usesC(), Parse.firstModule, mainObj, rtObj(rt, "cExit")^.chunk, | |
| 243 | - rtObj(rt, "exit")^.chunk, links[0..numLinks], numLinks); | |
| 274 | + entry := Gen.entryPoint(dynamic, Parse.firstModule, mainObj, cLinks[0..numCLinks], numCLinks, | |
| 275 | + objIn(rt, "exit")^.chunk, links[0..numLinks], numLinks); | |
| 244 | 276 | |
| 245 | 277 | Link.markLive(entry); |
| 246 | 278 | if not Parse.HasReals then |
| @@ -1,17 +1,25 @@ | ||
| 1 | 1 | module Thread; |
| 2 | - Threads for programs without C, made with the clone system call. | |
| 2 | + Threads, made with the clone system call in a program without C. | |
| 3 | 3 | Each runs on a stack of its own, mmaped, with a page below it that |
| 4 | 4 | can't be touched, so that running off the stack stops the program |
| 5 | 5 | rather than spoiling what lies below; above it, a little stack for |
| 6 | 6 | Rt's signal handlers, so that they can say so. join waits on the |
| 7 | 7 | thread's id, which the kernel clears, and wakes it for, when the |
| 8 | - thread ends; a Mutex sleeps on a futex. A program with C threads | |
| 9 | - uses C's, through pthread_create, and so can't use these. | |
| 8 | + thread ends; a Mutex sleeps on a futex. | |
| 9 | + | |
| 10 | + A program that uses C makes them with C's pthread_create instead, | |
| 11 | + since C keeps things of each thread's own, errno and its buffers, | |
| 12 | + that a thread clone makes wouldn't have. The compiler's entry point | |
| 13 | + finds cCreate and cJoin by name and stores C's calls in them, as it | |
| 14 | + stores C's exit in Rt.cExit; so this module uses no C itself, and | |
| 15 | + serves both kinds of program. C's thread has a stack and a guard | |
| 16 | + page of C's; the mapping here holds only the handlers' stack and | |
| 17 | + the Thread. | |
| 10 | 18 | |
| 11 | 19 | const Failed*: ERROR = "can't start a thread"; |
| 12 | 20 | |
| 13 | 21 | type Thread* = record |
| 14 | - tid: INT; (* the kernel's id, until the thread ends *) | |
| 22 | + tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *) | |
| 15 | 23 | base: ^U8; (* its stack's mapping, the guard page first *) |
| 16 | 24 | size: INT; |
| 17 | 25 | fn: proc(arg: ^U8); (* what it runs, and with what *) |
| @@ -1,8 +0,0 @@ | ||
| 1 | -test/err/ThreadC.raz:2:8: warning: module Thread is imported but never used | |
| 2 | - 2 | import Thread, C; | |
| 3 | - | ^^^^^^ | |
| 4 | -test/err/ThreadC.raz:2:16: warning: module C is imported but never used | |
| 5 | - 2 | import Thread, C; | |
| 6 | - | ^ | |
| 7 | -raz: 2 warnings | |
| 8 | -raz: Thread says link static, but C uses C |
| @@ -1,8 +0,0 @@ | ||
| 1 | -module ThreadC; | |
| 2 | -import Thread, C; | |
| 3 | - | |
| 4 | -proc main*; | |
| 5 | -begin | |
| 6 | -end; | |
| 7 | - | |
| 8 | -end ThreadC. |
| @@ -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,52 @@ | ||
| 1 | +(* Threads in a program that uses C, which Thread makes with C's | |
| 2 | + pthread_create: a counter under a Mutex, the heap and Out used from | |
| 3 | + every thread at once, and join. *) | |
| 4 | +module SpawnC; | |
| 5 | +import Out, Thread, Heap, Mem, C; | |
| 6 | + | |
| 7 | +const | |
| 8 | + Workers = 8; | |
| 9 | + Rounds = 20000; | |
| 10 | + | |
| 11 | +type Node = record next: ^Node; n: I64; end; | |
| 12 | + | |
| 13 | +var | |
| 14 | + m: Thread.Mutex; | |
| 15 | + count: I64; atomic: INT; | |
| 16 | + | |
| 17 | +proc work(arg: ^U8); | |
| 18 | +var i: I64; p, list: ^Node; | |
| 19 | +begin | |
| 20 | + list := nil; | |
| 21 | + for i := 1 to Rounds do | |
| 22 | + Thread.lock(ADR(m)); | |
| 23 | + INC(count); | |
| 24 | + Thread.unlock(ADR(m)); | |
| 25 | + _ := XADD(ADR(atomic), 1); | |
| 26 | + NEW(p, Heap.heap); | |
| 27 | + p^.next := list; | |
| 28 | + list := p; | |
| 29 | + if i mod 4 = 0 then | |
| 30 | + while list # nil do | |
| 31 | + p := list^.next; | |
| 32 | + Mem.free(Heap.heap, ^U8(list)); | |
| 33 | + list := p; | |
| 34 | + end; | |
| 35 | + end; | |
| 36 | + end; | |
| 37 | + for i := 1 to 25 do Out.str("the same line from every thread\n"); end; | |
| 38 | +end; | |
| 39 | + | |
| 40 | +proc main*; | |
| 41 | +var t: [Workers]^Thread.Thread; i: I64; err: ERROR; | |
| 42 | +begin | |
| 43 | + if C.getpid() <= 0 then return; end; | |
| 44 | + for i := 0 to Workers - 1 do | |
| 45 | + t[i], err := Thread.spawn(work, nil); | |
| 46 | + if err # nil then Out.str(MSG(err)); Out.ln; return; end; | |
| 47 | + end; | |
| 48 | + for i := 0 to Workers - 1 do Thread.join(t[i]); end; | |
| 49 | + Out.int(count); Out.char(' '); Out.int(atomic); Out.ln; | |
| 50 | +end; | |
| 51 | + | |
| 52 | +end SpawnC. |
| @@ -3,4 +3,4 @@ | ||
| 3 | 3 | in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926 |
| 4 | 4 | and 1926 more the same |
| 5 | 5 | in ThreadOverflow.work at test/run/ThreadOverflow.raz:15:5: _arg = nil |
| 6 | - in Thread.run at lib/Thread.linux-x86_64.raz:80:5: arg = 0x?, t = 0x? | |
| 6 | + in Thread.run at lib/Thread.linux-x86_64.raz:93:5: arg = 0x?, t = 0x? |
An empty file.
| @@ -0,0 +1,27 @@ | ||
| 1 | +module ThreadOverflowC; | |
| 2 | +(* A stack overflow in a thread of a program that uses C is told as | |
| 3 | + one in the main thread is: Thread gives C's thread a stack for the | |
| 4 | + signal handlers. One frame bigger than any stack C gives a thread. *) | |
| 5 | +import Thread, C; | |
| 6 | + | |
| 7 | +proc big: I64; | |
| 8 | +var pad: [64 * 1024 * 1024]U8; | |
| 9 | +begin | |
| 10 | + pad[0] := 1; | |
| 11 | + return I64(pad[0]); | |
| 12 | +end; | |
| 13 | + | |
| 14 | +proc work(_arg: ^U8); | |
| 15 | +begin | |
| 16 | + _ := big(); | |
| 17 | +end; | |
| 18 | + | |
| 19 | +proc main*; | |
| 20 | +var t: ^Thread.Thread; e: ERROR; | |
| 21 | +begin | |
| 22 | + _ := C.getpid(); | |
| 23 | + t, e := Thread.spawn(work, nil); | |
| 24 | + if e = nil then Thread.join(t); end; | |
| 25 | +end; | |
| 26 | + | |
| 27 | +end ThreadOverflowC. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1,5 @@ | ||
| 1 | +test/run/ThreadOverflowC.raz:7:1: stopped by a stack overflow | |
| 2 | + in ThreadOverflowC.big at test/run/ThreadOverflowC.raz:7:1 | |
| 3 | + in ThreadOverflowC.work at test/run/ThreadOverflowC.raz:16:5: _arg = nil | |
| 4 | + in Thread.run at lib/Thread.linux-x86_64.raz:93:5: arg = 0x?, t = 0x? | |
| 5 | + in Thread.cRun at lib/Thread.linux-x86_64.raz:99:5: arg = 0x? |