shijiang1130 发表于 2014-11-12 22:34

gen_server:enter_loop

enter_loop(Module, Options, State)
enter_loop(Module, Options, State, ServerName)
enter_loop(Module, Options, State, Timeout)
enter_loop(Module, Options, State, ServerName, Timeout)

Types:

Module = atom()
Options =
Option = {debug,Dbgs}
Dbgs =
   Dbg = trace | log | statistics
    | {log_to_file,FileName} | {install,{Func,FuncState}}
State = term()
ServerName = {local,Name} | {global,GlobalName} | {via,Module,ViaName}
Name = atom()
GlobalName = ViaName = term()
Timeout = int() | infinity
Makes an existing process into a gen_server. Does not return, instead the calling process will enter the gen_server receive loop and become a gen_server process. The process must have been started using one of the start functions in proc_lib, see proc_lib(3). The user is responsible for any initialization of the process, including registering a name for it.

This function is useful when a more complex initialization procedure is needed than the gen_server behaviour provides.

Module, Options and ServerName have the same meanings as when calling gen_server:start/3,4. However, if ServerName is specified, the process must have been registered accordingly before this function is called.

State and Timeout have the same meanings as in the return value of Module:init/1. Also, the callback module Module does not need to export an init/1 function.

Failure: If the calling process was not started by a proc_lib start function, or if it is not registered according to ServerName.

shijiang1130 发表于 2014-11-12 22:35

没看懂:dizzy:

shijiang1130 发表于 2014-11-12 22:43

enter_loop(Mod, Options, State) ->
    enter_loop(Mod, Options, State, self(), infinity).

enter_loop(Mod, Options, State, ServerName = {Scope, _})
when Scope == local; Scope == global ->
    enter_loop(Mod, Options, State, ServerName, infinity);

enter_loop(Mod, Options, State, ServerName = {via, _, _}) ->
    enter_loop(Mod, Options, State, ServerName, infinity);

enter_loop(Mod, Options, State, Timeout) ->
    enter_loop(Mod, Options, State, self(), Timeout).

enter_loop(Mod, Options, State, ServerName, Timeout) ->
    Name = get_proc_name(ServerName),
    Parent = get_parent(),
    Debug = debug_options(Name, Options),
    loop(Parent, Name, State, Mod, Timeout, Debug).

shijiang1130 发表于 2014-11-12 22:44

%%% ---------------------------------------------------
%%% Initiate the new process.
%%% Register the name using the Rfunc function
%%% Calls the Mod:init/Args function.
%%% Finally an acknowledge is sent to Parent and the main
%%% loop is entered.
%%% ---------------------------------------------------
init_it(Starter, self, Name, Mod, Args, Options) ->
    init_it(Starter, self(), Name, Mod, Args, Options);
init_it(Starter, Parent, Name0, Mod, Args, Options) ->
    Name = name(Name0),
    Debug = debug_options(Name, Options),
    case catch Mod:init(Args) of
        {ok, State} ->
          proc_lib:init_ack(Starter, {ok, self()}),           
          loop(Parent, Name, State, Mod, infinity, Debug);
        {ok, State, Timeout} ->
          proc_lib:init_ack(Starter, {ok, self()}),           
          loop(Parent, Name, State, Mod, Timeout, Debug);
        {stop, Reason} ->
          %% For consistency, we must make sure that the
          %% registered name (if any) is unregistered before
          %% the parent process is notified about the failure.
          %% (Otherwise, the parent process could get
          %% an 'already_started' error if it immediately
          %% tried starting the process again.)
          unregister_name(Name0),
          proc_lib:init_ack(Starter, {error, Reason}),
          exit(Reason);
        ignore ->
          unregister_name(Name0),
          proc_lib:init_ack(Starter, ignore),
          exit(normal);
        {'EXIT', Reason} ->
          unregister_name(Name0),
          proc_lib:init_ack(Starter, {error, Reason}),
          exit(Reason);
        Else ->
          Error = {bad_return_value, Else},
          proc_lib:init_ack(Starter, {error, Error}),
          exit(Error)
    end.

shijiang1130 发表于 2014-11-12 22:44

啊,是这么来的

arserangel 发表于 2014-11-17 08:42

最近没有人在这里发帖子了。。。
页: [1]
查看完整版本: gen_server:enter_loop