~rdoering/ubuntu/karmic/erlang/fix-535090

« back to all changes in this revision

Viewing changes to lib/kernel/doc/src/global.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE erlref SYSTEM "erlref.dtd">
 
3
 
 
4
<erlref>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>1996</year>
 
8
      <year>2007</year>
 
9
      <holder>Ericsson AB, All Rights Reserved</holder>
 
10
    </copyright>
 
11
    <legalnotice>
 
12
  The contents of this file are subject to the Erlang Public License,
 
13
  Version 1.1, (the "License"); you may not use this file except in
 
14
  compliance with the License. You should have received a copy of the
 
15
  Erlang Public License along with this software. If not, it can be
 
16
  retrieved online at http://www.erlang.org/.
 
17
 
 
18
  Software distributed under the License is distributed on an "AS IS"
 
19
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
20
  the License for the specific language governing rights and limitations
 
21
  under the License.
 
22
 
 
23
  The Initial Developer of the Original Code is Ericsson AB.
 
24
    </legalnotice>
 
25
 
 
26
    <title>global</title>
 
27
    <prepared>Martin Bj&ouml;rklund</prepared>
 
28
    <docno></docno>
 
29
    <date>1997-11-17</date>
 
30
    <rev></rev>
 
31
  </header>
 
32
  <module>global</module>
 
33
  <modulesummary>A Global Name Registration Facility</modulesummary>
 
34
  <description>
 
35
    <p>This documentation describes the Global module which consists
 
36
      of the following functionalities:
 
37
      </p>
 
38
    <list type="bulleted">
 
39
      <item>registration of global names;</item>
 
40
      <item>global locks;</item>
 
41
      <item>maintenance of the fully connected network.</item>
 
42
    </list>
 
43
    <p>These services are controlled via the process
 
44
      <c>global_name_server</c> which exists on every node. The global
 
45
      name server is started automatically when a node is started.
 
46
      With the term <em>global</em> is meant over a system consisting
 
47
      of several Erlang nodes.</p>
 
48
    <p>The ability to globally register names is a central concept in
 
49
      the programming of distributed Erlang systems. In this module,
 
50
      the equivalent of the <c>register/2</c> and <c>whereis/1</c>
 
51
      BIFs (for local name registration) are implemented, but for a
 
52
      network of Erlang nodes. A registered name is an alias for a
 
53
      process identifier (pid). The global name server monitors
 
54
      globally registered pids. If a process terminates, the name will
 
55
      also be globally unregistered.</p>
 
56
    <p>The registered names are stored in replica global name tables on
 
57
      every node. There is no central storage point. Thus,
 
58
      the translation of a name to a pid is fast, as it is always done
 
59
      locally. When any action in taken which results in a change to
 
60
      the global name table, all tables on other nodes are automatically
 
61
      updated.</p>
 
62
    <p>Global locks have lock identities and are set on a specific
 
63
      resource. For instance, the specified resource could be a pid.
 
64
      When a global lock is set, access to the locked resource is
 
65
      denied for all other resources other than the lock requester.</p>
 
66
    <p>Both the registration and lock functionalities are atomic. All
 
67
      nodes involved in these actions will have the same view of
 
68
      the information.</p>
 
69
    <p>The global name server also performs the critical task of
 
70
      continuously monitoring changes in node configuration: if a node
 
71
      which runs a globally registered process goes down, the name
 
72
      will be globally unregistered. To this end the global name
 
73
      server subscribes to <c>nodeup</c> and <c>nodedown</c> messages
 
74
      sent from the <c>net_kernel</c> module. Relevant Kernel
 
75
      application variables in this context are <c>net_setuptime</c>,
 
76
      <c>net_ticktime</c>, and <c>dist_auto_connect</c>. See also
 
77
      <seealso marker="kernel_app#net_setuptime">kernel(6)</seealso>.
 
78
      </p>
 
79
    <p>The server will also maintain a fully connected network. For
 
80
      example, if node <c>N1</c> connects to node <c>N2</c> (which is
 
81
      already connected to <c>N3</c>), the global name servers on the
 
82
      nodes <c>N1</c> and <c>N3</c> will make sure that also <c>N1</c>
 
83
      and <c>N3</c> are connected. If this is not desired, the command
 
84
      line flag <c>-connect_all false</c> can be used (see also
 
85
      <seealso marker="erts:erl#connect_all">erl(1)</seealso>). In
 
86
      this case the name registration facility cannot be used, but the
 
87
      lock mechanism will still work.
 
88
      </p>
 
89
    <p>If the global name server fails to connect nodes (<c>N1</c> and
 
90
      <c>N3</c> in the example above) a warning event is sent to the
 
