raz / commit 716e2b8e

commit
716e2b8e4635f86f8ad415ccf1cfed38dd2ff22d
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
c06e3bdb
Thread.signal: a signal to one thread

Sys.kill signals the process, and the kernel gives the signal to any
thread that doesn't block it, so one thread couldn't break into
another's wait. signal(t, sig) is tgkill, with or without C, since
pthread_kill is tgkill with C's id for the thread: without C, the id is
the one clone gave, which the kernel clears as the thread ends; with C,
the thread notes its own in ktid as it starts and marks it -1 as it
ends, and signal waits for a thread that hasn't noted it yet. An ended
thread is -3 (ESRCH). Tested in ThreadSignal and ThreadSignalC, and
over C's Sys in test/libc; ThreadOverflow's traces follow the lines.
Met in arno's screen; its proposal is removed.

patch · log

12 files changed, +207 -32

PROPOSALS.md modified +0 -27

@@ -601,30 +601,3 @@
601601
602602 Met in mosaico's `Mconfig`, where `MenuAction.run` and a status block's
603603 kind `run` met; the block's kind was renamed `command`.
604-
605-## A signal to one thread
606-
607-`Sys.kill` signals a process, and the kernel gives the signal to any
608-of its threads that doesn't block it. A program whose thread waits in
609-a system call, a read or a `Sys.ready`, has no way to break into that
610-wait from another thread: the signal may land on the thread that sent
611-it, or on a third.
612-
613-The proposal, in the library:
614-
615-```
616-proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone; 0, or minus an errno *)
617-```
618-
619-in `Thread`, beside `join`: `tgkill(getpid(), t^.tid, sig)` in a
620-program without C, `pthread_kill(t^.tid, sig)` in one with C, as
621-`Thread` already keeps either id in `tid`. With `Sys.handle(sig)` in
622-the program, the wait the thread is in fails with -4, as `^C` makes a
623-wait fail on the main thread today. It breaks no program, so it could
624-come with any release, not only a thaw.
625-
626-Met in arno's screen: a turn runs on a thread of its own, reading the
627-model's reply with rnet. The human's `C-c` is a key on the loop's
628-thread, which can only set a flag that the turn reads after each read
629-of the reply; a server that says nothing for a minute, as a model does
630-while it thinks, keeps the turn going for that minute.

doc/raz-lib.md (history) modified +10 -1

@@ -15,7 +15,7 @@
1515 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
1616 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
1717 | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike |
18-| `Thread` | threads, `spawn` and `join`, and `Mutex`, in any program; in one that uses C, made by C's `pthread_create` |
18+| `Thread` | threads, `spawn`, `join` and `signal`, and `Mutex`, in any program; in one that uses C, made by C's `pthread_create` |
1919 | `Mem` | `Allocator`, the interface of every allocator, and pages from the system |
2020 | `Heap` | a general allocator, `Heap.heap`, safe to call from several threads; freeing a block twice, or one it never gave out, stops the program |
2121 | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator |
@@ -100,6 +100,7 @@
100100 Mutex* = record ... end; (* zero is free *)
101101 proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *)
102102 proc join*(t: ^Thread); (* waits for t to end, then frees its stack *)
103+proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone: 0, or minus an errno *)
103104 proc lock*(m: ^Mutex);
104105 proc unlock*(m: ^Mutex);
105106 ```
@@ -112,6 +113,14 @@
112113 `Thread` makes its threads with C's `pthread_create`, so that C's
113114 library knows of them; in one that doesn't, with the system's own
114115 calls. The program is the same either way.
116+
117+`Sys.kill` signals a program, and the system gives the signal to any
118+of its threads; `signal` gives it to `t` alone, until `t` is joined,
119+and is -3 (ESRCH) once `t` has ended. With `Sys.handle(sig)`, a call
120+`t` waits in, a `read` or a `ready`, fails with -4, so that one thread
121+can break into another's wait, as `^C` breaks into the main thread's.
122+A signal sent before `t` begins its wait is only noted, so a program
123+sends it again until `t` says it has stopped.
115124
116125 ```
117126 module Out;

lib/Thread.linux-x86_64.raz (history) modified +29 -1

