<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Reacting (as always) when the word abuse is tagged in the subject line :-)<div><br></div><div>Reading the comments in beam_receive.erl (compiler) the layout of the code</div><div>using this "selective" feature must have the following layout:</div><div><br></div><div>1) Ref= make_ref(), %% or monitor(process, Pid) / spawn_monitor(..)</div><div>...</div><div>receive</div><div> 2) _Mesg = {Ref, Data} -> do_some_stuff()</div><div>end.</div><div><br></div><div>The compiler will only put out the code you want (recv_mark) if it follows this pattern.</div><div>The actual reference created in 1) must be matched in a syntactic pattern</div><div>that contain the variable 'Ref'. The beam_receive optimization is done pretty</div><div>late in the compiler process so it is a bit more complicated that that.</div><div><br></div><div>Example</div><div><br></div><div><div>-module(beam_prio_msg).</div><div>-compile(export_all).</div></div><div><br></div><div><div>test1() -></div><div> Ref = make_ref(),</div><div> receive</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{Ref, x} -> x</div><div> after</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>100 -> timeout</div><div> end.</div><div><br></div><div>test2() -></div><div> Ref = make_ref(),</div><div> Ref1 = Ref,</div><div> receive</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{Ref1, x} -> x</div><div> after</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>100 -> timeout</div><div> end.</div><div><br></div><div>test3() -></div><div> Ref = make_ref(),</div><div> Ref1 = {Ref},</div><div> receive</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>{Ref1, x} -> x</div><div> after</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>100 -> timeout</div><div> end.</div></div><div><br></div><div>Both test1 and test2 put out the (recv_mark) instruction even though test2 is matching on an</div><div>other variable Ref1. But test3 will not put out the (recv_mark) instruction, since the reference is put</div><div>into an other term and therefor is potentially not in a register anymore.</div><div><br></div><div>Anyway. If your plan is to work at all the code fragment must somehow send a message somewhere</div><div>saying that "now it is time to send a priority message to me" and then wait for that message.</div><div><br></div><div>PrioRef = make_ref(),</div><div>ManagerPid ! {send_me_prio_request_with_this_ref, PrioRef},</div><div><br></div><div><div>receive</div><div> {PrioRef, From, Msg} -> handle_call(From, Msg, State)</div><div>after 0 -></div><div> receive</div><div> Msg -> gen_server_like_dispatch_msg(Msg)</div><div> end</div><div>end</div></div><div><br></div><div>This plan could almost work (theoretically), but would spam ManagerPid for every call to probe for </div><div>high prio message. And you surly add latency since you need to wait more than 0</div><div>millis for a high prio response.</div><div><br></div><div>In other words, with my current mindset, the scheme is not really working.</div><div>But with some new and fresh input this may inspire to even more abuse :-)</div><div><br></div><div>/Tony</div><div><br></div><div><br><div><div>On 11 apr 2014, at 02:02, Dmitry Demeshchuk <<a href="mailto:demeshchuk@gmail.com">demeshchuk@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Hi list,<div><br></div><div>Today (yeah, just a couple years have passed since R14A) I learned about OTP-8623, which allows to do selective receive for messages that contain a known reference with O(1) complexity.</div>
<div><br></div><div>Which made me think: what if we make a gen_server'ish process that:</div><div>1. Creates a reference that is being passed along with the pid, meaning, our start_link returns {ok, {Pid, ManageRef}} or something like that. ManageRef is actually created within the new process inside init/1 or such.</div>
<div>2. Introduce an operation named priority_call, which, basically, sends something like {ManageRef, {Pid, Ref}, Msg}.</div><div>3. The process itself (on its own side) does a selective receive like that:</div><div><br>
</div><div>receive</div><div> {ManageRef, From, Msg} -> handle_call(From, Msg, State)</div><div>after 0 -></div><div> receive</div><div> Msg -> gen_server_like_dispatch_msg(Msg)</div><div> end</div>
<div>end</div><div><br></div><div>This could potentially help with sending managing messages to the processes that may have overloaded inbox with thousands of messages. Say, we see that the process gets overloaded and we send an operational message of some kind ("discard the entire inbox", for example).</div>
<div><br></div><div>Thoughts?</div><div><br></div><div>-- <br>Best regards,<br>Dmitry Demeshchuk
</div></div>
_______________________________________________<br>erlang-questions mailing list<br><a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>http://erlang.org/mailman/listinfo/erlang-questions<br></blockquote></div><br><div>
<span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px;"><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">"Installing applications can lead to corruption over time. </span><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; ">Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"</span></div><div><span class="Apple-style-span" style="color: rgb(51, 51, 51); font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 12px; "><br></span></div></span><br class="Apple-interchange-newline">
</div>
<br></div></body></html>