91
      error logger. The presence of such an event does not exclude the
 
92
      possibility that the nodes will later connect--one can for
 
93
      example try the command <c>rpc:call(N1, net_adm, ping, [N2]) in the Erlang shell</c>--but it indicates some kind of problem with
 
94
      the network.
 
95
      </p>
 
96
    <note>
 
97
      <p>If the fully connected network is not set up properly, the
 
98
        first thing to try is to increase the value of
 
99
        <c>net_setuptime</c>.</p>
 
100
    </note>
 
101
  </description>
 
102
  <funcs>
 
103
    <func>
 
104
      <name>del_lock(Id)</name>
 
105
      <name>del_lock(Id, Nodes) -> void()</name>
 
106
      <fsummary>Delete a lock</fsummary>
 
107
      <type>
 
108
        <v>Id = {ResourceId, LockRequesterId}</v>
 
109
        <v>&nbsp;ResourceId = term()</v>
 
110
        <v>&nbsp;LockRequesterId = term()</v>
 
111
        <v>Nodes = [node()]</v>
 
112
      </type>
 
113
      <desc>
 
114
        <p>Deletes the lock <c>Id</c> synchronously.</p>
 
115
      </desc>
 
116
    </func>
 
117
    <func>
 
118
      <name>notify_all_name(Name, Pid1, Pid2) -> none</name>
 
119
      <fsummary>Name resolving function that notifies both pids</fsummary>
 
120
      <type>
 
121
        <v>Name = term()</v>
 
122
        <v>Pid1 = Pid2 = pid()</v>
 
123
      </type>
 
124
      <desc>
 
125
        <p>This function can be used as a name resolving function for
 
126
          <c>register_name/3</c> and <c>re_register_name/3</c>. It
 
127
          unregisters both pids, and sends the message
 
128
          <c>{global_name_conflict, Name, OtherPid}</c> to both
 
129
          processes.</p>
 
130
      </desc>
 
131
    </func>
 
132
    <func>
 
133
      <name>random_exit_name(Name, Pid1, Pid2) -> Pid1 | Pid2</name>
 
134
      <fsummary>Name resolving function that kills one pid</fsummary>
 
135
      <type>
 
136
        <v>Name = term()</v>
 
137
        <v>Pid1 = Pid2 = pid()</v>
 
138
      </type>
 
139
      <desc>
 
140
        <p>This function can be used as a name resolving function for
 
141
          <c>register_name/3</c> and <c>re_register_name/3</c>. It
 
142
          randomly chooses one of the pids for registration and kills
 
143
          the other one.</p>
 
144
      </desc>
 
145
    </func>
 
146
    <func>
 
147
      <name>random_notify_name(Name, Pid1, Pid2) -> Pid1 | Pid2</name>
 
148
      <fsummary>Name resolving function that notifies one pid</fsummary>
 
149
      <type>
 
150
        <v>Name = term()</v>
 
151
        <v>Pid1 = Pid2 = pid()</v>
 
152
      </type>
 
153
      <desc>
 
154
        <p>This function can be used as a name resolving function for
 
155
          <c>register_name/3</c> and <c>re_register_name/3</c>. It
 
156
          randomly chooses one of the pids for registration, and sends
 
157
          the message <c>{global_name_conflict, Name}</c> to the other
 
158
          pid.</p>
 
159
      </desc>
 
160
    </func>
 
161
    <func>
 
162
      <name>register_name(Name, Pid)</name>
 
163
      <name>register_name(Name, Pid, Resolve) -> yes | no</name>
 
164
      <fsummary>Globally register a name for a pid</fsummary>
 
165
      <type>
 
166
        <v>Name = term()</v>
 
167
        <v>Pid = pid()</v>
 
168
        <v>Resolve = fun() or {Module, Function} where</v>
 
169
        <v>&nbsp;&nbsp;Resolve(Name, Pid, Pid2) -> Pid | Pid2 | none</v>
 
170
      </type>
 
171
      <desc>
 
172
        <p>Globally associates the name <c>Name</c> with a pid, that is,
 
173
          Globally notifies all nodes of a new global name in a network
 
174
          of Erlang nodes.</p>
 
175
        <p>When new nodes are added to the network, they are informed
 
176
          of the globally registered names that already exist.
 
177
          The network is also informed of any global names in newly
 
178
          connected nodes. If any name clashes are discovered,
 
179
          the <c>Resolve</c> function is called. Its purpose is to
 
180
          decide which pid is correct. If the function crashes, or
 
181
          returns anything other than one of the pids, the name is
 
182
          unregistered. This function is called once for each name
 
