ensure_started
Kent Boortz
kent@REDACTED
Fri Mar 21 15:28:32 CET 2003
Ulf Wiger <etxuwig@REDACTED> writes:
> I posted the following suggestion a few weeks ago, now
> slightly modified:
>
> ensure_started() ->
> Id = {{?MODULE,ensure_started}, self()},
> global:trans(
> Id, fun() ->
> case whereis(?MODULE) of
> undefined ->
> Pid = proc_lib:spawn(?MODULE,init,[]),
> register(?MODULE, Pid),
> {ok, Pid};
> Pid when pid(Pid) ->
> {ok, Pid}
> end
> end, [node()]).
>
>
> Since you know that the ensure_started() call is always
> local you can use global:trans(Id, Fun, [node()]) which will
> cut it down to two gen_server calls to the nearest global
> name server.
As an experiment I wrote a new bif that could simplify coding of
things like this. The bif will as an "atomic operation" start and
register a process if a process with that name doesn't exist.
If it does exist the pid of the existing process will be returned.
If 'link' is in SpawnOpts a link is created to the started/existing
process.
Haven't implemented the user API but it should look something like
erlang:ensure_started(RegisteredName, FunToRun, SpawnOpts)
and maybe a version for remote start
erlang:ensure_started(RegisteredName, Node, FunToRun, SpawnOpts)
With these functions it should be easy to implement a new function
proc_lib:ensure_started(...) that should cover what the code above
try to do.
Any thoughts?
kent
More information about the erlang-questions
mailing list