From 716e2b8e4635f86f8ad415ccf1cfed38dd2ff22d Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:22:44 +0000 Subject: [PATCH] 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. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 6ad0db8..766f388 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -601,30 +601,3 @@ Met in mosaico's `Mconfig`, where `MenuAction.run` and a status block's kind `run` met; the block's kind was renamed `command`. - -## A signal to one thread - -`Sys.kill` signals a process, and the kernel gives the signal to any -of its threads that doesn't block it. A program whose thread waits in -a system call, a read or a `Sys.ready`, has no way to break into that -wait from another thread: the signal may land on the thread that sent -it, or on a third. - -The proposal, in the library: - -``` -proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone; 0, or minus an errno *) -``` - -in `Thread`, beside `join`: `tgkill(getpid(), t^.tid, sig)` in a -program without C, `pthread_kill(t^.tid, sig)` in one with C, as -`Thread` already keeps either id in `tid`. With `Sys.handle(sig)` in -the program, the wait the thread is in fails with -4, as `^C` makes a -wait fail on the main thread today. It breaks no program, so it could -come with any release, not only a thaw. - -Met in arno's screen: a turn runs on a thread of its own, reading the -model's reply with rnet. The human's `C-c` is a key on the loop's -thread, which can only set a flag that the turn reads after each read -of the reply; a server that says nothing for a minute, as a model does -while it thinks, keeps the turn going for that minute. diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 1006e63..6c126c8 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -15,7 +15,7 @@ | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads | | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C | | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike | -| `Thread` | threads, `spawn` and `join`, and `Mutex`, in any program; in one that uses C, made by C's `pthread_create` | +| `Thread` | threads, `spawn`, `join` and `signal`, and `Mutex`, in any program; in one that uses C, made by C's `pthread_create` | | `Mem` | `Allocator`, the interface of every allocator, and pages from the system | | `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 | | `Arena` | an allocator that frees everything at once; `ADR(arena.base)` is its allocator | @@ -100,6 +100,7 @@ Mutex* = record ... end; (* zero is free *) proc spawn*(p: proc(arg: ^U8); arg: ^U8): (^Thread, ERROR); (* p(arg) in a new thread *) proc join*(t: ^Thread); (* waits for t to end, then frees its stack *) +proc signal*(t: ^Thread; sig: INT): INT; (* sig sent to t alone: 0, or minus an errno *) proc lock*(m: ^Mutex); proc unlock*(m: ^Mutex); ``` @@ -112,6 +113,14 @@ `Thread` makes its threads with C's `pthread_create`, so that C's library knows of them; in one that doesn't, with the system's own calls. The program is the same either way. + +`Sys.kill` signals a program, and the system gives the signal to any +of its threads; `signal` gives it to `t` alone, until `t` is joined, +and is -3 (ESRCH) once `t` has ended. With `Sys.handle(sig)`, a call +`t` waits in, a `read` or a `ready`, fails with -4, so that one thread +can break into another's wait, as `^C` breaks into the main thread's. +A signal sent before `t` begins its wait is only noted, so a program +sends it again until `t` says it has stopped. ``` module Out; diff --git a/lib/Thread.linux-x86_64.raz b/lib/Thread.linux-x86_64.raz index b8ed8f3..8b736f7 100644 --- a/lib/Thread.linux-x86_64.raz +++ b/lib/Thread.linux-x86_64.raz @@ -33,6 +33,7 @@ (* A thread, as spawn gives it, until join. *) Thread* = record tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *) + ktid: INT; (* in a C thread, the kernel's id: 0 until it starts, -1 once it ends *) base: ^U8; (* its stack's mapping, the guard page first *) size: INT; fn: proc(arg: ^U8); (* what it runs, and with what *) @@ -93,10 +94,16 @@ t^.fn(t^.arg); end; -(* Where a thread C makes starts, as pthread_create calls it *) +(* Where a thread C makes starts, as pthread_create calls it. C keeps + the kernel's id of its thread to itself, so the thread notes its + own, for signal, and marks it gone as it ends. *) proc cRun(arg: ^U8): ^U8; +var t: ^Thread; begin + t := ^Thread(arg); + _ := XCHG(ADR(t^.ktid), SYSCALL(186)); (* gettid *) run(arg); + _ := XCHG(ADR(t^.ktid), -1); return nil; end; @@ -150,6 +157,27 @@ base := t^.base; size := t^.size; Sys.munmap(base, size); +end; + +(* Sends the signal sig to t alone, not to any thread of the program + as Sys.kill does: 0, or minus an errno, -3 (ESRCH) if t has ended. + With Sys.handle(sig), a system call t waits in fails with -4, so + another thread can break into a read. tgkill, in a program with C + too, since pthread_kill is tgkill with C's id for the thread: a C + thread that hasn't noted its id yet is waited for. *) +proc signal*(t: ^Thread; sig: INT): INT; +var tid: ^VOLATILE INT; k: INT; +begin + if cCreate # nil then + tid := ADR(t^.ktid); + k := tid^; + while k = 0 do Sys.yield; k := tid^; end; + else + tid := ADR(t^.tid); + k := tid^; + end; + if k <= 0 then return -3; end; + return SYSCALL(234, Sys.pid(), k, sig); (* tgkill *) end; (* Takes m, sleeping while another thread holds it. *) diff --git a/test/doc/Thread.expect b/test/doc/Thread.expect index c9c52cb..928b7c3 100644 --- a/test/doc/Thread.expect +++ b/test/doc/Thread.expect @@ -20,6 +20,7 @@ type Thread* = record tid: INT; (* the kernel's id, until the thread ends; or C's pthread_t *) + ktid: INT; (* in a C thread, the kernel's id: 0 until it starts, -1 once it ends *) base: ^U8; (* its stack's mapping, the guard page first *) size: INT; fn: proc(arg: ^U8); (* what it runs, and with what *) @@ -36,6 +37,14 @@ proc join*(t: ^Thread); Waits for t to end, then frees its stack. + +proc signal*(t: ^Thread; sig: INT): INT; + Sends the signal sig to t alone, not to any thread of the program + as Sys.kill does: 0, or minus an errno, -3 (ESRCH) if t has ended. + With Sys.handle(sig), a system call t waits in fails with -4, so + another thread can break into a read. tgkill, in a program with C + too, since pthread_kill is tgkill with C's id for the thread: a C + thread that hasn't noted its id yet is waited for. proc lock*(m: ^Mutex); Takes m, sleeping while another thread holds it. diff --git a/test/libc/ThreadSignalC.expect b/test/libc/ThreadSignalC.expect new file mode 100644 index 0000000..f0ec17a --- /dev/null +++ b/test/libc/ThreadSignalC.expect @@ -0,0 +1,2 @@ +read -4 +ended -3 diff --git a/test/libc/ThreadSignalC.raz b/test/libc/ThreadSignalC.raz new file mode 100644 index 0000000..90b018e --- /dev/null +++ b/test/libc/ThreadSignalC.raz @@ -0,0 +1,51 @@ +module ThreadSignalC; +(* Thread.signal breaks into a read one thread waits in, the signal + handled: the read fails with -4. A thread that has ended, not yet + joined, is -3. ThreadSignal is the same, without C; this copy runs over + the C library's Sys. *) +import Thread, Sys, Fmt, C; + +var + r, got, done: INT; + +proc reader(_arg: ^U8); +var buf: [16]U8; +begin + got := Sys.read(r, buf); + _ := XCHG(ADR(done), 1); +end; + +proc quick(_arg: ^U8); +begin +end; + +proc main*; +var t: ^Thread.Thread; e: ERROR; w, tries, k: INT; +begin + if C.getpid() <= 0 then return; end; (* C used, so that Thread makes C's threads *) + r, w := Sys.pipe(); + if Sys.handle(Sys.sigUsr1) # 0 then Fmt.print("no handle\n", []); return; end; + t, e := Thread.spawn(reader, nil); + if e # nil then Fmt.print("no thread\n", []); return; end; + (* again until it lands: the first may come before the read begins *) + while (XADD(ADR(done), 0) = 0) and (tries < 1000) do + k := Thread.signal(t, Sys.sigUsr1); + if k # 0 then Fmt.print("signal: {}\n", [Fmt.int(k)]); end; + Sys.sleep(5); + INC(tries); + end; + Thread.join(t); + Fmt.print("read {}\n", [Fmt.int(got)]); + t, e := Thread.spawn(quick, nil); + tries := 0; + repeat + k := Thread.signal(t, 0); + Sys.sleep(1); + INC(tries); + until (k # 0) or (tries = 1000); + Thread.join(t); + Fmt.print("ended {}\n", [Fmt.int(k)]); + Sys.close(w); +end; + +end ThreadSignalC. diff --git a/test/run/ThreadOverflow.stderr b/test/run/ThreadOverflow.stderr index 8371e72..f4468c0 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:93:5: arg = 0x?, t = 0x? + in Thread.run at lib/Thread.linux-x86_64.raz:94:5: arg = 0x?, t = 0x? diff --git a/test/run/ThreadOverflowC.stderr b/test/run/ThreadOverflowC.stderr index 70a1963..c74f721 100644 --- a/test/run/ThreadOverflowC.stderr +++ b/test/run/ThreadOverflowC.stderr @@ -1,5 +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? + in Thread.run at lib/Thread.linux-x86_64.raz:94:5: arg = 0x?, t = 0x? + in Thread.cRun at lib/Thread.linux-x86_64.raz:105:5: arg = 0x?, t = 0x? diff --git a/test/run/ThreadSignal.expect b/test/run/ThreadSignal.expect new file mode 100644 index 0000000..f0ec17a --- /dev/null +++ b/test/run/ThreadSignal.expect @@ -0,0 +1,2 @@ +read -4 +ended -3 diff --git a/test/run/ThreadSignal.raz b/test/run/ThreadSignal.raz new file mode 100644 index 0000000..3086f44 --- /dev/null +++ b/test/run/ThreadSignal.raz @@ -0,0 +1,49 @@ +module ThreadSignal; +(* Thread.signal breaks into a read one thread waits in, the signal + handled: the read fails with -4. A thread that has ended, not yet + joined, is -3. ThreadSignalC is the same, over C. *) +import Thread, Sys, Fmt; + +var + r, got, done: INT; + +proc reader(_arg: ^U8); +var buf: [16]U8; +begin + got := Sys.read(r, buf); + _ := XCHG(ADR(done), 1); +end; + +proc quick(_arg: ^U8); +begin +end; + +proc main*; +var t: ^Thread.Thread; e: ERROR; w, tries, k: INT; +begin + r, w := Sys.pipe(); + if Sys.handle(Sys.sigUsr1) # 0 then Fmt.print("no handle\n", []); return; end; + t, e := Thread.spawn(reader, nil); + if e # nil then Fmt.print("no thread\n", []); return; end; + (* again until it lands: the first may come before the read begins *) + while (XADD(ADR(done), 0) = 0) and (tries < 1000) do + k := Thread.signal(t, Sys.sigUsr1); + if k # 0 then Fmt.print("signal: {}\n", [Fmt.int(k)]); end; + Sys.sleep(5); + INC(tries); + end; + Thread.join(t); + Fmt.print("read {}\n", [Fmt.int(got)]); + t, e := Thread.spawn(quick, nil); + tries := 0; + repeat + k := Thread.signal(t, 0); + Sys.sleep(1); + INC(tries); + until (k # 0) or (tries = 1000); + Thread.join(t); + Fmt.print("ended {}\n", [Fmt.int(k)]); + Sys.close(w); +end; + +end ThreadSignal. diff --git a/test/run/ThreadSignalC.expect b/test/run/ThreadSignalC.expect new file mode 100644 index 0000000..f0ec17a --- /dev/null +++ b/test/run/ThreadSignalC.expect @@ -0,0 +1,2 @@ +read -4 +ended -3 diff --git a/test/run/ThreadSignalC.raz b/test/run/ThreadSignalC.raz new file mode 100644 index 0000000..e8a5caa --- /dev/null +++ b/test/run/ThreadSignalC.raz @@ -0,0 +1,50 @@ +module ThreadSignalC; +(* Thread.signal breaks into a read one thread waits in, the signal + handled: the read fails with -4. A thread that has ended, not yet + joined, is -3. ThreadSignal is the same, without C. *) +import Thread, Sys, Fmt, C; + +var + r, got, done: INT; + +proc reader(_arg: ^U8); +var buf: [16]U8; +begin + got := Sys.read(r, buf); + _ := XCHG(ADR(done), 1); +end; + +proc quick(_arg: ^U8); +begin +end; + +proc main*; +var t: ^Thread.Thread; e: ERROR; w, tries, k: INT; +begin + if C.getpid() <= 0 then return; end; (* C used, so that Thread makes C's threads *) + r, w := Sys.pipe(); + if Sys.handle(Sys.sigUsr1) # 0 then Fmt.print("no handle\n", []); return; end; + t, e := Thread.spawn(reader, nil); + if e # nil then Fmt.print("no thread\n", []); return; end; + (* again until it lands: the first may come before the read begins *) + while (XADD(ADR(done), 0) = 0) and (tries < 1000) do + k := Thread.signal(t, Sys.sigUsr1); + if k # 0 then Fmt.print("signal: {}\n", [Fmt.int(k)]); end; + Sys.sleep(5); + INC(tries); + end; + Thread.join(t); + Fmt.print("read {}\n", [Fmt.int(got)]); + t, e := Thread.spawn(quick, nil); + tries := 0; + repeat + k := Thread.signal(t, 0); + Sys.sleep(1); + INC(tries); + until (k # 0) or (tries = 1000); + Thread.join(t); + Fmt.print("ended {}\n", [Fmt.int(k)]); + Sys.close(w); +end; + +end ThreadSignalC.