src/gen_server2.erl
author Simon MacMullen <simon@rabbitmq.com>
Fri Feb 03 15:59:12 2012 +0000 (12 hours ago)
changeset 8922 4f87837a40be
parent 8656 d6277162d985
permissions -rw-r--r--
Merge bug24702
     1 %% This file is a copy of gen_server.erl from the R13B-1 Erlang/OTP
     2 %% distribution, with the following modifications:
     3 %%
     4 %% 1) the module name is gen_server2
     5 %%
     6 %% 2) more efficient handling of selective receives in callbacks
     7 %% gen_server2 processes drain their message queue into an internal
     8 %% buffer before invoking any callback module functions. Messages are
     9 %% dequeued from the buffer for processing. Thus the effective message
    10 %% queue of a gen_server2 process is the concatenation of the internal
    11 %% buffer and the real message queue.
    12 %% As a result of the draining, any selective receive invoked inside a
    13 %% callback is less likely to have to scan a large message queue.
    14 %%
    15 %% 3) gen_server2:cast is guaranteed to be order-preserving
    16 %% The original code could reorder messages when communicating with a
    17 %% process on a remote node that was not currently connected.
    18 %%
    19 %% 4) The callback module can optionally implement prioritise_call/3,
    20 %% prioritise_cast/2 and prioritise_info/2.  These functions take
    21 %% Message, From and State or just Message and State and return a
    22 %% single integer representing the priority attached to the message.
    23 %% Messages with higher priorities are processed before requests with
    24 %% lower priorities. The default priority is 0.
    25 %%
    26 %% 5) The callback module can optionally implement
    27 %% handle_pre_hibernate/1 and handle_post_hibernate/1. These will be
    28 %% called immediately prior to and post hibernation, respectively. If
    29 %% handle_pre_hibernate returns {hibernate, NewState} then the process
    30 %% will hibernate. If the module does not implement
    31 %% handle_pre_hibernate/1 then the default action is to hibernate.
    32 %%
    33 %% 6) init can return a 4th arg, {backoff, InitialTimeout,
    34 %% MinimumTimeout, DesiredHibernatePeriod} (all in
    35 %% milliseconds). Then, on all callbacks which can return a timeout
    36 %% (including init), timeout can be 'hibernate'. When this is the
    37 %% case, the current timeout value will be used (initially, the
    38 %% InitialTimeout supplied from init). After this timeout has
    39 %% occurred, hibernation will occur as normal. Upon awaking, a new
    40 %% current timeout value will be calculated.
    41 %%
    42 %% The purpose is that the gen_server2 takes care of adjusting the
    43 %% current timeout value such that the process will increase the
    44 %% timeout value repeatedly if it is unable to sleep for the
    45 %% DesiredHibernatePeriod. If it is able to sleep for the
    46 %% DesiredHibernatePeriod it will decrease the current timeout down to
    47 %% the MinimumTimeout, so that the process is put to sleep sooner (and
    48 %% hopefully stays asleep for longer). In short, should a process
    49 %% using this receive a burst of messages, it should not hibernate
    50 %% between those messages, but as the messages become less frequent,
    51 %% the process will not only hibernate, it will do so sooner after
    52 %% each message.
    53 %%
    54 %% When using this backoff mechanism, normal timeout values (i.e. not
    55 %% 'hibernate') can still be used, and if they are used then the
    56 %% handle_info(timeout, State) will be called as normal. In this case,
    57 %% returning 'hibernate' from handle_info(timeout, State) will not
    58 %% hibernate the process immediately, as it would if backoff wasn't
    59 %% being used. Instead it'll wait for the current timeout as described
    60 %% above.
    61 %%
    62 %% 7) The callback module can return from any of the handle_*
    63 %% functions, a {become, Module, State} triple, or a {become, Module,
    64 %% State, Timeout} quadruple. This allows the gen_server to
    65 %% dynamically change the callback module. The State is the new state
    66 %% which will be passed into any of the callback functions in the new
    67 %% module. Note there is no form also encompassing a reply, thus if
    68 %% you wish to reply in handle_call/3 and change the callback module,
    69 %% you need to use gen_server2:reply/2 to issue the reply manually.
    70 %%
    71 %% 8) The callback module can optionally implement
    72 %% format_message_queue/2 which is the equivalent of format_status/2
    73 %% but where the second argument is specifically the priority_queue
    74 %% which contains the prioritised message_queue.
    75 
    76 %% All modifications are (C) 2009-2012 VMware, Inc.
    77 
    78 %% ``The contents of this file are subject to the Erlang Public License,
    79 %% Version 1.1, (the "License"); you may not use this file except in
    80 %% compliance with the License. You should have received a copy of the
    81 %% Erlang Public License along with this software. If not, it can be
    82 %% retrieved via the world wide web at http://www.erlang.org/.
    83 %%
    84 %% Software distributed under the License is distributed on an "AS IS"
    85 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
    86 %% the License for the specific language governing rights and limitations
    87 %% under the License.
    88 %%
    89 %% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
    90 %% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
    91 %% AB. All Rights Reserved.''
    92 %%
    93 %%     $Id$
    94 %%
    95 -module(gen_server2).
    96 
    97 %%% ---------------------------------------------------
    98 %%%
    99 %%% The idea behind THIS server is that the user module
   100 %%% provides (different) functions to handle different
   101 %%% kind of inputs.
   102 %%% If the Parent process terminates the Module:terminate/2
   103 %%% function is called.
   104 %%%
   105 %%% The user module should export:
   106 %%%
   107 %%%   init(Args)
   108 %%%     ==> {ok, State}
   109 %%%         {ok, State, Timeout}
   110 %%%         {ok, State, Timeout, Backoff}
   111 %%%         ignore
   112 %%%         {stop, Reason}
   113 %%%
   114 %%%   handle_call(Msg, {From, Tag}, State)
   115 %%%
   116 %%%    ==> {reply, Reply, State}
   117 %%%        {reply, Reply, State, Timeout}
   118 %%%        {noreply, State}
   119 %%%        {noreply, State, Timeout}
   120 %%%        {stop, Reason, Reply, State}
   121 %%%              Reason = normal | shutdown | Term terminate(State) is called
   122 %%%
   123 %%%   handle_cast(Msg, State)
   124 %%%
   125 %%%    ==> {noreply, State}
   126 %%%        {noreply, State, Timeout}
   127 %%%        {stop, Reason, State}
   128 %%%              Reason = normal | shutdown | Term terminate(State) is called
   129 %%%
   130 %%%   handle_info(Info, State) Info is e.g. {'EXIT', P, R}, {nodedown, N}, ...
   131 %%%
   132 %%%    ==> {noreply, State}
   133 %%%        {noreply, State, Timeout}
   134 %%%        {stop, Reason, State}
   135 %%%              Reason = normal | shutdown | Term, terminate(State) is called
   136 %%%
   137 %%%   terminate(Reason, State) Let the user module clean up
   138 %%%        always called when server terminates
   139 %%%
   140 %%%    ==> ok
   141 %%%
   142 %%%   handle_pre_hibernate(State)
   143 %%%
   144 %%%    ==> {hibernate, State}
   145 %%%        {stop, Reason, State}
   146 %%%              Reason = normal | shutdown | Term, terminate(State) is called
   147 %%%
   148 %%%   handle_post_hibernate(State)
   149 %%%
   150 %%%    ==> {noreply, State}
   151 %%%        {stop, Reason, State}
   152 %%%              Reason = normal | shutdown | Term, terminate(State) is called
   153 %%%
   154 %%% The work flow (of the server) can be described as follows:
   155 %%%
   156 %%%   User module                          Generic
   157 %%%   -----------                          -------
   158 %%%     start            ----->             start
   159 %%%     init             <-----              .
   160 %%%
   161 %%%                                         loop
   162 %%%     handle_call      <-----              .
   163 %%%                      ----->             reply
   164 %%%
   165 %%%     handle_cast      <-----              .
   166 %%%
   167 %%%     handle_info      <-----              .
   168 %%%
   169 %%%     terminate        <-----              .
   170 %%%
   171 %%%                      ----->             reply
   172 %%%
   173 %%%
   174 %%% ---------------------------------------------------
   175 
   176 %% API
   177 -export([start/3, start/4,
   178          start_link/3, start_link/4,
   179          call/2, call/3,
   180          cast/2, reply/2,
   181          abcast/2, abcast/3,
   182          multi_call/2, multi_call/3, multi_call/4,
   183          enter_loop/3, enter_loop/4, enter_loop/5, enter_loop/6, wake_hib/1]).
   184 
   185 -export([behaviour_info/1]).
   186 
   187 %% System exports
   188 -export([system_continue/3,
   189          system_terminate/4,
   190          system_code_change/4,
   191          format_status/2]).
   192 
   193 %% Internal exports
   194 -export([init_it/6]).
   195 
   196 -import(error_logger, [format/2]).
   197 
   198 %% State record
   199 -record(gs2_state, {parent, name, state, mod, time,
   200                     timeout_state, queue, debug, prioritise_call,
   201                     prioritise_cast, prioritise_info}).
   202 
   203 %%%=========================================================================
   204 %%%  Specs. These exist only to shut up dialyzer's warnings
   205 %%%=========================================================================
   206 
   207 -ifdef(use_specs).
   208 
   209 -type(gs2_state() :: #gs2_state{}).
   210 
   211 -spec(handle_common_termination/3 ::
   212         (any(), atom(), gs2_state()) -> no_return()).
   213 -spec(hibernate/1 :: (gs2_state()) -> no_return()).
   214 -spec(pre_hibernate/1 :: (gs2_state()) -> no_return()).
   215 -spec(system_terminate/4 :: (_, _, _, gs2_state()) -> no_return()).
   216 
   217 -endif.
   218 
   219 %%%=========================================================================
   220 %%%  API
   221 %%%=========================================================================
   222 
   223 behaviour_info(callbacks) ->
   224     [{init,1},{handle_call,3},{handle_cast,2},{handle_info,2},
   225      {terminate,2},{code_change,3}];
   226 behaviour_info(_Other) ->
   227     undefined.
   228 
   229 %%%  -----------------------------------------------------------------
   230 %%% Starts a generic server.
   231 %%% start(Mod, Args, Options)
   232 %%% start(Name, Mod, Args, Options)
   233 %%% start_link(Mod, Args, Options)
   234 %%% start_link(Name, Mod, Args, Options) where:
   235 %%%    Name ::= {local, atom()} | {global, atom()}
   236 %%%    Mod  ::= atom(), callback module implementing the 'real' server
   237 %%%    Args ::= term(), init arguments (to Mod:init/1)
   238 %%%    Options ::= [{timeout, Timeout} | {debug, [Flag]}]
   239 %%%      Flag ::= trace | log | {logfile, File} | statistics | debug
   240 %%%          (debug == log && statistics)
   241 %%% Returns: {ok, Pid} |
   242 %%%          {error, {already_started, Pid}} |
   243 %%%          {error, Reason}
   244 %%% -----------------------------------------------------------------
   245 start(Mod, Args, Options) ->
   246     gen:start(?MODULE, nolink, Mod, Args, Options).
   247 
   248 start(Name, Mod, Args, Options) ->
   249     gen:start(?MODULE, nolink, Name, Mod, Args, Options).
   250 
   251 start_link(Mod, Args, Options) ->
   252     gen:start(?MODULE, link, Mod, Args, Options).
   253 
   254 start_link(Name, Mod, Args, Options) ->
   255     gen:start(?MODULE, link, Name, Mod, Args, Options).
   256 
   257 
   258 %% -----------------------------------------------------------------
   259 %% Make a call to a generic server.
   260 %% If the server is located at another node, that node will
   261 %% be monitored.
   262 %% If the client is trapping exits and is linked server termination
   263 %% is handled here (? Shall we do that here (or rely on timeouts) ?).
   264 %% -----------------------------------------------------------------
   265 call(Name, Request) ->
   266     case catch gen:call(Name, '$gen_call', Request) of
   267         {ok,Res} ->
   268             Res;
   269         {'EXIT',Reason} ->
   270             exit({Reason, {?MODULE, call, [Name, Request]}})
   271     end.
   272 
   273 call(Name, Request, Timeout) ->
   274     case catch gen:call(Name, '$gen_call', Request, Timeout) of
   275         {ok,Res} ->
   276             Res;
   277         {'EXIT',Reason} ->
   278             exit({Reason, {?MODULE, call, [Name, Request, Timeout]}})
   279     end.
   280 
   281 %% -----------------------------------------------------------------
   282 %% Make a cast to a generic server.
   283 %% -----------------------------------------------------------------
   284 cast({global,Name}, Request) ->
   285     catch global:send(Name, cast_msg(Request)),
   286     ok;
   287 cast({Name,Node}=Dest, Request) when is_atom(Name), is_atom(Node) ->
   288     do_cast(Dest, Request);
   289 cast(Dest, Request) when is_atom(Dest) ->
   290     do_cast(Dest, Request);
   291 cast(Dest, Request) when is_pid(Dest) ->
   292     do_cast(Dest, Request).
   293 
   294 do_cast(Dest, Request) ->
   295     do_send(Dest, cast_msg(Request)),
   296     ok.
   297 
   298 cast_msg(Request) -> {'$gen_cast',Request}.
   299 
   300 %% -----------------------------------------------------------------
   301 %% Send a reply to the client.
   302 %% -----------------------------------------------------------------
   303 reply({To, Tag}, Reply) ->
   304     catch To ! {Tag, Reply}.
   305 
   306 %% -----------------------------------------------------------------
   307 %% Asyncronous broadcast, returns nothing, it's just send'n pray
   308 %% -----------------------------------------------------------------
   309 abcast(Name, Request) when is_atom(Name) ->
   310     do_abcast([node() | nodes()], Name, cast_msg(Request)).
   311 
   312 abcast(Nodes, Name, Request) when is_list(Nodes), is_atom(Name) ->
   313     do_abcast(Nodes, Name, cast_msg(Request)).
   314 
   315 do_abcast([Node|Nodes], Name, Msg) when is_atom(Node) ->
   316     do_send({Name,Node},Msg),
   317     do_abcast(Nodes, Name, Msg);
   318 do_abcast([], _,_) -> abcast.
   319 
   320 %%% -----------------------------------------------------------------
   321 %%% Make a call to servers at several nodes.
   322 %%% Returns: {[Replies],[BadNodes]}
   323 %%% A Timeout can be given
   324 %%%
   325 %%% A middleman process is used in case late answers arrives after
   326 %%% the timeout. If they would be allowed to glog the callers message
   327 %%% queue, it would probably become confused. Late answers will
   328 %%% now arrive to the terminated middleman and so be discarded.
   329 %%% -----------------------------------------------------------------
   330 multi_call(Name, Req)
   331   when is_atom(Name) ->
   332     do_multi_call([node() | nodes()], Name, Req, infinity).
   333 
   334 multi_call(Nodes, Name, Req)
   335   when is_list(Nodes), is_atom(Name) ->
   336     do_multi_call(Nodes, Name, Req, infinity).
   337 
   338 multi_call(Nodes, Name, Req, infinity) ->
   339     do_multi_call(Nodes, Name, Req, infinity);
   340 multi_call(Nodes, Name, Req, Timeout)
   341   when is_list(Nodes), is_atom(Name), is_integer(Timeout), Timeout >= 0 ->
   342     do_multi_call(Nodes, Name, Req, Timeout).
   343 
   344 
   345 %%-----------------------------------------------------------------
   346 %% enter_loop(Mod, Options, State, <ServerName>, <TimeOut>, <Backoff>) ->_
   347 %%
   348 %% Description: Makes an existing process into a gen_server.
   349 %%              The calling process will enter the gen_server receive
   350 %%              loop and become a gen_server process.
   351 %%              The process *must* have been started using one of the
   352 %%              start functions in proc_lib, see proc_lib(3).
   353 %%              The user is responsible for any initialization of the
   354 %%              process, including registering a name for it.
   355 %%-----------------------------------------------------------------
   356 enter_loop(Mod, Options, State) ->
   357     enter_loop(Mod, Options, State, self(), infinity, undefined).
   358 
   359 enter_loop(Mod, Options, State, Backoff = {backoff, _, _ , _}) ->
   360     enter_loop(Mod, Options, State, self(), infinity, Backoff);
   361 
   362 enter_loop(Mod, Options, State, ServerName = {_, _}) ->
   363     enter_loop(Mod, Options, State, ServerName, infinity, undefined);
   364 
   365 enter_loop(Mod, Options, State, Timeout) ->
   366     enter_loop(Mod, Options, State, self(), Timeout, undefined).
   367 
   368 enter_loop(Mod, Options, State, ServerName, Backoff = {backoff, _, _, _}) ->
   369     enter_loop(Mod, Options, State, ServerName, infinity, Backoff);
   370 
   371 enter_loop(Mod, Options, State, ServerName, Timeout) ->
   372     enter_loop(Mod, Options, State, ServerName, Timeout, undefined).
   373 
   374 enter_loop(Mod, Options, State, ServerName, Timeout, Backoff) ->
   375     Name = get_proc_name(ServerName),
   376     Parent = get_parent(),
   377     Debug = debug_options(Name, Options),
   378     Queue = priority_queue:new(),
   379     Backoff1 = extend_backoff(Backoff),
   380     loop(find_prioritisers(
   381            #gs2_state { parent = Parent, name = Name, state = State,
   382                         mod = Mod, time = Timeout, timeout_state = Backoff1,
   383                         queue = Queue, debug = Debug })).
   384 
   385 %%%========================================================================
   386 %%% Gen-callback functions
   387 %%%========================================================================
   388 
   389 %%% ---------------------------------------------------
   390 %%% Initiate the new process.
   391 %%% Register the name using the Rfunc function
   392 %%% Calls the Mod:init/Args function.
   393 %%% Finally an acknowledge is sent to Parent and the main
   394 %%% loop is entered.
   395 %%% ---------------------------------------------------
   396 init_it(Starter, self, Name, Mod, Args, Options) ->
   397     init_it(Starter, self(), Name, Mod, Args, Options);
   398 init_it(Starter, Parent, Name0, Mod, Args, Options) ->
   399     Name = name(Name0),
   400     Debug = debug_options(Name, Options),
   401     Queue = priority_queue:new(),
   402     GS2State = find_prioritisers(
   403                  #gs2_state { parent  = Parent,
   404                               name    = Name,
   405                               mod     = Mod,
   406                               queue   = Queue,
   407                               debug   = Debug }),
   408     case catch Mod:init(Args) of
   409         {ok, State} ->
   410             proc_lib:init_ack(Starter, {ok, self()}),
   411             loop(GS2State #gs2_state { state         = State,
   412                                        time          = infinity,
   413                                        timeout_state = undefined });
   414         {ok, State, Timeout} ->
   415             proc_lib:init_ack(Starter, {ok, self()}),
   416             loop(GS2State #gs2_state { state         = State,
   417                                        time          = Timeout,
   418                                        timeout_state = undefined });
   419         {ok, State, Timeout, Backoff = {backoff, _, _, _}} ->
   420             Backoff1 = extend_backoff(Backoff),
   421             proc_lib:init_ack(Starter, {ok, self()}),
   422             loop(GS2State #gs2_state { state         = State,
   423                                        time          = Timeout,
   424                                        timeout_state = Backoff1 });
   425         {stop, Reason} ->
   426             %% For consistency, we must make sure that the
   427             %% registered name (if any) is unregistered before
   428             %% the parent process is notified about the failure.
   429             %% (Otherwise, the parent process could get
   430             %% an 'already_started' error if it immediately
   431             %% tried starting the process again.)
   432             unregister_name(Name0),
   433             proc_lib:init_ack(Starter, {error, Reason}),
   434             exit(Reason);
   435         ignore ->
   436             unregister_name(Name0),
   437             proc_lib:init_ack(Starter, ignore),
   438             exit(normal);
   439         {'EXIT', Reason} ->
   440             unregister_name(Name0),
   441             proc_lib:init_ack(Starter, {error, Reason}),
   442             exit(Reason);
   443         Else ->
   444             Error = {bad_return_value, Else},
   445             proc_lib:init_ack(Starter, {error, Error}),
   446             exit(Error)
   447     end.
   448 
   449 name({local,Name}) -> Name;
   450 name({global,Name}) -> Name;
   451 %% name(Pid) when is_pid(Pid) -> Pid;
   452 %% when R12 goes away, drop the line beneath and uncomment the line above
   453 name(Name) -> Name.
   454 
   455 unregister_name({local,Name}) ->
   456     _ = (catch unregister(Name));
   457 unregister_name({global,Name}) ->
   458     _ = global:unregister_name(Name);
   459 unregister_name(Pid) when is_pid(Pid) ->
   460     Pid;
   461 %% Under R12 let's just ignore it, as we have a single term as Name.
   462 %% On R13 it will never get here, as we get tuple with 'local/global' atom.
   463 unregister_name(_Name) -> ok.
   464 
   465 extend_backoff(undefined) ->
   466     undefined;
   467 extend_backoff({backoff, InitialTimeout, MinimumTimeout, DesiredHibPeriod}) ->
   468     {backoff, InitialTimeout, MinimumTimeout, DesiredHibPeriod, now()}.
   469 
   470 %%%========================================================================
   471 %%% Internal functions
   472 %%%========================================================================
   473 %%% ---------------------------------------------------
   474 %%% The MAIN loop.
   475 %%% ---------------------------------------------------
   476 loop(GS2State = #gs2_state { time          = hibernate,
   477                              timeout_state = undefined }) ->
   478     pre_hibernate(GS2State);
   479 loop(GS2State) ->
   480     process_next_msg(drain(GS2State)).
   481 
   482 drain(GS2State) ->
   483     receive
   484         Input -> drain(in(Input, GS2State))
   485     after 0 -> GS2State
   486     end.
   487 
   488 process_next_msg(GS2State = #gs2_state { time          = Time,
   489                                          timeout_state = TimeoutState,
   490                                          queue         = Queue }) ->
   491     case priority_queue:out(Queue) of
   492         {{value, Msg}, Queue1} ->
   493             process_msg(Msg, GS2State #gs2_state { queue = Queue1 });
   494         {empty, Queue1} ->
   495             {Time1, HibOnTimeout}
   496                 = case {Time, TimeoutState} of
   497                       {hibernate, {backoff, Current, _Min, _Desired, _RSt}} ->
   498                           {Current, true};
   499                       {hibernate, _} ->
   500                           %% wake_hib/7 will set Time to hibernate. If
   501                           %% we were woken and didn't receive a msg
   502                           %% then we will get here and need a sensible
   503                           %% value for Time1, otherwise we crash.
   504                           %% R13B1 always waits infinitely when waking
   505                           %% from hibernation, so that's what we do
   506                           %% here too.
   507                           {infinity, false};
   508                       _ -> {Time, false}
   509                   end,
   510             receive
   511                 Input ->
   512                     %% Time could be 'hibernate' here, so *don't* call loop
   513                     process_next_msg(
   514                       drain(in(Input, GS2State #gs2_state { queue = Queue1 })))
   515             after Time1 ->
   516                     case HibOnTimeout of
   517                         true ->
   518                             pre_hibernate(
   519                               GS2State #gs2_state { queue = Queue1 });
   520                         false ->
   521                             process_msg(timeout,
   522                                         GS2State #gs2_state { queue = Queue1 })
   523                     end
   524             end
   525     end.
   526 
   527 wake_hib(GS2State = #gs2_state { timeout_state = TS }) ->
   528     TimeoutState1 = case TS of
   529                         undefined ->
   530                             undefined;
   531                         {SleptAt, TimeoutState} ->
   532                             adjust_timeout_state(SleptAt, now(), TimeoutState)
   533                     end,
   534     post_hibernate(
   535       drain(GS2State #gs2_state { timeout_state = TimeoutState1 })).
   536 
   537 hibernate(GS2State = #gs2_state { timeout_state = TimeoutState }) ->
   538     TS = case TimeoutState of
   539              undefined             -> undefined;
   540              {backoff, _, _, _, _} -> {now(), TimeoutState}
   541          end,
   542     proc_lib:hibernate(?MODULE, wake_hib,
   543                        [GS2State #gs2_state { timeout_state = TS }]).
   544 
   545 pre_hibernate(GS2State = #gs2_state { state   = State,
   546                                       mod     = Mod }) ->
   547     case erlang:function_exported(Mod, handle_pre_hibernate, 1) of
   548         true ->
   549             case catch Mod:handle_pre_hibernate(State) of
   550                 {hibernate, NState} ->
   551                     hibernate(GS2State #gs2_state { state = NState } );
   552                 Reply ->
   553                     handle_common_termination(Reply, pre_hibernate, GS2State)
   554             end;
   555         false ->
   556             hibernate(GS2State)
   557     end.
   558 
   559 post_hibernate(GS2State = #gs2_state { state = State,
   560                                        mod   = Mod }) ->
   561     case erlang:function_exported(Mod, handle_post_hibernate, 1) of
   562         true ->
   563             case catch Mod:handle_post_hibernate(State) of
   564                 {noreply, NState} ->
   565                     process_next_msg(GS2State #gs2_state { state = NState,
   566                                                            time  = infinity });
   567                 {noreply, NState, Time} ->
   568                     process_next_msg(GS2State #gs2_state { state = NState,
   569                                                            time  = Time });
   570                 Reply ->
   571                     handle_common_termination(Reply, post_hibernate, GS2State)
   572             end;
   573         false ->
   574             %% use hibernate here, not infinity. This matches
   575             %% R13B. The key is that we should be able to get through
   576             %% to process_msg calling sys:handle_system_msg with Time
   577             %% still set to hibernate, iff that msg is the very msg
   578             %% that woke us up (or the first msg we receive after
   579             %% waking up).
   580             process_next_msg(GS2State #gs2_state { time = hibernate })
   581     end.
   582 
   583 adjust_timeout_state(SleptAt, AwokeAt, {backoff, CurrentTO, MinimumTO,
   584                                         DesiredHibPeriod, RandomState}) ->
   585     NapLengthMicros = timer:now_diff(AwokeAt, SleptAt),
   586     CurrentMicros = CurrentTO * 1000,
   587     MinimumMicros = MinimumTO * 1000,
   588     DesiredHibMicros = DesiredHibPeriod * 1000,
   589     GapBetweenMessagesMicros = NapLengthMicros + CurrentMicros,
   590     Base =
   591         %% If enough time has passed between the last two messages then we
   592         %% should consider sleeping sooner. Otherwise stay awake longer.
   593         case GapBetweenMessagesMicros > (MinimumMicros + DesiredHibMicros) of
   594             true -> lists:max([MinimumTO, CurrentTO div 2]);
   595             false -> CurrentTO
   596         end,
   597     {Extra, RandomState1} = random:uniform_s(Base, RandomState),
   598     CurrentTO1 = Base + Extra,
   599     {backoff, CurrentTO1, MinimumTO, DesiredHibPeriod, RandomState1}.
   600 
   601 in({'$gen_cast', Msg} = Input,
   602    GS2State = #gs2_state { prioritise_cast = PC }) ->
   603     in(Input, PC(Msg, GS2State), GS2State);
   604 in({'$gen_call', From, Msg} = Input,
   605    GS2State = #gs2_state { prioritise_call = PC }) ->
   606     in(Input, PC(Msg, From, GS2State), GS2State);
   607 in({'EXIT', Parent, _R} = Input, GS2State = #gs2_state { parent = Parent }) ->
   608     in(Input, infinity, GS2State);
   609 in({system, _From, _Req} = Input, GS2State) ->
   610     in(Input, infinity, GS2State);
   611 in(Input, GS2State = #gs2_state { prioritise_info = PI }) ->
   612     in(Input, PI(Input, GS2State), GS2State).
   613 
   614 in(Input, Priority, GS2State = #gs2_state { queue = Queue }) ->
   615     GS2State # gs2_state { queue = priority_queue:in(Input, Priority, Queue) }.
   616 
   617 process_msg({system, From, Req},
   618             GS2State = #gs2_state { parent = Parent, debug  = Debug }) ->
   619     %% gen_server puts Hib on the end as the 7th arg, but that version
   620     %% of the fun seems not to be documented so leaving out for now.
   621     sys:handle_system_msg(Req, From, Parent, ?MODULE, Debug, GS2State);
   622 process_msg({'EXIT', Parent, Reason} = Msg,
   623             GS2State = #gs2_state { parent = Parent }) ->
   624     terminate(Reason, Msg, GS2State);
   625 process_msg(Msg, GS2State = #gs2_state { debug  = [] }) ->
   626     handle_msg(Msg, GS2State);
   627 process_msg(Msg, GS2State = #gs2_state { name = Name, debug  = Debug }) ->
   628     Debug1 = sys:handle_debug(Debug, fun print_event/3, Name, {in, Msg}),
   629     handle_msg(Msg, GS2State #gs2_state { debug = Debug1 }).
   630 
   631 %%% ---------------------------------------------------
   632 %%% Send/recive functions
   633 %%% ---------------------------------------------------
   634 do_send(Dest, Msg) ->
   635     catch erlang:send(Dest, Msg).
   636 
   637 do_multi_call(Nodes, Name, Req, infinity) ->
   638     Tag = make_ref(),
   639     Monitors = send_nodes(Nodes, Name, Tag, Req),
   640     rec_nodes(Tag, Monitors, Name, undefined);
   641 do_multi_call(Nodes, Name, Req, Timeout) ->
   642     Tag = make_ref(),
   643     Caller = self(),
   644     Receiver =
   645         spawn(
   646           fun () ->
   647                   %% Middleman process. Should be unsensitive to regular
   648                   %% exit signals. The sychronization is needed in case
   649                   %% the receiver would exit before the caller started
   650                   %% the monitor.
   651                   process_flag(trap_exit, true),
   652                   Mref = erlang:monitor(process, Caller),
   653                   receive
   654                       {Caller,Tag} ->
   655                           Monitors = send_nodes(Nodes, Name, Tag, Req),
   656                           TimerId = erlang:start_timer(Timeout, self(), ok),
   657                           Result = rec_nodes(Tag, Monitors, Name, TimerId),
   658                           exit({self(),Tag,Result});
   659                       {'DOWN',Mref,_,_,_} ->
   660                           %% Caller died before sending us the go-ahead.
   661                           %% Give up silently.
   662                           exit(normal)
   663                   end
   664           end),
   665     Mref = erlang:monitor(process, Receiver),
   666     Receiver ! {self(),Tag},
   667     receive
   668         {'DOWN',Mref,_,_,{Receiver,Tag,Result}} ->
   669             Result;
   670         {'DOWN',Mref,_,_,Reason} ->
   671             %% The middleman code failed. Or someone did
   672             %% exit(_, kill) on the middleman process => Reason==killed
   673             exit(Reason)
   674     end.
   675 
   676 send_nodes(Nodes, Name, Tag, Req) ->
   677     send_nodes(Nodes, Name, Tag, Req, []).
   678 
   679 send_nodes([Node|Tail], Name, Tag, Req, Monitors)
   680   when is_atom(Node) ->
   681     Monitor = start_monitor(Node, Name),
   682     %% Handle non-existing names in rec_nodes.
   683     catch {Name, Node} ! {'$gen_call', {self(), {Tag, Node}}, Req},
   684     send_nodes(Tail, Name, Tag, Req, [Monitor | Monitors]);
   685 send_nodes([_Node|Tail], Name, Tag, Req, Monitors) ->
   686     %% Skip non-atom Node
   687     send_nodes(Tail, Name, Tag, Req, Monitors);
   688 send_nodes([], _Name, _Tag, _Req, Monitors) ->
   689     Monitors.
   690 
   691 %% Against old nodes:
   692 %% If no reply has been delivered within 2 secs. (per node) check that
   693 %% the server really exists and wait for ever for the answer.
   694 %%
   695 %% Against contemporary nodes:
   696 %% Wait for reply, server 'DOWN', or timeout from TimerId.
   697 
   698 rec_nodes(Tag, Nodes, Name, TimerId) ->
   699     rec_nodes(Tag, Nodes, Name, [], [], 2000, TimerId).
   700 
   701 rec_nodes(Tag, [{N,R}|Tail], Name, Badnodes, Replies, Time, TimerId ) ->
   702     receive
   703         {'DOWN', R, _, _, _} ->
   704             rec_nodes(Tag, Tail, Name, [N|Badnodes], Replies, Time, TimerId);
   705         {{Tag, N}, Reply} ->  %% Tag is bound !!!
   706             unmonitor(R),
   707             rec_nodes(Tag, Tail, Name, Badnodes,
   708                       [{N,Reply}|Replies], Time, TimerId);
   709         {timeout, TimerId, _} ->
   710             unmonitor(R),
   711             %% Collect all replies that already have arrived
   712             rec_nodes_rest(Tag, Tail, Name, [N|Badnodes], Replies)
   713     end;
   714 rec_nodes(Tag, [N|Tail], Name, Badnodes, Replies, Time, TimerId) ->
   715     %% R6 node
   716     receive
   717         {nodedown, N} ->
   718             monitor_node(N, false),
   719             rec_nodes(Tag, Tail, Name, [N|Badnodes], Replies, 2000, TimerId);
   720         {{Tag, N}, Reply} ->  %% Tag is bound !!!
   721             receive {nodedown, N} -> ok after 0 -> ok end,
   722             monitor_node(N, false),
   723             rec_nodes(Tag, Tail, Name, Badnodes,
   724                       [{N,Reply}|Replies], 2000, TimerId);
   725         {timeout, TimerId, _} ->
   726             receive {nodedown, N} -> ok after 0 -> ok end,
   727             monitor_node(N, false),
   728             %% Collect all replies that already have arrived
   729             rec_nodes_rest(Tag, Tail, Name, [N | Badnodes], Replies)
   730     after Time ->
   731             case rpc:call(N, erlang, whereis, [Name]) of
   732                 Pid when is_pid(Pid) -> % It exists try again.
   733                     rec_nodes(Tag, [N|Tail], Name, Badnodes,
   734                               Replies, infinity, TimerId);
   735                 _ -> % badnode
   736                     receive {nodedown, N} -> ok after 0 -> ok end,
   737                     monitor_node(N, false),
   738                     rec_nodes(Tag, Tail, Name, [N|Badnodes],
   739                               Replies, 2000, TimerId)
   740             end
   741     end;
   742 rec_nodes(_, [], _, Badnodes, Replies, _, TimerId) ->
   743     case catch erlang:cancel_timer(TimerId) of
   744         false ->  % It has already sent it's message
   745             receive
   746                 {timeout, TimerId, _} -> ok
   747             after 0 ->
   748                     ok
   749             end;
   750         _ -> % Timer was cancelled, or TimerId was 'undefined'
   751             ok
   752     end,
   753     {Replies, Badnodes}.
   754 
   755 %% Collect all replies that already have arrived
   756 rec_nodes_rest(Tag, [{N,R}|Tail], Name, Badnodes, Replies) ->
   757     receive
   758         {'DOWN', R, _, _, _} ->
   759             rec_nodes_rest(Tag, Tail, Name, [N|Badnodes], Replies);
   760         {{Tag, N}, Reply} -> %% Tag is bound !!!
   761             unmonitor(R),
   762             rec_nodes_rest(Tag, Tail, Name, Badnodes, [{N,Reply}|Replies])
   763     after 0 ->
   764             unmonitor(R),
   765             rec_nodes_rest(Tag, Tail, Name, [N|Badnodes], Replies)
   766     end;
   767 rec_nodes_rest(Tag, [N|Tail], Name, Badnodes, Replies) ->
   768     %% R6 node
   769     receive
   770         {nodedown, N} ->
   771             monitor_node(N, false),
   772             rec_nodes_rest(Tag, Tail, Name, [N|Badnodes], Replies);
   773         {{Tag, N}, Reply} ->  %% Tag is bound !!!
   774             receive {nodedown, N} -> ok after 0 -> ok end,
   775             monitor_node(N, false),
   776             rec_nodes_rest(Tag, Tail, Name, Badnodes, [{N,Reply}|Replies])
   777     after 0 ->
   778             receive {nodedown, N} -> ok after 0 -> ok end,
   779             monitor_node(N, false),
   780             rec_nodes_rest(Tag, Tail, Name, [N|Badnodes], Replies)
   781     end;
   782 rec_nodes_rest(_Tag, [], _Name, Badnodes, Replies) ->
   783     {Replies, Badnodes}.
   784 
   785 
   786 %%% ---------------------------------------------------
   787 %%% Monitor functions
   788 %%% ---------------------------------------------------
   789 
   790 start_monitor(Node, Name) when is_atom(Node), is_atom(Name) ->
   791     if node() =:= nonode@nohost, Node =/= nonode@nohost ->
   792             Ref = make_ref(),
   793             self() ! {'DOWN', Ref, process, {Name, Node}, noconnection},
   794             {Node, Ref};
   795        true ->
   796             case catch erlang:monitor(process, {Name, Node}) of
   797                 {'EXIT', _} ->
   798                     %% Remote node is R6
   799                     monitor_node(Node, true),
   800                     Node;
   801                 Ref when is_reference(Ref) ->
   802                     {Node, Ref}
   803             end
   804     end.
   805 
   806 %% Cancels a monitor started with Ref=erlang:monitor(_, _).
   807 unmonitor(Ref) when is_reference(Ref) ->
   808     erlang:demonitor(Ref),
   809     receive
   810         {'DOWN', Ref, _, _, _} ->
   811             true
   812     after 0 ->
   813             true
   814     end.
   815 
   816 %%% ---------------------------------------------------
   817 %%% Message handling functions
   818 %%% ---------------------------------------------------
   819 
   820 dispatch({'$gen_cast', Msg}, Mod, State) ->
   821     Mod:handle_cast(Msg, State);
   822 dispatch(Info, Mod, State) ->
   823     Mod:handle_info(Info, State).
   824 
   825 common_reply(_Name, From, Reply, _NState, [] = _Debug) ->
   826     reply(From, Reply),
   827     [];
   828 common_reply(Name, From, Reply, NState, Debug) ->
   829     reply(Name, From, Reply, NState, Debug).
   830 
   831 common_debug([] = _Debug, _Func, _Info, _Event) ->
   832     [];
   833 common_debug(Debug, Func, Info, Event) ->
   834     sys:handle_debug(Debug, Func, Info, Event).
   835 
   836 handle_msg({'$gen_call', From, Msg}, GS2State = #gs2_state { mod = Mod,
   837                                                              state = State,
   838                                                              name = Name,
   839                                                              debug = Debug }) ->
   840     case catch Mod:handle_call(Msg, From, State) of
   841         {reply, Reply, NState} ->
   842             Debug1 = common_reply(Name, From, Reply, NState, Debug),
   843             loop(GS2State #gs2_state { state = NState,
   844                                        time  = infinity,
   845                                        debug = Debug1 });
   846         {reply, Reply, NState, Time1} ->
   847             Debug1 = common_reply(Name, From, Reply, NState, Debug),
   848             loop(GS2State #gs2_state { state = NState,
   849                                        time  = Time1,
   850                                        debug = Debug1});
   851         {noreply, NState} ->
   852             Debug1 = common_debug(Debug, fun print_event/3, Name,
   853                                   {noreply, NState}),
   854             loop(GS2State #gs2_state {state = NState,
   855                                       time  = infinity,
   856                                       debug = Debug1});
   857         {noreply, NState, Time1} ->
   858             Debug1 = common_debug(Debug, fun print_event/3, Name,
   859                                   {noreply, NState}),
   860             loop(GS2State #gs2_state {state = NState,
   861                                       time  = Time1,
   862                                       debug = Debug1});
   863         {stop, Reason, Reply, NState} ->
   864             {'EXIT', R} =
   865                 (catch terminate(Reason, Msg,
   866                                  GS2State #gs2_state { state = NState })),
   867             reply(Name, From, Reply, NState, Debug),
   868             exit(R);
   869         Other ->
   870             handle_common_reply(Other, Msg, GS2State)
   871     end;
   872 handle_msg(Msg, GS2State = #gs2_state { mod = Mod, state = State }) ->
   873     Reply = (catch dispatch(Msg, Mod, State)),
   874     handle_common_reply(Reply, Msg, GS2State).
   875 
   876 handle_common_reply(Reply, Msg, GS2State = #gs2_state { name  = Name,
   877                                                         debug = Debug}) ->
   878     case Reply of
   879         {noreply, NState} ->
   880             Debug1 = common_debug(Debug, fun print_event/3, Name,
   881                                   {noreply, NState}),
   882             loop(GS2State #gs2_state { state = NState,
   883                                        time  = infinity,
   884                                        debug = Debug1 });
   885         {noreply, NState, Time1} ->
   886             Debug1 = common_debug(Debug, fun print_event/3, Name,
   887                                   {noreply, NState}),
   888             loop(GS2State #gs2_state { state = NState,
   889                                        time  = Time1,
   890                                        debug = Debug1 });
   891         {become, Mod, NState} ->
   892             Debug1 = common_debug(Debug, fun print_event/3, Name,
   893                                   {become, Mod, NState}),
   894             loop(find_prioritisers(
   895                    GS2State #gs2_state { mod   = Mod,
   896                                          state = NState,
   897                                          time  = infinity,
   898                                          debug = Debug1 }));
   899         {become, Mod, NState, Time1} ->
   900             Debug1 = common_debug(Debug, fun print_event/3, Name,
   901                                   {become, Mod, NState}),
   902             loop(find_prioritisers(
   903                    GS2State #gs2_state { mod   = Mod,
   904                                          state = NState,
   905                                          time  = Time1,
   906                                          debug = Debug1 }));
   907         _ ->
   908             handle_common_termination(Reply, Msg, GS2State)
   909     end.
   910 
   911 handle_common_termination(Reply, Msg, GS2State) ->
   912     case Reply of
   913         {stop, Reason, NState} ->
   914             terminate(Reason, Msg, GS2State #gs2_state { state = NState });
   915         {'EXIT', What} ->
   916             terminate(What, Msg, GS2State);
   917         _ ->
   918             terminate({bad_return_value, Reply}, Msg, GS2State)
   919     end.
   920 
   921 reply(Name, {To, Tag}, Reply, State, Debug) ->
   922     reply({To, Tag}, Reply),
   923     sys:handle_debug(
   924       Debug, fun print_event/3, Name, {out, Reply, To, State}).
   925 
   926 
   927 %%-----------------------------------------------------------------
   928 %% Callback functions for system messages handling.
   929 %%-----------------------------------------------------------------
   930 system_continue(Parent, Debug, GS2State) ->
   931     loop(GS2State #gs2_state { parent = Parent, debug = Debug }).
   932 
   933 system_terminate(Reason, _Parent, Debug, GS2State) ->
   934     terminate(Reason, [], GS2State #gs2_state { debug = Debug }).
   935 
   936 system_code_change(GS2State = #gs2_state { mod   = Mod,
   937                                            state = State },
   938                    _Module, OldVsn, Extra) ->
   939     case catch Mod:code_change(OldVsn, State, Extra) of
   940         {ok, NewState} ->
   941             NewGS2State = find_prioritisers(
   942                             GS2State #gs2_state { state = NewState }),
   943             {ok, [NewGS2State]};
   944         Else ->
   945             Else
   946     end.
   947 
   948 %%-----------------------------------------------------------------
   949 %% Format debug messages.  Print them as the call-back module sees
   950 %% them, not as the real erlang messages.  Use trace for that.
   951 %%-----------------------------------------------------------------
   952 print_event(Dev, {in, Msg}, Name) ->
   953     case Msg of
   954         {'$gen_call', {From, _Tag}, Call} ->
   955             io:format(Dev, "*DBG* ~p got call ~p from ~w~n",
   956                       [Name, Call, From]);
   957         {'$gen_cast', Cast} ->
   958             io:format(Dev, "*DBG* ~p got cast ~p~n",
   959                       [Name, Cast]);
   960         _ ->
   961             io:format(Dev, "*DBG* ~p got ~p~n", [Name, Msg])
   962     end;
   963 print_event(Dev, {out, Msg, To, State}, Name) ->
   964     io:format(Dev, "*DBG* ~p sent ~p to ~w, new state ~w~n",
   965               [Name, Msg, To, State]);
   966 print_event(Dev, {noreply, State}, Name) ->
   967     io:format(Dev, "*DBG* ~p new state ~w~n", [Name, State]);
   968 print_event(Dev, Event, Name) ->
   969     io:format(Dev, "*DBG* ~p dbg  ~p~n", [Name, Event]).
   970 
   971 
   972 %%% ---------------------------------------------------
   973 %%% Terminate the server.
   974 %%% ---------------------------------------------------
   975 
   976 terminate(Reason, Msg, #gs2_state { name  = Name,
   977                                     mod   = Mod,
   978                                     state = State,
   979                                     debug = Debug }) ->
   980     case catch Mod:terminate(Reason, State) of
   981         {'EXIT', R} ->
   982             error_info(R, Reason, Name, Msg, State, Debug),
   983             exit(R);
   984         _ ->
   985             case Reason of
   986                 normal ->
   987                     exit(normal);
   988                 shutdown ->
   989                     exit(shutdown);
   990                 {shutdown,_}=Shutdown ->
   991                     exit(Shutdown);
   992                 _ ->
   993                     error_info(Reason, undefined, Name, Msg, State, Debug),
   994                     exit(Reason)
   995             end
   996     end.
   997 
   998 error_info(_Reason, _RootCause, application_controller, _Msg, _State, _Debug) ->
   999     %% OTP-5811 Don't send an error report if it's the system process
  1000     %% application_controller which is terminating - let init take care
  1001     %% of it instead
  1002     ok;
  1003 error_info(Reason, RootCause, Name, Msg, State, Debug) ->
  1004     Reason1 = error_reason(Reason),
  1005     Fmt =
  1006         "** Generic server ~p terminating~n"
  1007         "** Last message in was ~p~n"
  1008         "** When Server state == ~p~n"
  1009         "** Reason for termination == ~n** ~p~n",
  1010     case RootCause of
  1011         undefined -> format(Fmt, [Name, Msg, State, Reason1]);
  1012         _         -> format(Fmt ++ "** In 'terminate' callback "
  1013                             "with reason ==~n** ~p~n",
  1014                             [Name, Msg, State, Reason1,
  1015                              error_reason(RootCause)])
  1016     end,
  1017     sys:print_log(Debug),
  1018     ok.
  1019 
  1020 error_reason({undef,[{M,F,A}|MFAs]} = Reason) ->
  1021     case code:is_loaded(M) of
  1022         false -> {'module could not be loaded',[{M,F,A}|MFAs]};
  1023         _     -> case erlang:function_exported(M, F, length(A)) of
  1024                      true  -> Reason;
  1025                      false -> {'function not exported',[{M,F,A}|MFAs]}
  1026                  end
  1027     end;
  1028 error_reason(Reason) ->
  1029     Reason.
  1030 
  1031 %%% ---------------------------------------------------
  1032 %%% Misc. functions.
  1033 %%% ---------------------------------------------------
  1034 
  1035 opt(Op, [{Op, Value}|_]) ->
  1036     {ok, Value};
  1037 opt(Op, [_|Options]) ->
  1038     opt(Op, Options);
  1039 opt(_, []) ->
  1040     false.
  1041 
  1042 debug_options(Name, Opts) ->
  1043     case opt(debug, Opts) of
  1044         {ok, Options} -> dbg_options(Name, Options);
  1045         _ -> dbg_options(Name, [])
  1046     end.
  1047 
  1048 dbg_options(Name, []) ->
  1049     Opts =
  1050         case init:get_argument(generic_debug) of
  1051             error ->
  1052                 [];
  1053             _ ->
  1054                 [log, statistics]
  1055         end,
  1056     dbg_opts(Name, Opts);
  1057 dbg_options(Name, Opts) ->
  1058     dbg_opts(Name, Opts).
  1059 
  1060 dbg_opts(Name, Opts) ->
  1061     case catch sys:debug_options(Opts) of
  1062         {'EXIT',_} ->
  1063             format("~p: ignoring erroneous debug options - ~p~n",
  1064                    [Name, Opts]),
  1065             [];
  1066         Dbg ->
  1067             Dbg
  1068     end.
  1069 
  1070 get_proc_name(Pid) when is_pid(Pid) ->
  1071     Pid;
  1072 get_proc_name({local, Name}) ->
  1073     case process_info(self(), registered_name) of
  1074         {registered_name, Name} ->
  1075             Name;
  1076         {registered_name, _Name} ->
  1077             exit(process_not_registered);
  1078         [] ->
  1079             exit(process_not_registered)
  1080     end;
  1081 get_proc_name({global, Name}) ->
  1082     case whereis_name(Name) of
  1083         undefined ->
  1084             exit(process_not_registered_globally);
  1085         Pid when Pid =:= self() ->
  1086             Name;
  1087         _Pid ->
  1088             exit(process_not_registered_globally)
  1089     end.
  1090 
  1091 get_parent() ->
  1092     case get('$ancestors') of
  1093         [Parent | _] when is_pid(Parent)->
  1094             Parent;
  1095         [Parent | _] when is_atom(Parent)->
  1096             name_to_pid(Parent);
  1097         _ ->
  1098             exit(process_was_not_started_by_proc_lib)
  1099     end.
  1100 
  1101 name_to_pid(Name) ->
  1102     case whereis(Name) of
  1103         undefined ->
  1104             case whereis_name(Name) of
  1105                 undefined ->
  1106                     exit(could_not_find_registerd_name);
  1107                 Pid ->
  1108                     Pid
  1109             end;
  1110         Pid ->
  1111             Pid
  1112     end.
  1113 
  1114 whereis_name(Name) ->
  1115     case ets:lookup(global_names, Name) of
  1116     [{_Name, Pid, _Method, _RPid, _Ref}] ->
  1117         if node(Pid) == node() ->
  1118             case is_process_alive(Pid) of
  1119             true  -> Pid;
  1120             false -> undefined
  1121             end;
  1122            true ->
  1123             Pid
  1124         end;
  1125     [] -> undefined
  1126     end.
  1127 
  1128 find_prioritisers(GS2State = #gs2_state { mod = Mod }) ->
  1129     PrioriCall = function_exported_or_default(
  1130                    Mod, 'prioritise_call', 3,
  1131                    fun (_Msg, _From, _State) -> 0 end),
  1132     PrioriCast = function_exported_or_default(Mod, 'prioritise_cast', 2,
  1133                                               fun (_Msg, _State) -> 0 end),
  1134     PrioriInfo = function_exported_or_default(Mod, 'prioritise_info', 2,
  1135                                               fun (_Msg, _State) -> 0 end),
  1136     GS2State #gs2_state { prioritise_call = PrioriCall,
  1137                           prioritise_cast = PrioriCast,
  1138                           prioritise_info = PrioriInfo }.
  1139 
  1140 function_exported_or_default(Mod, Fun, Arity, Default) ->
  1141     case erlang:function_exported(Mod, Fun, Arity) of
  1142         true -> case Arity of
  1143                     2 -> fun (Msg, GS2State = #gs2_state { state = State }) ->
  1144                                  case catch Mod:Fun(Msg, State) of
  1145                                      Res when is_integer(Res) ->
  1146                                          Res;
  1147                                      Err ->
  1148                                          handle_common_termination(Err, Msg, GS2State)
  1149                                  end
  1150                          end;
  1151                     3 -> fun (Msg, From, GS2State = #gs2_state { state = State }) ->
  1152                                  case catch Mod:Fun(Msg, From, State) of
  1153                                      Res when is_integer(Res) ->
  1154                                          Res;
  1155                                      Err ->
  1156                                          handle_common_termination(Err, Msg, GS2State)
  1157                                  end
  1158                          end
  1159                 end;
  1160         false -> Default
  1161     end.
  1162 
  1163 %%-----------------------------------------------------------------
  1164 %% Status information
  1165 %%-----------------------------------------------------------------
  1166 format_status(Opt, StatusData) ->
  1167     [PDict, SysState, Parent, Debug,
  1168      #gs2_state{name = Name, state = State, mod = Mod, queue = Queue}] =
  1169         StatusData,
  1170     NameTag = if is_pid(Name) ->
  1171                       pid_to_list(Name);
  1172                  is_atom(Name) ->
  1173                       Name
  1174               end,
  1175     Header = lists:concat(["Status for generic server ", NameTag]),
  1176     Log = sys:get_debug(log, Debug, []),
  1177     Specfic = callback(Mod, format_status, [Opt, [PDict, State]],
  1178                        fun () -> [{data, [{"State", State}]}] end),
  1179     Messages = callback(Mod, format_message_queue, [Opt, Queue],
  1180                         fun () -> priority_queue:to_list(Queue) end),
  1181     [{header, Header},
  1182      {data, [{"Status", SysState},
  1183              {"Parent", Parent},
  1184              {"Logged events", Log},
  1185              {"Queued messages", Messages}]} |
  1186      Specfic].
  1187 
  1188 callback(Mod, FunName, Args, DefaultThunk) ->
  1189     case erlang:function_exported(Mod, FunName, length(Args)) of
  1190         true  -> case catch apply(Mod, FunName, Args) of
  1191                      {'EXIT', _} -> DefaultThunk();
  1192                      Success     -> Success
  1193                  end;
  1194         false -> DefaultThunk()
  1195     end.