@@ -33,6 +33,7 @@
3333 (* A thread, as spawn gives it, until join. *)
3434 Thread* = record
3535 tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *)
36+ ktid: INT; (* in a C thread, the kernel's id: 0 until it starts, -1 once it ends *)
3637 base: ^U8; (* its stack's mapping, the guard page first *)
3738 size: INT;
3839 fn: proc(arg: ^U8); (* what it runs, and with what *)
@@ -93,10 +94,16 @@
9394 t^.fn(t^.arg);
9495 end;
9596
96-(* Where a thread C makes starts, as pthread_create calls it *)
97+(* Where a thread C makes starts, as pthread_create calls it. C keeps
98+ the kernel's id of its thread to itself, so the thread notes its
99+ own, for signal, and marks it gone as it ends. *)
97100 proc cRun(arg: ^U8): ^U8;
101+var t: ^Thread;
98102 begin
103+ t := ^Thread(arg);
104+ _ := XCHG(ADR(t^.ktid), SYSCALL(186)); (* gettid *)
99105 run(arg);
106+ _ := XCHG(ADR(t^.ktid), -1);
100107 return nil;
101108 end;
102109
@@ -150,6 +157,27 @@
150157 base := t^.base;
151158 size := t^.size;
152159 Sys.munmap(base, size);
160+end;
161+
162+(* Sends the signal sig to t alone, not to any thread of the program
163+ as Sys.kill does: 0, or minus an errno, -3 (ESRCH) if t has ended.
164+ With Sys.handle(sig), a system call t waits in fails with -4, so
165+ another thread can break into a read. tgkill, in a program with C
166+ too, since pthread_kill is tgkill with C's id for the thread: a C
167+ thread that hasn't noted its id yet is waited for. *)
168+proc signal*(t: ^Thread; sig: INT): INT;
169+var tid: ^VOLATILE INT; k: INT;
170+begin
171+ if cCreate # nil then
172+ tid := ADR(t^.ktid);
173+ k := tid^;
174+ while k = 0 do Sys.yield; k := tid^; end;
175+ else
176+ tid := ADR(t^.tid);
177+ k := tid^;
178+ end;
179+ if k <= 0 then return -3; end;
180+ return SYSCALL(234, Sys.pid(), k, sig); (* tgkill *)
153181 end;
154182
155183 (* Takes m, sleeping while another thread holds it. *)

test/doc/Thread.expect (history) modified +9 -0

@@ -20,6 +20,7 @@
2020
2121 type Thread* = record
2222 tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *)
23+ ktid: INT; (* in a C thread, the kernel's id: 0 until it starts, -1 once it ends *)
2324 base: ^U8; (* its stack's mapping, the guard page first *)
2425 size: INT;
2526 fn: proc(arg: ^U8); (* what it runs, and with what *)
@@ -36,6 +37,14 @@
3637
3738 proc join*(t: ^Thread);
3839 Waits for t to end, then frees its stack.
40+
41+proc signal*(t: ^Thread; sig: INT): INT;
42+ Sends the signal sig to t alone, not to any thread of the program
43+ as Sys.kill does: 0, or minus an errno, -3 (ESRCH) if t has ended.
44+ With Sys.handle(sig), a system call t waits in fails with -4, so
45+ another thread can break into a read. tgkill, in a program with C
46+ too, since pthread_kill is tgkill with C's id for the thread: a C
47+ thread that hasn't noted its id yet is waited for.
3948
4049 proc lock*(m: ^Mutex);
4150 Takes m, sleeping while another thread holds it.

test/libc/ThreadSignalC.expect (history) added +2 -0

@@ -0,0 +1,2 @@
1+read -4
2+ended -3

test/libc/ThreadSignalC.raz (history) added +51 -0

@@ -0,0 +1,51 @@
1+module ThreadSignalC;
2+(* Thread.signal breaks into a read one thread waits in, the signal
3+ handled: the read fails with -4. A thread that has ended, not yet
4+ joined, is -3. ThreadSignal is the same, without C; this copy runs over
5+ the C library's Sys. *)
6+import Thread, Sys, Fmt, C;
7+
8+var
9+ r, got, done: INT;
10+
11+proc reader(_arg: ^U8);
12+var buf: [16]U8;
13+begin
14+ got := Sys.read(r, buf);
15+ _ := XCHG(ADR(done), 1);
16+end;
17+
18+proc quick(_arg: ^U8);
19+begin
20+end;
21+
22+proc main*;
23+var t: ^Thread.Thread; e: ERROR; w, tries, k: INT;
24+begin
25+ if C.getpid() <= 0 then return; end; (* C used, so that Thread makes C's threads *)
26+ r, w := Sys.pipe();
27+ if Sys.handle(Sys.sigUsr1) # 0 then Fmt.print("no handle\n", []); return; end;
28+ t, e := Thread.spawn(reader, nil);
29+ if e # nil then Fmt.print("no thread\n", []); return; end;
30+ (* again until it lands: the first may come before the read begins *)
31+ while (XADD(ADR(done), 0) = 0) and (tries < 1000) do
32+ k := Thread.signal(t, Sys.sigUsr1);
33+ if k # 0 then Fmt.print("signal: {}\n", [Fmt.int(k)]); end;
34+ Sys.sleep(5);
35+ INC(tries);
36+ end;
37+ Thread.join(t);
38+ Fmt.print("read {}\n", [Fmt.int(got)]);
39+ t, e := Thread.spawn(quick, nil);
40+ tries := 0;
41+ repeat
42+ k := Thread.signal(t, 0);
43+ Sys.sleep(1);
44+ INC(tries);
45+ until (k # 0) or (tries = 1000);
46+ Thread.join(t);
47+ Fmt.print("ended {}\n", [Fmt.int(k)]);
48+ Sys.close(w);
49+end;
50+
51+end ThreadSignalC.

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:93:5: arg = 0x?, t = 0x?
6+ in Thread.run at lib/Thread.linux-x86_64.raz:94:5: arg = 0x?, t = 0x?

test/run/ThreadOverflowC.stderr (history) modified +2 -2

@@ -1,5 +1,5 @@
11 test/run/ThreadOverflowC.raz:7:1: stopped by a stack overflow
22 in ThreadOverflowC.big at test/run/ThreadOverflowC.raz:7:1
33 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?
4+ in Thread.run at lib/Thread.linux-x86_64.raz:94:5: arg = 0x?, t = 0x?
5+ in Thread.cRun at lib/Thread.linux-x86_64.raz:105:5: arg = 0x?, t = 0x?

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

@@ -0,0 +1,2 @@
1+read -4
2+ended -3

test/run/ThreadSignal.raz (history) added +49 -0

@@ -0,0 +1,49 @@
1+module ThreadSignal;
2+(* Thread.signal breaks into a read one thread waits in, the signal
3+ handled: the read fails with -4. A thread that has ended, not yet
4+ joined, is -3. ThreadSignalC is the same, over C. *)
5+import Thread, Sys, Fmt;
6+
7+var
8+ r, got, done: INT;
9+
10+proc reader(_arg: ^U8);
11+var buf: [16]U8;
12+begin
13+ got := Sys.read(r, buf);
14+ _ := XCHG(ADR(done), 1);
15+end;
16+
17+proc quick(_arg: ^U8);
18+begin
19+end;
20+
21+proc main*;
22+var t: ^Thread.Thread; e: ERROR; w, tries, k: INT;
23+begin
24+ r, w := Sys.pipe();
25+ if Sys.handle(Sys.sigUsr1) # 0 then Fmt.print("no handle\n", []); return; end;
26+ t, e := Thread.spawn(reader, nil);
27+ if e # nil then Fmt.print("no thread\n", []); return; end;
28+ (* again until it lands: the first may come before the read begins *)
29+ while (XADD(ADR(done), 0) = 0) and (tries < 1000) do
30+ k := Thread.signal(t, Sys.sigUsr1);
31+ if k # 0 then Fmt.print("signal: {}\n", [Fmt.int(k)]); end;
32+ Sys.sleep(5);
33+ INC(tries);
34+ end;
35+ Thread.join(t);
36+ Fmt.print("read {}\n", [Fmt.int(got)]);
37+ t, e := Thread.spawn(quick, nil);
38+ tries := 0;
39+ repeat
40+ k := Thread.signal(t, 0);
41+ Sys.sleep(1);
42+ INC(tries);
43+ until (k # 0) or (tries = 1000);
44+ Thread.join(t);
45+ Fmt.print("ended {}\n", [Fmt.int(k)]);
46+ Sys.close(w);
47+end;
48+
49+end ThreadSignal.

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

@@ -0,0 +1,2 @@
1+read -4
2+ended -3

test/run/ThreadSignalC.raz (history) added +50 -0

@@ -0,0 +1,50 @@
1+module ThreadSignalC;
2+(* Thread.signal breaks into a read one thread waits in, the signal
3+ handled: the read fails with -4. A thread that has ended, not yet
4+ joined, is -3. ThreadSignal is the same, without C. *)
5+import Thread, Sys, Fmt, C;
6+
7+var
8+ r, got, done: INT;
9+
10+proc reader(_arg: ^U8);
11+var buf: [16]U8;
12+begin
13+ got := Sys.read(r, buf);
14+ _ := XCHG(ADR(done), 1);
15+end;
16+
17+proc quick(_arg: ^U8);
18+begin
19+end;
20+
21+proc main*;
22+var t: ^Thread.Thread; e: ERROR; w, tries, k: INT;
23+begin
24+ if C.getpid() <= 0 then return; end; (* C used, so that Thread makes C's threads *)
25+ r, w := Sys.pipe();
26+ if Sys.handle(Sys.sigUsr1) # 0 then Fmt.print("no handle\n", []); return; end;
27+ t, e := Thread.spawn(reader, nil);
28+ if e # nil then Fmt.print("no thread\n", []); return; end;
29+ (* again until it lands: the first may come before the read begins *)
30+ while (XADD(ADR(done), 0) = 0) and (tries < 1000) do
31+ k := Thread.signal(t, Sys.sigUsr1);
32+ if k # 0 then Fmt.print("signal: {}\n", [Fmt.int(k)]); end;
33+ Sys.sleep(5);
34+ INC(tries);
35+ end;
36+ Thread.join(t);
37+ Fmt.print("read {}\n", [Fmt.int(got)]);
38+ t, e := Thread.spawn(quick, nil);
39+ tries := 0;
40+ repeat
41+ k := Thread.signal(t, 0);
42+ Sys.sleep(1);
43+ INC(tries);
44+ until (k # 0) or (tries = 1000);
45+ Thread.join(t);
46+ Fmt.print("ended {}\n", [Fmt.int(k)]);
47+ Sys.close(w);
48+end;
49+
50+end ThreadSignalC.