File: deadlock.erl

package info (click to toggle)
erlang 1%3A28.1.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 235,640 kB
  • sloc: erlang: 1,685,753; ansic: 436,891; cpp: 178,143; xml: 82,195; makefile: 15,086; sh: 14,588; lisp: 9,856; java: 8,603; asm: 6,829; perl: 5,874; python: 5,482; sed: 72
file content (81 lines) | stat: -rw-r--r-- 2,161 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-module(deadlock).
-behaviour(application).
-compile(export_all).
-define(SUP,deadlock_sup).
-define(CHILD,deadlock_child).


%%%-----------------------------------------------------------------
%%% application callbacks
start(_StartType, _StartArgs) ->
    supervisor:start_link({local, ?SUP}, ?MODULE, [sup]).

stop(_State) ->
    ok.



%%%-----------------------------------------------------------------
%%% supervisor callbacks
init([sup]) ->
    {ok, {{one_for_one, 5, 10}, [
        {
            sasl_syslog_dm, {?MODULE, start_link, []},
            permanent, 25000, worker,
            [deadlock]
        }
    ]}};


%%%-----------------------------------------------------------------
%%% gen_server callbacks
init([child]) ->
    case application:get_env(deadlock, fail_start) of
        {ok, false} ->
	    process_flag(trap_exit, true),
	    io:format("~p: Traps exit~n",[?MODULE]),
            %% we must not fail on the first init, otherwise supervisor
            %% terminates immediately
            {ok, []};
        {ok, true}  ->
	    timer:sleep(infinity), % init hangs!!!!
	    {ok, []}
    end.

handle_call(_Req, _From, State) ->
    {reply, ok, State}.

handle_cast(restart, State) ->
    {stop, error, State}.

handle_info(_Msg, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    case application:get_env(deadlock, fail_stop) of
        {ok, false} -> ok;
        {ok, Fun} when is_function(Fun, 0) ->
            Fun();
        {ok, Tester} when is_pid(Tester) ->
	    Tester ! {deadlock, self()},
	    io:format("~p: Waiting in terminate (~p)~n",[?MODULE,Tester]),
	    receive continue -> ok end
    end,
    io:format("~p: terminates~n", [?MODULE]),
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.


%%%-----------------------------------------------------------------
%%% Start child
start_link() ->
    gen_server:start_link({local, ?CHILD}, ?MODULE, [child], []).


%%%-----------------------------------------------------------------
%%% Provoke hanging
restart_and_fail() ->
    application:set_env(deadlock, fail_start, true), % next init will hang
    gen_server:cast(?CHILD, restart).