From 4d903e7e0f12d40461efda84ee169e8e65cf4c71 Mon Sep 17 00:00:00 2001 From: Raimo Niskanen Date: Fri, 19 Feb 2016 10:47:01 +0100 Subject: Write gen_statem with code font in the docs --- lib/stdlib/doc/src/gen_statem.xml | 253 ++++++++++++++++++++------------------ 1 file changed, 134 insertions(+), 119 deletions(-) (limited to 'lib/stdlib/doc') diff --git a/lib/stdlib/doc/src/gen_statem.xml b/lib/stdlib/doc/src/gen_statem.xml index 9cddce98da..c685d24b78 100644 --- a/lib/stdlib/doc/src/gen_statem.xml +++ b/lib/stdlib/doc/src/gen_statem.xml @@ -40,14 +40,14 @@ callback function for all states.

- A generic state machine process (gen_statem) implemented using - this module will have a standard set of interface functions + 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 OTP Design Principles for more information.

-

A gen_statem assumes all specific parts to be located in a +

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:

@@ -74,17 +74,17 @@ erlang:'!' -----> Module:StateName/5 and how to respond.

If a callback function fails or returns a bad value, - the gen_statem will terminate. An exception of class + the gen_statem will terminate. An exception of class throw, however, is not regarded as an error but as a valid return.

The "state function" for a specific state - in a gen_statem is the callback function that is called + in a gen_statem is the callback function that is called for all events in this state, and is selected depending on callback_mode - that the implementation selects during gen_statem init. + that the implementation selects during gen_statem init.

When callback_mode @@ -114,11 +114,11 @@ erlang:'!' -----> Module:StateName/5 callback_mode) is permitted with a small gotcha regarding the state undefined that is used as the previous state when - the first gen_statem state function is called. + the first gen_statem state function is called. You might need to know about this faked state if you inspect the previous state argument in your state functions.

-

The gen_statem enqueues incoming events in order of arrival +

The gen_statem enqueues incoming events in order of arrival and presents these to the state function in that order. The state function can postpone an event @@ -126,8 +126,8 @@ erlang:'!' -----> Module:StateName/5 After a state change all enqueued events (including postponed) are again presented to the state function.

-

The gen_statem event queue model is sufficient to emulate - the normal process message queue and selective receive +

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. @@ -151,23 +151,25 @@ erlang:'!' -----> Module:StateName/5 state handling functions that you often would have to resort to in e.g gen_fsm to force processing a faked event before others. - If you for example in gen_statem postpone an event + If you for example in gen_statem postpone an event in one state and then call some other state function of yours, you have not changed states and hence the postponed event will not be retried, which is logical but might be confusing.

-

A gen_statem handles system messages as documented in +

A gen_statem handles system messages as documented in sys. The sys module - can be used for debugging a gen_statem. + can be used for debugging a gen_statem.

-

Note that a gen_statem does not trap exit signals automatically, - this must be explicitly initiated by the callback module. +

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 specified gen_statem does not exist or + if bad arguments are given.

-

