raz / commit c7550b66

commit
c7550b66493cabbfe4cc99e49eaeb29e10da5f74
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
16eaf7cb
Sys.poll: wait on any number of descriptors, and say which are ready

readyFor and readyOf are written over it.

patch · log

3 files changed, +191 -12

doc/raz-lib.md (history) modified +20 -0

@@ -257,6 +257,11 @@
257257 port*: INT;
258258 uid*, pid*: INT;
259259 end;
260+ Poll* = record (* a descriptor for poll to wait on *)
261+ fd*: INT;
262+ read*, write*: BOOL; (* wait until it may be read, written *)
263+ ready*: BOOL; (* set by poll *)
264+ end;
260265
261266 proc write*(fd: INT; s: []U8): INT; (* the bytes written *)
262267 proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *)
@@ -316,6 +321,7 @@
316321 proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *)
317322 proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
318323 proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
324+proc poll*(var fds: []Poll; ms: INT): INT; (* sets each fd's ready, as each read and write say; the number ready, 0 on a timeout, or minus an errno *)
319325 proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *)
320326 proc blocking*(fd: INT): INT; (* 1 if reads and writes wait, 0 if they are -11 at once, or minus an errno *)
321327
@@ -707,6 +713,20 @@
707713 the first 64 of `fds`, and `connect` and `listen` take a path of at
708714 most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36
709715 (ENAMETOOLONG).
716+
717+`poll` is `readyFor` without its limits. Each `Poll` names a
718+descriptor `fd` and whether to wait to `read` from it, to `write` to
719+it, or both; `poll` waits up to `ms` milliseconds (as long as it takes
720+if `ms < 0`), sets every one's `ready`, and gives how many are: all of
721+them, in one call, not the first. It takes any number of descriptors
722+(the list the system wants is on the stack for 64 or fewer, and in
723+memory from `mmap`, given back before it returns, for more; -12 if
724+there is none). A descriptor at its end or failed is ready, as for
725+`readyOf`; so is one that isn't open, and a read or a write of it is
726+-9 (EBADF); a negative `fd` is never ready. A timeout is 0 and a
727+failure minus an errno (-4, if a signal `handle` took broke in), where
728+`readyFor` gives -1 for both. `readyFor` and `readyOf` are written over
729+it.
710730
711731 A program that both reads from a descriptor and writes much to it, as
712732 a terminal writes what is pasted to its program while it reads what

lib/Sys.linux-x86_64.raz (history) modified +83 -5

