aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib/src
diff options
context:
space:
mode:
authorSiri Hansen <[email protected]>2011-04-28 12:44:05 +0200
committerSiri Hansen <[email protected]>2011-04-28 12:44:05 +0200
commitfbe3499614deb0165e93d03e6d84cfd8a3510958 (patch)
tree407a63fd0ed2b6e421483430f04f95f316ae3984 /lib/stdlib/src
parent269f663aa051a1de83ee51d46a09cf62cde3d55b (diff)
parent1a8341595c1bca1f46aa0afee8e5f01675379980 (diff)
downloadotp-fbe3499614deb0165e93d03e6d84cfd8a3510958.tar.gz
otp-fbe3499614deb0165e93d03e6d84cfd8a3510958.tar.bz2
otp-fbe3499614deb0165e93d03e6d84cfd8a3510958.zip
Merge branch 'siri/stdlib/supervisor-terminate_child-simple_one_for_one/OTP-9201' into dev
* siri/stdlib/supervisor-terminate_child-simple_one_for_one/OTP-9201: Add terminate_child(Sup, Pid) for simple_one_for_one Allow supervisor:terminate_child(SupRef,Pid) for simple_one_for_one
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r--lib/stdlib/src/supervisor.erl58
1 files changed, 42 insertions, 16 deletions
diff --git a/lib/stdlib/src/supervisor.erl b/lib/stdlib/src/supervisor.erl
index 368dc2e3e5..4fd7f1d47c 100644
--- a/lib/stdlib/src/supervisor.erl
+++ b/lib/stdlib/src/supervisor.erl
@@ -138,7 +138,7 @@ delete_child(Supervisor, Name) ->
%%-----------------------------------------------------------------
-type term_err() :: 'not_found' | 'simple_one_for_one'.
--spec terminate_child(sup_ref(), term()) -> 'ok' | {'error', term_err()}.
+-spec terminate_child(sup_ref(), pid() | term()) -> 'ok' | {'error', term_err()}.
terminate_child(Supervisor, Name) ->
call(Supervisor, {terminate_child, Name}).
@@ -297,8 +297,26 @@ handle_call({start_child, EArgs}, _From, State) when ?is_simple(State) ->
{reply, What, State}
end;
-%%% The requests terminate_child, delete_child and restart_child are
-%%% invalid for simple_one_for_one supervisors.
+%% terminate_child for simple_one_for_one can only be done with pid
+handle_call({terminate_child, Name}, _From, State) when not is_pid(Name),
+ ?is_simple(State) ->
+ {reply, {error, simple_one_for_one}, State};
+
+handle_call({terminate_child, Name}, _From, State) ->
+ case get_child(Name, State, ?is_simple(State)) of
+ {value, Child} ->
+ case do_terminate(Child, State#state.name) of
+ #child{restart_type=RT} when RT=:=temporary; ?is_simple(State) ->
+ {reply, ok, state_del_child(Child, State)};
+ NChild ->
+ {reply, ok, replace_child(NChild, State)}
+ end;
+ false ->
+ {reply, {error, not_found}, State}
+ end;
+
+%%% The requests delete_child and restart_child are invalid for
+%%% simple_one_for_one supervisors.
handle_call({_Req, _Data}, _From, State) when ?is_simple(State) ->
{reply, {error, simple_one_for_one}, State};
@@ -341,19 +359,6 @@ handle_call({delete_child, Name}, _From, State) ->
{reply, {error, not_found}, State}
end;
-handle_call({terminate_child, Name}, _From, State) ->
- case get_child(Name, State) of
- {value, Child} ->
- case do_terminate(Child, State#state.name) of
- #child{restart_type = temporary} = NChild ->
- {reply, ok, state_del_child(NChild, State)};
- NChild ->
- {reply, ok, replace_child(NChild, State)}
- end;
- _ ->
- {reply, {error, not_found}, State}
- end;
-
handle_call(which_children, _From, #state{children = [#child{restart_type = temporary,
child_type = CT,
modules = Mods}]} =
@@ -849,7 +854,28 @@ split_child(_, [], After) ->
{lists:reverse(After), []}.
get_child(Name, State) ->
+ get_child(Name, State, false).
+get_child(Pid, State, AllowPid) when AllowPid, is_pid(Pid) ->
+ get_dynamic_child(Pid, State);
+get_child(Name, State, _) ->
lists:keysearch(Name, #child.name, State#state.children).
+
+get_dynamic_child(Pid, #state{children=[Child], dynamics=Dynamics}) ->
+ case is_dynamic_pid(Pid, dynamics_db(Child#child.restart_type, Dynamics)) of
+ true ->
+ {value, Child#child{pid=Pid}};
+ false ->
+ case erlang:is_process_alive(Pid) of
+ true -> false;
+ false -> {value, Child}
+ end
+ end.
+
+is_dynamic_pid(Pid, Dynamics) when is_list(Dynamics) ->
+ lists:member(Pid, Dynamics);
+is_dynamic_pid(Pid, Dynamics) ->
+ dict:is_key(Pid, Dynamics).
+
replace_child(Child, State) ->
Chs = do_replace_child(Child, State#state.children),
State#state{children = Chs}.