Browse Source

gproc_dist:set_value_shared/2

Ulf Wiger 11 years ago
parent
commit
a4190c6e36
4 changed files with 76 additions and 6 deletions
  1. 36 1
      doc/gproc.md
  2. 8 1
      doc/gproc_dist.md
  3. 23 2
      src/gproc.erl
  4. 9 2
      src/gproc_dist.erl

+ 36 - 1
doc/gproc.md

@@ -280,7 +280,7 @@ unique_id() = {n | a, <a href="#type-scope">scope()</a>, any()}
 
 
 <table width="100%" border="1" cellspacing="0" cellpadding="2" summary="function index"><tr><td valign="top"><a href="#add_global_aggr_counter-1">add_global_aggr_counter/1</a></td><td>Registers a global (unique) aggregated counter.</td></tr><tr><td valign="top"><a href="#add_global_counter-2">add_global_counter/2</a></td><td>Registers a global (non-unique) counter.</td></tr><tr><td valign="top"><a href="#add_global_name-1">add_global_name/1</a></td><td>Registers a global (unique) name.</td></tr><tr><td valign="top"><a href="#add_global_property-2">add_global_property/2</a></td><td>Registers a global (non-unique) property.</td></tr><tr><td valign="top"><a href="#add_local_aggr_counter-1">add_local_aggr_counter/1</a></td><td>Registers a local (unique) aggregated counter.</td></tr><tr><td valign="top"><a href="#add_local_counter-2">add_local_counter/2</a></td><td>Registers a local (non-unique) counter.</td></tr><tr><td valign="top"><a href="#add_local_name-1">add_local_name/1</a></td><td>Registers a local (unique) name.</td></tr><tr><td valign="top"><a href="#add_local_property-2">add_local_property/2</a></td><td>Registers a local (non-unique) property.</td></tr><tr><td valign="top"><a href="#add_shared_local_counter-2">add_shared_local_counter/2</a></td><td>Registers a local shared (unique) counter.</td></tr><tr><td valign="top"><a href="#audit_process-1">audit_process/1</a></td><td></td></tr><tr><td valign="top"><a href="#await-1">await/1</a></td><td>Equivalent to <a href="#await-2"><tt>await(Key, infinity)</tt></a>.</td></tr><tr><td valign="top"><a href="#await-2">await/2</a></td><td>Wait for a local name to be registered.</td></tr><tr><td valign="top"><a href="#await-3">await/3</a></td><td>Wait for a local name to be registered on <code>Node</code>.</td></tr><tr><td valign="top"><a href="#bcast-2">bcast/2</a></td><td>Equivalent to <a href="#bcast-3"><tt>bcast(nodes(), Key, Msg)</tt></a>.</td></tr><tr><td valign="top"><a href="#bcast-3">bcast/3</a></td><td>Sends a message to processes corresponding to Key on Nodes.</td></tr><tr><td valign="top"><a href="#cancel_wait-2">cancel_wait/2</a></td><td>Cancels a previous call to nb_wait/1.</td></tr><tr><td valign="top"><a href="#cancel_wait-3">cancel_wait/3</a></td><td>Cancels a previous call to nb_wait/2.</td></tr><tr><td valign="top"><a href="#cancel_wait_or_monitor-1">cancel_wait_or_monitor/1</a></td><td></td></tr><tr><td valign="top"><a href="#default-1">default/1</a></td><td></td></tr><tr><td valign="top"><a href="#demonitor-2">demonitor/2</a></td><td>Remove a monitor on a registered name
