View Source plain_fsm behaviour (plain_fsm v1.4.4)
A behaviour/support library for writing plain Erlang FSMs.
This module implements an OTP behaviour for writing plain Erlang FSMs, alleviating a long-standing gripe of mine that the OTP behaviours, for all their power, force programmers into a coding style that is very much different from that taught in the Basic Erlang Course (or the book, or online tutorials, ...) -- the type of programming that made us want to use Erlang in the first place.
Only in my old age have I begun to understand fully what a sacrifice this is. See e.g. my presentation Death by Accidental Complexity (QCon SF 2010) for a more detailed discussion of the issues involved. (Slides also available in the doc/
directory of this repos)
The requirements that drove us away from plain Erlang programming in the first place were:
- The need to support system messages to control upgrade, state inspection, shutdown, etc. The plain_fsm library solves this in a reasonable way, I think.
- The need for debugging support. The debugging support in e.g. gen_server is, I believe, rendered obsolete by the new powerful trace support (and dbg) in later versions of OTP.
- In the case of gen_server, reducing the need to reinvent the wheel, a valid point, but more so for e.g. the client side of gen_server:call(). In a protocol state machine, the only thing that really needs reusing is the handling of system messages.
However, the behaviours provided by OTP for FSM programming, gen_server
and gen_fsm
(gen_server
is perhaps a more common choice than gen_fsm
), both have the distinct drawback that you cannot normally start with a classic Erlang design and then migrate to a behaviour without significant code rewrite. In addition, the two behaviours are semantically different from the classic Erlang design
Using plain_fsm
First, write your state machine without worrying about OTP system messages. Once you're happy with it, figure out where you really want to handle system messages. Normally, it will suffice to do it in a fairly stable state. A good rule of thumb is that the top-level state machine should handle system messages, while the transient (sub-) states shouldn't
In the states where you want to handle system messages, you have three choices:
(A) Insert the system messages in the receive clause:
idle(S) ->
Parent = plain_fsm:info(parent),
receive
{system, From, Req} ->
plain_fsm:handle_system_msg(
Req, From, S, fun(S1) -> idle(S1) end);
{'EXIT', Parent, Reason} ->
plain_fsm:parent_EXIT(Reason, S);
... %% your original code here
end.
This has the advantage that everyone can understand what's going on. The part that plain_fsm.erl helps you with is the set of functions system_code_change()
, system_continue()
, system_shutdown()
, format_status()
, which are required callbacks when you handle system messages directly.
(B) Handle system messages and unknown messages together:
idle(S) ->
Parent = plain_fsm:info(parent),
receive
... %% your original code here
Msg ->
plain_fsm:handle_msg(Msg, State, fun(S1) -> idle(S1) end)
end.
This is quite convenient if the receive statement already has a 'catch-all' clause, discarding unknown messages. plain_fsm:handle_msg/3
will handle system messages properly and ignore any other message.
(C) Write a pseudo wrapper function around your receive clause:
idle(S) ->
plain_fsm:extended_receive(
receive
... %% your original code
end).
The function plain_fsm:extended_receive/1
is replaced in a parse_transform into something that looks very much like the previous program (A). The code, as it reads, requires the reader to know that the transformation takes place, otherwise the semantics would be confusing (you cannot solve the problem using a real function that way.) On the plus side, this is a fairly small violation of both the original code and Erlang's semantics.
Note that for this to work, you must include "plain_fsm.hrl" in your module.
Example
In the module fsm_example.erl (included in the plain_fsm package), we choose to handle system messages in the idle state. The example code is runnable, and supports suspend, resume, status inspection, and code change.
Imagine that the code initially looked like this:
idle(S) ->
receive
a ->
io:format("going to state a~n", []),
a(S);
b ->
io:format("going to state b~n", []),
b(S)
after 10000 ->
io:format("timeout in idle~n", []),
idle(S)
end).
The change required to handle system messages is as follows:
idle(S) ->
plain_fsm:extended_receive(
receive
a ->
io:format("going to state a~n", []),
a(S);
b ->
io:format("going to state b~n", []),
b(S)
after 10000 ->
io:format("timeout in idle~n", []),
idle(S)
end).
In addition, we change the start function from, in this case:
spawn_link() ->
spawn_link(fun() ->
process_flag(trap_exit, true),
idle(mystate)
end).
Is changed into:
spawn_link() ->
plain_fsm:spawn_link(?MODULE, fun() ->
process_flag(trap_exit,true),
idle(mystate)
end).
See also spawn/2 and spawn_opt/3 for information on other possible start functions.
To be fully compliant, you also need to supply a code_change/3 function. See behaviour_info/1 for details.
Summary
Functions
This function call is expanded by the plain_fsm
parse transform into the name and arity ({Module, Function, Arity}
) of the current function. It cannot be used from code that hasn't been transformed.
This function cannot be called directly, but is intended as a syntactic wrapper around a receive clause. It will be transformed at compile time to a set of receive patterns handling system messages and parent termination according to the OTP rules. The transform requires that the surrounding function has exactly one argument (the "State" or "Loop Data".)
This function never returns. It will handle system messages properly and ignore anything else. Example
This function never returns. If the program handles system messages explicitly, this function can be called to handle them in the plain_fsm way. Example
This function cannot be called directly, but translates to the call erlang:hibernate(plain_fsm,wake_up,[data_vsn(),Module,M,F,A])
where Module:data_vsn()
and Module:code_change/3
are expected to exist (the parse_transform will add and export the function data_vsn() -< 0
, if it doesn't already exist.)
Description of available meta-data
This function is called when the parent of a plain_fsm instance dies. The OTP rules state that the child should die with the same reason as the parent (especially in the case of Reason='shutdown'.)
proc_lib:spawn(StartF)
. This function also initializes the plain_fsm meta-data.proc_lib:spawn_link(StartF)
. This function also initializes the plain_fsm meta-data.proc_lib:spawn_opt(StartF, Opts)
. This function also initializes the plain_fsm meta-data.proc_lib:spawn_opt(Node, StartF, Opts)
. This function also initializes the sysFsm meta-data.Similar to proc_lib:start(M,F,A, Timeout, Opts)
.
sys:get_status()
). This can be used if the FSM were started as an anonymous process (the only kind currently supported). Note that this function does not register the name. The name stored is the one that shows up in sys:get_status/1. No restriction is made here regarding the data type.sys:get_state/1
. See also system_replace_state/2
. Note that the internal state is represented as {Options, State}
.sys:replace_state/2
. Note that the external representation of the state is {Options, State}
, and the options return is the 'processed' options list, possibly ignoring some elements provided by StateFun
. See also system_get_state/1
.Helper function to dispatch blocking calls as tail calls. During code change, it can be a problem that processes lie in blocking calls - say, e.g., to gen_tcp:connect(...)
. If the module is reloaded, the calling function will still be on the call stack, and may eventually get the process killed (as the VM only holds two versions of the module).
Types
-type cont() :: fun((State :: term()) -> no_return()).
-type from() :: {pid(), reference()}.
-type misc() :: {#sys{}, State :: term()}.
-type pdict() :: [{Key :: term(), Value :: term()}].
-type sys_options() :: [{cont, atom()} | {mod, atom()} | {name, atom()}].
Callbacks
-callback code_change(OldVsn, State, Extra) -> {ok, NewState} | {error, Reason}
when
OldVsn :: any() | {down, any()},
State :: any(),
Extra :: any(),
NewState :: any(),
Reason :: any().
-callback data_vsn() -> any().
-callback format_status(Opt, StatusData) -> Status when Opt :: normal | terminate, StatusData :: [PDict | State], PDict :: pdict(), State :: term(), Status :: term().
Functions
-spec current_function() -> {Module, Function, Arity}
when Module :: atom(), Function :: atom(), Arity :: integer().
This function call is expanded by the plain_fsm
parse transform into the name and arity ({Module, Function, Arity}
) of the current function. It cannot be used from code that hasn't been transformed.
-spec extended_receive(any()) -> no_return().
This function cannot be called directly, but is intended as a syntactic wrapper around a receive clause. It will be transformed at compile time to a set of receive patterns handling system messages and parent termination according to the OTP rules. The transform requires that the surrounding function has exactly one argument (the "State" or "Loop Data".)
To trigger the parse_transform, include the file plain_fsm.hrl
(found in plain_fsm/inc/
) in your module, and the Erlang compiler must be able to find the module plain_fsm_xform.beam
. If erlc
is used, this is accomplished by adding -pa .../plain_fsm/ebin
to the erlc
command.
-spec handle_msg(Msg, State, Cont) -> no_return() when Msg :: {system, From :: from(), Req :: term()} | term(), State :: term(), Cont :: cont().
This function never returns. It will handle system messages properly and ignore anything else. Example:
idle(S) ->
receive
...
Msg ->
plain_fsm:handle_msg(Msg, S, fun(S1) ->
idle(S1)
end)
end.
Note that this function should only be used if it is known to be safe to discard unknown messages. In most state machines there should be at least one state where unknown messages are discarded; in these states, the handle_msg/3 function can be a convenient way to handle both unknown messages and system messages.
The Cont
argument should be either a fun with one argument (the new state), which jumps back into the user code in the proper place, or it can be the name of a function (in this case, 'idle'). In the latter case, the function in question must be exported; in the former case, this is not necessary.
-spec handle_system_msg(Req, From, State, Cont) -> no_return() when Req :: term(), From :: from(), State :: term(), Cont :: cont().
This function never returns. If the program handles system messages explicitly, this function can be called to handle them in the plain_fsm way. Example:
idle(S) ->
receive
{system, From, Req} ->
plain_fsm:handle_system_msg(Req, From, S, fun(S1) ->
idle(S1)
end);
...
end.
The Cont
argument should be either a fun with one argument (the new state), which jumps back into the user code in the proper place, or it can be the name of a function (in this case, 'idle'). In the latter case, the function in question must be exported; in the former case, this is not necessary.
-spec hibernate(Module, Function, Arity) -> no_return()
when Module :: atom(), Function :: atom(), Arity :: integer().
This function cannot be called directly, but translates to the call erlang:hibernate(plain_fsm,wake_up,[data_vsn(),Module,M,F,A])
where Module:data_vsn()
and Module:code_change/3
are expected to exist (the parse_transform will add and export the function data_vsn() -< 0
, if it doesn't already exist.)
The function plain_fsm:wake_up/5
will begin by calling Module:data_vsn()
, and if it is the same as before, simply call apply(M,F,A)
. Otherwise, Module:code_change(OldVsn, IntState, hibernate)
will be called first. This allows a plain_fsm behaviour module to be "bootstrapped" to a new version during hibernation.
-spec info(What) -> term() when What :: debug | name | mod | parent.
Description of available meta-data:
debug : See the manual for sys.erl
name : Internal name, normally the same as the registered name.
initially undefined, can be set via plain_fsm:store_name/1.
mod : Name of the callback module.
parent: The pid() of the parent process.
-spec parent_EXIT(Reason, State) -> no_return() when Reason :: term(), State :: term().
This function is called when the parent of a plain_fsm instance dies. The OTP rules state that the child should die with the same reason as the parent (especially in the case of Reason='shutdown'.)
It is possible to provide a function terminate
in the callback module. If such function is exported, it will be called as Mod:terminate(Reason, State)
. This behaviour is borrowed from sys.erl.
-spec spawn(Mod :: atom(), StartF :: function()) -> pid().
proc_lib:spawn(StartF)
. This function also initializes the plain_fsm meta-data.
-spec spawn_link(Mod :: atom(), StartF :: function()) -> pid().
proc_lib:spawn_link(StartF)
. This function also initializes the plain_fsm meta-data.
-spec spawn_opt(Mod :: atom(), StartF :: function(), Opts :: list()) -> pid().
proc_lib:spawn_opt(StartF, Opts)
. This function also initializes the plain_fsm meta-data.
-spec spawn_opt(Node :: atom(), Mod :: atom(), StartF :: function(), Opts :: list()) -> pid().
proc_lib:spawn_opt(Node, StartF, Opts)
. This function also initializes the sysFsm meta-data.
-spec start_opt(Mod :: atom(), InitF :: function(), Timeout :: integer(), Opts :: list()) ->
{ok, pid()} | {error, term()}.
Similar to proc_lib:start(M,F,A, Timeout, Opts)
.
This function works in a similar fashion to proc_lib:start/5
, but takes a fun instead of a {M,F,A}
argument.
InitF()
may return one of the following:
{reply, Reply, Cont}
, where Reply will be sent back to the parent, and Cont
is a continuation function with no arguments. * {noreply, Cont}
, which sends no ack message back to the parent (presumably, this is done elsewhere in the code then).
-spec store_name(Name :: term()) -> ok.
sys:get_status()
). This can be used if the FSM were started as an anonymous process (the only kind currently supported). Note that this function does not register the name. The name stored is the one that shows up in sys:get_status/1. No restriction is made here regarding the data type.
-spec system_code_change(State, Module, OldVsn, Extra) -> {ok, NState}
when
State :: term(),
Module :: atom(),
OldVsn :: term(),
Extra :: term(),
NState :: term().
-spec system_continue(Parent, Debug, IntState) -> no_return()
when Parent :: pid(), Debug :: term(), IntState :: term().
-spec system_get_state(Misc) -> {ok, {Opts, State}} when Misc :: misc(), Opts :: sys_options(), State :: term().
sys:get_state/1
. See also system_replace_state/2
. Note that the internal state is represented as {Options, State}
.
-spec system_replace_state(StateFun, misc()) -> {ok, NIntState, misc()} when StateFun :: fun(({sys_options(), State}) -> {sys_options(), NState}), State :: term(), NIntState :: {sys_options(), NState}, NState :: term().
sys:replace_state/2
. Note that the external representation of the state is {Options, State}
, and the options return is the 'processed' options list, possibly ignoring some elements provided by StateFun
. See also system_get_state/1
.
-spec system_terminate(Reason, Parent, Debug, State) -> no_return()
when Reason :: term(), Parent :: pid(), Debug :: term(), State :: term().
-spec tail_apply(Fun, OldVsn, Module, ContF, State) -> no_return()
when
Fun :: function(),
OldVsn :: term(),
Module :: atom(),
ContF :: atom(),
State :: term().
Helper function to dispatch blocking calls as tail calls. During code change, it can be a problem that processes lie in blocking calls - say, e.g., to gen_tcp:connect(...)
. If the module is reloaded, the calling function will still be on the call stack, and may eventually get the process killed (as the VM only holds two versions of the module).
?tail_apply(F, ContF, S)
, which expands to plain_fsm:tail_apply(F, ?MODULE:data_vsn(), ?MODULE, ContF, S)
In this case, ?MODULE:data_vsn()
will have been automatically generated by plain_fsm, or is manually updated whenever the internal representation of the state S
is changed.
ContF
represents an exported function in the calling module, ContF(Status, Result, S)
Status :: ok | error Result :: fun() | any()
If the call to Fun()
fails, the exception (throw, error or exit) will be caught, and Result
will be a fun (arity 0), which can be called to "re-throw" the exception. This way, the continuation function can catch exceptions in its own try/catch pattern.
'Status' will be error
if Fun()
fails, otherwise ok
.
ContF
would be: ContF(ok, Result, S) ->
handle_result(Result, S);
ContF(error, E, _S) ->
E().
Note that this solution does not throw away the call stack, as e.g. a call to hibernate/3
does. Thus, it is basically only tail-recursive as regards the calling function, placing plain_fsm:tail_apply/5 on the call stack rather than a function in the user module.