aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'lib/stdlib')
-rw-r--r--lib/stdlib/src/gen_statem.erl75
-rw-r--r--lib/stdlib/test/gen_statem_SUITE.erl8
2 files changed, 48 insertions, 35 deletions
diff --git a/lib/stdlib/src/gen_statem.erl b/lib/stdlib/src/gen_statem.erl
index 9c1e126507..b3a149a569 100644
--- a/lib/stdlib/src/gen_statem.erl
+++ b/lib/stdlib/src/gen_statem.erl
@@ -942,12 +942,32 @@ loop_event_actions(
State, NextState, NewData, [Action|Actions],
Postpone, Hibernate, Timeout, NextEvents) ->
case Action of
+ %% Actual actions
+ {reply,From,Reply} ->
+ case from(From) of
+ true ->
+ NewDebug = do_reply(Debug, S, From, Reply),
+ loop_event_actions(
+ Parent, NewDebug, S, Events, Event,
+ State, NextState, NewData, Actions,
+ Postpone, Hibernate, Timeout, NextEvents);
+ false ->
+ ?TERMINATE(
+ error, {bad_action,Action}, Debug, S, [Event|Events])
+ end;
+ {next_event,Type,Content} ->
+ case event_type(Type) of
+ true ->
+ loop_event_actions(
+ Parent, Debug, S, Events, Event,
+ State, NextState, NewData, Actions,
+ Postpone, Hibernate, Timeout,
+ [{Type,Content}|NextEvents]);
+ false ->
+ ?TERMINATE(
+ error, {bad_action,Action}, Debug, S, [Event|Events])
+ end;
%% Actions that set options
- postpone ->
- loop_event_actions(
- Parent, Debug, S, Events, Event,
- State, NextState, NewData, Actions,
- true, Hibernate, Timeout, NextEvents);
{postpone,NewPostpone} when is_boolean(NewPostpone) ->
loop_event_actions(
Parent, Debug, S, Events, Event,
@@ -956,11 +976,11 @@ loop_event_actions(
{postpone,_} ->
?TERMINATE(
error, {bad_action,Action}, Debug, S, [Event|Events]);
- hibernate ->
+ postpone ->
loop_event_actions(
Parent, Debug, S, Events, Event,
State, NextState, NewData, Actions,
- Postpone, true, Timeout, NextEvents);
+ true, Hibernate, Timeout, NextEvents);
{hibernate,NewHibernate} when is_boolean(NewHibernate) ->
loop_event_actions(
Parent, Debug, S, Events, Event,
@@ -969,6 +989,11 @@ loop_event_actions(
{hibernate,_} ->
?TERMINATE(
error, {bad_action,Action}, Debug, S, [Event|Events]);
+ hibernate ->
+ loop_event_actions(
+ Parent, Debug, S, Events, Event,
+ State, NextState, NewData, Actions,
+ Postpone, true, Timeout, NextEvents);
{timeout,infinity,_} -> % Clear timer - it will never trigger
loop_event_actions(
Parent, Debug, S, Events, Event,
@@ -982,31 +1007,17 @@ loop_event_actions(
{timeout,_,_} ->
?TERMINATE(
error, {bad_action,Action}, Debug, S, [Event|Events]);
- %% Actual actions
- {reply,From,Reply} ->
- case from(From) of
- true ->
- NewDebug = do_reply(Debug, S, From, Reply),
- loop_event_actions(
- Parent, NewDebug, S, Events, Event,
- State, NextState, NewData, Actions,
- Postpone, Hibernate, Timeout, NextEvents);
- false ->
- ?TERMINATE(
- error, {bad_action,Action}, Debug, S, [Event|Events])
- end;
- {next_event,Type,Content} ->
- case event_type(Type) of
- true ->
- loop_event_actions(
- Parent, Debug, S, Events, Event,
- State, NextState, NewData, Actions,
- Postpone, Hibernate, Timeout,
- [{Type,Content}|NextEvents]);
- false ->
- ?TERMINATE(
- error, {bad_action,Action}, Debug, S, [Event|Events])
- end;
+ infinity -> % Clear timer - it will never trigger
+ loop_event_actions(
+ Parent, Debug, S, Events, Event,
+ State, NextState, NewData, Actions,
+ Postpone, Hibernate, undefined, NextEvents);
+ Time when is_integer(Time), Time >= 0 ->
+ NewTimeout = {timeout,Time,Time},
+ loop_event_actions(
+ Parent, Debug, S, Events, Event,
+ State, NextState, NewData, Actions,
+ Postpone, Hibernate, NewTimeout, NextEvents);
_ ->
?TERMINATE(
error, {bad_action,Action}, Debug, S, [Event|Events])
diff --git a/lib/stdlib/test/gen_statem_SUITE.erl b/lib/stdlib/test/gen_statem_SUITE.erl
index 573a64bbb7..27fb7d9650 100644
--- a/lib/stdlib/test/gen_statem_SUITE.erl
+++ b/lib/stdlib/test/gen_statem_SUITE.erl
@@ -1305,10 +1305,10 @@ terminate(_Reason, _State, _Data) ->
idle(cast, {connect,Pid}, Data) ->
Pid ! accept,
- {next_state,wfor_conf,Data};
+ {next_state,wfor_conf,Data,infinity}; % NoOp timeout just to test API
idle({call,From}, connect, Data) ->
gen_statem:reply(From, accept),
- {next_state,wfor_conf,Data};
+ {next_state,wfor_conf,Data,infinity}; % NoOp timeout just to test API
idle(cast, badreturn, _Data) ->
badreturn;
idle({call,_From}, badreturn, _Data) ->
@@ -1343,13 +1343,15 @@ idle(Type, Content, Data) ->
timeout(timeout, idle, {From,Time}) ->
TRef = erlang:start_timer(Time, self(), ok),
+ {keep_state,{From,TRef},0}; % Immediate timeout 0
+timeout(timeout, 0, {From,TRef}) ->
{next_state,timeout2,{From,TRef},
[{timeout,1,should_be_cancelled},
postpone]}; % Should cancel state timeout
timeout(_, _, _) ->
keep_state_and_data.
-timeout2(timeout, idle, _) ->
+timeout2(timeout, 0, _) ->
keep_state_and_data;
timeout2(timeout, Reason, _) ->
{stop,Reason};