-This function is the reverse of monitor/1.</td></tr><tr><td valign="top"><a href="#first-1">first/1</a></td><td>Behaves as ets:first(Tab) for a given type of registration.</td></tr><tr><td valign="top"><a href="#get_attribute-2">get_attribute/2</a></td><td>Get attribute value of <code>Attr</code> associated with <code>Key</code> for most likely Pid.</td></tr><tr><td valign="top"><a href="#get_attribute-3">get_attribute/3</a></td><td>Get the attribute value of <code>Attr</code> associated with <code>Key</code> for process Pid.</td></tr><tr><td valign="top"><a href="#get_attributes-1">get_attributes/1</a></td><td>Get attributes associated with registration.</td></tr><tr><td valign="top"><a href="#get_attributes-2">get_attributes/2</a></td><td>Returns the list of attributes associated with the registration.</td></tr><tr><td valign="top"><a href="#get_env-3">get_env/3</a></td><td>Equivalent to <a href="#get_env-4"><tt>get_env(Scope, App, Key, [app_env])</tt></a>.</td></tr><tr><td valign="top"><a href="#get_env-4">get_env/4</a></td><td>Read an environment value, potentially cached as a <code>gproc_env</code> property.</td></tr><tr><td valign="top"><a href="#get_set_env-3">get_set_env/3</a></td><td>Equivalent to <a href="#get_set_env-4"><tt>get_set_env(Scope, App, Key, [app_env])</tt></a>.</td></tr><tr><td valign="top"><a href="#get_set_env-4">get_set_env/4</a></td><td>Fetch and cache an environment value, if not already cached.</td></tr><tr><td valign="top"><a href="#get_value-1">get_value/1</a></td><td>Reads the value stored with a key registered to the current process.</td></tr><tr><td valign="top"><a href="#get_value-2">get_value/2</a></td><td>Reads the value stored with a key registered to the process Pid.</td></tr><tr><td valign="top"><a href="#give_away-2">give_away/2</a></td><td>Atomically transfers the key <code>From</code> to the process identified by <code>To</code>.</td></tr><tr><td valign="top"><a href="#goodbye-0">goodbye/0</a></td><td>Unregister all items of the calling process and inform gproc
+This function is the reverse of monitor/1.</td></tr><tr><td valign="top"><a href="#first-1">first/1</a></td><td>Behaves as ets:first(Tab) for a given type of registration.</td></tr><tr><td valign="top"><a href="#get_attribute-2">get_attribute/2</a></td><td>Get attribute value of <code>Attr</code> associated with <code>Key</code> for most likely Pid.</td></tr><tr><td valign="top"><a href="#get_attribute-3">get_attribute/3</a></td><td>Get the attribute value of <code>Attr</code> associated with <code>Key</code> for process Pid.</td></tr><tr><td valign="top"><a href="#get_attribute_shared-2">get_attribute_shared/2</a></td><td>Get the attribute value of <code>Attr</code> associated with the shared <code>Key</code>.</td></tr><tr><td valign="top"><a href="#get_attributes-1">get_attributes/1</a></td><td>Get attributes associated with registration.</td></tr><tr><td valign="top"><a href="#get_attributes-2">get_attributes/2</a></td><td>Returns the list of attributes associated with the registration.</td></tr><tr><td valign="top"><a href="#get_env-3">get_env/3</a></td><td>Equivalent to <a href="#get_env-4"><tt>get_env(Scope, App, Key, [app_env])</tt></a>.</td></tr><tr><td valign="top"><a href="#get_env-4">get_env/4</a></td><td>Read an environment value, potentially cached as a <code>gproc_env</code> property.</td></tr><tr><td valign="top"><a href="#get_set_env-3">get_set_env/3</a></td><td>Equivalent to <a href="#get_set_env-4"><tt>get_set_env(Scope, App, Key, [app_env])</tt></a>.</td></tr><tr><td valign="top"><a href="#get_set_env-4">get_set_env/4</a></td><td>Fetch and cache an environment value, if not already cached.</td></tr><tr><td valign="top"><a href="#get_value-1">get_value/1</a></td><td>Reads the value stored with a key registered to the current process.</td></tr><tr><td valign="top"><a href="#get_value-2">get_value/2</a></td><td>Reads the value stored with a key registered to the process Pid.</td></tr><tr><td valign="top"><a href="#get_value_shared-1">get_value_shared/1</a></td><td>Reads the value stored with a shared key.</td></tr><tr><td valign="top"><a href="#give_away-2">give_away/2</a></td><td>Atomically transfers the key <code>From</code> to the process identified by <code>To</code>.</td></tr><tr><td valign="top"><a href="#goodbye-0">goodbye/0</a></td><td>Unregister all items of the calling process and inform gproc
 to forget about the calling process.</td></tr><tr><td valign="top"><a href="#i-0">i/0</a></td><td>Similar to the built-in shell command <code>i()</code> but inserts information
 about names and properties registered in Gproc, where applicable.</td></tr><tr><td valign="top"><a href="#info-1">info/1</a></td><td>Similar to <code>process_info(Pid)</code> but with additional gproc info.</td></tr><tr><td valign="top"><a href="#info-2">info/2</a></td><td>Similar to process_info(Pid, Item), but with additional gproc info.</td></tr><tr><td valign="top"><a href="#last-1">last/1</a></td><td>Behaves as ets:last(Tab) for a given type of registration.</td></tr><tr><td valign="top"><a href="#lookup_global_aggr_counter-1">lookup_global_aggr_counter/1</a></td><td>Lookup a global (unique) aggregated counter and returns its value.</td></tr><tr><td valign="top"><a href="#lookup_global_counters-1">lookup_global_counters/1</a></td><td>Look up all global (non-unique) instances of a given Counter.</td></tr><tr><td valign="top"><a href="#lookup_global_name-1">lookup_global_name/1</a></td><td>Lookup a global unique name.</td></tr><tr><td valign="top"><a href="#lookup_global_properties-1">lookup_global_properties/1</a></td><td>Look up all global (non-unique) instances of a given Property.</td></tr><tr><td valign="top"><a href="#lookup_local_aggr_counter-1">lookup_local_aggr_counter/1</a></td><td>Lookup a local (unique) aggregated counter and returns its value.</td></tr><tr><td valign="top"><a href="#lookup_local_counters-1">lookup_local_counters/1</a></td><td>Look up all local (non-unique) instances of a given Counter.</td></tr><tr><td valign="top"><a href="#lookup_local_name-1">lookup_local_name/1</a></td><td>Lookup a local unique name.</td></tr><tr><td valign="top"><a href="#lookup_local_properties-1">lookup_local_properties/1</a></td><td>Look up all local (non-unique) instances of a given Property.</td></tr><tr><td valign="top"><a href="#lookup_pid-1">lookup_pid/1</a></td><td>Lookup the Pid stored with a key.</td></tr><tr><td valign="top"><a href="#lookup_pids-1">lookup_pids/1</a></td><td>Returns a list of pids with the published key Key.</td></tr><tr><td valign="top"><a href="#lookup_value-1">lookup_value/1</a></td><td>Lookup the value stored with a key.</td></tr><tr><td valign="top"><a href="#lookup_values-1">lookup_values/1</a></td><td>Retrieve the <code>{Pid,Value}</code> pairs corresponding to Key.</td></tr><tr><td valign="top"><a href="#monitor-1">monitor/1</a></td><td>monitor a registered name
 This function works much like erlang:monitor(process, Pid), but monitors
