Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's gen:start meaning?

Tags:

erlang

In project gproc's file gen_leader.erl,a customized behavior is created. But In the following statement, what is module "gen"? I can't find this module in the "erlang document tools http://www.erlang.org/erldoc"? Could you give me some explanation?

behaviour_info(callbacks) ->
    [{init,1},
     {elected,2},
     {surrendered,3},
     {handle_leader_call,4},
     {handle_leader_cast,3},
     {handle_local_only, 4},
     {from_leader,3},
     {handle_call,3},
     {handle_cast,2},
     {handle_DOWN,3},
     {handle_info,2},
     {terminate,2},
     {code_change,4}];
behaviour_info(_Other) ->
    undefined.

start_link(Name, [_|_] = CandidateNodes, Workers, 
       Mod, Arg, Options) when is_atom(Name) ->
    gen:start(?MODULE, link, {local,Name}, Mod, %<<++++++ What's the meaning?
          {CandidateNodes, Workers, Arg}, Options).
like image 919
Chen Yu Avatar asked Dec 22 '22 01:12

Chen Yu


1 Answers

It looks like gen:start() is referring to gen.erl. According to the documentation in the file, gen.erl implements the generic parts of gen_server, gen_fsm and other OTP behaviors. In this case, it looks like gen_start handles spawning new processes. It checks to see if a process has already been spawned with the given name. If it has, an error is returned. If it has not, a new process is spawned by calling the module's start or start_link function.

In other words, when you call gen_server:start or gen_fsm:start, it calls gen:start (which does basic sanity checking) and gen:start, in turn, calls the module's start or start_link. When you're creating custom OTP behaviors, you'll have to call gen:start directly so that you don't need to replicate the error checking code in gen.erl.

like image 104
quanticle Avatar answered Dec 23 '22 15:12

quanticle