Skip to content

Commit

Permalink
tested local gproc
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.ulf.wiger.net/gproc/branches/experimental-0906/gproc@18 f3948e33-8234-0410-8a80-a07eae3b6c4d
  • Loading branch information
uwiger committed Jun 8, 2009
1 parent 0f8efa7 commit 7517919
Show file tree
Hide file tree
Showing 6 changed files with 469 additions and 14 deletions.
53 changes: 53 additions & 0 deletions src/Makefile
@@ -0,0 +1,53 @@
## The MIT License
##
## Copyright (c) 2008 Ulf Wiger <ulf@wiger.net>,
##
## Permission is hereby granted, free of charge, to any person obtaining a
## copy of this software and associated documentation files (the "Software"),
## to deal in the Software without restriction, including without limitation
## the rights to use, copy, modify, merge, publish, distribute, sublicense,
## and/or sell copies of the Software, and to permit persons to whom the
## Software is furnished to do so, subject to the following conditions:
##
## The above copyright notice and this permission notice shall be included in
## all copies or substantial portions of the Software.
##
## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
## FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
## DEALINGS IN THE SOFTWARE.


.SUFFIXES: .erl .beam

vpath %.beam ../ebin
vpath %.pdf ../doc

ERLC = erlc -W -o ../ebin +debug_info
EQC = /host/dev/eqc-1.16/ebin
ERL_EQC = erl -pz ../ebin -pz $(EQC) -kernel error_logger silent

beams = gproc.beam \
gproc_app.beam \
gproc_dist.beam \
gproc_lib.beam \
gproc_sup.beam

all : $(beams)
test : all gproc_eqc.beam
$(ERL_EQC) -s gproc_eqc run

itest : all gproc_eqc.beam
$(ERL_EQC) -s eqc start

clean :
rm -f *~ #*#

$(beams) : %.beam : %.erl
$(ERLC) $<

gproc_eqc.beam : gproc_eqc.erl
$(ERLC) -pz $(EQC) gproc_eqc.erl
81 changes: 67 additions & 14 deletions src/gproc.erl
Expand Up @@ -27,6 +27,9 @@
mreg/3,
set_value/2,
get_value/1,
where/1,
lookup_pid/1,
lookup_pids/1,
update_counter/2,
send/2,
info/1, info/2,
Expand Down Expand Up @@ -82,7 +85,7 @@ reg({T,l,_} = Key, Value) when T==n; T==a ->
reg({c,l,_} = Key, Value) ->
%% local counter
if is_integer(Value) ->
local_reg(Key, Value);
call({reg, Key, Value});
true ->
erlang:error(badarg)
end;
Expand Down Expand Up @@ -194,6 +197,36 @@ get_value(_, _) ->
erlang:error(badarg).


%%% @spec (Key) -> Pid
%%% @doc Lookup the Pid stored with a key.
%%%
lookup_pid({T,_,_} = Key) ->
case where(Key) of
undefined -> erlang:error(badarg);
P -> P
end.


where({T,_,_}=Key) ->
if T==n; T==a ->
case ets:lookup(?TAB, {Key,T}) of
[] ->
undefined;
[{_, P, _Value}] ->
P
end;
true ->
erlang:error(badarg)
end.

lookup_pids({T,_,_} = Key) ->
if T==n; T==a; T==c ->
ets:select(?TAB, [{{{Key,T}, '$1', '_'},[],['$1']}]);
true ->
erlang:error(badarg)
end.


update_counter({c,l,_} = Key, Incr) when is_integer(Incr) ->
gproc_lib:update_counter(Key, Incr);
update_counter({c,g,_} = Key, Incr) when is_integer(Incr) ->
Expand Down Expand Up @@ -303,15 +336,15 @@ info(Pid, I) ->

handle_cast({monitor_me, Pid}, S) ->
erlang:monitor(process, Pid),
{ok, S}.
{noreply, S}.

handle_call({reg, {_,l,_} = Key, Val}, {Pid,_}, S) ->
case gproc_lib:insert_reg(Key, Val, Pid, l) of
false ->
{reply, badarg, S};
handle_call({reg, {T,l,_} = Key, Val}, {Pid,_}, S) ->
case try_insert_reg(Key, Val, Pid) of
true ->
ensure_monitor(Pid),
{reply, true, S}
{reply, true, S};
false ->
{reply, badarg, S}
end;
handle_call({unreg, {_,l,_} = Key}, {Pid,_}, S) ->
case ets:member(?TAB, {Pid,Key}) of
Expand Down Expand Up @@ -339,14 +372,10 @@ handle_call(_, _, S) ->
{reply, badarg, S}.

handle_info({'DOWN', _MRef, process, Pid, _}, S) ->
Keys = ets:select(?TAB, [{{{Pid,'$1'}},
[{'==',{element,2,'$1'},l}], ['$1']}]),
ets:select_delete(?TAB, [{{{Pid,{'_',l,'_'}}}, [], [true]}]),
ets:delete(?TAB, Pid),
lists:foreach(fun(Key) -> gproc_lib:remove_reg_1(Key, Pid) end, Keys),
{ok, S};
process_is_down(Pid),
{noreply, S};
handle_info(_, S) ->
{ok, S}.
{noreply, S}.



Expand All @@ -370,6 +399,30 @@ cast(Msg) ->




try_insert_reg({T,l,_} = Key, Val, Pid) ->
case gproc_lib:insert_reg(Key, Val, Pid, l) of
false ->
[{_, OtherPid, _}] = ets:lookup(?TAB, {Key,T}),
case is_process_alive(OtherPid) of
true ->
false;
false ->
process_is_down(Pid),
true = gproc_lib:insert_reg(Key, Val, Pid, l)
end;
true ->
true
end.

process_is_down(Pid) ->
Keys = ets:select(?TAB, [{{{Pid,'$1'}},
[{'==',{element,2,'$1'},l}], ['$1']}]),
ets:select_delete(?TAB, [{{{Pid,{'_',l,'_'}}}, [], [true]}]),
ets:delete(?TAB, Pid),
lists:foreach(fun(Key) -> gproc_lib:remove_reg_1(Key, Pid) end, Keys).


create_tabs() ->
ets:new(?MODULE, [ordered_set, public, named_table]).

Expand Down
43 changes: 43 additions & 0 deletions src/gproc_app.erl
@@ -0,0 +1,43 @@
%%%----------------------------------------------------------------------
%%% File : gproc_app.erl
%%% Purpose : GPROC application callback module
%%%----------------------------------------------------------------------

-module(gproc_app).

-behaviour(application).

%% application callbacks
-export([start/0, start/2, stop/1]).

%%%----------------------------------------------------------------------
%%% Callback functions from application
%%%----------------------------------------------------------------------

%%----------------------------------------------------------------------
%% Func: start/2
%% Returns: {ok, Pid} |
%% {ok, Pid, State} |
%% {error, Reason}
%%----------------------------------------------------------------------
start() ->
start(xxxwhocares, []).

start(_Type, StartArgs) ->
case gproc_sup:start_link(StartArgs) of
{ok, Pid} ->
{ok, Pid};
Error ->
Error
end.

%%----------------------------------------------------------------------
%% Func: stop/1
%% Returns: any
%%----------------------------------------------------------------------
stop(_State) ->
ok.

%%%----------------------------------------------------------------------
%%% Internal functions
%%%----------------------------------------------------------------------

0 comments on commit 7517919

Please sign in to comment.