Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 66 additions & 44 deletions src/gproc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
lookup_global_properties/1,
lookup_local_counters/1,
lookup_global_counters/1,
lookup_local_resources/1,
lookup_global_resources/1,
lookup_local_aggr_counter/1,
lookup_global_aggr_counter/1]).

Expand Down Expand Up @@ -354,6 +356,25 @@ lookup_local_counters(P) -> lookup_values({c,l,P}).
%%
lookup_global_counters(P) -> lookup_values({c,g,P}).

%% @spec (Resource::any()) -> [{pid(), Value::integer()}]
%%
%% @doc Look up all local (non-unique) instances of a given Resource.
%% Returns a list of {Pid, Value} tuples for all matching objects.
%% @equiv lookup_values({r, l, Resource})
%% @end
%%
lookup_local_resources(P) -> lookup_values({r,l,P}).


%% @spec (Resource::any()) -> [{pid(), Value::integer()}]
%%
%% @doc Look up all global (non-unique) instances of a given Resource.
%% Returns a list of {Pid, Value} tuples for all matching objects.
%% @equiv lookup_values({r, g, Resource})
%% @end
%%
lookup_global_resources(P) -> lookup_values({r,g,P}).

%% @spec get_env(Scope::scope(), App::atom(), Key::atom()) -> term()
%% @equiv get_env(Scope, App, Key, [app_env])
get_env(Scope, App, Key) ->
Expand Down Expand Up @@ -933,10 +954,10 @@ monitor(Key, Type) when Type==info;
Type==standby ->
?CATCH_GPROC_ERROR(monitor1(Key, Type), [Key, Type]).

monitor1({T,g,_} = Key, Type) when T==n; T==a ->
monitor1({T,g,_} = Key, Type) when T==n; T==a; T==rc ->
?CHK_DIST,
gproc_dist:monitor(Key, Type);
monitor1({T,l,_} = Key, Type) when T==n; T==a ->
monitor1({T,l,_} = Key, Type) when T==n; T==a; T==rc ->
call({monitor, Key, self(), Type}, l);
monitor1(_, _) ->
?THROW_GPROC_ERROR(badarg).
Expand All @@ -950,10 +971,10 @@ monitor1(_, _) ->
demonitor(Key, Ref) ->
?CATCH_GPROC_ERROR(demonitor1(Key, Ref), [Key, Ref]).

demonitor1({T,g,_} = Key, Ref) when T==n; T==a ->
demonitor1({T,g,_} = Key, Ref) when T==n; T==a; T==rc ->
?CHK_DIST,
gproc_dist:demonitor(Key, Ref);
demonitor1({T,l,_} = Key, Ref) when T==n; T==a ->
demonitor1({T,l,_} = Key, Ref) when T==n; T==a; T==rc ->
call({demonitor, Key, Ref, self()}, l);
demonitor1(_, _) ->
?THROW_GPROC_ERROR(badarg).
Expand Down Expand Up @@ -1169,7 +1190,7 @@ reg_shared(Key) ->
?CATCH_GPROC_ERROR(reg_shared1(valid_key(Key)), [Key]).

%% @private
reg_shared1({T,_,_} = Key) when T==a; T==p; T==c ->
reg_shared1({T,_,_} = Key) when T==a; T==p; T==c; T==r ->
reg_shared(Key, default(Key)).

%% @spec reg_shared(Key::key(), Value) -> true
Expand Down Expand Up @@ -1247,7 +1268,7 @@ munreg(T, C, L) ->
munreg1(T, g, L) ->
?CHK_DIST,
gproc_dist:munreg(T, existing(T,g,L));
munreg1(T, l, L) when T==a; T==n ->
munreg1(T, l, L) when T==p; T==a; T==n; T==rc; T==r ->
if is_list(L) ->
call({munreg, T, l, existing(T,l,L)});
true ->
Expand All @@ -1259,9 +1280,9 @@ munreg1(_, _, _) ->
?THROW_GPROC_ERROR(badarg).