183
          clash.</p>
 
184
        <p>There are three pre-defined resolve functions:
 
185
          <c>random_exit_name/3</c>, <c>random_notify_name/3</c>, and
 
186
          <c>notify_all_name/3</c>. If no <c>Resolve</c> function is
 
187
          defined, <c>random_exit_name</c> is used. This means that one
 
188
          of the two registered processes will be selected as correct
 
189
          while the other is killed.</p>
 
190
        <p>This function is completely synchronous. This means that
 
191
          when this function returns, the name is either registered on
 
192
          all nodes or none.</p>
 
193
        <p>The function returns <c>yes</c> if successful, <c>no</c> if
 
194
          it fails. For example, <c>no</c> is returned if an attempt
 
195
          is made to register an already registered process or to
 
196
          register a process with a name that is already in use.</p>
 
197
        <note>
 
198
          <p>Releases up to and including OTP R10 did not check if
 
199
            the process was already registered. As a consequence the
 
200
            global name table could become inconsistent. The old (buggy)
 
201
            behavior can be chosen by giving the Kernel application
 
202
            variable <c>global_multi_name_action</c> the value
 
203
            <c>allow</c>.</p>
 
204
        </note>
 
205
        <p>If a process with a registered name dies, or the node goes
 
206
          down, the name is unregistered on all nodes.</p>
 
207
      </desc>
 
208
    </func>
 
209
    <func>
 
210
      <name>registered_names() -> [Name]</name>
 
211
      <fsummary>All globally registered names</fsummary>
 
212
      <type>
 
213
        <v>Name = term()</v>
 
214
      </type>
 
215
      <desc>
 
216
        <p>Returns a lists of all globally registered names.</p>
 
217
      </desc>
 
218
    </func>
 
219
    <func>
 
220
      <name>re_register_name(Name, Pid)</name>
 
221
      <name>re_register_name(Name, Pid, Resolve) -> void()</name>
 
222
      <fsummary>Atomically re-register a name</fsummary>
 
223
      <type>
 
224
        <v>Name = term()</v>
 
225
        <v>Pid = pid()</v>
 
226
        <v>Resolve = fun() or {Module, Function} where</v>
 
227
        <v>&nbsp;&nbsp;Resolve(Name, Pid, Pid2) -> Pid | Pid2 | none</v>
 
228
      </type>
 
229
      <desc>
 
230
        <p>Atomically changes the registered name <c>Name</c> on all
 
231
          nodes to refer to <c>Pid</c>.</p>
 
232
        <p>The <c>Resolve</c> function has the same behavior as in
 
233
          <c>register_name/2,3</c>.</p>
 
234
      </desc>
 
235
    </func>
 
236
    <func>
 
237
      <name>send(Name, Msg) -> Pid</name>
 
238
      <fsummary>Send a message to a globally registered pid</fsummary>
 
239
      <type>
 
240
        <v>Name = term()</v>
 
241
        <v>Msg = term()</v>
 
242
        <v>Pid = pid()</v>
 
243
      </type>
 
244
      <desc>
 
245
        <p>Sends the message <c>Msg</c> to the pid globally registered
 
246
          as <c>Name</c>.</p>
 
247
        <p>Failure: If <c>Name</c> is not a globally registered
 
248
          name, the calling function will exit with reason
 
249
          <c>{badarg, {Name, Msg}}</c>.</p>
 
250
      </desc>
 
251
    </func>
 
252
    <func>
 
253
      <name>set_lock(Id)</name>
 
254
      <name>set_lock(Id, Nodes)</name>
 
255
      <name>set_lock(Id, Nodes, Retries) -> boolean()</name>
 
256
      <fsummary>Set a lock on the specified nodes</fsummary>
 
257
      <type>
 
258
        <v>Id = {ResourceId, LockRequesterId}</v>
 
259
        <v>&nbsp;ResourceId = term()</v>
 
260
        <v>&nbsp;LockRequesterId = term()</v>
 
261
        <v>Nodes = [node()]</v>
 
262
        <v>Retries = int() >= 0 | infinity</v>
 
263
      </type>
 
264
      <desc>
 
265
        <p>Sets a lock on the specified nodes (or on all nodes if none
 
266
          are specified) on <c>ResourceId</c> for
 
267
          <c>LockRequesterId</c>. If a lock already exists on
 
268
          <c>ResourceId</c> for another requester than
 
269
          <c>LockRequesterId</c>, and <c>Retries</c> is not equal to 0,
 
270
          the process sleeps for a while and will try to execute
 
271
          the action later. When <c>Retries</c> attempts have been made,
 
