A behaviour module for implementing a state machine.
Two callback modes are supported. One for a finite state
machine like
A generic state machine process (gen_statem) implemented using
this module will have a standard set of interface functions
and include functionality for tracing and error reporting.
It will also fit into an OTP supervision tree. Refer to
A gen_statem assumes all specific parts to be located in a callback module exporting a pre-defined set of functions. The relationship between the behaviour functions and the callback functions can be illustrated as follows:
gen_statem module Callback module ----------------- --------------- gen_statem:start gen_statem:start_link -----> Module:init/1 gen_statem:stop -----> Module:terminate/2 gen_statem:call gen_statem:cast erlang:send erlang:'!' -----> Module:StateName/5 Module:handle_event/5 - -----> Module:terminate/3 - -----> Module:code_change/3
Events are of different
If a callback function fails or returns a bad value,
the gen_statem will terminate. An exception of class
The "state function" for a specific
When
When
Any state name or any state value (depending on
The gen_statem enqueues incoming events in order of arrival
and presents these to the
The gen_statem event queue model is sufficient to emulate the normal process message queue and selective receive with postponing an event corresponding to not matching it in a receive statement and changing states corresponding to entering a new receive statement.
The
Inserting an event replaces the trick of calling your own
state handling functions that you often would have to
resort to in e.g
A gen_statem handles system messages as documented in
Note that a gen_statem does not trap exit signals automatically, this must be explicitly initiated by the callback module.
Unless otherwise stated, all functions in this module fail if the specified gen_statem does not exist or if bad arguments are given.
The gen_statem process can go into hibernation (see
Name specification to use when starting a gen_statem server.
See
Server specification to use when addressing a gen_statem server.
See
It can be:
Debug option that can be used when starting
a gen_statem server through for example
For every entry in
Options that can be used when starting
a gen_statem server through for example
Return value from the start functions for_example
Client address to use when replying through for example the
After a state change (
If
A
External events are of 3 different type:
A
The predicate may not use a throw exception to return its result.
Option that only is valid when initializing the gen_statem
that is it can be returned from
Either a
These may be returned from the
The processing order for a state change is:
If multiple state options of the same type are present in the containing list these are set in the list order and the last value is kept.
The state operations are executed in the containing
list order. This matters for
Creates a gen_statem process according to OTP design principles
(using
The gen_statem process calls
If the option
If the option
If the option
Using the spawn option
If the gen_statem is successfully created and initialized
this function returns
If
Creates a stand-alone gen_statem process according to
OTP design principles (using
See
The same as
Orders the gen_statem
This function returns
If the process does not exist, a
Makes a synchronous call to the gen_statem
A
To avoid getting a late reply in the caller's
inbox this function spawns a proxy process that
does the call. A late reply gets delivered to the
dead proxy process hence gets discarded. This is
less efficient than using
The call may fail for example if the gen_statem dies before or during this function call.
Sends an asynchronous event to the gen_statem
This function can be used by a gen_statem to explicitly send
a reply to a client that called
A reply sent with this function will not be visible
in
The same as
If
Otherwise the same as
Makes an the calling process become a gen_statem. Does not return,
instead the calling process will enter the gen_statem receive
loop and become a gen_statem server. The process
must have been started using one of the start
functions in
This function is useful when a more complex initialization procedure is needed than the gen_statem behaviour provides.
Failure: If the calling process was not started by a
The following functions should be exported from a
Whenever a gen_statem is started using
If the initialization is successful, the function should
return
The
This function allows an option to select the callback mode
of the gen_statem. See
If something goes wrong during the initialization
the function should return
Whenever a gen_statem receives an event from
If
If this function returns with a new state that
does not match equal (
See
This function is called by a gen_statem when it is about to
terminate. It should be the opposite of
If the gen_statem is part of a supervision tree and is
ordered by its supervisor to terminate, this function will be
called with
Even if the gen_statem is not part of a supervision tree,
this function will be called if it receives an
Otherwise, the gen_statem will be immediately terminated.
Note that for any other reason than
This function is called by a gen_statem when it should
update its internal state during a release upgrade/downgrade,
i.e. when the instruction
In the case of an upgrade,
If successful, the function shall return the updated
internal state in an
If the function returns
This callback is optional, so callback modules need not export it. The gen_statem module provides a default implementation of this function that returns the callback module state.
This function is called by a gen_statem process when:
This function is useful for customising the form and
appearance of the gen_statem status for these cases. A
callback module wishing to customise the
The function should return
One use for this function is to return compact alternative state representations to avoid having large state terms printed in logfiles.