raz / commit f871b244

commit
f871b24415a979367b1b016a42d719ef1b0fac00
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
8319dbc3
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.

patch · log

15 files changed, +425 -67

CLAUDE.md modified +7 -3

@@ -163,7 +163,10 @@
163163 - C symbols are reached through quadwords the loader fills in
164164 (GLOB_DAT), with no PLT; the executable exports nothing. In a C
165165 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.
167170 - Past an extern's parameters, a string goes as a C string and an
168171 integer constant as an `INT`.
169172 - A real constant is held as an F64; one typed `F32` holds a value an
@@ -177,8 +180,9 @@
177180 its prologue, and the program ends by `Rt.exit(2)`, after its
178181 `atExit` procedures, as a failed check does (report 2026-10.22; a
179182 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
182186 by the signal with no message.
183187 - The smallest value of a signed type divided by -1 stops the program
184188 (SIGFPE), as 6.1 says, `mod` as `div`; for a type narrower than 64

doc/compiler.md (history) modified +15 -7

@@ -18,12 +18,12 @@
1818 | `src/Sym.raz` | 684 | 555 | Types, objects, scopes, layout |
1919 | `src/Chunk.raz` | 354 | 272 | The compiled program: chunks, fixups, line notes |
2020 | `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 |
2222 | `src/Item.raz` | 1,763 | 1,458 | Items: types, constants, variables, operators, conversions, calls |
2323 | `src/Parse.raz` | 3,846 | 3,355 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 380 | 298 | Tree shaking, layout, patching, the places |
2525 | `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 |
2727
2828 Every file opens with a comment that says what it is, what it depends
2929 on and what it leaves out. Read those first; they are the map.
@@ -1395,7 +1395,11 @@
13951395 `Raz.raz` parses the arguments, calls `Parse.compileMain`, and works out what
13961396 the program uses: `use` marks the modules imported from the main module
13971397 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
13991403 gen's `entryPoint` write the entry point, and `Link.markLive` mark what it
14001404 reaches. The program is `proc main*: INT`; where raz is, for the library
14011405 beside it, it reads from `/proc/self/exe`, and the version it prints is
@@ -1487,16 +1491,20 @@
14871491 ends the program with status 2, after the procedures registered with
14881492 `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
14891493 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.
14921497 - **Locks.** `Heap` and `Out` lock with `Lock`, a spinlock that yields,
14931498 since they serve C's threads and `Thread`'s alike; `Thread.Mutex` is
14941499 a futex.
14951500 - **C.** A program uses C if a module it uses has an `extern` or a
14961501 `link` of a library. C's symbols are reached through quadwords the
14971502 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.
15001508 - **Frames.** A deferred statement's slots stay reserved for the rest
15011509 of its procedure. A generic instance first used in a procedure, or in
15021510 a module's body, is made before that code begins (`instancesFor`).

lib/Thread.linux-x86_64.raz (history) modified +47 -17

@@ -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.
22 Each runs on a stack of its own, mmaped, with a page below it that
33 can't be touched, so that running off the stack stops the program
44 rather than spoiling what lies below; above it, a little stack for
55 Rt's signal handlers, so that they can say so. join waits on the
66 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. *)
917 module Thread;
1018 import Sys, Rt;
11-link static;
1219
1320 const
1421 Failed*: ERROR = "can't start a thread";
@@ -25,7 +32,7 @@
2532 type
2633 (* A thread, as spawn gives it, until join. *)
2734 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 *)
2936 base: ^U8; (* its stack's mapping, the guard page first *)
3037 size: INT;
3138 fn: proc(arg: ^U8); (* what it runs, and with what *)
@@ -37,6 +44,12 @@
3744 free, 1 taken, 2 taken with a thread waiting. *)
3845 Mutex* = record state: INT; end;
3946
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+
4053 (* clone(flags, stack, tid, tid), the new thread starting with fn(arg)
4154 on stack and ending, when fn returns, with the exit system call.
4255 fn and arg go on the new stack first, since the new thread has no
@@ -80,19 +93,21 @@
8093 t^.fn(t^.arg);
8194 end;
8295
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+
83103 (* Starts p(arg) in a new thread. *)
84104 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;
86106 begin
87107 size := Page + Stack + Alt + Page;
108+ if cCreate # nil then size := Alt + Page; end; (* C gives the stack *)
88109 base := Sys.mmap(size);
89110 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;
96111 (* from the top: the Thread in the last page, the handlers' stack,
97112 then the thread's, starting on a page *)
98113 t := ^Thread(base + size - Page);
@@ -100,7 +115,18 @@
100115 t^.size := size;
101116 t^.fn := p;
102117 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
104130 Sys.munmap(base, size);
105131 return nil, Failed;
106132 end;
@@ -111,11 +137,15 @@
111137 proc join*(t: ^Thread);
112138 var tid: ^VOLATILE INT; v: INT; base: ^U8; size: INT;
113139 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);
118144 v := tid^;
145+ while v # 0 do
146+ _ := futex(ADR(t^.tid), FutexWait, v);
147+ v := tid^;
148+ end;
119149 end;
120150 base := t^.base;
121151 size := t^.size;

src/Gen.raz modified +12 -6

@@ -89,7 +89,8 @@
8989 CodeMark* = record pc*, fixups*, lines*, said*, fails*: INT; end;
9090
9191 (* 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 *)
9394 Link* = record chunk*, into*: INT; end;
9495
9596 CallInfo* = record
@@ -2365,15 +2366,18 @@
23652366 main; and ends with Rt.exit, its status main's or 0. A dynamic program
23662367 starts as C's crt1 does, through __libc_start_main(main, argc, argv,
23672368 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.
23692371
23702372 dynamic: the program uses C. first: the first module, whose chain
23712373 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
23732377 first numLinks of links: the addresses Rt is given, of what the program
23742378 has: the places, and the profiler and calls; what it isn't given,
23752379 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;
23772381 var entry, start, i: INT; m: ^Sym.Module;
23782382 begin
23792383 entry := Chunk.newChunk(Chunk.secText, -1, internStr("_start"));
@@ -2399,8 +2403,10 @@
23992403 X.push(RBP); (* aligns the stack again *)
24002404 X.store(X.memRip(argcChunk, 0), RDI, 8);
24012405 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;
24042410 else
24052411 start := entry;
24062412 Chunk.beginChunk(entry);

src/Raz.raz (history) modified +45 -13

@@ -37,6 +37,8 @@
3737 var
3838 links: [4]Gen.Link;
3939 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;
4042
4143 proc say1(f, a: []U8): []U8;
4244 begin
@@ -108,11 +110,21 @@
108110 Out.flush();
109111 end;
110112
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;
112124 var o: ^Sym.Obj;
113125 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;
116128 return o;
117129 end;
118130
@@ -124,6 +136,14 @@
124136 INC(numLinks);
125137 end;
126138
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+
127147 (* s as a C string, in memory that is never freed *)
128148 proc cstr(s: []U8): ^U8;
129149 var p: ^U8;
@@ -138,8 +158,8 @@
138158 var
139159 path, outPath, s: []U8; dir: []U8;
140160 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;
143163 begin
144164 path := "";
145165 outPath := "";
@@ -210,8 +230,7 @@
210230 if Diag.errors > 0 then Diag.finish(); end;
211231
212232 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");
215234 if rt # nil then use(rt); end;
216235
217236 mainObj := Sym.find(m^.scope, internStr("main"));
@@ -234,13 +253,26 @@
234253 if not Elf.stripped then
235254 Link.setPlaces(Chunk.newChunk(Chunk.secRodata, -1, -1));
236255 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;
241273 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);
244276
245277 Link.markLive(entry);
246278 if not Parse.HasReals then

test/doc/Thread.expect (history) modified +12 -4

@@ -1,17 +1,25 @@
11 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.
33 Each runs on a stack of its own, mmaped, with a page below it that
44 can't be touched, so that running off the stack stops the program
55 rather than spoiling what lies below; above it, a little stack for
66 Rt's signal handlers, so that they can say so. join waits on the
77 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.
1018
1119 const Failed*: ERROR = "can't start a thread";
1220
1321 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 *)
1523 base: ^U8; (* its stack's mapping, the guard page first *)
1624 size: INT;
1725 fn: proc(arg: ^U8); (* what it runs, and with what *)

test/err/ThreadC.err deleted +0 -8

@@ -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

test/err/ThreadC.raz deleted +0 -8

@@ -1,8 +0,0 @@
1-module ThreadC;
2-import Thread, C;
3-
4-proc main*;
5-begin
6-end;
7-
8-end ThreadC.

test/run/SpawnC.expect (history) added +201 -0

@@ -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

test/run/SpawnC.raz (history) added +52 -0

@@ -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.

test/run/ThreadOverflow.stderr (history) modified +1 -1

@@ -3,4 +3,4 @@
33 in ThreadOverflow.down at test/run/ThreadOverflow.raz:10:5: n = 1926
44 and 1926 more the same
55 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?

test/run/ThreadOverflowC.expect (history) added +0 -0

An empty file.

test/run/ThreadOverflowC.raz (history) added +27 -0

@@ -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.

test/run/ThreadOverflowC.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/ThreadOverflowC.stderr (history) added +5 -0

@@ -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?