The gen_statem process can go into hibernation (see +

The gen_statem process can go into hibernation (see erlang:hibernate/3 ) if a @@ -187,7 +189,8 @@ erlang:'!' -----> Module:StateName/5 -

Name specification to use when starting a gen_statem server. +

Name specification to use when starting + agen_statem server. See start_link/3 and @@ -200,7 +203,8 @@ erlang:'!' -----> Module:StateName/5 -

Server specification to use when addressing a gen_statem server. +

Server specification to use when addressing + a gen_statem server. See call/2 and server_name() @@ -210,16 +214,17 @@ erlang:'!' -----> Module:StateName/5 pid() LocalName - The gen_statem is locally registered. + The gen_statem is locally registered. Name, Node - The gen_statem is locally registered on another node. + The gen_statem is locally registered + on another node. GlobalName - The gen_statem is globally registered + The gen_statem is globally registered in global. RegMod, ViaName - The gen_statem is registered through + The gen_statem is registered through an alternative process registry. The registry callback module RegMod should export the functions @@ -237,7 +242,7 @@ erlang:'!' -----> Module:StateName/5

Debug option that can be used when starting - a gen_statem server through for example + a gen_statem server through for example enter_loop/4.

For every entry in Dbgs @@ -250,7 +255,7 @@ erlang:'!' -----> Module:StateName/5

Options that can be used when starting - a gen_statem server through for example + a gen_statem server through for example start_link/3.

@@ -271,7 +276,7 @@ erlang:'!' -----> Module:StateName/5 state_op() {reply,Client,Reply} - to a client that has called the gen_statem server using + to a client that has called the gen_statem server using call/2.

@@ -316,7 +321,7 @@ erlang:'!' -----> Module:StateName/5 originate from the corresponding API functions. For calls the event contain whom to reply to. Type info originates from normal messages sent - to the gen_statem process. + to the gen_statem process. It is also possible for the state machine implementation to insert events to itself, in particular of types @@ -343,8 +348,8 @@ erlang:'!' -----> Module:StateName/5 -

Option that only is valid when initializing the gen_statem - that is it can be returned from +

Option that only is valid when initializing + the gen_statem that is it can be returned from Module:init/1 or given to enter_loop/5,6. @@ -408,12 +413,12 @@ erlang:'!' -----> Module:StateName/5 The (possibly new) state function is called with the oldest enqueued event if there is any, - otherwise the gen_statem goes into receive + otherwise the gen_statem goes into receive or hibernation (if the option hibernate is true) to wait for the next message. In hibernation the next - non-system event awakens the gen_statem, or rather - the next incoming message awakens the gen_statem but if it - is a system event it goes back into hibernation. + non-system event awakens the gen_statem, or rather + the next incoming message awakens the gen_statem + but if it is a system event it goes back into hibernation. @@ -438,13 +443,14 @@ erlang:'!' -----> Module:StateName/5 hibernate If Hibernate =:= true - or plain hibernate hibernate the gen_statem by calling + or plain hibernate hibernate the gen_statem + by calling proc_lib:hibernate/3 before receive to wait for a new event. If there are enqueued events the hibernate operation is ignored as if an event just arrived and awakened - the gen_statem. + the gen_statem. timeout @@ -500,7 +506,7 @@ erlang:'!' -----> Module:StateName/5 with TimerRef, clean the process message queue from any late timeout message, and removes any late timeout message - from the gen_statem event queue using + from the gen_statem event queue using {remove_event,EventPredicate} above. This is a convenience function that saves quite some lines of code and testing time over doing it from @@ -540,27 +546,27 @@ erlang:'!' -----> Module:StateName/5 stop Send all Replies if given, - then terminate the gen_statem by calling + then terminate the gen_statem by calling Module:terminate/3 with Reason and NewStateData, if given. next_state - The gen_statem will do a state transition to + The gen_statem will do a state transition to NewState (which may be the same as the current state), set NewStateData and execute all StateOps keep_state - The gen_statem will keep the current state, or + The gen_statem will keep the current state, or do a state transition to the current state if you like, set NewStateData and execute all StateOps keep_state_and_data - The gen_statem will keep the current state, or + The gen_statem will keep the current state, or do a state transition to the current state if you like, keep the current state data, and execute all StateOps @@ -575,17 +581,18 @@ erlang:'!' -----> Module:StateName/5 - Create a linked gen_statem process + Create a linked gen_statem process -

Creates a gen_statem process according to OTP design principles +

Creates a gen_statem process according + to OTP design principles (using proc_lib primitives) that is linked to the calling process. - This is essential when the gen_statem shall be part of + This is essential when the gen_statem shall be part of a supervision tree so it gets linked to its supervisor.

-

The gen_statem process calls +

The gen_statem process calls Module:init/1 to initialize the server. To ensure a synchronized start-up procedure, start_link/3,4 does not return until @@ -595,10 +602,10 @@ erlang:'!' -----> Module:StateName/5

ServerName specifies the server_name() - to register for the gen_statem. - If the gen_statem is started with start_link/3 + to register for the gen_statem. + If the gen_statem is started with start_link/3 no ServerName is provided and - the gen_statem is not registered. + the gen_statem is not registered.

Module is the name of the callback module.

Args is an arbitrary term which is passed as @@ -607,9 +614,9 @@ erlang:'!' -----> Module:StateName/5 .

If the option {timeout,Time} is present in - Options, the gen_statem is allowed to spend - Time milliseconds initializing or it will be - terminated and the start function will return + Options, the gen_statem + is allowed to spend Time milliseconds initializing + or it will be terminated and the start function will return {error,timeout} . @@ -624,18 +631,19 @@ erlang:'!' -----> Module:StateName/5 as option list to the spawn_opt BIF which is used to spawn - the gen_statem. + the gen_statem.

Using the spawn option monitor is currently not allowed, but will cause this function to fail with reason badarg.

-

If the gen_statem is successfully created and initialized - this function returns +

If the gen_statem is successfully created + and initialized this function returns {ok,Pid}, - where Pid is the pid() of the gen_statem. + where Pid is the pid() + of the gen_statem. If there already exists a process with the specified ServerName this function returns @@ -669,9 +677,9 @@ erlang:'!' -----> Module:StateName/5 - Create a stand-alone gen_statem process + Create a stand-alone gen_statem process -

Creates a stand-alone gen_statem process according to +

Creates a stand-alone gen_statem process according to OTP design principles (using proc_lib primitives). @@ -700,12 +708,12 @@ erlang:'!' -----> Module:StateName/5 Synchronously stop a generic server -

Orders the gen_statem +

Orders the gen_statem ServerRef to exit with the given Reason and waits for it to terminate. - The gen_statem will call + The gen_statem will call Module:terminate/3 before exiting. @@ -735,14 +743,14 @@ erlang:'!' -----> Module:StateName/5 - Make a synchronous call to a gen_statem + Make a synchronous call to a gen_statem -

Makes a synchronous call to the gen_statem +

Makes a synchronous call to the gen_statem ServerRef by sending a request and waiting until its reply arrives. - The gen_statem will call the + The gen_statem will call the state function with event_type() {call,Client} and event content @@ -771,7 +779,7 @@ erlang:'!' -----> Module:StateName/5

-

The call may fail for example if the gen_statem dies +

The call may fail for example if the gen_statem dies before or during this function call.

@@ -779,14 +787,15 @@ erlang:'!' -----> Module:StateName/5 - Send an asynchronous event to a gen_statem + Send an asynchronous event to a gen_statem -

Sends an asynchronous event to the gen_statem +

Sends an asynchronous event to the gen_statem ServerRef and returns ok immediately, - ignoring if the destination node or gen_statem does not exist. - The gen_statem will call the + ignoring if the destination node or gen_statem + does not exist. + The gen_statem will call the state function with event_type() cast and event content @@ -800,8 +809,8 @@ erlang:'!' -----> Module:StateName/5 Send a reply to a client -

This function can be used by a gen_statem to explicitly send - a reply to a client that called +

This function can be used by a gen_statem + to explicitly send a reply to a client that called call/2 when the reply cannot be defined in the return value of the @@ -828,7 +837,7 @@ erlang:'!' -----> Module:StateName/5 - Enter the gen_statem receive loop + Enter the gen_statem receive loop

The same as enter_loop/6 @@ -841,7 +850,7 @@ erlang:'!' -----> Module:StateName/5 - Enter the gen_statem receive loop + Enter the gen_statem receive loop

If Server_or_StateOps is a list() the same as @@ -862,25 +871,27 @@ erlang:'!' -----> Module:StateName/5 - Enter the gen_statem receive loop + Enter the gen_statem receive loop -

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 +

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 proc_lib. The user is responsible for any initialization of the process, including registering a name for it.

This function is useful when a more complex initialization - procedure is needed than the gen_statem behaviour provides. + procedure is needed than + the gen_statem behaviour provides.

Module, Options and Server have the same meanings as when calling - gen_statem:start[_link]/3,4 + gen_statem:start[_link]/3,4 . However, the @@ -912,7 +923,7 @@ erlang:'!' -----> Module:StateName/5

CALLBACK FUNCTIONS

The following functions should be exported from a - gen_statem callback module. + gen_statem callback module.

@@ -937,10 +948,10 @@ erlang:'!' -----> Module:StateName/5 -

Whenever a gen_statem is started using - gen_statem:start_link/3,4 +

Whenever a gen_statem is started using + start_link/3,4 or - gen_statem:start/3,4, + start/3,4, this function is called by the new process to initialize the implementation loop data.

@@ -950,7 +961,7 @@ erlang:'!' -----> Module:StateName/5 return {ok,State,StateData} or {ok,State,StateData,StateOps}. State is the state - of the gen_statem. + of the gen_statem.

The StateOps are executed when entering the first @@ -958,14 +969,14 @@ erlang:'!' -----> Module:StateName/5 state function.

This function allows an option to select the callback mode - of the gen_statem. See + of the gen_statem. See init_option. This option is not allowed from the state function(s).

If something goes wrong during the initialization the function should return {stop,Reason} or ignore. See - gen_statem:start_link/3,4. + start_link/3,4.

@@ -1002,9 +1013,9 @@ erlang:'!' -----> Module:StateName/5 -

Whenever a gen_statem receives an event from - gen_statem:call/2, - gen_statem:cast/2 or +

Whenever a gen_statem receives an event from + call/2, + cast/2 or as a normal process message this function is called. If callback_mode @@ -1022,7 +1033,7 @@ erlang:'!' -----> Module:StateName/5 Replies or by calling - gen_statem:reply(Client, Reply) + reply(Client, Reply) .

StateName is useful in some odd cases for example @@ -1032,7 +1043,7 @@ erlang:'!' -----> Module:StateName/5

PrevStateName and PrevState are useful in some odd cases for example when you want to do something only at the first event in a state. - Note that when gen_statem enters its first state + Note that when gen_statem enters its first state this is set to undefined.

If this function returns with a new state that @@ -1041,7 +1052,7 @@ erlang:'!' -----> Module:StateName/5

See state_op() for options that can be set and operations that can be done - by gen_statem after returning from this function. + by gen_statem after returning from this function.

@@ -1060,45 +1071,49 @@ erlang:'!' -----> Module:StateName/5 Ignored = term() -

This function is called by a gen_statem when it is about to - terminate. It should be the opposite of +

This function is called by a gen_statem + when it is about to terminate. It should be the opposite of Module:init/1 and do any necessary cleaning up. When it returns, - the gen_statem terminates with Reason. The return + the gen_statem terminates with Reason. The return value is ignored.

Reason is a term denoting the stop reason and State - is the internal state of the gen_statem. + is the internal state of the gen_statem.

-

Reason depends on why the gen_statem is terminating. +

Reason depends on why the gen_statem + is terminating. If it is because another callback function has returned a stop tuple {stop,Reason} in StateOps, Reason will have the value specified in that tuple. If it is due to a failure, Reason is the error reason.

-

If the gen_statem is part of a supervision tree and is +

If the gen_statem is part of a supervision tree and is ordered by its supervisor to terminate, this function will be called with Reason = shutdown if the following conditions apply:

- the gen_statem has been set to trap exit signals, and + the gen_statem has been set + to trap exit signals, and + the shutdown strategy as defined in the supervisor's child specification is an integer timeout value, not brutal_kill. -

Even if the gen_statem is not part of a supervision tree, - this function will be called if it receives an 'EXIT' - message from its parent. Reason will be the same as in - the 'EXIT' message. +

Even if the gen_statem is not + part of a supervision tree, this function will be called + if it receives an 'EXIT' message from its parent. + Reason will be the same as + in the 'EXIT' message.

-

Otherwise, the gen_statem will be immediately terminated. +

Otherwise, the gen_statem will be immediately terminated.

Note that for any other reason than normal, - shutdown, or {shutdown,Term} the gen_statem is - assumed to terminate due to an error and - an error report is issued using + shutdown, or {shutdown,Term} + the gen_statem is assumed to terminate due to an error + and an error report is issued using error_logger:format/2 . @@ -1126,7 +1141,7 @@ erlang:'!' -----> Module:StateName/5 Reason = term() -

This function is called by a gen_statem when it should +

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 {update,Module,Change,...} where Change={advanced,Extra} is given in @@ -1144,7 +1159,7 @@ erlang:'!' -----> Module:StateName/5 is the checksum of the BEAM file.

OldState and OldStateData is the internal state - of the gen_statem. + of the gen_statem.

Extra is passed as-is from the {advanced,Extra} part of the update instruction. @@ -1163,7 +1178,7 @@ erlang:'!' -----> Module:StateName/5 Status Optional function for providing a term describing the - current gen_statem status + current gen_statem status Opt = normal | terminate PDict = [{Key, Value}] @@ -1180,43 +1195,43 @@ erlang:'!' -----> Module:StateName/5

This callback is optional, so callback modules need not - export it. The gen_statem module provides a default + 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 called by a gen_statem process when:

One of sys:get_status/1,2 - is invoked to get the gen_statem status. Opt is set + is invoked to get the gen_statem status. Opt is set to the atom normal for this case. - The gen_statem terminates abnormally and logs an error. + The gen_statem terminates abnormally and logs an error. Opt is set to the atom terminate for this case.

This function is useful for customising the form and - appearance of the gen_statem status for these cases. A + appearance of the gen_statem status for these cases. A callback module wishing to customise the sys:get_status/1,2 return value as well as how its status appears in termination error logs exports an instance of format_status/2 that returns a term - describing the current status of the gen_statem. + describing the current status of the gen_statem.

-

PDict is the current value of the gen_statem's +

PDict is the current value of the gen_statem's process dictionary.

State - is the internal state of the gen_statem. + is the internal state of the gen_statem.

The function should return Status, a term that customises the details of the current state and status of - the gen_statem. There are no restrictions on the + the gen_statem. There are no restrictions on the form Status can take, but for the sys:get_status/1,2 @@ -1224,7 +1239,7 @@ erlang:'!' -----> Module:StateName/5 is normal), the recommended form for the Status value is [{data, [{"State", Term}]}] where Term provides relevant details of - the gen_statem state. Following this recommendation isn't + the gen_statem state. Following this recommendation isn't required, but doing so will make the callback module status consistent with the rest of the -- cgit v1.2.3