272
          <c>false</c> is returned, otherwise <c>true</c>. If
 
273
          <c>Retries</c> is <c>infinity</c>, <c>true</c> is eventually
 
274
          returned (unless the lock is never released).</p>
 
275
        <p>If no value for <c>Retries</c> is given, <c>infinity</c> is
 
276
          used.</p>
 
277
        <p>This function is completely synchronous.</p>
 
278
        <p>If a process which holds a lock dies, or the node goes
 
279
          down, the locks held by the process are deleted.</p>
 
280
        <p>The global name server keeps track of all processes sharing
 
281
          the same lock, that is, if two processes set the same lock,
 
282
          both processes must delete the lock.</p>
 
283
        <p>This function does not address the problem of a deadlock. A
 
284
          deadlock can never occur as long as processes only lock one
 
285
          resource at a time. But if some processes try to lock two or
 
286
          more resources, a deadlock may occur. It is up to the
 
287
          application to detect and rectify a deadlock.</p>
 
288
        <note>
 
289
          <p>Some values of <c>ResourceId</c> should be avoided or
 
290
            Erlang/OTP will not work properly. A list of
 
291
            resources to avoid: <c>global</c>, <c>dist_ac</c>,
 
292
            <c>mnesia_table_lock</c>, <c>mnesia_adjust_log_writes</c>,
 
293
            <c>pg2</c>.</p>
 
294
        </note>
 
295
      </desc>
 
296
    </func>
 
297
    <func>
 
298
      <name>sync() -> void()</name>
 
299
      <fsummary>Synchronize the global name server</fsummary>
 
300
      <desc>
 
301
        <p>Synchronizes the global name server with all nodes known to
 
302
          this node. These are the nodes which are returned from
 
303
          <c>erlang:nodes()</c>. When this function returns,
 
304
          the global name server will receive global information from
 
305
          all nodes. This function can be called when new nodes are
 
306
          added to the network.</p>
 
307
      </desc>
 
308
    </func>
 
309
    <func>
 
310
      <name>trans(Id, Fun)</name>
 
311
      <name>trans(Id, Fun, Nodes)</name>
 
312
      <name>trans(Id, Fun, Nodes, Retries) -> Res | aborted</name>
 
313
      <fsummary>Micro transaction facility</fsummary>
 
314
      <type>
 
315
        <v>Id = {ResourceId, LockRequesterId}</v>
 
316
        <v>&nbsp;ResourceId = term()</v>
 
317
        <v>&nbsp;LockRequesterId = term()</v>
 
318
        <v>Fun = fun() | {M, F}</v>
 
319
        <v>Nodes = [node()]</v>
 
320
        <v>Retries = int() >= 0 | infinity</v>
 
321
        <v>Res = term()</v>
 
322
      </type>
 
323
      <desc>
 
324
        <p>Sets a lock on <c>Id</c> (using <c>set_lock/3</c>). If this
 
325
          succeeds, <c>Fun()</c> is evaluated and the result <c>Res</c>
 
326
          is returned.  Returns <c>aborted</c> if the lock attempt
 
327
          failed. If <c>Retries</c> is set to <c>infinity</c>,
 
328
          the transaction will not abort.</p>
 
329
        <p><c>infinity</c> is the default setting and will be used if 
 
330
          no value is given for <c>Retries</c>.</p>
 
331
      </desc>
 
332
    </func>
 
333
    <func>
 
334
      <name>unregister_name(Name) -> void()</name>
 
335
      <fsummary>Remove a globally registered name for a pid</fsummary>
 
336
      <type>
 
337
        <v>Name = term()</v>
 
338
      </type>
 
339
      <desc>
 
340
        <p>Removes the globally registered name <c>Name</c> from
 
341
          the network of Erlang nodes.</p>
 
342
      </desc>
 
343
    </func>
 
344
    <func>
 
345
      <name>whereis_name(Name) -> pid() | undefined</name>
 
346
      <fsummary>Get the pid with a given globally registered name</fsummary>
 
347
      <type>
 
348
        <v>Name = term()</v>
 
349
      </type>
 
350
      <desc>
 
351
        <p>Returns the pid with the globally registered name
 
352
          <c>Name</c>. Returns <c>undefined</c> if the name is not
 
353
          globally registered.</p>
 
354
      </desc>
 
355
    </func>
 
356
  </funcs>
 
357
 
 
358
  <section>
 
359
    <title>See Also</title>
 
360
    <p><seealso marker="global_group">global_group(3)</seealso>,
 
361
      <seealso marker="net_kernel">net_kernel(3)</seealso>,</p>
 
362
  </section>
 
363
</erlref>
 
364