summaryrefslogtreecommitdiff
path: root/src/counter.erl
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-10-29 14:00:41 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-10-29 14:00:41 +0100
commitfc86381ccac10fa30aefe76996a716ae1d677f5b (patch)
treec7a0c6c2ab9ab0c254bdb01088c2aad3119ff277 /src/counter.erl
parentb9697094228db7832e75a8699a2678338f7c7c22 (diff)
Prepare for rebar
Diffstat (limited to 'src/counter.erl')
-rw-r--r--src/counter.erl41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/counter.erl b/src/counter.erl
new file mode 100644
index 0000000..98605f6
--- /dev/null
+++ b/src/counter.erl
@@ -0,0 +1,41 @@
+-module(counter).
+
+-export([start/0, counter/0, next/0, set/1, stop/0]).
+
+-define(MAXCOUNT, 32767).
+
+start() ->
+ register(counterPid, spawn_link(?MODULE, counter, [0])).
+
+counter() ->
+ process_flag(trap_exit, true),
+ count(0).
+
+count(N) when N > ?MAXCOUNT ->
+ count(0);
+
+count(N) ->
+ receive
+ {next, FromPID} ->
+ FromPID ! {next, N},
+ count(N+1);
+ {set, New} ->
+ count(New);
+ {'EXIT', Pid, Reason} ->
+ io:format("~p: ~p~n", [Pid, Reason]),
+ exit(normal);
+ {stop} ->
+ exit(normal)
+ end.
+
+set({present, N}) ->
+ counterPid ! {set, N}.
+
+next() ->
+ counterPid ! {next, self()},
+ receive
+ {next, N} -> {present, N}
+ end.
+
+stop() ->
+ counterPid ! {stop}.