raz / commit 701be1bc

commit
701be1bcbf2b107f69dbb2812491d5fe4b76d88e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4c342317
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.

patch · log

6 files changed, +122 -21

CLAUDE.md modified +4 -3

@@ -86,9 +86,10 @@
8686 defect, and the library grows only by what breaks no program written
8787 for it. A wish for more, the owner's or yours, goes on the list in
8888 `PLAN.md`, for a later thaw; it isn't built. v0.6.0 was such a thaw,
89-by the owner's word: report 2026-10.12, which the compiler follows,
90-its parser reaching the machine only through `gen.pas`; its steps 5
91-to 10 await the owner's review, and git has them. Keep
89+by the owner's word: report 2026-10.12; the compiler follows 2026-10.13,
90+whose `Sys` writes without waiting, for rat's pastes. Its parser
91+reaches the machine only through `gen.pas`; its steps 5 to 10 await
92+the owner's review, and git has them. Keep
9293 `doc/compiler.md`'s table of lines and its routine names true when the
9394 compiler changes.
9495

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

@@ -9,7 +9,7 @@
99 This report defines the language. It is short on purpose. Whatever it
1010 doesn't allow, raz doesn't have.
1111
12-This is version 2026-10.12 of the report; section 12 says what each
12+This is version 2026-10.13 of the report; section 12 says what each
1313 version changed. It is a thaw the owner chose, for a language anyone
1414 can build again, and it breaks programs written for 2026-10.10.
1515
@@ -1281,6 +1281,8 @@
12811281 proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *)
12821282 proc ready*(fd: INT; ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *)
12831283 proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
1284+proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
1285+proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *)
12841286
12851287 proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *)
12861288 proc workDir*: ([]U8, INT); (* the working directory, good until the next call *)
@@ -1430,6 +1432,16 @@
14301432 most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36
14311433 (ENAMETOOLONG).
14321434
1435+A program that both reads from a descriptor and writes much to it, as
1436+a terminal writes what is pasted to its program while it reads what
1437+the program writes back, must not wait on either: the program may be
1438+waiting for its own writes to be read. `setBlocking(fd, false)` has a
1439+read or a write on `fd` that would wait return -11 (EAGAIN) at once,
1440+a write having written what there was room for; `readyFor` waits, as
1441+`readyOf` does, for something to read on any of `reads` or for room to
1442+write on any of `writes`. A descriptor in both is given once, when it
1443+is ready for either; the 64 it looks at are `reads`, then `writes`.
1444+
14331445 **Str, Map, Sort, Math and Fmt** gain these:
14341446
14351447 ```
@@ -1742,6 +1754,8 @@
17421754 language, not as bytes (section 8; appendix A). A program that stops
17431755 says the calls under way and their variables, and `Rt.trace` says
17441756 them on demand (sections 10 and 11).
1757+- **2026-10.13.** `Sys` gains writing without waiting, and waiting for
1758+ room to write (section 11).
17451759
17461760 ## Appendix A. What an implementation chooses
17471761

lib/C.raz (history) modified +1 -0

@@ -32,6 +32,7 @@
3232 extern proc cfmakeraw*(t: ^U8);
3333 extern proc ioctl*(fd: I32; request: UINT; ...): I32;
3434 extern proc poll*(fds: ^U8; n: UINT; ms: I32): I32;
35+extern proc fcntl*(fd, cmd: I32; ...): I32;
3536 extern proc socket*(domain, kind, protocol: I32): I32;
3637 extern proc connect*(fd: I32; addr: ^U8; len: U32): I32;
3738 extern proc send*(fd: I32; buf: ^U8; n: UINT; flags: I32): INT;

lib/Sys.linux-x86_64.raz (history) modified +50 -8

@@ -568,22 +568,54 @@
568568 return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *)
569569 end;
570570
571-(* The first of fds that has something to read within ms milliseconds,
572- or that has reached its end or failed, so that a read won't wait; -1
573- if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
574-proc readyOf*(fds: []INT; ms: INT): INT;
571+(* The first of reads that has something to read within ms
572+ milliseconds, or of writes that has room to write, or that has
573+ reached its end or failed, so that a read or a write won't wait; -1
574+ if none has. ms < 0 waits as long as it takes. At most 64 fds in
575+ all. *)
576+proc readyFor*(reads, writes: []INT; ms: INT): INT;
575577 var p: [64]Pollfd; n, i: INT;
576578 begin
577- n := LEN(fds);
578- if n > LEN(p) then n := LEN(p); end;
579- for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *)
579+ n := 0;
580+ for i := 0 to LEN(reads) - 1 do
581+ if n < LEN(p) then
582+ p[n] := {fd: I32(reads[i]), events: 1}; (* POLLIN *)
583+ INC(n);
584+ end;
585+ end;
586+ for i := 0 to LEN(writes) - 1 do
587+ if n < LEN(p) then
588+ p[n] := {fd: I32(writes[i]), events: 4}; (* POLLOUT *)
589+ INC(n);
590+ end;
591+ end;
580592 if SYSCALL(7, ADR(p), n, ms) <= 0 then return -1; end; (* poll *)
581593 for i := 0 to n - 1 do
582- if p[i].revents # 0 then return fds[i]; end;
594+ if p[i].revents # 0 then return INT(p[i].fd); end;
583595 end;
584596 return -1;
585597 end;
586598
599+(* The first of fds that has something to read within ms milliseconds,
600+ or that has reached its end or failed, so that a read won't wait; -1
601+ if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
602+proc readyOf*(fds: []INT; ms: INT): INT;
603+begin
604+ return readyFor(fds, [], ms);
605+end;
606+
607+(* Reads and writes on fd made to wait, as they do at first; or, if not
608+ yes, to return -11 (EAGAIN) at once where they would wait. 0, or
609+ minus an errno. *)
610+proc setBlocking*(fd: INT; yes: BOOL): INT;
611+var flags: INT;
612+begin
613+ flags := SYSCALL(72, fd, 3, 0); (* fcntl F_GETFL *)
614+ if flags < 0 then return flags; end;
615+ if yes then flags := flags & ~INT(800H); else flags := flags | 800H; end; (* O_NONBLOCK *)
616+ return SYSCALL(72, fd, 4, flags); (* F_SETFL *)
617+end;
618+
587619 (* The value of the environment variable name; "" if it isn't set. The
588620 environment follows the arguments and their nil. *)
589621 proc env*(name: []U8): []U8;
@@ -927,6 +959,16 @@
927959 if buf[0..n] # "x\n/tmp\n" then exit(2); end;
928960 r, st, c := wait(child, true);
929961 if (r # child) or (st # exited) or (c # 0) then exit(2); end;
962+
963+ (* a pipe filled without waiting, then emptied: room to write again *)
964+ r, w := pipe();
965+ if (w < 0) or (setBlocking(w, false) # 0) or (setBlocking(r, false) # 0) then exit(2); end;
966+ repeat n := write(w, buf); until n < 0;
967+ if (n # -11) or (readyFor([], [w], 0) # -1) or (readyFor([r], [w], 0) # r) then exit(2); end;
968+ repeat n := read(r, buf); until n < 0;
969+ if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end;
970+ close(r);
971+ close(w);
930972
931973 child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1});
932974 r, st, c := wait(child, false);

lib/Sys.raz (history) modified +51 -8

@@ -519,23 +519,56 @@
519519 return r;
520520 end;
521521
522-(* The first of fds that has something to read within ms milliseconds,
523- or that has reached its end or failed, so that a read won't wait; -1
524- if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
525-proc readyOf*(fds: []INT; ms: INT): INT;
522+(* The first of reads that has something to read within ms
523+ milliseconds, or of writes that has room to write, or that has
524+ reached its end or failed, so that a read or a write won't wait; -1
525+ if none has. ms < 0 waits as long as it takes. At most 64 fds in
526+ all. *)
527+proc readyFor*(reads, writes: []INT; ms: INT): INT;
526528 var p: [64]Pollfd; n, i: INT; r: I32;
527529 begin
528- n := LEN(fds);
529- if n > LEN(p) then n := LEN(p); end;
530- for i := 0 to n - 1 do p[i] := {fd: I32(fds[i]), events: 1}; end; (* POLLIN *)
530+ n := 0;
531+ for i := 0 to LEN(reads) - 1 do
532+ if n < LEN(p) then
533+ p[n] := {fd: I32(reads[i]), events: 1}; (* POLLIN *)
534+ INC(n);
535+ end;
536+ end;
537+ for i := 0 to LEN(writes) - 1 do
538+ if n < LEN(p) then
539+ p[n] := {fd: I32(writes[i]), events: 4}; (* POLLOUT *)
540+ INC(n);
541+ end;
542+ end;
531543 repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *)
532544 if r <= 0 then return -1; end;
533545 for i := 0 to n - 1 do
534- if p[i].revents # 0 then return fds[i]; end;
546+ if p[i].revents # 0 then return INT(p[i].fd); end;
535547 end;
536548 return -1;
537549 end;
538550
551+(* The first of fds that has something to read within ms milliseconds,
552+ or that has reached its end or failed, so that a read won't wait; -1
553+ if none has. ms < 0 waits as long as it takes. At most 64 fds. *)
554+proc readyOf*(fds: []INT; ms: INT): INT;
555+begin
556+ return readyFor(fds, [], ms);
557+end;
558+
559+(* Reads and writes on fd made to wait, as they do at first; or, if not
560+ yes, to return -11 (EAGAIN) at once where they would wait. 0, or
561+ minus an errno. *)
562+proc setBlocking*(fd: INT; yes: BOOL): INT;
563+var flags: I32;
564+begin
565+ flags := C.fcntl(I32(fd), 3, 0); (* F_GETFL *)
566+ if flags < 0 then return failed(); end;
567+ if yes then flags := flags & ~I32(800H); else flags := flags | 800H; end; (* O_NONBLOCK *)
568+ if C.fcntl(I32(fd), 4, flags) < 0 then return failed(); end; (* F_SETFL *)
569+ return 0;
570+end;
571+
539572 (* The value of the environment variable name; "" if it isn't set. *)
540573 proc env*(name: []U8): []U8;
541574 var a: [4096]U8; s: ^U8; n: INT;
@@ -890,6 +923,16 @@
890923 if buf[0..n] # "x\n/tmp\n" then exit(2); end;
891924 r, st, c := wait(child, true);
892925 if (r # child) or (st # exited) or (c # 0) then exit(2); end;
926+
927+ (* a pipe filled without waiting, then emptied: room to write again *)
928+ r, w := pipe();
929+ if (w < 0) or (setBlocking(w, false) # 0) or (setBlocking(r, false) # 0) then exit(2); end;
930+ repeat n := write(w, buf); until n < 0;
931+ if (n # -11) or (readyFor([], [w], 0) # -1) or (readyFor([r], [w], 0) # r) then exit(2); end;
932+ repeat n := read(r, buf); until n < 0;
933+ if (n # -11) or (readyFor([r], [w], 0) # w) or (setBlocking(w, true) # 0) then exit(2); end;
934+ close(r);
935+ close(w);
893936
894937 child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1});
895938 r, st, c := wait(child, false);

src/raz.pas modified +1 -1

@@ -32,7 +32,7 @@
3232 { what git describe says, made by make: 0.3.0 at the tag v0.3.0,
3333 0.3.0-5-g38d29a8 five commits past it }
3434 Version = {$I version.inc};
35- Report = '2026-10.12'; { the version of doc/raz.md it follows }
35+ Report = '2026-10.13'; { the version of doc/raz.md it follows }
3636 MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it }
3737 MfdCloexec = 1; { an ELF still executes: the kernel opens it first }
3838