|
Using the low-level BEAM instructions, we can loop over each message in
the process queue and removes the first message that matches, without
receiving them all to later send them back to itself.
The function prim_eval:'receive'/2 is equivalent to the
following pseudo-code:
'receive'(F, T) ->
RESET MESSAGE QUEUE POINTER,
LOOP:
case PEEK CURRENT MESSAGE WITH TIMEOUT T of
{ok,Msg} ->
case F(Msg) of
nomatch ->
DECREMENT TIMEOUT T,
ADVANCE MESSAGE QUEUE POINTER,
GOTO LOOP;
Result ->
RESET MESSAGE QUEUE POINTER,
Result
end;
timeout ->
RESET MESSAGE QUEUE POINTER,
timeout
end.
To not break Dialyzer and other tools, we use a stub Erlang module which
abstract code is forcefully inserted into prim_inet.erl afterwards
compilation.
|