diff options
Diffstat (limited to 'src/gen_server2.erl')
-rw-r--r-- | src/gen_server2.erl | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/src/gen_server2.erl b/src/gen_server2.erl index f8537487..78bbbe06 100644 --- a/src/gen_server2.erl +++ b/src/gen_server2.erl @@ -31,13 +31,13 @@ %% handle_pre_hibernate/1 then the default action is to hibernate. %% %% 6) init can return a 4th arg, {backoff, InitialTimeout, -%% MinimumTimeout, DesiredHibernatePeriod} (all in -%% milliseconds). Then, on all callbacks which can return a timeout -%% (including init), timeout can be 'hibernate'. When this is the -%% case, the current timeout value will be used (initially, the -%% InitialTimeout supplied from init). After this timeout has -%% occurred, hibernation will occur as normal. Upon awaking, a new -%% current timeout value will be calculated. +%% MinimumTimeout, DesiredHibernatePeriod} (all in milliseconds, +%% 'infinity' does not make sense here). Then, on all callbacks which +%% can return a timeout (including init), timeout can be +%% 'hibernate'. When this is the case, the current timeout value will +%% be used (initially, the InitialTimeout supplied from init). After +%% this timeout has occurred, hibernation will occur as normal. Upon +%% awaking, a new current timeout value will be calculated. %% %% The purpose is that the gen_server2 takes care of adjusting the %% current timeout value such that the process will increase the @@ -135,9 +135,10 @@ %%% Reason = normal | shutdown | Term, terminate(State) is called %%% %%% terminate(Reason, State) Let the user module clean up +%%% Reason = normal | shutdown | {shutdown, Term} | Term %%% always called when server terminates %%% -%%% ==> ok +%%% ==> ok | Term %%% %%% handle_pre_hibernate(State) %%% @@ -182,8 +183,6 @@ multi_call/2, multi_call/3, multi_call/4, enter_loop/3, enter_loop/4, enter_loop/5, enter_loop/6, wake_hib/1]). --export([behaviour_info/1]). - %% System exports -export([system_continue/3, system_terminate/4, @@ -200,12 +199,12 @@ timeout_state, queue, debug, prioritise_call, prioritise_cast, prioritise_info}). +-ifdef(use_specs). + %%%========================================================================= %%% Specs. These exist only to shut up dialyzer's warnings %%%========================================================================= --ifdef(use_specs). - -type(gs2_state() :: #gs2_state{}). -spec(handle_common_termination/3 :: @@ -214,18 +213,58 @@ -spec(pre_hibernate/1 :: (gs2_state()) -> no_return()). -spec(system_terminate/4 :: (_, _, _, gs2_state()) -> no_return()). --endif. +-type(millis() :: non_neg_integer()). %%%========================================================================= %%% API %%%========================================================================= +-callback init(Args :: term()) -> + {ok, State :: term()} | + {ok, State :: term(), timeout() | hibernate} | + {ok, State :: term(), timeout() | hibernate, + {backoff, millis(), millis(), millis()}} | + ignore | + {stop, Reason :: term()}. +-callback handle_call(Request :: term(), From :: {pid(), Tag :: term()}, + State :: term()) -> + {reply, Reply :: term(), NewState :: term()} | + {reply, Reply :: term(), NewState :: term(), timeout() | hibernate} | + {noreply, NewState :: term()} | + {noreply, NewState :: term(), timeout() | hibernate} | + {stop, Reason :: term(), + Reply :: term(), NewState :: term()}. +-callback handle_cast(Request :: term(), State :: term()) -> + {noreply, NewState :: term()} | + {noreply, NewState :: term(), timeout() | hibernate} | + {stop, Reason :: term(), NewState :: term()}. +-callback handle_info(Info :: term(), State :: term()) -> + {noreply, NewState :: term()} | + {noreply, NewState :: term(), timeout() | hibernate} | + {stop, Reason :: term(), NewState :: term()}. +-callback terminate(Reason :: (normal | shutdown | {shutdown, term()} | term()), + State :: term()) -> + ok | term(). +-callback code_change(OldVsn :: (term() | {down, term()}), State :: term(), + Extra :: term()) -> + {ok, NewState :: term()} | {error, Reason :: term()}. + +%% It's not possible to define "optional" -callbacks, so putting specs +%% for handle_pre_hibernate/1 and handle_post_hibernate/1 will result +%% in warnings (the same applied for the behaviour_info before). + +-else. + +-export([behaviour_info/1]). + behaviour_info(callbacks) -> [{init,1},{handle_call,3},{handle_cast,2},{handle_info,2}, {terminate,2},{code_change,3}]; behaviour_info(_Other) -> undefined. +-endif. + %%% ----------------------------------------------------------------- %%% Starts a generic server. %%% start(Mod, Args, Options) |