diff options
author | Siri Hansen <[email protected]> | 2014-06-11 14:29:19 +0200 |
---|---|---|
committer | Siri Hansen <[email protected]> | 2014-06-11 14:29:19 +0200 |
commit | da42410cb15c0749ea6cf9b6196b81320d8391b6 (patch) | |
tree | 829f5cdb59cf92cdaf381564fe51885644494650 /lib/stdlib/src/proc_lib.erl | |
parent | 5f2b70f205bc64be545b75db5419111aac11291d (diff) | |
parent | 154a057dcbf087deb38b13e97f0a0373e6a72f1d (diff) | |
download | otp-da42410cb15c0749ea6cf9b6196b81320d8391b6.tar.gz otp-da42410cb15c0749ea6cf9b6196b81320d8391b6.tar.bz2 otp-da42410cb15c0749ea6cf9b6196b81320d8391b6.zip |
Merge branch 'siri/sync-stop-gen/OTP-11173'
* siri/sync-stop-gen/OTP-11173:
Add synchronous stop function to wx_object
Fix minor bugs in gen_server and gen_fsm documentation
Update gen_event:stop to be synchronous
Add synchronous stop functions to gen_server and gen_fsm
Add synchronous stop function to proc_lib
Add system message 'terminate'
Remove old code from stdlib/test/sys_sp2.erl
Diffstat (limited to 'lib/stdlib/src/proc_lib.erl')
-rw-r--r-- | lib/stdlib/src/proc_lib.erl | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/lib/stdlib/src/proc_lib.erl b/lib/stdlib/src/proc_lib.erl index 1eb6fc2e86..c925e70613 100644 --- a/lib/stdlib/src/proc_lib.erl +++ b/lib/stdlib/src/proc_lib.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 1996-2013. All Rights Reserved. +%% Copyright Ericsson AB 1996-2014. All Rights Reserved. %% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in @@ -30,7 +30,8 @@ hibernate/3, init_ack/1, init_ack/2, init_p/3,init_p/5,format/1,format/2,initial_call/1, - translate_initial_call/1]). + translate_initial_call/1, + stop/1, stop/3]). %% Internal exports. -export([wake_up/3]). @@ -750,3 +751,50 @@ format_tag(Tag, Data) -> modifier(latin1) -> ""; modifier(_) -> "t". + + +%%% ----------------------------------------------------------- +%%% Stop a process and wait for it to terminate +%%% ----------------------------------------------------------- +-spec stop(Process) -> 'ok' when + Process :: pid() | RegName | {RegName,node()}, + RegName :: atom(). +stop(Process) -> + stop(Process, normal, infinity). + +-spec stop(Process, Reason, Timeout) -> 'ok' when + Process :: pid() | RegName | {RegName,node()}, + RegName :: atom(), + Reason :: term(), + Timeout :: timeout(). +stop(Process, Reason, Timeout) -> + {Pid, Mref} = erlang:spawn_monitor(do_stop(Process, Reason)), + receive + {'DOWN', Mref, _, _, Reason} -> + ok; + {'DOWN', Mref, _, _, {noproc,{sys,terminate,_}}} -> + exit(noproc); + {'DOWN', Mref, _, _, CrashReason} -> + exit(CrashReason) + after Timeout -> + exit(Pid, kill), + receive + {'DOWN', Mref, _, _, _} -> + exit(timeout) + end + end. + +-spec do_stop(Process, Reason) -> Fun when + Process :: pid() | RegName | {RegName,node()}, + RegName :: atom(), + Reason :: term(), + Fun :: fun(() -> no_return()). +do_stop(Process, Reason) -> + fun() -> + Mref = erlang:monitor(process, Process), + ok = sys:terminate(Process, Reason, infinity), + receive + {'DOWN', Mref, _, _, ExitReason} -> + exit(ExitReason) + end + end. |