Sys.poll: wait on any number of descriptors, and say which are ready readyFor and readyOf are written over it.
| @@ -257,6 +257,11 @@ | ||
| 257 | 257 | port*: INT; |
| 258 | 258 | uid*, pid*: INT; |
| 259 | 259 | 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; | |
| 260 | 265 | |
| 261 | 266 | proc write*(fd: INT; s: []U8): INT; (* the bytes written *) |
| 262 | 267 | proc read*(fd: INT; buf: []U8): INT; (* the bytes read; 0 at the end *) |
| @@ -316,6 +321,7 @@ | ||
| 316 | 321 | proc ready*(fd, ms: INT): BOOL; (* something to read within ms, or ever if ms < 0 *) |
| 317 | 322 | proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *) |
| 318 | 323 | 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 *) | |
| 319 | 325 | proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *) |
| 320 | 326 | proc blocking*(fd: INT): INT; (* 1 if reads and writes wait, 0 if they are -11 at once, or minus an errno *) |
| 321 | 327 | |
| @@ -707,6 +713,20 @@ | ||
| 707 | 713 | the first 64 of `fds`, and `connect` and `listen` take a path of at |
| 708 | 714 | most 107 bytes, as `struct sockaddr_un` holds; a longer one is -36 |
| 709 | 715 | (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. | |
| 710 | 730 | |
| 711 | 731 | A program that both reads from a descriptor and writes much to it, as |
| 712 | 732 | a terminal writes what is pasted to its program while it reads what |
| @@ -35,6 +35,17 @@ | ||
| 35 | 35 | errInProgress* = -115; |
| 36 | 36 | |
| 37 | 37 | 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 | + | |
| 38 | 49 | (* What open is asked for: reading, writing or both, and what else |
| 39 | 50 | it does to the file; nonblocking, that open doesn't wait (for a |
| 40 | 51 | fifo's other end) and reads and writes on the file don't either. *) |
| @@ -1046,30 +1057,63 @@ | ||
| 1046 | 1057 | end; |
| 1047 | 1058 | |
| 1048 | 1059 | |
| 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 | + | |
| 1049 | 1092 | (* The first of reads that has something to read within ms |
| 1050 | 1093 | milliseconds, or of writes that has room to write, or that has |
| 1051 | 1094 | reached its end or failed, so that a read or a write won't wait; -1 |
| 1052 | 1095 | if none has. ms < 0 waits as long as it takes. At most 64 fds in |
| 1053 | 1096 | all. *) |
| 1054 | 1097 | 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; | |
| 1056 | 1099 | begin |
| 1057 | 1100 | n := 0; |
| 1058 | 1101 | for i := 0 to LEN(reads) - 1 do |
| 1059 | 1102 | if n < LEN(p) then |
| 1060 | - p[n] := {fd: I32(reads[i]), events: 1}; (* POLLIN *) | |
| 1103 | + p[n] := {fd: reads[i], read: true}; | |
| 1061 | 1104 | INC(n); |
| 1062 | 1105 | end; |
| 1063 | 1106 | end; |
| 1064 | 1107 | for i := 0 to LEN(writes) - 1 do |
| 1065 | 1108 | if n < LEN(p) then |
| 1066 | - p[n] := {fd: I32(writes[i]), events: 4}; (* POLLOUT *) | |
| 1109 | + p[n] := {fd: writes[i], write: true}; | |
| 1067 | 1110 | INC(n); |
| 1068 | 1111 | end; |
| 1069 | 1112 | 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; | |
| 1071 | 1115 | 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; | |
| 1073 | 1117 | end; |
| 1074 | 1118 | return -1; |
| 1075 | 1119 | end; |
| @@ -2545,12 +2589,46 @@ | ||
| 2545 | 2589 | munmap(p, 8192); |
| 2546 | 2590 | end; |
| 2547 | 2591 | |
| 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 | + | |
| 2548 | 2625 | (* The module's test: a file written, read back and closed; a |
| 2549 | 2626 | directory made, listed and emptied; no terminal; a socket, sent to |
| 2550 | 2627 | after its end; programs; time and the network. *) |
| 2551 | 2628 | proc main*; |
| 2552 | 2629 | 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; |
| 2553 | 2630 | begin |
| 2631 | + testPoll(); | |
| 2554 | 2632 | fd := create("/tmp/raz-sys-test"); |
| 2555 | 2633 | if fd < 0 then exit(2); end; |
| 2556 | 2634 | if write(fd, "hello") # 5 then exit(2); end; |
| @@ -52,6 +52,17 @@ | ||
| 52 | 52 | errInProgress* = -115; |
| 53 | 53 | |
| 54 | 54 | 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 | + | |
| 55 | 66 | (* What open is asked for: reading, writing or both, and what else |
| 56 | 67 | it does to the file; nonblocking, that open doesn't wait (for a |
| 57 | 68 | fifo's other end) and reads and writes on the file don't either. *) |
| @@ -1092,6 +1103,43 @@ | ||
| 1092 | 1103 | if LEN(s) = 0 then return 0; end; |
| 1093 | 1104 | r := C.send(I32(fd), ADR(s[0]), UINT(LEN(s)), CAbi.msgNosignal | CAbi.msgDontwait); |
| 1094 | 1105 | 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; | |
| 1095 | 1143 | return r; |
| 1096 | 1144 | end; |
| 1097 | 1145 | |
| @@ -1101,26 +1149,25 @@ | ||
| 1101 | 1149 | if none has. ms < 0 waits as long as it takes. At most 64 fds in |
| 1102 | 1150 | all. *) |
| 1103 | 1151 | 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; | |
| 1105 | 1153 | begin |
| 1106 | 1154 | n := 0; |
| 1107 | 1155 | for i := 0 to LEN(reads) - 1 do |
| 1108 | 1156 | if n < LEN(p) then |
| 1109 | - p[n] := {fd: I32(reads[i]), events: CAbi.pollin}; | |
| 1157 | + p[n] := {fd: reads[i], read: true}; | |
| 1110 | 1158 | INC(n); |
| 1111 | 1159 | end; |
| 1112 | 1160 | end; |
| 1113 | 1161 | for i := 0 to LEN(writes) - 1 do |
| 1114 | 1162 | if n < LEN(p) then |
| 1115 | - p[n] := {fd: I32(writes[i]), events: CAbi.pollout}; | |
| 1163 | + p[n] := {fd: writes[i], write: true}; | |
| 1116 | 1164 | INC(n); |
| 1117 | 1165 | end; |
| 1118 | 1166 | 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; | |
| 1122 | 1169 | 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; | |
| 1124 | 1171 | end; |
| 1125 | 1172 | return -1; |
| 1126 | 1173 | end; |
| @@ -2496,12 +2543,46 @@ | ||
| 2496 | 2543 | munmap(p, 8192); |
| 2497 | 2544 | end; |
| 2498 | 2545 | |
| 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 | + | |
| 2499 | 2579 | (* The module's test, as Linux's Sys has it: a file written, read back |
| 2500 | 2580 | and closed; a directory made, listed and emptied; no terminal; a |
| 2501 | 2581 | socket, sent to after its end; programs; time and the network. *) |
| 2502 | 2582 | proc main*; |
| 2503 | 2583 | 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; |
| 2504 | 2584 | begin |
| 2585 | + testPoll(); | |
| 2505 | 2586 | fd := create("/tmp/raz-sys-test"); |
| 2506 | 2587 | if fd < 0 then exit(2); end; |
| 2507 | 2588 | if write(fd, "hello") # 5 then exit(2); end; |