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; import SYSTEM; var r, got, done: INT; proc reader(_arg: ^U8); var buf: [16]U8; begin got := Sys.read(r, buf); _ := XCHG(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(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.