existing(T,Scope,L) ->
Keys = if T==p; T==c ->
Keys = if T==p; T==c; T==r ->
[{{T,Scope,K}, self()} || K <- L];
T==a; T==n ->
T==a; T==n; T==rc ->
[{{T,Scope,K}, T} || K <- L]
end,
_ = [case ets:member(?TAB, K) of
Expand Down Expand Up @@ -1828,18 +1849,19 @@ lookup_values({T,_,_} = Key) ->
-spec update_counter(key(), increment()) -> integer() | [integer()].
update_counter(Key, Incr) ->
Pid = case Key of
{n,_,_} -> n;
{c,_,_} -> self()
end,
{n,_,_} -> n;
{T,_,_} when T==c; T==r ->
self()
end,
?CATCH_GPROC_ERROR(update_counter1(Key, Pid, Incr), [Key, Incr]).

update_counter(Key, Pid, Incr) when is_pid(Pid);
Pid == shared; Pid == n ->
?CATCH_GPROC_ERROR(update_counter1(Key, Pid, Incr), [Key, Pid, Incr]).

update_counter1({T,l,_} = Key, Pid, Incr) when T==c; T==n ->
update_counter1({T,l,_} = Key, Pid, Incr) when T==c; T==r; T==n ->
gproc_lib:update_counter(Key, Incr, Pid);
update_counter1({T,g,_} = Key, Pid, Incr) when T==c; T==n ->
update_counter1({T,g,_} = Key, Pid, Incr) when T==c; T==r; T==n ->
?CHK_DIST,
gproc_dist:update_counter(Key, Pid, Incr);
update_counter1(_, _, _) ->
Expand Down Expand Up @@ -2302,15 +2324,15 @@ handle_call({reg_or_locate, {T,l,_} = Key, Val, P}, _, S) ->
{reply, {OtherPid, OtherValue}, S}
end;
handle_call({monitor, {T,l,_} = Key, Pid, Type}, _From, S)
when T==n; T==a ->
when T==n; T==a; T==rc ->
Ref = make_ref(),
Lookup = ets:lookup(?TAB, {Key, T}),
IsRegged = is_regged(Lookup),
_ = case {IsRegged, Type} of
{false, info} ->
Pid ! {gproc, unreg, Ref, Key};
{false, info} ->
Pid ! {gproc, unreg, Ref, Key};
{false, follow} ->
Pid ! {gproc, unreg, Ref, Key},
Pid ! {gproc, unreg, Ref, Key},
_ = gproc_lib:ensure_monitor(Pid, l),
case Lookup of
[{K, Waiters}] ->
Expand All @@ -2326,19 +2348,19 @@ handle_call({monitor, {T,l,_} = Key, Pid, Type}, _From, S)
true = gproc_lib:insert_reg(Key, undefined, Pid, l, Evt),
Pid ! {gproc, Evt, Ref, Key},
_ = gproc_lib:ensure_monitor(Pid, l);
{true, _} ->
{true, _} ->
[{_, RegPid, _}] = Lookup,
_ = gproc_lib:ensure_monitor(Pid, l),
case ets:lookup(?TAB, {RegPid, Key}) of
[{K,r}] ->
ets:insert(?TAB, {K, [{monitor, [{Pid,Ref,Type}]}]}),
case ets:lookup(?TAB, {RegPid, Key}) of
[{K,r}] ->
ets:insert(?TAB, {K, [{monitor, [{Pid,Ref,Type}]}]}),
ets:insert_new(?TAB, {{Pid,Key}, []});
[{K, Opts}] ->
ets:insert(?TAB, {K, gproc_lib:add_monitor(
[{K, Opts}] ->
ets:insert(?TAB, {K, gproc_lib:add_monitor(
Opts, Pid, Ref, Type)}),
ets:insert_new(?TAB, {{Pid,Key}, []})
end
end,
end
end,
{reply, Ref, S};
handle_call({demonitor, {T,l,_} = Key, Ref, Pid}, _From, S)
when T==n; T==a; T==rc ->
Expand Down Expand Up @@ -2910,7 +2932,7 @@ scope(S) when S==l; S==g -> S.

type('_') -> '_';
type(all) -> '_';
type(T) when T==n; T==p; T==c; T==a -> T;
type(T) when T==n; T==p; T==c; T==a; T==r; T==rc -> T;
type(names) -> n;
type(props) -> p;
type(resources) -> r;
Expand Down Expand Up @@ -3084,24 +3106,24 @@ remove_dead(true, Objs) ->
%% a test case that verifies the difference between having the option and not.
qlc_lookup_pid(Pid, Scope, Check) ->
case Check andalso ?PID_IS_DEAD(Pid) of
true ->
[];
false ->
Found =
ets:select(?TAB, [{{{Pid, rev_keypat(Scope)}, '_'},
[], ['$_']}]),
lists:flatmap(
fun({{_,{T,_,_}=K}, _}) ->
K2 = if T==n orelse T==a -> T;
true -> Pid
end,
case ets:lookup(?TAB, {K,K2}) of
[{{Key,_},_,Value}] ->
[{Key, Pid, Value}];
[] ->
[]
end
end, Found)
true ->
[];
false ->
Found =
ets:select(?TAB, [{{{Pid, rev_keypat(Scope)}, '_'},
[], ['$_']}]),
lists:flatmap(
fun({{_,{T,_,_}=K}, _}) ->
K2 = if T==n orelse T==a orelse T==rc -> T;
true -> Pid
end,
case ets:lookup(?TAB, {K,K2}) of
[{{Key,_},_,Value}] ->
[{Key, Pid, Value}];
[] ->
[]
end
end, Found)
end.


Expand Down
81 changes: 50 additions & 31 deletions src/gproc_dist.erl
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ give_away({_,g,_} = Key, To) ->


update_counter({T,g,_} = Key, Pid, Incr) when is_integer(Incr), T==c;
is_integer(Incr), T==n ->
is_integer(Incr), T==r;
is_integer(Incr), T==n ->
leader_call({update_counter, Key, Incr, Pid});
update_counter(_, _, _) ->
?THROW_GPROC_ERROR(badarg).
Expand Down Expand Up @@ -518,14 +519,15 @@ handle_leader_call({reg_or_locate, {n,g,_} = K, Value, P},
end;
handle_leader_call({update_counter, {T,g,_Ctr} = Key, Incr, Pid}, _From, S, _E)
when is_integer(Incr), T==c;
is_integer(Incr), T==r;
is_integer(Incr), T==n ->
try New = ets:update_counter(?TAB, {Key, Pid}, {3,Incr}),
RealPid = case Pid of
n -> ets:lookup_element(?TAB, {Key,Pid}, 2);
shared -> shared;
P when is_pid(P) -> P
end,
Vals = [{{Key,Pid},RealPid,New} | update_aggr_counter(Key, Incr)],
RealPid = case Pid of
n -> ets:lookup_element(?TAB, {Key,Pid}, 2);
shared -> shared;
P when is_pid(P) -> P
end,
Vals = [{{Key,Pid},RealPid,New} | update_aggr_counter(Key, Incr)],
{reply, New, [{insert, Vals}], S}
catch
error:_ ->
Expand Down Expand Up @@ -1012,13 +1014,15 @@ surrendered_1(Globs) ->
batch_update_counters(Cs) ->
batch_update_counters(Cs, [], []).

batch_update_counters([{{c,g,_} = Key, Pid, Incr}|T], Returns, Updates) ->
batch_update_counters([{{T,g,_} = Key, Pid, Incr}|Cs], Returns, Updates)
when T==c; T==n; T==r ->
case update_counter_g(Key, Incr, Pid) of
[{_,_,_} = A, {_, _, V} = C] ->
batch_update_counters(T, [{Key,Pid,V}|Returns], add_object(
A, add_object(C, Updates)));
[{_, _, V} = C] ->
batch_update_counters(T, [{Key,Pid,V}|Returns], add_object(C, Updates))
[{_,_,_} = A, {_, _, V} = C] ->
batch_update_counters(Cs, [{Key,Pid,V}|Returns],
add_object(A, add_object(C, Updates)));
[{_, _, V} = C] ->
batch_update_counters(
Cs, [{Key,Pid,V}|Returns], add_object(C, Updates))
end;
batch_update_counters([], Returns, Updates) ->
{lists:reverse(Returns), Updates}.
Expand All @@ -1033,22 +1037,37 @@ add_object(Obj, []) ->



update_counter_g({c,g,_} = Key, Incr, Pid) when is_integer(Incr) ->
Res = ets:update_counter(?TAB, {Key, Pid}, {3,Incr}),
update_aggr_counter(Key, Incr, [{{Key,Pid},Pid,Res}]);
update_counter_g({c,g,_} = Key, {Incr, Threshold, SetValue}, Pid)
when is_integer(Incr), is_integer(Threshold), is_integer(SetValue) ->
[Prev, New] = ets:update_counter(?TAB, {Key, Pid},
[{3, 0}, {3, Incr, Threshold, SetValue}]),
update_aggr_counter(Key, New - Prev, [{{Key,Pid},Pid,New}]);
update_counter_g({c,g,_} = Key, Ops, Pid) when is_list(Ops) ->
case ets:update_counter(?TAB, {Key, Pid},
[{3, 0} | expand_ops(Ops)]) of
[_] ->
[];
[Prev | Rest] ->
[New | _] = lists:reverse(Rest),
update_aggr_counter(Key, New - Prev, [{Key, Pid, Rest}])
update_counter_g({T,g,_} = Key, Incr, Pid) when is_integer(Incr), T==c;
is_integer(Incr), T==r;
is_integer(Incr), T==n ->
R = if T==n -> T;
true -> Pid
end,
Res = ets:update_counter(?TAB, {Key, R}, {3,Incr}),
update_aggr_counter(Key, Incr, [{{Key,R},Pid,Res}]);
update_counter_g({T,g,_} = Key, {Incr, Threshold, SetValue}, Pid)
when is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==c;
is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==r;
is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==n ->
R = if T==n -> T;
true -> Pid
end,
[Prev, New] = ets:update_counter(?TAB, {Key, R},
[{3, 0}, {3, Incr, Threshold, SetValue}]),
update_aggr_counter(Key, New - Prev, [{{Key,R},Pid,New}]);
update_counter_g({T,g,_} = Key, Ops, Pid) when is_list(Ops), T==c;
is_list(Ops), T==r;
is_list(Ops), T==n ->
R = if T==n -> T;
true -> Pid
end,
case ets:update_counter(?TAB, {Key, R},
[{3, 0} | expand_ops(Ops)]) of
[_] ->
[];
[Prev | Rest] ->
[New | _] = lists:reverse(Rest),
update_aggr_counter(Key, New - Prev, [{{Key,R}, Pid, Rest}])
end;
update_counter_g(_, _, _) ->
?THROW_GPROC_ERROR(badarg).
Expand All @@ -1064,11 +1083,11 @@ expand_ops([]) ->
expand_ops(_) ->
?THROW_GPROC_ERROR(badarg).

update_aggr_counter({n,_,_}, _) ->
[];
update_aggr_counter(Key, Incr) ->
update_aggr_counter(Key, Incr, []).

update_aggr_counter({T,g,_}, _Incr, Acc) when T==n; T==r ->
Acc;
update_aggr_counter({c,g,Ctr}, Incr, Acc) ->
Key = {{a,g,Ctr},a},
case ets:lookup(?TAB, Key) of
Expand Down
Loading