One simple use case is for a dynamic graph of channels, where one wishes to be able to 'cancel' a blocking send, that being done by having a 'cancel' read channel which one can select upon. Then one uses an Alt over both the send and the receive, with the expectation that usually it is the send which completes.
I've used that pattern in recent Go code.
Of course I can add yet another built-in procedure, e.g. SELECT() which accepts both in and out channels; but since Oberon tries to be as simple as possible, I shouldn't do so as long as there are feasible work-arounds.
No, one can not just close the channel, because it is the sending end (which can close the channel) which is blocked, and the desire is to tear down the graph upon which its send is blocked.
Certainly in Go, if one closes the channel upon which the sender is blocked, then it panics due to sending on a closed channel. It doesn't matter if one closes the channel before the send, or closes from another goroutine during the blocked send, the sender will still panic.
So the only way to safely unblock the sender, is for its blocking send to be part of a select containing a read channel. Then when woken up, the send channel can be closed.
Is there an added value for a non-blocking variang (i.e. like default in the Go select statement)? I could do that e.g. with a boolean parameter.
EDIT: so far my SEND would not panic if the channel closes or is already closed, but just return 0; I thought that there could be yet another process instead of a cancel channel and this process could close channels even if another process is blocked on it, but maybe the SELECT solution would be more elegant (not sure though).
EDIT2: or how about a SELECT procedure with just the in channels first (instead of the number), then a boolean (true..non blocking), and then the out channels?
SEND(VAR c: CHANNEL OF T; v: T) and RECEIVE(VAR v: T; VAR c: CHANNEL OF T), i.e. just a single send/receive, not built-in select, and data flows from right to left.
SELECT( { VAR vN: Tn; VAR cN: CHANNEL OF Tn [;] } { VAR cM: CHANNEL OF Tm; vM: Tm [;] } ), i.e. distinguish receive or send by the order of value and channel, instead of an explicit number or a boolean separator. Still a boolean param could be added to allow non-blocking selects.
The underlying problem is that Oberon lacks an unordered select (WHILE ELSIF comes close but has the unfortunate —in this case, but not for many actor scenarios— repetition). Maybe something could be done with a CASE Poll(...) OF ... END?
If life were a Ponyhof, I would ask for something along the lines of:
PROCEDURE sendit(text: STRING): BOOLEAN;
VAR fail: BOOLEAN;
FailChan: CHANNEL OF BOOLEAN;
BEGIN
fail := FALSE;
FailChan := After(TimeOut);
SELECT
SENDABLE(TxChan) THEN SEND(TxChan, text) |
RECEIVABLE(FailChan) THEN RECEIVE(fail, FailChan)
END
RETURN fail END sendit;
but that'd obviously be too large a change.Concerning your proposal, I would rather not introduce a completely new control structure. But one could - like Wirth with the type CASE statement in Oberon-07 - reuse the WITH statement like:
WITH
SEND(TxChan, text) DO
| RECEIVE(FailChan, fail) DO
END
where I would just have to change the Guard a bit like Guard = qualident ':' qualident | SEND ActualParameters | RECEIVE ActualParameters
and even ELSE is available. What do you think?Assuming I'm correctly inferring from the above, the WITH looks better. Presumably one could have multiple SEND and/or RECEIVE function clauses in the WITH statement, so acting as an equivalent to the Go Select?
What would ELSE do? It it acting the same as the default clause in a Go select, i.e. allowing for a non-blocking call?
The one other question with the above is how would one detect a closed channel when the RECEIVE clause hits?
Correct. Here is the specification: https://github.com/oberon-lang/specification/blob/master/The.... You can have an unlimited number of guards.
> What would ELSE do? It it acting the same as the default clause in a Go select, i.e. allowing for a non-blocking call?
Correct, that's the idea.
> how would one detect a closed channel when the RECEIVE clause hits?
EDIT: I added yet another build-in procedure which returns the closed state of a channel. A blocking WITH would continue if no channel was ready to communicate, but at least one was closed.
A potentially useful SELECT function (if runtime-variable communication graphs are of interest, which they may not be?) would be one which took an array of channels and directions (à la poll), or similar data structure, as a parameter.
Edit: I also suspect that the semantics of channel closure are unlikely to be as simple as you are hoping. (but maybe do something like tcp sockets, and force a consumer to drain the channel before orderly closure?)
(between array riders and lack of display access to variables in enclosing scopes, OBERON SA seems to have evolved in a direction converging upon C?)
That would actually be even a better idea than my present SELECT function, but I don't have a good solution yet for the (receive) variables. The best I can think of is just an array for the values, but that needs a lot of extra copies. I would rather prefer a built-in function than a separate module as in Go.
> OBERON SA seems to have evolved in a direction converging upon C
Not sure what you mean.
EDIT: concerning close synchronization see https://news.ycombinator.com/item?id=38777973
So for the rx (mux) side, they all just write in to one shared chan which it then further processed. For the tx (demux) side, it is a bit simpler, but again with just an extra goroutine per tx side.
This was in a scenario where multiple clients were performing run-time registration with a mux/demux API based upon the comms needs of those specific clients.
There is the Go reflect.Select, however it is awkward, and seems to be discouraged, hence I simply used the pattern above.
Rather something like:
RXORCLOSE(c_j, v_j, VAR closed: BOOLEAN) DO Sj
as an additional guard seems necessary, otherwise these two send side sequences can not be distinguished: SEND(ch, 'a'); SEND(ch, 'a'); CLOSE(ch)
SEND(ch, 'a'); CLOSE(ch)
Or am I missing something?(and FWIW, discarding the SELECT() in favour of the WITH scheme would strike me as preferable)
EDIT: or maybe just allow CLOSED guards in WITH, similar to SEND or RECEIVE, like
WITH
SEND(TxChan, text) DO
| RECEIVE(FailChan, fail) DO
| CLOSED(FailChan) DO
END
But I think CLOSE makes everything more complicated without a clear use; I mostly added it because Go has it.EDIT2: yet another idea:
WITH
SEND(TxChan, text) DO
| ok := RECEIVE(FailChan, fail) DO
END
where ok is a BOOLEAN variable and the assignment is optional; looks a bit strange but allows the same closed handling as in Go.I first came across it in the Go version of them. The libtask/libthread and Alef versions do not include it. Its absence is not really an issue, as the same information can simply be explicitly included in how one uses the channel and the data it carries.
For Go, it works well only because of the multiple return values being present in the select statement.
Rochus, have you considered asking Dr Wirth for feedback? I haven't corresponded with him in about a decade but he was very willing to chat about tradeoffs in language features at the time.
I still think a close feature which - in contrast to Go - would just signal all waiting threads and abandon communication could be useful. I implemented this in the C library I use for experiments: https://github.com/rochus-keller/CspChan
> the proposal for it to take a timeout, which could then be detected via execution of ELSE?
It could still be added in future, e.g. as an additional parameter to SEND or RECEIVE, but in a similar way to CLOSE it makes things more complicated, and there is an alternative solution with a separate thread sending after a delay a over a channel which is received in a WITH statement where also the candidate channel waits.
> have you considered asking Dr Wirth for feedback?
I have qualms about bothering him with this in his well-deserved retirement, especially as he has demonstrated with Oberon-07 in which direction he would develop the language (see also https://oberon-lang.github.io/2021/07/16/comparing-oberon+-w...).
func sendit(text string) bool {
select {
case TxChan <- text:
case <-time.After(10 * time.Millisecond):
return false
}
return true
}But in general any time you might want to block until the first ready channel, it doesn't work if eg a channel becomes SEND'able but your code is currently blocked on the group RECEIVE. (this situation can be kludged with nonblocking calls, but a blocking call is friendlier to whatever else may be running in the system)
> in general any time you might want to block until the first ready channel, it doesn't work if eg a channel becomes SEND'able but your code is currently blocked on the group RECEIVE
Sure, but I think this can easily be avoided by using different go routines, i.e. not routing everything through only one. So I would be interested in examples where this is not possible or not feasible for some reason.
VAR_x = (left?y -> VAR_y | right!x -> VAR_x)
as Hoare notes on p116: (not wrt mixed select, but the ultimate logic is the same)> To wait for the first of many possible events is the only way of achieving this: purchase of faster computers is useless.
(note that Hoare78 discusses the desirability of output guards in §7.8 but there it isn't clear that they ought to occur mixed with input guards)
FWIW Hoare thinks it makes for a simpler system if it is always possible to rewrite any SEQ as a PAR and vice versa.