Repository navigation
Conversation
…that do not wait, shared with Chan.send and Chan.recv Chan.send parks while the channel is full and Chan.recv while it is empty, and IO.within does not cancel the loser (WONTFIX), so a receive under a deadline still takes the next value. A writer that drains what is queued into one transaction, or a queue that drops under load rather than block its producers, needs an operation that answers now (bendlang#1147). Chan.try_send(A, chan, value) -> IO(Bool) answers whether the value went: to a parked receiver, or into the ring while it has room. False on a full or closed channel, and the value is dropped, as Chan.send drops it on a closed one. Chan.try_recv(A, chan) -> IO(Chan.Poll<&1, A>) answers Got{value} from the ring or from a parked sender, Wait{} while nothing is ready, and Closed{} once the channel is closed and drained; three answers, as Maybe cannot tell Wait from Closed and a poller on a closed channel would never stop. In both runtimes the step that does not wait is now one function each way, chan_put and chan_get, and Chan.send and Chan.recv are that step followed by the park; the two new effects are that step followed by an answer. tests/io/chan_try covers a channel with room for one (a send that goes, one that does not, a receive that takes, one that waits, one that finds it closed) and one with no room (a parked sender hands its value to try_recv, a parked receiver takes try_send's, and a try_send to nobody does not go). It prints its lines on the check, interpreted, JS and C lanes; the seven other channel tests answer as on main (channel_send_recv is an io pin that fails on main alike); gates/repo.ts passes 49 / 49. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
I don't like how this adds yet another variant of these same IO functions. I'll try to come up with a more general solution that can do polling in the general case, like an IO combinator to apply over IO functions that park |
|
Thanks for this, the use cases and the tests are clear, and the split of the non-waiting step ( Before merging, I want to look at one thing: Base is starting to get one non-blocking API per type ( That design is not finalized. It still has open problems: a dropped Note: this reply was written by an AI at my request. If anything here is wrong, reply and I will review it myself. |
IO.poll is the effect itself and answers Ready or Wait, with no Or and no IO.poll.step between. io_poll_step answers io_step 1 to go on in place (a popped frame, a hold) rather than queueing the computation again, frames are recycled, and a polled act's last request (its continuation the act's end, as in IO.poll(0, Chan.recv(c))) answers its frame itself, with no Emit. On an M-series Mac, 1M loops of a send and a recv both polled take C 90 ms (was 160), JS 350 ms (was 610), beside 50 and 230 ms unpolled; a poll that answers Wait takes C 60 ms per 1M (was 90 to 100). tests/io/io_poll_writer.bend and io_poll_try.bend are @aldeni's: the writer that drains, commits and blocks on the rest, and #1162's chan_try cases with partners already parked. The poller now goes on before a partner it woke, as try_* did. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
|
Closing in favor of #1259 . Please tell me if that one is good enough |
Implements #1147, asked by @LucasGois1, with two departures from its sketch said below.
Why
Chan.sendparks while the channel is full andChan.recvwhile it is empty, andIO.withindoes not cancel the loser (WONTFIX), so a receive under a deadline still takes the next value once it arrives. Two programs need an operation that answers now: a writer that drains everything already queued into one transaction, then commits and blocks for the next; and a queue that drops under load rather than block its producers. Neither can be a hub package: the answer lives in the channel's row inside the runtime.What changes
try_sendanswers whether the value went: to a parked receiver, or into the ring while it has room.Falseon a full or closed channel, and the value is dropped, asChan.senddrops it on a closed one.try_recvanswersGot{value}from the ring or from a parked sender,Wait{}while nothing is ready,Closed{}once the channel is closed and drained. Three answers, sinceMaybecannot tellWaitfromClosed, and a poller on a closed channel would never stop.Where this departs from #1147's sketch, and why:
try_sendanswersBoollikeChan.sendrather than handing the value back in aMaybe, so the two sends read alike and aNonedoes not mean "sent"; and the type's cases areWait/Closed/Got(Emptyis already a type in Base).In both runtimes the step that does not wait is now one function each way,
chan_putandchan_get;Chan.sendandChan.recvare that step followed by the park, and the two new effects are that step followed by an answer.chan_send_run/chan_recv_runand their JS twins lose their copies of it.Checks (Linux x86-64)
tests/io/chan_try.bend: a channel with room for one (a send that goes, one that does not, a receive that takes, one that waits, one that finds it closed), and one with no room (a parked sender hands its value totry_recv, a parked receiver takestry_send's, atry_sendto nobody does not go). It prints its lines on the check, interpreted, JS and C lanes.chan_*,channel_*,nat_host_chan) answer as on main;channel_send_recvis an io pin that fails on main alike.gates/repo.tspasses 49 / 49. The guide is at 7968 of its 8000 ttok, so I left it alone; a clause on the two in the channels paragraph would fit if you trim elsewhere.Not run: macOS.