@@ -35,6 +35,17 @@
3535 errInProgress* = -115;
3636
3737 type
38+ (* One descriptor poll waits on: whether to wait to read from it, to
39+ write to it, or both, and, when poll returns, whether it may be.
40+ One that has reached its end, has failed or isn't open is ready
41+ too, so that a read or a write finds out. *)
42+ Poll* = record
43+ fd*: INT; (* the file descriptor; a negative one is never ready *)
44+ read*: BOOL; (* wait until it has something to read *)
45+ write*: BOOL; (* wait until it has room to write *)
46+ ready*: BOOL; (* set by poll: a read or a write on it won't wait *)
47+ end;
48+
3849 (* What open is asked for: reading, writing or both, and what else
3950 it does to the file; nonblocking, that open doesn't wait (for a
4051 fifo's other end) and reads and writes on the file don't either. *)
@@ -1046,30 +1057,63 @@
10461057 end;
10471058
10481059
1060+(* Waits up to ms milliseconds (ms < 0: as long as it takes) until one
1061+ of fds may be read or written as each one's read and write say, and
1062+ sets each one's ready. The number ready, 0 on a timeout, or minus an
1063+ errno. It takes as many as it is given: the system's list is built
1064+ on the stack for 64 or fewer, else in memory from mmap, given back
1065+ before it returns (-12 if there is none). *)
1066+proc poll*(var fds: []Poll; ms: INT): INT;
1067+var small: [64]Pollfd; list: []Pollfd; big: ^Pollfd; n, i, r: INT; events: I16;
1068+begin
1069+ n := LEN(fds);
1070+ big := nil;
1071+ if n <= LEN(small) then
1072+ list := small[0..n];
1073+ else
1074+ big := ^Pollfd(mmap(n * SIZE(Pollfd)));
1075+ if big = nil then return -12; end; (* ENOMEM *)
1076+ list := big[0..n];
1077+ end;
1078+ for i := 0 to n - 1 do
1079+ events := 0;
1080+ if fds[i].read then events := 1; end; (* POLLIN *)
1081+ if fds[i].write then events := events | 4; end; (* POLLOUT *)
1082+ list[i] := {fd: I32(fds[i].fd), events: events};
1083+ end;
1084+ r := SYSCALL(7, PTR(list), n, ms); (* poll *)
1085+ for i := 0 to n - 1 do
1086+ fds[i].ready := (r > 0) and (list[i].revents # 0);
1087+ end;
1088+ if big # nil then munmap(^U8(big), n * SIZE(Pollfd)); end;
1089+ return r;
1090+end;
1091+
10491092 (* The first of reads that has something to read within ms
10501093 milliseconds, or of writes that has room to write, or that has
10511094 reached its end or failed, so that a read or a write won't wait; -1
10521095 if none has. ms < 0 waits as long as it takes. At most 64 fds in
10531096 all. *)
10541097 proc readyFor*(reads, writes: []INT; ms: INT): INT;
1055-var p: [64]Pollfd; n, i: INT;
1098+var p: [64]Poll; ps: []Poll; n, i: INT;
10561099 begin
10571100 n := 0;
10581101 for i := 0 to LEN(reads) - 1 do
10591102 if n < LEN(p) then
1060- p[n] := {fd: I32(reads[i]), events: 1}; (* POLLIN *)
1103+ p[n] := {fd: reads[i], read: true};
10611104 INC(n);
10621105 end;
10631106 end;
10641107 for i := 0 to LEN(writes) - 1 do
10651108 if n < LEN(p) then
1066- p[n] := {fd: I32(writes[i]), events: 4}; (* POLLOUT *)
1109+ p[n] := {fd: writes[i], write: true};
10671110 INC(n);
10681111 end;
10691112 end;
1070- if SYSCALL(7, ADR(p), n, ms) <= 0 then return -1; end; (* poll *)
1113+ ps := p[0..n];
1114+ if poll(ps, ms) <= 0 then return -1; end;
10711115 for i := 0 to n - 1 do
1072- if p[i].revents # 0 then return INT(p[i].fd); end;
1116+ if p[i].ready then return p[i].fd; end;
10731117 end;
10741118 return -1;
10751119 end;
@@ -2545,12 +2589,46 @@
25452589 munmap(p, 8192);
25462590 end;
25472591
2592+(* poll over 100 socket pairs with a byte sent on three, so that all
2593+ three are reported in one call, at their places; a timeout, 0; a
2594+ closed descriptor, ready, and -9 from a read. *)
2595+proc testPoll;
2596+var a: [100]Poll; ends: [100]INT; ps: []Poll; buf: [4]U8; x, y, r, i: INT;
2597+begin
2598+ for i := 0 to 99 do
2599+ x, y, r := socketPair();
2600+ if r # 0 then exit(2); end;
2601+ a[i] := {fd: x, read: true};
2602+ ends[i] := y;
2603+ end;
2604+ ps := a[0..100];
2605+ if poll(ps, 0) # 0 then exit(2); end; (* a timeout *)
2606+ for i := 3 to 77 by 37 do (* 3, 40, 77 *)
2607+ if send(ends[i], "x") # 1 then exit(2); end;
2608+ end;
2609+ if poll(ps, 1000) # 3 then exit(2); end;
2610+ for i := 0 to 99 do
2611+ if a[i].ready # ((i = 3) or (i = 40) or (i = 77)) then exit(2); end;
2612+ end;
2613+ for i := 3 to 77 by 37 do
2614+ if read(a[i].fd, buf) # 1 then exit(2); end;
2615+ end;
2616+ if poll(ps, 0) # 0 then exit(2); end;
2617+ close(a[5].fd); (* not open: ready, and a read says why *)
2618+ if (poll(ps, 0) # 1) or not a[5].ready or (read(a[5].fd, buf) # -9) then exit(2); end;
2619+ for i := 0 to 99 do
2620+ close(ends[i]);
2621+ if i # 5 then close(a[i].fd); end;
2622+ end;
2623+end;
2624+
25482625 (* The module's test: a file written, read back and closed; a
25492626 directory made, listed and emptied; no terminal; a socket, sent to
25502627 after its end; programs; time and the network. *)
25512628 proc main*;
25522629 var free, total: I64; fd, n, err, w, h, l, c, a, m, k, o: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i, j: Info; d: Dir; name: []U8; ok: BOOL;
25532630 begin
2631+ testPoll();
25542632 fd := create("/tmp/raz-sys-test");
25552633 if fd < 0 then exit(2); end;
25562634 if write(fd, "hello") # 5 then exit(2); end;

lib/Sys.raz (history) modified +88 -7

@@ -52,6 +52,17 @@
5252 errInProgress* = -115;
5353
5454 type
55+ (* One descriptor poll waits on: whether to wait to read from it, to
56+ write to it, or both, and, when poll returns, whether it may be.
57+ One that has reached its end, has failed or isn't open is ready
58+ too, so that a read or a write finds out. *)
59+ Poll* = record
60+ fd*: INT; (* the file descriptor; a negative one is never ready *)
61+ read*: BOOL; (* wait until it has something to read *)
62+ write*: BOOL; (* wait until it has room to write *)
63+ ready*: BOOL; (* set by poll: a read or a write on it won't wait *)
64+ end;
65+
5566 (* What open is asked for: reading, writing or both, and what else
5667 it does to the file; nonblocking, that open doesn't wait (for a
5768 fifo's other end) and reads and writes on the file don't either. *)
@@ -1092,6 +1103,43 @@
10921103 if LEN(s) = 0 then return 0; end;
10931104 r := C.send(I32(fd), ADR(s[0]), UINT(LEN(s)), CAbi.msgNosignal | CAbi.msgDontwait);
10941105 if r < 0 then return failed(); end;
1106+ return r;
1107+end;
1108+
1109+(* Waits up to ms milliseconds (ms < 0: as long as it takes) until one
1110+ of fds may be read or written as each one's read and write say, and
1111+ sets each one's ready. The number ready, 0 on a timeout, or minus an
1112+ errno. It takes as many as it is given: the system's list is built
1113+ on the stack for 64 or fewer, else in memory from mmap, given back
1114+ before it returns (-12 if there is none). A signal that a catch took
1115+ doesn't end the wait; one that handle took does, with -4. *)
1116+proc poll*(var fds: []Poll; ms: INT): INT;
1117+var small: [64]Pollfd; list: []Pollfd; big: ^Pollfd; n, i, r, e: INT; events: I16;
1118+begin
1119+ n := LEN(fds);
1120+ big := nil;
1121+ if n <= LEN(small) then
1122+ list := small[0..n];
1123+ else
1124+ big := ^Pollfd(mmap(n * SIZE(Pollfd)));
1125+ if big = nil then return -12; end; (* ENOMEM *)
1126+ list := big[0..n];
1127+ end;
1128+ for i := 0 to n - 1 do
1129+ events := 0;
1130+ if fds[i].read then events := CAbi.pollin; end;
1131+ if fds[i].write then events := events | CAbi.pollout; end;
1132+ list[i] := {fd: I32(fds[i].fd), events: events};
1133+ end;
1134+ _ := XCHG(ADR(broke), 0);
1135+ repeat r := INT(C.poll(^U8(PTR(list)), UINT(n), I32(ms))); until (r >= 0) or not again();
1136+ e := 0;
1137+ if r < 0 then e := failed(); end;
1138+ for i := 0 to n - 1 do
1139+ fds[i].ready := (r > 0) and (list[i].revents # 0);
1140+ end;
1141+ if big # nil then munmap(^U8(big), n * SIZE(Pollfd)); end;
1142+ if r < 0 then return e; end;
10951143 return r;
10961144 end;
10971145
@@ -1101,26 +1149,25 @@
11011149 if none has. ms < 0 waits as long as it takes. At most 64 fds in
11021150 all. *)
11031151 proc readyFor*(reads, writes: []INT; ms: INT): INT;
1104-var p: [64]Pollfd; n, i: INT; r: I32;
1152+var p: [64]Poll; ps: []Poll; n, i: INT;
11051153 begin
11061154 n := 0;
11071155 for i := 0 to LEN(reads) - 1 do
11081156 if n < LEN(p) then
1109- p[n] := {fd: I32(reads[i]), events: CAbi.pollin};
1157+ p[n] := {fd: reads[i], read: true};
11101158 INC(n);
11111159 end;
11121160 end;
11131161 for i := 0 to LEN(writes) - 1 do
11141162 if n < LEN(p) then
1115- p[n] := {fd: I32(writes[i]), events: CAbi.pollout};
1163+ p[n] := {fd: writes[i], write: true};
11161164 INC(n);
11171165 end;
11181166 end;
1119- _ := XCHG(ADR(broke), 0);
1120- repeat r := C.poll(^U8(ADR(p)), UINT(n), I32(ms)); until (r >= 0) or not again();
1121- if r <= 0 then return -1; end;
1167+ ps := p[0..n];
1168+ if poll(ps, ms) <= 0 then return -1; end;
11221169 for i := 0 to n - 1 do
1123- if p[i].revents # 0 then return INT(p[i].fd); end;
1170+ if p[i].ready then return p[i].fd; end;
11241171 end;
11251172 return -1;
11261173 end;
@@ -2496,12 +2543,46 @@
24962543 munmap(p, 8192);
24972544 end;
24982545
2546+(* poll over 100 socket pairs with a byte sent on three, so that all
2547+ three are reported in one call, at their places; a timeout, 0; a
2548+ closed descriptor, ready, and -9 from a read. *)
2549+proc testPoll;
2550+var a: [100]Poll; ends: [100]INT; ps: []Poll; buf: [4]U8; x, y, r, i: INT;
2551+begin
2552+ for i := 0 to 99 do
2553+ x, y, r := socketPair();
2554+ if r # 0 then exit(2); end;
2555+ a[i] := {fd: x, read: true};
2556+ ends[i] := y;
2557+ end;
2558+ ps := a[0..100];
2559+ if poll(ps, 0) # 0 then exit(2); end; (* a timeout *)
2560+ for i := 3 to 77 by 37 do (* 3, 40, 77 *)
2561+ if send(ends[i], "x") # 1 then exit(2); end;
2562+ end;
2563+ if poll(ps, 1000) # 3 then exit(2); end;
2564+ for i := 0 to 99 do
2565+ if a[i].ready # ((i = 3) or (i = 40) or (i = 77)) then exit(2); end;
2566+ end;
2567+ for i := 3 to 77 by 37 do
2568+ if read(a[i].fd, buf) # 1 then exit(2); end;
2569+ end;
2570+ if poll(ps, 0) # 0 then exit(2); end;
2571+ close(a[5].fd); (* not open: ready, and a read says why *)
2572+ if (poll(ps, 0) # 1) or not a[5].ready or (read(a[5].fd, buf) # -9) then exit(2); end;
2573+ for i := 0 to 99 do
2574+ close(ends[i]);
2575+ if i # 5 then close(a[i].fd); end;
2576+ end;
2577+end;
2578+
24992579 (* The module's test, as Linux's Sys has it: a file written, read back
25002580 and closed; a directory made, listed and emptied; no terminal; a
25012581 socket, sent to after its end; programs; time and the network. *)
25022582 proc main*;
25032583 var free, total: I64; fd, n, err, w, h, l, c, a, m, k, o: INT; got: [1]INT; buf: [16]U8; p, q: ^U8; i, j: Info; d: Dir; name: []U8; ok: BOOL;
25042584 begin
2585+ testPoll();
25052586 fd := create("/tmp/raz-sys-test");
25062587 if fd < 0 then exit(2); end;
25072588 if write(fd, "hello") # 5 then exit(2); end;