@@ -574,6 +574,24 @@ Get the attribute value of `Attr` associated with `Key` for process Pid.
 
 If `Pid == shared`, the attribute of a shared key (see [`reg_shared/1`](#reg_shared-1))
 will be read.
+<a name="get_attribute_shared-2"></a>
+
+### get_attribute_shared/2 ###
+
+
+<pre><code>
+get_attribute_shared(Key, Attr::atom()) -&gt; Value
+</code></pre>
+
+<br></br>
+
+
+
+Get the attribute value of `Attr` associated with the shared `Key`.
+
+
+Equivalent to `get_attribute(Key, shared, Attr)`
+(see [`get_attribute/3`](#get_attribute-3)).
 <a name="get_attributes-1"></a>
 
 ### get_attributes/1 ###
@@ -751,6 +769,23 @@ Reads the value stored with a key registered to the process Pid.
 
 If `Pid == shared`, the value of a shared key (see [`reg_shared/1`](#reg_shared-1))
 will be read.
+<a name="get_value_shared-1"></a>
+
+### get_value_shared/1 ###
+
+
+<pre><code>
+get_value_shared(Key) -&gt; Value
+</code></pre>
+
+<br></br>
+
+
+
+Reads the value stored with a shared key.
+
+
+If no such shared key is registered, this function exits.
 <a name="give_away-2"></a>
 
 ### give_away/2 ###

+ 8 - 1
doc/gproc_dist.md

@@ -28,7 +28,7 @@ Class = n  - unique name
 | p  - non-unique property
 | c  - counter
 | a  - aggregated counter
-Scope = l | g (global or local).</td></tr><tr><td valign="top"><a href="#reg_or_locate-3">reg_or_locate/3</a></td><td></td></tr><tr><td valign="top"><a href="#reg_shared-2">reg_shared/2</a></td><td></td></tr><tr><td valign="top"><a href="#reset_counter-1">reset_counter/1</a></td><td></td></tr><tr><td valign="top"><a href="#set_attributes-2">set_attributes/2</a></td><td></td></tr><tr><td valign="top"><a href="#set_attributes_shared-2">set_attributes_shared/2</a></td><td></td></tr><tr><td valign="top"><a href="#set_value-2">set_value/2</a></td><td></td></tr><tr><td valign="top"><a href="#start_link-0">start_link/0</a></td><td></td></tr><tr><td valign="top"><a href="#start_link-1">start_link/1</a></td><td></td></tr><tr><td valign="top"><a href="#surrendered-3">surrendered/3</a></td><td></td></tr><tr><td valign="top"><a href="#sync-0">sync/0</a></td><td>Synchronize with the gproc leader.</td></tr><tr><td valign="top"><a href="#terminate-2">terminate/2</a></td><td></td></tr><tr><td valign="top"><a href="#unreg-1">unreg/1</a></td><td></td></tr><tr><td valign="top"><a href="#unreg_shared-1">unreg_shared/1</a></td><td></td></tr><tr><td valign="top"><a href="#update_counter-3">update_counter/3</a></td><td></td></tr><tr><td valign="top"><a href="#update_counters-1">update_counters/1</a></td><td></td></tr><tr><td valign="top"><a href="#update_shared_counter-2">update_shared_counter/2</a></td><td></td></tr></table>
+Scope = l | g (global or local).</td></tr><tr><td valign="top"><a href="#reg_or_locate-3">reg_or_locate/3</a></td><td></td></tr><tr><td valign="top"><a href="#reg_shared-2">reg_shared/2</a></td><td></td></tr><tr><td valign="top"><a href="#reset_counter-1">reset_counter/1</a></td><td></td></tr><tr><td valign="top"><a href="#set_attributes-2">set_attributes/2</a></td><td></td></tr><tr><td valign="top"><a href="#set_attributes_shared-2">set_attributes_shared/2</a></td><td></td></tr><tr><td valign="top"><a href="#set_value-2">set_value/2</a></td><td></td></tr><tr><td valign="top"><a href="#set_value_shared-2">set_value_shared/2</a></td><td></td></tr><tr><td valign="top"><a href="#start_link-0">start_link/0</a></td><td></td></tr><tr><td valign="top"><a href="#start_link-1">start_link/1</a></td><td></td></tr><tr><td valign="top"><a href="#surrendered-3">surrendered/3</a></td><td></td></tr><tr><td valign="top"><a href="#sync-0">sync/0</a></td><td>Synchronize with the gproc leader.</td></tr><tr><td valign="top"><a href="#terminate-2">terminate/2</a></td><td></td></tr><tr><td valign="top"><a href="#unreg-1">unreg/1</a></td><td></td></tr><tr><td valign="top"><a href="#unreg_shared-1">unreg_shared/1</a></td><td></td></tr><tr><td valign="top"><a href="#update_counter-3">update_counter/3</a></td><td></td></tr><tr><td valign="top"><a href="#update_counters-1">update_counters/1</a></td><td></td></tr><tr><td valign="top"><a href="#update_shared_counter-2">update_shared_counter/2</a></td><td></td></tr></table>
 
 
 <a name="functions"></a>
@@ -228,6 +228,13 @@ Scope = l | g (global or local)
 `set_value(Key, Value) -> any()`
 
 
+<a name="set_value_shared-2"></a>
+
+### set_value_shared/2 ###
+
+`set_value_shared(Key, Value) -> any()`
+
+
 <a name="start_link-0"></a>
 
 ### start_link/0 ###

+ 23 - 2
src/gproc.erl

@@ -78,8 +78,8 @@
          mreg/3,
          munreg/3,
          set_value/2,
-         get_value/1, get_value/2,
-	 get_attribute/2, get_attribute/3,
+         get_value/1, get_value/2, get_value_shared/1,
+	 get_attribute/2, get_attribute/3, get_attribute_shared/2,
 	 get_attributes/1, get_attributes/2,
          where/1,
          await/1, await/2, await/3,
@@ -980,6 +980,7 @@ reg_or_locate1(_, _, _) ->
 reg_shared(Key) ->
     ?CATCH_GPROC_ERROR(reg_shared1(Key), [Key]).
 
+%% @private
 reg_shared1({T,_,_} = Key) when T==a; T==p; T==c ->
     reg_shared(Key, default(Key)).
 
@@ -1001,6 +1002,7 @@ reg_shared1({T,_,_} = Key) when T==a; T==p; T==c ->
 reg_shared(Key, Value) ->
     ?CATCH_GPROC_ERROR(reg_shared1(Key, Value), [Key, Value]).
 
+%% @private
 reg_shared1({_,g,_} = Key, Value) ->
     %% anything global
     ?CHK_DIST,
@@ -1129,6 +1131,7 @@ set_attributes1(Key, Props) ->
 unreg_shared(Key) ->
     ?CATCH_GPROC_ERROR(unreg_shared1(Key), [Key]).
 
+%% @private
 unreg_shared1(Key) ->
     case Key of
         {_, g, _} ->
@@ -1338,6 +1341,14 @@ set_value_shared1({_,l,_} = Key, Value) ->
 get_value(Key) ->
     ?CATCH_GPROC_ERROR(get_value1(Key, self()), [Key]).
 
+%% @spec (Key) -> Value
+%% @doc Reads the value stored with a shared key.
+%%
+%% If no such shared key is registered, this function exits.
+%% @end
+get_value_shared(Key) ->
+    ?CATCH_GPROC_ERROR(get_value1(Key, shared), [Key]).
+
 %% @spec (Key, Pid) -> Value
 %% @doc Reads the value stored with a key registered to the process Pid.
 %%
@@ -1397,6 +1408,16 @@ get_attribute(Key, A) ->
 get_attribute(Key, Pid, A) ->
     ?CATCH_GPROC_ERROR(get_attribute1(Key, Pid, A), [Key, Pid, A]).
 
+%% @spec (Key, Attr::atom()) -> Value
+%% @doc Get the attribute value of `Attr' associated with the shared `Key'.
+%%
+%% Equivalent to `get_attribute(Key, shared, Attr)'
+%% (see {@link get_attribute/3}).
+%% @end
+get_attribute_shared(Key, Attr) ->
+    ?CATCH_GPROC_ERROR(get_attribute1(Key, shared, Attr), [Key, Attr]).
+
+%% @private
 get_attribute1({_,_,_} = Key, Pid, A) when is_pid(Pid); Pid==shared ->
     case ets:lookup(?TAB, {Pid, Key}) of
 	[{_, Attrs}] ->

+ 9 - 2
src/gproc_dist.erl

@@ -31,6 +31,7 @@
          mreg/2,
          munreg/2,
          set_value/2,
+	 set_value_shared/2,
          give_away/2,
          update_counter/3,
 	 update_counters/1,
@@ -159,7 +160,7 @@ unreg_shared(_) ->
 
 set_value({T,g,_} = Key, Value) when T==a; T==c ->
     if is_integer(Value) ->
-            leader_call({set, Key, Value});
+            leader_call({set, Key, Value, self()});
        true ->
             ?THROW_GPROC_ERROR(badarg)
     end;
@@ -168,6 +169,12 @@ set_value({_,g,_} = Key, Value) ->
 set_value(_, _) ->
     ?THROW_GPROC_ERROR(badarg).
 
+set_value_shared({T,g,_} = Key, Value) when T==a; T==c; T==p ->
+    leader_call({set, Key, Value, shared});
+set_value_shared(_, _) ->
+    ?THROW_GPROC_ERROR(badarg).
+
+
 give_away({_,g,_} = Key, To) ->
     leader_call({give_away, Key, To, self()}).
 
@@ -734,7 +741,7 @@ surrendered_1(Globs) ->
 		  _ = gproc_lib:ensure_monitor(Pid, g),
 		  ets:insert_new(?TAB, {{Pid,Key}, []}),
                   Acc;
-	     ({{Pid,_}=K, Opts}, Acc) -> % when node(Pid) =/= node() ->
+	     ({{_Pid,_}=K, Opts}, Acc) -> % when node(Pid) =/= node() ->
 		     ets:insert(?TAB, {K, Opts}),
 		     Acc;
              ({_, Pid, _} = Obj, Acc) when node(Pid) == node() ->