From f871b24415a979367b1b016a42d719ef1b0fac00 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Wed, 14 Oct 2026 15:17:50 +0100 Subject: [PATCH] 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. --- diff --git a/CLAUDE.md b/CLAUDE.md index b24f166..d941535 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -163,7 +163,10 @@ - C symbols are reached through quadwords the loader fills in (GLOB_DAT), with no PLT; the executable exports nothing. In a C program the entry point stores C's `exit` in `Rt.cExit`, found by - name like `Rt.exit`, before any module body runs. + name like `Rt.exit`, before any module body runs; and, if the + program has `Thread`, `pthread_create` and `pthread_join` in + `Thread.cCreate` and `Thread.cJoin`, so that `Thread` makes C's + threads there and serves programs with C and without. - Past an extern's parameters, a string goes as a C string and an integer constant as an `INT`. - A real constant is held as an F64; one typed `F32` holds a value an @@ -177,8 +180,9 @@ its prologue, and the program ends by `Rt.exit(2)`, after its `atExit` procedures, as a failed check does (report 2026-10.22; a fault inside `Out`'s or `Heap`'s lock would hang there). The handlers run on a stack of their own: `Rt`'s for - the main thread, and one in each `Thread` thread's mapping. C's - threads have none, so a stack overflow in one still ends the program + the main thread, and one in each `Thread` thread's mapping, made + by clone or by C. Threads a program makes with `pthread_create` + itself have none, so a stack overflow in one still ends the program by the signal with no message. - The smallest value of a signed type divided by -1 stops the program (SIGFPE), as 6.1 says, `mod` as `div`; for a type narrower than 64 diff --git a/doc/compiler.md b/doc/compiler.md index 2faa1c2..617e2a2 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -18,12 +18,12 @@ | `src/Sym.raz` | 684 | 555 | Types, objects, scopes, layout | | `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes | | `src/X64.raz` | 1,493 | 1,237 | The instruction encoder, jumps within a procedure, the assembler | -| `src/Gen.raz` | 2,471 | 1,778 | The stack machine, for x86-64: values, operations, checks, calls, frames | +| `src/Gen.raz` | 2,478 | 1,780 | The stack machine, for x86-64: values, operations, checks, calls, frames | | `src/Item.raz` | 1,763 | 1,458 | Items: types, constants, variables, operators, conversions, calls | | `src/Parse.raz` | 3,846 | 3,355 | Declarations, statements, expressions, built-ins, recovery, warnings | | `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places | | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers | -| `src/Raz.raz` | 273 | 214 | The command line | +| `src/Raz.raz` | 305 | 238 | The command line | Every file opens with a comment that says what it is, what it depends on and what it leaves out. Read those first; they are the map. @@ -1395,7 +1395,11 @@ `Raz.raz` parses the arguments, calls `Parse.compileMain`, and works out what the program uses: `use` marks the modules imported from the main module and `Rt`; `usesC` decides whether the program is dynamic, refusing one -that both uses C and has a module saying `link static`. Then it has +that both uses C and has a module saying `link static`. In a program +that uses C, it gives the entry point the C symbols the library needs, +each with the variable it goes in, found by name (`addCLink`): `exit` +for `Rt.cExit`, and, if the program has `Thread`, `pthread_create` and +`pthread_join` for `Thread.cCreate` and `Thread.cJoin`. Then it has gen's `entryPoint` write the entry point, and `Link.markLive` mark what it reaches. The program is `proc main*: INT`; where raz is, for the library beside it, it reads from `/proc/self/exe`, and the version it prints is @@ -1487,16 +1491,20 @@ ends the program with status 2, after the procedures registered with `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 of their own: `Rt`'s for the main thread, one in each `Thread` - thread's mapping. C's threads have none, so a stack overflow in one - ends the program by the signal with no message. + thread's mapping, whether clone or C made the thread. Threads a + program makes with C's `pthread_create` itself have none, so a stack + overflow in one ends the program by the signal with no message. - **Locks.** `Heap` and `Out` lock with `Lock`, a spinlock that yields, since they serve C's threads and `Thread`'s alike; `Thread.Mutex` is a futex. - **C.** A program uses C if a module it uses has an `extern` or a `link` of a library. C's symbols are reached through quadwords the loader fills in (GLOB_DAT), with no PLT; the executable exports - nothing. Its entry point stores C's `exit` in `Rt.cExit` before any - module body runs. + nothing. Its entry point stores C's `exit` in `Rt.cExit`, and, if + the program has `Thread`, `pthread_create` and `pthread_join` in + `Thread.cCreate` and `Thread.cJoin`, before any module body runs: + `Thread` then makes C's threads, which C's library knows of, and + uses no C itself. - **Frames.** A deferred statement's slots stay reserved for the rest of its procedure. A generic instance first used in a procedure, or in a module's body, is made before that code begins (`instancesFor`). diff --git a/lib/Thread.linux-x86_64.raz b/lib/Thread.linux-x86_64.raz index 08ee50e..b8ed8f3 100644 --- a/lib/Thread.linux-x86_64.raz +++ b/lib/Thread.linux-x86_64.raz @@ -1,14 +1,21 @@ -(* Threads for programs without C, made with the clone system call. +(* Threads, made with the clone system call in a program without C. Each runs on a stack of its own, mmaped, with a page below it that can't be touched, so that running off the stack stops the program rather than spoiling what lies below; above it, a little stack for Rt's signal handlers, so that they can say so. join waits on the thread's id, which the kernel clears, and wakes it for, when the - thread ends; a Mutex sleeps on a futex. A program with C threads - uses C's, through pthread_create, and so can't use these. *) + thread ends; a Mutex sleeps on a futex. + + A program that uses C makes them with C's pthread_create instead, + since C keeps things of each thread's own, errno and its buffers, + that a thread clone makes wouldn't have. The compiler's entry point + finds cCreate and cJoin by name and stores C's calls in them, as it + stores C's exit in Rt.cExit; so this module uses no C itself, and + serves both kinds of program. C's thread has a stack and a guard + page of C's; the mapping here holds only the handlers' stack and + the Thread. *) module Thread; import Sys, Rt; -link static; const Failed*: ERROR = "can't start a thread"; @@ -25,7 +32,7 @@ type (* A thread, as spawn gives it, until join. *) Thread* = record - tid: INT; (* the kernel's id, until the thread ends *) + tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *) base: ^U8; (* its stack's mapping, the guard page first *) size: INT; fn: proc(arg: ^U8); (* what it runs, and with what *) @@ -37,6 +44,12 @@ free, 1 taken, 2 taken with a thread waiting. *) Mutex* = record state: INT; end; +var + (* C's pthread_create and pthread_join, in a program that uses C; + nil in one that doesn't *) + cCreate: proc(t: ^UINT; attr: ^U8; start: proc(arg: ^U8): ^U8; arg: ^U8): I32; + cJoin: proc(t: UINT; result: ^^U8): I32; + (* clone(flags, stack, tid, tid), the new thread starting with fn(arg) on stack and ending, when fn returns, with the exit system call. fn and arg go on the new stack first, since the new thread has no @@ -80,19 +93,21 @@ t^.fn(t^.arg); end; +(* Where a thread C makes starts, as pthread_create calls it *) +proc cRun(arg: ^U8): ^U8; +begin + run(arg); + return nil; +end; + (* Starts p(arg) in a new thread. *) proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); -var base: ^U8; t: ^Thread; size: INT; +var base: ^U8; t: ^Thread; size: INT; tid: UINT; begin size := Page + Stack + Alt + Page; + if cCreate # nil then size := Alt + Page; end; (* C gives the stack *) base := Sys.mmap(size); if base = nil then return nil, Failed; end; - (* mprotect: the guard page, no access; it fails when the process - has as many mappings as it may *) - if SYSCALL(10, base, Page, 0) < 0 then - Sys.munmap(base, size); - return nil, Failed; - end; (* from the top: the Thread in the last page, the handlers' stack, then the thread's, starting on a page *) t := ^Thread(base + size - Page); @@ -100,7 +115,18 @@ t^.size := size; t^.fn := p; t^.arg := arg; - if start(Flags, base + Page + Stack, ADR(t^.tid), ADR(t^.tid), run, ^U8(t)) < 0 then + if cCreate # nil then + if cCreate(ADR(tid), nil, cRun, ^U8(t)) # 0 then + Sys.munmap(base, size); + return nil, Failed; + end; + t^.tid := INT(tid); + return t, nil; + end; + (* mprotect: the guard page, no access; it fails when the process + has as many mappings as it may *) + if (SYSCALL(10, base, Page, 0) < 0) + or (start(Flags, base + Page + Stack, ADR(t^.tid), ADR(t^.tid), run, ^U8(t)) < 0) then Sys.munmap(base, size); return nil, Failed; end; @@ -111,11 +137,15 @@ proc join*(t: ^Thread); var tid: ^VOLATILE INT; v: INT; base: ^U8; size: INT; begin - tid := ADR(t^.tid); - v := tid^; - while v # 0 do - _ := futex(ADR(t^.tid), FutexWait, v); + if cJoin # nil then + _ := cJoin(UINT(t^.tid), nil); + else + tid := ADR(t^.tid); v := tid^; + while v # 0 do + _ := futex(ADR(t^.tid), FutexWait, v); + v := tid^; + end; end; base := t^.base; size := t^.size; diff --git a/src/Gen.raz b/src/Gen.raz index 618dd5b..3cd350b 100644 --- a/src/Gen.raz +++ b/src/Gen.raz @@ -89,7 +89,8 @@ CodeMark* = record pc*, fixups*, lines*, said*, fails*: INT; end; (* For entryPoint: the address of chunk, put in the variable whose - chunk is into before the program starts *) + chunk is into before the program starts; or, for C's symbols, the + address in chunk, the quadword the loader fills in *) Link* = record chunk*, into*: INT; end; CallInfo* = record @@ -2365,15 +2366,18 @@ main; and ends with Rt.exit, its status main's or 0. A dynamic program starts as C's crt1 does, through __libc_start_main(main, argc, argv, nil, nil, rtld_fini, stack_end), with rsp aligned; its main first - gives Rt C's exit, in cExit. + gives the library what it needs of C: Rt C's exit, in cExit, and + Thread pthread_create and pthread_join. dynamic: the program uses C. first: the first module, whose chain gives the order of the bodies. mainObj: the program's main procedure, or - nil. cExit and exitProc: the chunks of Rt's cExit and exit. The + nil. The first numCLinks of cLinks: C's symbols, each loaded from the + quadword the loader fills in and stored in a variable of the + library's, in a program that uses C. exitProc: the chunk of Rt's exit. The first numLinks of links: the addresses Rt is given, of what the program has: the places, and the profiler and calls; what it isn't given, nothing reaches, and the linker leaves out. *) -proc entryPoint*(dynamic: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cExit, exitProc: INT; links: []Link; numLinks: INT): INT; +proc entryPoint*(dynamic: BOOL; first: ^Sym.Module; mainObj: ^Sym.Obj; cLinks: []Link; numCLinks, exitProc: INT; links: []Link; numLinks: INT): INT; var entry, start, i: INT; m: ^Sym.Module; begin entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start")); @@ -2399,8 +2403,10 @@ X.push(RBP); (* aligns the stack again *) X.store(X.memRip(argcChunk, 0), RDI, 8); X.store(X.memRip(argvChunk, 0), RSI, 8); - X.load(RAX, X.memRip(Chunk.externChunk(internStr("exit")), 0), 8, false); - X.store(X.memRip(cExit, 0), RAX, 8); + for i := 0 to numCLinks - 1 do + X.load(RAX, X.memRip(cLinks[i].chunk, 0), 8, false); + X.store(X.memRip(cLinks[i].into, 0), RAX, 8); + end; else start := entry; Chunk.beginChunk(entry); diff --git a/src/Raz.raz b/src/Raz.raz index 6145e75..61bb76e 100644 --- a/src/Raz.raz +++ b/src/Raz.raz @@ -37,6 +37,8 @@ var links: [4]Gen.Link; numLinks: INT; + cLinks: [3]Gen.Link; (* C's symbols the entry point gives the library, in a program that uses C *) + numCLinks: INT; proc say1(f, a: []U8): []U8; begin @@ -108,11 +110,21 @@ Out.flush(); end; -proc rtObj(rt: ^Sym.Module; n: []U8): ^Sym.Obj; +(* The module named n, or nil if the program has none *) +proc moduleNamed(n: []U8): ^Sym.Module; +var m: ^Sym.Module; +begin + m := Parse.firstModule; + while (m # nil) and (Scan.nameOf(m^.name) # n) do m := m^.next; end; + return m; +end; + +(* What the library module m, found by name, declares as n *) +proc objIn(m: ^Sym.Module; n: []U8): ^Sym.Obj; var o: ^Sym.Obj; begin - o := Sym.find(rt^.scope, internStr(n)); - if o = nil then Diag.fatal(say1("Rt has no {}", n)); end; + o := Sym.find(m^.scope, internStr(n)); + if o = nil then Diag.fatal(Diag.say("{} has no {}", [Fmt.str(Scan.nameOf(m^.name)), Fmt.str(n)])); end; return o; end; @@ -124,6 +136,14 @@ INC(numLinks); end; +(* The library is given C's symbol sym, in its variable v *) +proc addCLink(sym: []U8; v: ^Sym.Obj); +begin + cLinks[numCLinks].chunk := Chunk.externChunk(internStr(sym)); + cLinks[numCLinks].into := v^.chunk; + INC(numCLinks); +end; + (* s as a C string, in memory that is never freed *) proc cstr(s: []U8): ^U8; var p: ^U8; @@ -138,8 +158,8 @@ var path, outPath, s: []U8; dir: []U8; i, first, entry, fd, last, errno: INT; - m, rt: ^Sym.Module; mainObj: ^Sym.Obj; - run, noProfile, listing: BOOL; err: ERROR; + m, rt, thread: ^Sym.Module; mainObj: ^Sym.Obj; + run, noProfile, listing, dynamic: BOOL; err: ERROR; begin path := ""; outPath := ""; @@ -210,8 +230,7 @@ if Diag.errors > 0 then Diag.finish(); end; use(m); - rt := Parse.firstModule; - while (rt # nil) and (Scan.nameOf(rt^.name) # "Rt") do rt := rt^.next; end; + rt := moduleNamed("Rt"); if rt # nil then use(rt); end; mainObj := Sym.find(m^.scope, internStr("main")); @@ -234,13 +253,26 @@ if not Elf.stripped then Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1)); Chunk.setData(Link.placesChunk, nil, 0, 8); - addLink(Link.placesChunk, rtObj(rt, "places")); - addLink(rtObj(rt, "calls")^.chunk, rtObj(rt, "teller")); - addLink(rtObj(rt, "place")^.chunk, rtObj(rt, "placer")); - if not noProfile then addLink(rtObj(rt, "startProfile")^.chunk, rtObj(rt, "profiler")); end; + addLink(Link.placesChunk, objIn(rt, "places")); + addLink(objIn(rt, "calls")^.chunk, objIn(rt, "teller")); + addLink(objIn(rt, "place")^.chunk, objIn(rt, "placer")); + if not noProfile then addLink(objIn(rt, "startProfile")^.chunk, objIn(rt, "profiler")); end; + end; + (* what a program that uses C is given of C: Rt its exit, and + Thread, if the program has it, the calls that make and join C's + threads, so that the C library knows of them *) + dynamic := usesC(); + numCLinks := 0; + if dynamic then + addCLink("exit", objIn(rt, "cExit")); + thread := moduleNamed("Thread"); + if (thread # nil) and thread^.used then + addCLink("pthread_create", objIn(thread, "cCreate")); + addCLink("pthread_join", objIn(thread, "cJoin")); + end; end; - entry := Gen.entryPoint(usesC(), Parse.firstModule, mainObj, rtObj(rt, "cExit")^.chunk, - rtObj(rt, "exit")^.chunk, links[0..numLinks], numLinks); + entry := Gen.entryPoint(dynamic, Parse.firstModule, mainObj, cLinks[0..numCLinks], numCLinks, + objIn(rt, "exit")^.chunk, links[0..numLinks], numLinks); Link.markLive(entry); if not Parse.HasReals then diff --git a/test/doc/Thread.expect b/test/doc/Thread.expect index b6449b6..c9c52cb 100644 --- a/test/doc/Thread.expect +++ b/test/doc/Thread.expect @@ -1,17 +1,25 @@ module Thread; - Threads for programs without C, made with the clone system call. + Threads, made with the clone system call in a program without C. Each runs on a stack of its own, mmaped, with a page below it that can't be touched, so that running off the stack stops the program rather than spoiling what lies below; above it, a little stack for Rt's signal handlers, so that they can say so. join waits on the thread's id, which the kernel clears, and wakes it for, when the - thread ends; a Mutex sleeps on a futex. A program with C threads - uses C's, through pthread_create, and so can't use these. + thread ends; a Mutex sleeps on a futex. + + A program that uses C makes them with C's pthread_create instead, + since C keeps things of each thread's own, errno and its buffers, + that a thread clone makes wouldn't have. The compiler's entry point + finds cCreate and cJoin by name and stores C's calls in them, as it + stores C's exit in Rt.cExit; so this module uses no C itself, and + serves both kinds of program. C's thread has a stack and a guard + page of C's; the mapping here holds only the handlers' stack and + the Thread. const Failed*: ERROR = "can't start a thread"; type Thread* = record - tid: INT; (* the kernel's id, until the thread ends *) + tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *) base: ^U8; (* its stack's mapping, the guard page first *) size: INT; fn: proc(arg: ^U8); (* what it runs, and with what *) diff --git a/test/err/ThreadC.err b/test/err/ThreadC.err deleted file mode 100644 index 12e417e..0000000 --- a/test/err/ThreadC.err +++ /dev/null @@ -1,8 +0,0 @@ -test/err/ThreadC.raz:2:8: warning: module Thread is imported but never used - 2 | import Thread, C; - | ^^^^^^ -test/err/ThreadC.raz:2:16: warning: module C is imported but never used - 2 | import Thread, C; - | ^ -raz: 2 warnings -raz: Thread says link static, but C uses C diff --git a/test/err/ThreadC.raz b/test/err/ThreadC.raz deleted file mode 100644 index f3794b8..0000000 --- a/test/err/ThreadC.raz +++ /dev/null @@ -1,8 +0,0 @@ -module ThreadC; -import Thread, C; - -proc main*; -begin -end; - -end ThreadC. diff --git a/test/run/SpawnC.expect b/test/run/SpawnC.expect new file mode 100644 index 0000000..4269e12 --- /dev/null +++ b/test/run/SpawnC.expect @@ -0,0 +1,201 @@ +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +the same line from every thread +160000 160000 diff --git a/test/run/SpawnC.raz b/test/run/SpawnC.raz new file mode 100644 index 0000000..8b4b53b --- /dev/null +++ b/test/run/SpawnC.raz @@ -0,0 +1,52 @@ +(* Threads in a program that uses C, which Thread makes with C's + pthread_create: a counter under a Mutex, the heap and Out used from + every thread at once, and join. *) +module SpawnC; +import Out, Thread, Heap, Mem, C; + +const + Workers = 8; + Rounds = 20000; + +type Node = record next: ^Node; n: I64; end; + +var + m: Thread.Mutex; + count: I64; atomic: INT; + +proc work(arg: ^U8); +var i: I64; p, list: ^Node; +begin + list := nil; + for i := 1 to Rounds do + Thread.lock(ADR(m)); + INC(count); + Thread.unlock(ADR(m)); + _ := XADD(ADR(atomic), 1); + NEW(p, Heap.heap); + p^.next := list; + list := p; + if i mod 4 = 0 then + while list # nil do + p := list^.next; + Mem.free(Heap.heap, ^U8(list)); + list := p; + end; + end; + end; + for i := 1 to 25 do Out.str("the same line from every thread\n"); end; +end; + +proc main*; +var t: [Workers]^Thread.Thread; i: I64; err: ERROR; +begin + if C.getpid() <= 0 then return; end; + for i := 0 to Workers - 1 do + t[i], err := Thread.spawn(work, nil); + if err # nil then Out.str(MSG(err)); Out.ln; return; end; + end; + for i := 0 to Workers - 1 do Thread.join(t[i]); end; + Out.int(count); Out.char(' '); Out.int(atomic); Out.ln; +end; + +end SpawnC. diff --git a/test/run/ThreadOverflow.stderr b/test/run/ThreadOverflow.stderr index c3884f5..8371e72 100644 --- a/test/run/ThreadOverflow.stderr +++ b/test/run/ThreadOverflow.stderr @@ -3,4 +3,4 @@ in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926 and 1926 more the same in ThreadOverflow.work at test/run/ThreadOverflow.raz:15:5: _arg = nil - in Thread.run at lib/Thread.linux-x86_64.raz:80:5: arg = 0x?, t = 0x? + in Thread.run at lib/Thread.linux-x86_64.raz:93:5: arg = 0x?, t = 0x? diff --git a/test/run/ThreadOverflowC.expect b/test/run/ThreadOverflowC.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/ThreadOverflowC.raz b/test/run/ThreadOverflowC.raz new file mode 100644 index 0000000..004f110 --- /dev/null +++ b/test/run/ThreadOverflowC.raz @@ -0,0 +1,27 @@ +module ThreadOverflowC; +(* A stack overflow in a thread of a program that uses C is told as + one in the main thread is: Thread gives C's thread a stack for the + signal handlers. One frame bigger than any stack C gives a thread. *) +import Thread, C; + +proc big: I64; +var pad: [64 * 1024 * 1024]U8; +begin + pad[0] := 1; + return I64(pad[0]); +end; + +proc work(_arg: ^U8); +begin + _ := big(); +end; + +proc main*; +var t: ^Thread.Thread; e: ERROR; +begin + _ := C.getpid(); + t, e := Thread.spawn(work, nil); + if e = nil then Thread.join(t); end; +end; + +end ThreadOverflowC. diff --git a/test/run/ThreadOverflowC.status b/test/run/ThreadOverflowC.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/ThreadOverflowC.status @@ -0,0 +1 @@ +2 diff --git a/test/run/ThreadOverflowC.stderr b/test/run/ThreadOverflowC.stderr new file mode 100644 index 0000000..70a1963 --- /dev/null +++ b/test/run/ThreadOverflowC.stderr @@ -0,0 +1,5 @@ +test/run/ThreadOverflowC.raz:7:1: stopped by a stack overflow + in ThreadOverflowC.big at test/run/ThreadOverflowC.raz:7:1 + in ThreadOverflowC.work at test/run/ThreadOverflowC.raz:16:5: _arg = nil + in Thread.run at lib/Thread.linux-x86_64.raz:93:5: arg = 0x?, t = 0x? + in Thread.cRun at lib/Thread.linux-x86_64.raz:99:5: arg = 0x?