Skip to content

Commit 512a1b2

Browse files
committed
Merge pull request atomvm#495 from fadushin/gen_statem-start_link
Add start_link/3,4 to gen_statem This PR adds support for the gen_statem start_link function, so that gen_statem implementations can work with supervisors. These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents dae5617 + 7f06059 commit 512a1b2

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

libs/estdlib/src/gen_statem.erl

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@
4242
%%-----------------------------------------------------------------------------
4343
-module(gen_statem).
4444

45-
-export([start/3, start/4, stop/1, stop/3, call/2, call/3, cast/2, reply/2]).
45+
-export([
46+
start/3, start/4, start_link/3, start_link/4, stop/1, stop/3, call/2, call/3, cast/2, reply/2
47+
]).
4648
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
4749

4850
-record(state, {
@@ -98,6 +100,53 @@ start({local, Name} = ServerName, Module, Args, Options) when is_atom(Name) ->
98100
start(Module, Args, Options) ->
99101
gen_server:start(?MODULE, {Module, Args}, Options).
100102

103+
%%-----------------------------------------------------------------------------
104+
%% @param ServerName the name with which to register the gen_statem
105+
%% @param Module the module in which the gen_statem callbacks are defined
106+
%% @param Args the arguments to pass to the module's init callback
107+
%% @param Options the options used to create the gen_statem
108+
%% @returns the gen_statem pid, if successful; {error, Reason}, otherwise.
109+
%% @doc Start a named gen_statem.
110+
%%
111+
%% This function will start a gen_statem instance and register the
112+
%% newly created process with the process registry. Subsequent calls
113+
%% may use the gen_statem name, in lieu of the process id.
114+
%%
115+
%% This version of the start function will link the started gen_statem
116+
%% process to the calling process.
117+
%%
118+
%% <em><b>Note.</b> The Options argument is currently ignored.</em>
119+
%% @end
120+
%%-----------------------------------------------------------------------------
121+
-spec start_link(
122+
ServerName :: {local, Name :: atom()},
123+
Module :: module(),
124+
Args :: term(),
125+
Options :: options()
126+
) -> {ok, pid()} | {error, Reason :: term()}.
127+
start_link({local, Name} = ServerName, Module, Args, Options) when is_atom(Name) ->
128+
gen_server:start_link(ServerName, ?MODULE, {Module, Args}, Options).
129+
130+
%%-----------------------------------------------------------------------------
131+
%% @param Module the module in which the gen_statem callbacks are defined
132+
%% @param Args the arguments to pass to the module's init callback
133+
%% @param Options the options used to create the gen_statem
134+
%% @returns the gen_statem pid, if successful; {error, Reason}, otherwise.
135+
%% @doc Start an un-named gen_statem.
136+
%%
137+
%% This function will start a gen_statem instance.
138+
%%
139+
%% This version of the start function will link the started gen_statem
140+
%% process to the calling process.
141+
%%
142+
%% <em><b>Note.</b> The Options argument is currently ignored.</em>
143+
%% @end
144+
%%-----------------------------------------------------------------------------
145+
-spec start_link(Module :: module(), Args :: term(), Options :: options()) ->
146+
{ok, pid()} | {error, Reason :: term()}.
147+
start_link(Module, Args, Options) ->
148+
gen_server:start_link(?MODULE, {Module, Args}, Options).
149+
101150
%%-----------------------------------------------------------------------------
102151
%% @equiv stop(ServerRef, normal, infinity)
103152
%% @doc Stop a previously started gen_statem.

tests/libs/estdlib/test_gen_statem.erl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ test() ->
3434
ok = test_cast(),
3535
ok = test_info(),
3636
ok = test_timeout(),
37+
ok = test_start_link(),
3738
ok.
3839

3940
test_call() ->
@@ -87,6 +88,18 @@ test_timeout() ->
8788
gen_statem:stop(Pid),
8889
ok.
8990

91+
test_start_link() ->
92+
{ok, Pid} = gen_statem:start_link(?MODULE, [], []),
93+
94+
pong = gen_statem:call(Pid, ping),
95+
96+
erlang:process_flag(trap_exit, true),
97+
ok = gen_statem:cast(Pid, crash),
98+
receive
99+
{'EXIT', Pid, _Reason} -> ok
100+
after 30000 -> timeout
101+
end.
102+
90103
%%
91104
%% callbacks
92105
%%
@@ -107,7 +120,9 @@ initial({call, From}, get_num_casts, #data{num_casts = NumCasts} = Data) ->
107120
initial({call, From}, get_num_infos, #data{num_infos = NumInfos} = Data) ->
108121
{next_state, initial, Data#data{num_infos = 0}, [{reply, From, NumInfos}]};
109122
initial({call, From}, {go_to_jail, Ms}, Data) ->
110-
{next_state, jail, Data, [{reply, From, ok}, {state_timeout, Ms, you_are_free}]}.
123+
{next_state, jail, Data, [{reply, From, ok}, {state_timeout, Ms, you_are_free}]};
124+
initial(cast, crash, _Data) ->
125+
throw(test_crash).
111126

112127
jail({call, From}, are_you_free, Data) ->
113128
{next_state, jail, Data, [{reply, From, no}]};

0 commit comments

Comments
 (0)