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.
| @@ -86,9 +86,10 @@ | ||
| 86 | 86 | defect, and the library grows only by what breaks no program written |
| 87 | 87 | for it. A wish for more, the owner's or yours, goes on the list in |
| 88 | 88 | `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 | |
| 92 | 93 | `doc/compiler.md`'s table of lines and its routine names true when the |
| 93 | 94 | compiler changes. |
| 94 | 95 |
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | This report defines the language. It is short on purpose. Whatever it |
| 10 | 10 | doesn't allow, raz doesn't have. |
| 11 | 11 | |
| 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 | |
| 13 | 13 | version changed. It is a thaw the owner chose, for a language anyone |
| 14 | 14 | can build again, and it breaks programs written for 2026-10.10. |
| 15 | 15 | |
| @@ -1281,6 +1281,8 @@ | ||
| 1281 | 1281 | proc termSize*(fd: INT): (INT, INT); (* columns and rows; 80 and 24 if unknown *) |
| 1282 | 1282 | proc ready*(fd: INT; ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 1283 | 1283 | 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 *) | |
| 1284 | 1286 | |
| 1285 | 1287 | proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *) |
| 1286 | 1288 | proc workDir*: ([]U8, INT); (* the working directory, good until the next call *) |
| @@ -1430,6 +1432,16 @@ | ||
| 1430 | 1432 | most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36 |
| 1431 | 1433 | (ENAMETOOLONG). |
| 1432 | 1434 | |
| 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 | + | |
| 1433 | 1445 | **Str, Map, Sort, Math and Fmt** gain these: |
| 1434 | 1446 | |
| 1435 | 1447 | ``` |
| @@ -1742,6 +1754,8 @@ | ||
| 1742 | 1754 | language, not as bytes (section 8; appendix A). A program that stops |
| 1743 | 1755 | says the calls under way and their variables, and `Rt.trace` says |
| 1744 | 1756 | 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). | |
| 1745 | 1759 | |
| 1746 | 1760 | ## Appendix A. What an implementation chooses |
| 1747 | 1761 |
| @@ -32,6 +32,7 @@ | ||
| 32 | 32 | extern proc cfmakeraw*(t: ^U8); |
| 33 | 33 | extern proc ioctl*(fd: I32; request: UINT; ...): I32; |
| 34 | 34 | extern proc poll*(fds: ^U8; n: UINT; ms: I32): I32; |
| 35 | +extern proc fcntl*(fd, cmd: I32; ...): I32; | |
| 35 | 36 | extern proc socket*(domain, kind, protocol: I32): I32; |
| 36 | 37 | extern proc connect*(fd: I32; addr: ^U8; len: U32): I32; |
| 37 | 38 | extern proc send*(fd: I32; buf: ^U8; n: UINT; flags: I32): INT; |
| @@ -568,22 +568,54 @@ | ||
| 568 | 568 | return SYSCALL(44, fd, ADR(s[0]), LEN(s), 4000H, 0, 0); (* sendto, MSG_NOSIGNAL, no address *) |
| 569 | 569 | end; |
| 570 | 570 | |
| 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; | |
| 575 | 577 | var p: [64]Pollfd; n, i: INT; |
| 576 | 578 | 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; | |
| 580 | 592 | if SYSCALL(7, ADR(p), n, ms) <= 0 then return -1; end; (* poll *) |
| 581 | 593 | 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; | |
| 583 | 595 | end; |
| 584 | 596 | return -1; |
| 585 | 597 | end; |
| 586 | 598 | |
| 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 | + | |
| 587 | 619 | (* The value of the environment variable name; "" if it isn't set. The |
| 588 | 620 | environment follows the arguments and their nil. *) |
| 589 | 621 | proc env*(name: []U8): []U8; |
| @@ -927,6 +959,16 @@ | ||
| 927 | 959 | if buf[0..n] # "x\n/tmp\n" then exit(2); end; |
| 928 | 960 | r, st, c := wait(child, true); |
| 929 | 961 | 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); | |
| 930 | 972 | |
| 931 | 973 | child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); |
| 932 | 974 | r, st, c := wait(child, false); |
| @@ -519,23 +519,56 @@ | ||
| 519 | 519 | return r; |
| 520 | 520 | end; |
| 521 | 521 | |
| 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; | |
| 526 | 528 | var p: [64]Pollfd; n, i: INT; r: I32; |
| 527 | 529 | 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; | |
| 531 | 543 | repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or (failed() # -4); (* EINTR *) |
| 532 | 544 | if r <= 0 then return -1; end; |
| 533 | 545 | 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; | |
| 535 | 547 | end; |
| 536 | 548 | return -1; |
| 537 | 549 | end; |
| 538 | 550 | |
| 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 | + | |
| 539 | 572 | (* The value of the environment variable name; "" if it isn't set. *) |
| 540 | 573 | proc env*(name: []U8): []U8; |
| 541 | 574 | var a: [4096]U8; s: ^U8; n: INT; |
| @@ -890,6 +923,16 @@ | ||
| 890 | 923 | if buf[0..n] # "x\n/tmp\n" then exit(2); end; |
| 891 | 924 | r, st, c := wait(child, true); |
| 892 | 925 | 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); | |
| 893 | 936 | |
| 894 | 937 | child := spawn({path: "/bin/sleep", args: ["sleep", "10"], group: -1}); |
| 895 | 938 | r, st, c := wait(child, false); |
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | { what git describe says, made by make: 0.3.0 at the tag v0.3.0, |
| 33 | 33 | 0.3.0-5-g38d29a8 five commits past it } |
| 34 | 34 | 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 } | |
| 36 | 36 | MemfdCreate = 319; { the system call; FPC 3.2.2 has no name for it } |
| 37 | 37 | MfdCloexec = 1; { an ELF still executes: the kernel opens it first } |
| 38 | 38 |