From 701be1bcbf2b107f69dbb2812491d5fe4b76d88e Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 10:35:34 +0100 Subject: [PATCH] Report 2026-10.13: Sys writes without waiting Sys.setBlocking and Sys.readyFor, in both Sys's (system calls, and C's with C.fcntl): a read or write that would wait is EAGAIN at once, and readyFor waits for reading on some fds and room to write on others. readyOf is now readyFor with no writes. rat needs them to paste much into a program that writes back as it reads, by the owner's word. Sys's test fills a pipe without waiting and empties it. --- diff --git a/CLAUDE.md b/CLAUDE.md index 3dbfe36..84ad956 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -86,9 +86,10 @@ defect, and the library grows only by what breaks no program written for it. A wish for more, the owner's or yours, goes on the list in `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw, -by the owner's word: report 2026-10.12, which the compiler follows, -its parser reaching the machine only through `gen.pas`; its steps 5 -to 10 await the owner's review, and git has them. Keep +by the owner's word: report 2026-10.12; the compiler follows 2026-10.13, +whose `Sys` writes without waiting, for rat's pastes. Its parser +reaches the machine only through `gen.pas`; its steps 5 to 10 await +the owner's review, and git has them. Keep `doc/compiler.md`'s table of lines and its routine names true when the compiler changes. diff --git a/doc/raz.md b/doc/raz.md index 36def9a..ae26cb9 100644 --- a/doc/raz.md +++ b/doc/raz.md @@ -9,7 +9,7 @@ This report defines the language. It is short on purpose. Whatever it doesn't allow, raz doesn't have. -This is version 2026-10.12 of the report; section 12 says what each +This is version 2026-10.13 of the report; section 12 says what each version changed. It is a thaw the owner chose, for a language anyone can build again, and it breaks programs written for 2026-10.10. @@ -1281,6 +1281,8 @@ proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) proc ready*(fd: INT; ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) +proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *) +proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) @@ -1430,6 +1432,16 @@ most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36 (ENAMETOOLONG). +A program that both reads from a descriptor and writes much to it, as +a terminal writes what is pasted to its program while it reads what +the program writes back, must not wait on either: the program may be +waiting for its own writes to be read. `setBlocking(fd, false)` has a +read or a write on `fd` that would wait return -11 (EAGAIN) at once, +a write having written what there was room for; `readyFor` waits, as +`readyOf` does, for something to read on any of `reads` or for room to +write on any of `writes`. A descriptor in both is given once, when it +is ready for either; the 64 it looks at are `reads`, then `writes`. + **Str, Map, Sort, Math and Fmt** gain these: ``` @@ -1742,6 +1754,8 @@ language, not as bytes (section 8; appendix A). A program that stops says the calls under way and their variables, and `Rt.trace` says them on demand (sections 10 and 11). +- **2026-10.13.** `Sys` gains writing without waiting, and waiting for + room to write (section 11). ## Appendix A. What an implementation chooses diff --git a/lib/C.raz b/lib/C.raz index ebdd87c..3466099 100644 --- a/lib/C.raz +++ b/lib/C.raz @@ -32,6 +32,7 @@ extern proc cfmakeraw*(t: ^U8); extern proc ioctl*(fd: I32; request: UINT; ...): I32; extern proc poll*(fds: ^U8; n: UINT; ms: I32): I32; +extern proc fcntl*(fd, cmd: I32; ...): I32; extern proc socket*(domain, kind, protocol: I32): I32; extern proc connect*(fd: I32; addr: ^U8; len: U32): I32; extern proc send*(fd: I32; buf: ^U8; n: UINT; flags: I32): INT; diff --git a/lib/Sys.linux-x86_64.raz b/lib/Sys.linux-x86_64.raz index 8e0d099..dcc2030 100644 --- a/lib/Sys.linux-x86_64.raz +++ b/lib/Sys.linux-x86_64.raz @@ -568,22 +568,54 @@ return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *) end; -(* The first of fds that has something to read within ms milliseconds, - or that has reached its end or failed, so that a read won't wait; -1 - if none has. ms < 0 waits as long as it takes. At most 64 fds. *) -proc readyOf*(fds: []INT; ms: INT): INT; +(* The first of reads that has something to read within ms + milliseconds, or of writes that has room to write, or that has + reached its end or failed, so that a read or a write won't wait; -1 + if none has. ms < 0 waits as long as it takes. At most 64 fds in + all. *) +proc readyFor*(reads, writes: []INT; ms: INT): INT; var p: [64]Pollfd; n, i: INT; begin - n := LEN(fds); - if n > LEN(p) then n := LEN(p); end; - for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *) + n := 0; + for i := 0 to LEN(reads) - 1 do + if n < LEN(p) then + p[n] := {fd: I32(reads[i]), events: 1}; (* POLLIN *) + INC(n); + end; + end; + for i := 0 to LEN(writes) - 1 do + if n < LEN(p) then + p[n] := {fd: I32(writes[i]), events: 4}; (* POLLOUT *) + INC(n); + end; + end; if SYSCALL(7, ADR(p), n, ms) <= 0 then return -1; end; (* poll *) for i := 0 to n - 1 do - if p[i].revents # 0 then return fds[i]; end; + if p[i].revents # 0 then return INT(p[i].fd); end; end; return -1; end; +(* The first of fds that has something to read within ms milliseconds, + or that has reached its end or failed, so that a read won't wait; -1 + if none has. ms < 0 waits as long as it takes. At most 64 fds. *) +proc readyOf*(fds: []INT; ms: INT): INT; +begin + return readyFor(fds, [], ms); +end; + +(* Reads and writes on fd made to wait, as they do at first; or, if not + yes, to return -11 (EAGAIN) at once where they would wait. 0, or + minus an errno. *) +proc setBlocking*(fd: INT; yes: BOOL): INT; +var flags: INT; +begin + flags := SYSCALL(72, fd, 3, 0); (* fcntl F_GETFL *) + if flags < 0 then return flags; end; + if yes then flags := flags & ~INT(800H); else flags := flags | 800H; end; (* O_NONBLOCK *) + return SYSCALL(72, fd, 4, flags); (* F_SETFL *) +end; + (* The value of the environment variable name; "" if it isn't set. The environment follows the arguments and their nil. *) proc env*(name: []U8): []U8; @@ -927,6 +959,16 @@ if buf[0..n] # "x\n/tmp\n" then exit(2); end; r, st, c := wait(child, true); if (r # child) or (st # exited) or (c # 0) then exit(2); end; + + (* a pipe filled without waiting, then emptied: room to write again *) + r, w := pipe(); + if (w < 0) or (setBlocking(w, false) # 0) or (setBlocking(r, false) # 0) then exit(2); end; + repeat n := write(w, buf); until n < 0; + if (n # -11) or (readyFor([], [w], 0) # -1) or (readyFor([r], [w], 0) # r) then exit(2); end; + repeat n := read(r, buf); until n < 0; + if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end; + close(r); + close(w); child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); r, st, c := wait(child, false); diff --git a/lib/Sys.raz b/lib/Sys.raz index 1ffb6d2..01db7d5 100644 --- a/lib/Sys.raz +++ b/lib/Sys.raz @@ -519,23 +519,56 @@ return r; end; -(* The first of fds that has something to read within ms milliseconds, - or that has reached its end or failed, so that a read won't wait; -1 - if none has. ms < 0 waits as long as it takes. At most 64 fds. *) -proc readyOf*(fds: []INT; ms: INT): INT; +(* The first of reads that has something to read within ms + milliseconds, or of writes that has room to write, or that has + reached its end or failed, so that a read or a write won't wait; -1 + if none has. ms < 0 waits as long as it takes. At most 64 fds in + all. *) +proc readyFor*(reads, writes: []INT; ms: INT): INT; var p: [64]Pollfd; n, i: INT; r: I32; begin - n := LEN(fds); - if n > LEN(p) then n := LEN(p); end; - for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *) + n := 0; + for i := 0 to LEN(reads) - 1 do + if n < LEN(p) then + p[n] := {fd: I32(reads[i]), events: 1}; (* POLLIN *) + INC(n); + end; + end; + for i := 0 to LEN(writes) - 1 do + if n < LEN(p) then + p[n] := {fd: I32(writes[i]), events: 4}; (* POLLOUT *) + INC(n); + end; + end; repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *) if r <= 0 then return -1; end; for i := 0 to n - 1 do - if p[i].revents # 0 then return fds[i]; end; + if p[i].revents # 0 then return INT(p[i].fd); end; end; return -1; end; +(* The first of fds that has something to read within ms milliseconds, + or that has reached its end or failed, so that a read won't wait; -1 + if none has. ms < 0 waits as long as it takes. At most 64 fds. *) +proc readyOf*(fds: []INT; ms: INT): INT; +begin + return readyFor(fds, [], ms); +end; + +(* Reads and writes on fd made to wait, as they do at first; or, if not + yes, to return -11 (EAGAIN) at once where they would wait. 0, or + minus an errno. *) +proc setBlocking*(fd: INT; yes: BOOL): INT; +var flags: I32; +begin + flags := C.fcntl(I32(fd), 3, 0); (* F_GETFL *) + if flags < 0 then return failed(); end; + if yes then flags := flags & ~I32(800H); else flags := flags | 800H; end; (* O_NONBLOCK *) + if C.fcntl(I32(fd), 4, flags) < 0 then return failed(); end; (* F_SETFL *) + return 0; +end; + (* The value of the environment variable name; "" if it isn't set. *) proc env*(name: []U8): []U8; var a: [4096]U8; s: ^U8; n: INT; @@ -890,6 +923,16 @@ if buf[0..n] # "x\n/tmp\n" then exit(2); end; r, st, c := wait(child, true); if (r # child) or (st # exited) or (c # 0) then exit(2); end; + + (* a pipe filled without waiting, then emptied: room to write again *) + r, w := pipe(); + if (w < 0) or (setBlocking(w, false) # 0) or (setBlocking(r, false) # 0) then exit(2); end; + repeat n := write(w, buf); until n < 0; + if (n # -11) or (readyFor([], [w], 0) # -1) or (readyFor([r], [w], 0) # r) then exit(2); end; + repeat n := read(r, buf); until n < 0; + if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end; + close(r); + close(w); child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); r, st, c := wait(child, false); diff --git a/src/raz.pas b/src/raz.pas index bf308d0..2353658 100644 --- a/src/raz.pas +++ b/src/raz.pas @@ -32,7 +32,7 @@ { what git describe says, made by make: 0.3.0 at the tag v0.3.0, 0.3.0-5-g38d29a8 five commits past it } Version = {$I version.inc}; - Report = '2026-10.12'; { the version of doc/raz.md it follows } + Report = '2026-10.13'; { the version of doc/raz.md it follows } MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } MfdCloexec = 1; { an ELF still executes: the kernel opens it first }