some basic questions

Ulf Wiger etxuwig@REDACTED
Tue Oct 12 13:05:59 CEST 1999


On Tue, 12 Oct 1999, Hakan Mattsson wrote:


hakan> In order to make the startup safe (despite of gen_server) the
hakan> gen_server calls are sometimes wrapped by explicitly linking
hakan> to the process BEFORE message delivery and unlinking it when
hakan> the reply has arrived. It is reliable but costly.

Oh, yes.
I do something similar in my own read-write locker. The server has a link
table containing counters of link requests, so that I know when to really
unlink. This can of course be avoided sometimes, depending on the behaviour
of the clients. 

In my case, it's linking on the server side, because I care less about the
event that the locker crashes, than about when clients crash.


handle_call({creq, Req}, {From, Ref}, S) ->
    handle_link(From, Req),
    client_request(Req, {From, Ref}, S);
...


handle_link(Pid, Req) when element(1, Req) == set ->
    add_link(Pid);
handle_link(Pid, Req) when element(1, Req) == unset ->
    remove_link(Pid);
handle_link(Pid, Req) ->
    ?DEBUG("no link operation (Req == ~p).~n", [Req]),
    ok.

add_link(Pid) ->
    case catch ets:update_counter(?LINK_TABLE, Pid, 1) of
        {'EXIT', _} ->
            ets:insert(?LINK_TABLE, {Pid, 1});
        _ ->
            true
    end,
    ?DEBUG("linking (~p).~n", [Pid]),
    link(Pid).

remove_link(Pid) ->
    case ets:update_counter(?LINK_TABLE, Pid, -1) of
        0 ->
            ?DEBUG("unlinking (~p).~n", [Pid]),
            unlink(Pid);
        _ ->
            true
    end.


Ulf Wiger, Chief Designer AXD 301
Ericsson Telecom AB                          tfn: +46  8 719 81 95
Varuvägen 9, Älvsjö                          mob: +46 70 519 81 95
S-126 25 Stockholm, Sweden                   fax: +46  8 719 43 44




More information about the erlang-questions mailing list