File: gen_server.html

package info (click to toggle)
erlang-doc-html 1%3A8.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 18,028 kB
  • ctags: 7,419
  • sloc: perl: 1,841; ansic: 323; erlang: 155
file content (394 lines) | stat: -rw-r--r-- 23,027 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<HTML>
<HEAD>
<!-- refpage -->
<TITLE>gen_server</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>


<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Erlang Systems]" SRC="min_head.gif"></A>
<H1>gen_server</H1>
</CENTER>
<H3>MODULE</H3>
<UL>
gen_server</UL>
<H3>MODULE SUMMARY</H3>
<UL>
Generic Server Behaviour</UL>
<H3>DESCRIPTION</H3>
<UL>
<P>A behaviour module for implementing the server of a client-server
relation. A generic server process (gen_server) implemented using this
module will have a standard set of interface functions and include
functionality for tracing and error reporting. It will also fit into
an OTP supervision tree. Refer to <STRONG>OTP Design Principles</STRONG> for
more information.<P>A gen_server assumes all specific parts to be located in a callback
module exporting a pre-defined set of functions. The relationship
between the behaviour functions and the callback functions can be
illustrated as follows:<PRE>gen_server module            Callback module
-----------------            ---------------
gen_server:start      -----&#62; Module:init/1

gen_server:call
gen_server:multi_call -----&#62; Module:handle_call/3

gen_server:cast
gen_server:abcast     -----&#62; Module:handle_cast/2

-                     -----&#62; Module:handle_info/2

-                     -----&#62; Module:terminate/2

-                     -----&#62; Module:code_change/3</PRE>
<P>If a callback function fails or returns a bad value, the gen_server
will terminate.<P>The <CODE>sys</CODE> module can be used for debugging a gen_server.<P>Note that a gen_server does not trap exit signals automatically,
this must be explicitly initiated in the callback module.<P>Unless otherwise stated, all functions in this module fail if
the specified gen_server does not exist or if bad arguments are given.
</UL>
<H3>EXPORTS</H3>
<P><A NAME="start%3"><STRONG><CODE>start(Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="start%4"><STRONG><CODE>start(ServerName, Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="start_link%3"><STRONG><CODE>start_link(Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="start_link%4"><STRONG><CODE>start_link(ServerName, Module, Args, Options) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>ServerName = {local,Name} | {global,Name}</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = atom()</CODE></STRONG><BR>
<STRONG><CODE>Module = atom()</CODE></STRONG><BR>
<STRONG><CODE>Args = term()</CODE></STRONG><BR>
<STRONG><CODE>Options = [Option]</CODE></STRONG><BR>
<STRONG><CODE>&#160;Option = {debug,Dbgs} | {timeout,Time}</CODE></STRONG><BR>
<STRONG><CODE>&#160;&#160;Dbgs = [Dbg]</CODE></STRONG><BR>
<STRONG><CODE>&#160;&#160;&#160;Dbg = trace | log | statistics |
         {log_to_file,FileName} | {install,{Func,FuncState}}</CODE></STRONG><BR>
<STRONG><CODE>Result = {ok,Pid} | ignore | {error,Error}</CODE></STRONG><BR>
<STRONG><CODE>&#160;Pid = pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Error = {already_started,Pid} | term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Creates a gen_server process which calls <CODE>Module:init/1</CODE> to
         initialize. To ensure a synchronized start-up procedure, this
         function does not return until <CODE>Module:init/1</CODE> has returned.
        <P>A gen_server started using <CODE>start_link</CODE> is linked to
         the calling process, this function must be used if the gen_server
         is included in a supervision tree. A gen_server started using
         <CODE>start</CODE> is not linked to the calling process.<P>If <CODE>ServerName={local,Name}</CODE> the gen_server is registered
         locally as <CODE>Name</CODE> using <CODE>register/2</CODE>.
         If <CODE>ServerName={global,Name}</CODE> the gen_server is registered
         globally as <CODE>Name</CODE> using <CODE>global:register_name/2</CODE>.
         If no name is provided, the gen_server is not registered.<P><CODE>Module</CODE> is the name of the callback module.<P><CODE>Args</CODE> is an arbitrary term which is passed as the argument
         to <CODE>Module:init/1</CODE>.<P>If the option <CODE>{timeout,Time}</CODE> is present, the gen_server is
         allowed to spend <CODE>Time</CODE> milliseconds initializing or it will
         be terminated and the start function will return
         <CODE>{error,timeout}</CODE>.<P>If the option <CODE>{debug,Dbgs}</CODE> is present, the corresponding
         <CODE>sys</CODE> function will be called for each item in <CODE>Dbgs</CODE>.
         Refer to <CODE>sys(3)</CODE> for more information.<P>If the gen_server is successfully created and initialized
         the function returns <CODE>{ok,Pid}</CODE>, where <CODE>Pid</CODE> is the pid
         of the gen_server. If there already exists a process with
         the specified <CODE>ServerName</CODE> the function returns
         <CODE>{error,{already_started,Pid}}</CODE>, where <CODE>Pid</CODE> is the pid
         of that process.<P>If <CODE>Module:init/1</CODE> fails with <CODE>Reason</CODE>, the function
         returns <CODE>{error,Reason}</CODE>. If <CODE>Module:init/1</CODE> returns
         <CODE>{stop,Reason}</CODE> or <CODE>ignore</CODE>, the process is terminated
         and the function returns <CODE>{error,Reason}</CODE> or <CODE>ignore</CODE>,
         respectively.</UL>
<P><A NAME="call%2"><STRONG><CODE>call(ServerRef, Request) -&#62; Reply</CODE></STRONG></A><BR>
<A NAME="call%3"><STRONG><CODE>call(ServerRef, Request, Timeout) -&#62; Reply</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>ServerRef = Name | {Name,Node} | {global,Name} | pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Request = term()</CODE></STRONG><BR>
<STRONG><CODE>Timeout = int()&#62;0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>Reply = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Makes a synchronous call to the gen_server <CODE>ServerRef</CODE> by
         sending a request and waiting until a reply arrives or a timout
         occurs. The gen_server will call <CODE>Module:handle_call/3</CODE> to
         handle the request.<P><CODE>ServerRef</CODE> can be:<P><UL>
<LI>the pid,</LI><BR>
<LI><CODE>Name</CODE>, if the gen_server is locally registered,</LI><BR>
<LI><CODE>{Name,Node}</CODE>, if the gen_server is locally
         registered at another node, or</LI><BR>
<LI><CODE>{global,Name}</CODE>, if the gen_server is globally
         registered.</LI><BR>
</UL>
<P><CODE>Request</CODE> is an arbitrary term which is passed as one of
         the arguments to <CODE>Module:handle_call/3</CODE>.<P><CODE>Timeout</CODE> is an integer greater than zero which specifies
         how many milliseconds to wait for a reply, or the atom
         <CODE>infinity</CODE> to wait indefinitely. Default value is 5000.
         If no reply is received within the specified time, the function
         call fails.<P>The return value <CODE>Reply</CODE> is defined in the return value of
         <CODE>Module:handle_call/3</CODE>.<P>In the case where the gen_server terminates during the handling of
         the request and the client is linked to the gen_server and trapping
         exits, the exit message is removed from the client's receive
         queue before the function call fails.<BR>

         This behaviour is retained for backwards compatibility only and may
         change in the future. Note that if the gen_server crashes in
         between calls, the client must take care of the exit message
         anyway.<BR>

         Warning: Under certain circumstances
         (e.g. <CODE>ServerRef = {Name,Node}</CODE>, and <CODE>Node</CODE> goes down)
         the exit message cannot be removed.</UL>
<P><A NAME="multi_call%2"><STRONG><CODE>multi_call(Name, Request) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="multi_call%3"><STRONG><CODE>multi_call(Nodes, Name, Request) -&#62; Result</CODE></STRONG></A><BR>
<A NAME="multi_call%4"><STRONG><CODE>multi_call(Nodes, Name, Request, Timeout) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Nodes = [Node]</CODE></STRONG><BR>
<STRONG><CODE>&#160;Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Name = atom()</CODE></STRONG><BR>
<STRONG><CODE>Request = term()</CODE></STRONG><BR>
<STRONG><CODE>Timeout = int()&#62;=0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>Result = {Replies,BadNodes}</CODE></STRONG><BR>
<STRONG><CODE>&#160;Replies = [{Node,Reply}]</CODE></STRONG><BR>
<STRONG><CODE>&#160;&#160;Reply = term()</CODE></STRONG><BR>
<STRONG><CODE>BadNodes = [Node]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Makes a synchronous call to all gen_servers locally registered as
         <CODE>Name</CODE> at the specified nodes by first sending a request to
         every node and then waiting for the replies. The gen_servers will
         call <CODE>Module:handle_call/3</CODE> to handle the request.<P>The function returns a tuple <CODE>{Replies,BadNodes}</CODE> where
         <CODE>Replies</CODE> is a list of <CODE>{Node,Reply}</CODE> and <CODE>BadNodes</CODE>
         is a list of node that either did not exist, or where
         the gen_server <CODE>Name</CODE> did not exist or did not reply.<P><CODE>Nodes</CODE> is a list of node names to which the request
         should be sent. Default value is the list of all known nodes
         <CODE>[node()|nodes()]</CODE>.<P><CODE>Name</CODE> is the locally registered name of each gen_server.<P><CODE>Request</CODE> is an arbitrary term which is passed as one of
         the arguments to <CODE>Module:handle_call/3</CODE>.<P><CODE>Timeout</CODE> is an integer greater than zero which specifies
         how many milliseconds to wait for each reply, or the atom
         <CODE>infinity</CODE> to wait indefinitely. Default value is
         <CODE>infinity</CODE>. If no reply is received from a node within
         the specified time, the node is added to <CODE>BadNodes</CODE>.<P>When a reply <CODE>Reply</CODE> is received from the gen_server at a
         node <CODE>Node</CODE>, <CODE>{Node,Reply}</CODE> is added to <CODE>Replies</CODE>.
         <CODE>Reply</CODE> is defined in the return value of
         <CODE>Module:handle_call/3</CODE>.<P><TABLE CELLPADDING=4><TR>
      <TD VALIGN=TOP><IMG ALT="Warning!" SRC="warning.gif"></TD>
      <TD>
<P>If one of the nodes is running Erlang/OTP R6B or older, and
         the gen_server is not started when the requests are sent, but
         starts within 2 seconds, this function waits the whole
         <CODE>Timeout</CODE>, which may be infinity.<P>This problem does not exist if all nodes are running Erlang/OTP
         R7B or later.</TD></TR>
      </TABLE>
<P>This function does <STRONG>not</STRONG> read out any exit messages 
         like <CODE>call/2,3</CODE> does.<P>The previously undocumented functions <CODE>safe_multi_call/2,3,4</CODE>
         were removed in OTP R7B/Erlang 5.0 since this function is now safe,
         except in the case mentioned above.</UL>
<P><A NAME="cast%2"><STRONG><CODE>cast(ServerRef, Request) -&#62; ok</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>ServerRef = Name | {Name,Node} | {global,Name} | pid()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Name = Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Request = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sends an asynchronous request to the gen_server <CODE>ServerRef</CODE>
         and returns <CODE>ok</CODE> immediately. The gen_server will call
         <CODE>Module:handle_cast/2</CODE> to handle the request.<P>See <CODE>call/2,3</CODE> for a description of <CODE>ServerRef</CODE>.<P><CODE>Request</CODE> is an arbitrary term which is passed as one
         of the arguments to <CODE>Module:handle_cast/2</CODE>.</UL>
<P><A NAME="abcast%2"><STRONG><CODE>abcast(Name, Request) -&#62; abcast</CODE></STRONG></A><BR>
<A NAME="abcast%3"><STRONG><CODE>abcast(Nodes, Name, Request) -&#62; abcast</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Nodes = [Node]</CODE></STRONG><BR>
<STRONG><CODE>&#160;Node = atom()</CODE></STRONG><BR>
<STRONG><CODE>Name = atom()</CODE></STRONG><BR>
<STRONG><CODE>Request = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Sends an asynchronous request to the gen_servers locally
         registered as <CODE>Name</CODE> at the specified nodes. The function
         returns immediately and ignores nodes that does not exist, or
         where the gen_server <CODE>Name</CODE> does not exist. The gen_servers
         will call <CODE>Module:handle_cast/2</CODE> to handle the request.<P>See <CODE>multi_call/2,3,4</CODE> for a description of the arguments.
        </UL>
<P><A NAME="reply%2"><STRONG><CODE>reply(Client, Reply) -&#62; true</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Client - see below</CODE></STRONG><BR>
<STRONG><CODE>Reply = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function can be used by a gen_server to explicitly send a
         reply to a client that called <CODE>call</CODE> or <CODE>multi_call</CODE>,
         when the reply cannot be defined in the return value of
         <CODE>Module:handle_call/3</CODE>.<P><CODE>Client</CODE> must be the <CODE>From</CODE> argument provided to
         the callback function. <CODE>Reply</CODE> is an arbitrary term, which
         will be given back to the client as the return value of <CODE>call</CODE>
         or <CODE>multi_call</CODE>.</UL>
<H3>CALLBACK FUNCTIONS</H3>
<UL>
<P>The following functions should be exported from a <CODE>gen_server</CODE>
callback module.</UL>
<H3>EXPORTS</H3>
<P><A NAME="Module:init%1"><STRONG><CODE>Module:init(Args) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Args = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {ok,State} | {ok,State,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160;| {stop,Reason} | ignore</CODE></STRONG><BR>
<STRONG><CODE>&#160;State = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;=0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Whenever a gen_server is started using <CODE>gen_server:start/3,4</CODE>
         or <CODE>gen_server:start_link/3.4</CODE>, this function is called by
         the new process to initialize.<P><CODE>Args</CODE> is the <CODE>Args</CODE> argument provided to the start
         function.<P>If the initialization is successful, the function should return
         <CODE>{ok,State}</CODE> or <CODE>{ok,State,Timout}</CODE>, where <CODE>State</CODE>
         is the internal state of the gen_server.<P>If an integer timout value is provided, a timout will occur
         unless a request or a message is received within <CODE>Timeout</CODE>
         milliseconds. A timout is represented by the atom <CODE>timeout</CODE>
         which should be handled by the <CODE>handle_info/2</CODE> callback
         function. The atom <CODE>inifinity</CODE> can be used to wait
         indefinitely, this is the default value.<P>If something goes wrong during the initialization the function
         should return <CODE>{stop,Reason}</CODE> where <CODE>Reason</CODE> is any
         term, or <CODE>ignore</CODE>.</UL>
<P><A NAME="Module:handle_call%3"><STRONG><CODE>Module:handle_call(Request, From, State) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Request = term()</CODE></STRONG><BR>
<STRONG><CODE>From = {pid(),Tag}</CODE></STRONG><BR>
<STRONG><CODE>State = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {reply,Reply,NewState} | {reply,Reply,NewState,Timeout}
        </CODE></STRONG><BR>
<STRONG><CODE>&#160;| {noreply,NewState} | {noreply,NewState,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160;| {stop,Reason,Reply,NewState} | {stop,Reason,NewState}
        </CODE></STRONG><BR>
<STRONG><CODE>&#160;Reply = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewState = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;=0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Whenever a gen_server receives a request sent using
         <CODE>gen_server:call/2,3</CODE> or <CODE>gen_server:multi_call/2,3,4</CODE>,
         this function is called to handle the request.<P><CODE>Request</CODE> is the <CODE>Request</CODE> argument provided
         to <CODE>call</CODE> or <CODE>multi_call</CODE>.<P><CODE>From</CODE> is a tuple <CODE>{Pid,Tag}</CODE> where <CODE>Pid</CODE> is
         the pid of the client and <CODE>Tag</CODE> is a unique tag.<P><CODE>State</CODE> is the internal state of the gen_server.<P>If the function returns <CODE>{reply,Reply,NewState}</CODE> or
         <CODE>{reply,Reply,NewState,Timout}</CODE>, <CODE>Reply</CODE> will be given
         back to <CODE>From</CODE> as the return value of <CODE>call</CODE> or included
         in the return value of <CODE>multi_call</CODE>. The gen_server then
         continues executing with the possibly updated internal state
         <CODE>NewState</CODE>.
         See <CODE>Module:init/1</CODE> for a description of <CODE>Timeout</CODE>.<P>If the functions returns <CODE>{noreply,NewState}</CODE> or
         <CODE>{noreply,NewState,Timeout}</CODE>, the gen_server will continue
         executing with <CODE>NewState</CODE>. Any reply to <CODE>From</CODE> must
         be given explicitly using <CODE>gen_server:reply/2</CODE>.<P>If the function returns <CODE>{stop,Reason,Reply,NewState}</CODE>,
         <CODE>Reply</CODE> will be given back to <CODE>From</CODE>. If the function
         returns <CODE>{stop,Reason,NewState}</CODE>, any reply to <CODE>From</CODE>
         must be given explicitly using <CODE>gen_server:reply/2</CODE>.
         The gen_server will then call
         <CODE>Module:terminate(Reason,NewState)</CODE> and terminate.</UL>
<P><A NAME="Module:handle_cast%2"><STRONG><CODE>Module:handle_cast(Request, State) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Request = term()</CODE></STRONG><BR>
<STRONG><CODE>State = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {noreply,NewState} | {noreply,NewState,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160;| {stop,Reason,NewState}</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewState = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;=0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Whenever a gen_server receives a request sent using
         <CODE>gen_server:cast/2</CODE> or <CODE>gen_server:abcast/2,3</CODE>, this
         function is called to handle the request.<P>See <CODE>Module:handle_call/3</CODE> for a description of
         the arguments and possible return values.</UL>
<P><A NAME="Module:handle_info%2"><STRONG><CODE>Module:handle_info(Info, State) -&#62; Result</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Info = timeout | term()</CODE></STRONG><BR>
<STRONG><CODE>State = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {noreply,NewState} | {noreply,NewState,Timeout}</CODE></STRONG><BR>
<STRONG><CODE>&#160;| {stop,Reason,NewState}</CODE></STRONG><BR>
<STRONG><CODE>&#160;NewState = term()</CODE></STRONG><BR>
<STRONG><CODE>&#160;Timeout = int()&#62;=0 | infinity</CODE></STRONG><BR>
<STRONG><CODE>&#160;Reason = normal | term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function is called by a gen_server when a timeout occurs or
         when it receives any other message than a synchronous or
         asynchronous request (or a system message).<P><CODE>Info</CODE> is either the atom <CODE>timeout</CODE>, if a timeout has
         occured, or the received message.<P>See <CODE>Module:handle_call/3</CODE> for a description of the other
         arguments and possible return values.</UL>
<P><A NAME="Module:terminate%2"><STRONG><CODE>Module:terminate(Reason, State)</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Reason = normal | shutdown | term()</CODE></STRONG><BR>
<STRONG><CODE>State = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function is called by a gen_server when it is about to
         terminate. It should be the opposite of <CODE>Module:init/1</CODE> and
         do any necessary cleaning up. When it returns, the gen_server
         terminates with <CODE>Reason</CODE>. The return value is ignored.<P><CODE>Reason</CODE> is a term denoting the stop reason and <CODE>State</CODE>
         is the internal state of the gen_server.<P><CODE>Reason</CODE> depends on why the gen_server is terminating. If it
         is because another callback function has returned a stop tuple
         <CODE>{stop,..}</CODE>, <CODE>Reason</CODE> will have the value specified in
         that tuple. If it is due to a failure, <CODE>Reason</CODE> is the error
         reason.<P>If the gen_server is part of a supervision tree and is ordered
         by its supervisor to terminate, this function will be called with
         <CODE>Reason=shutdown</CODE> if the following conditions apply:<P><UL>
<LI>the gen_server has been set to trap exit signals, and</LI><BR>
<LI>the shutdown strategy as defined in the supervisor's child
         specification is an integer timeout value, not
         <CODE>brutal_kill</CODE>.
         </LI><BR>
</UL>
<P>Otherwise, the gen_server will be immediately terminated.<P>Note that for any other reason than <CODE>normal</CODE> or
         <CODE>shutdown</CODE>, the gen_server is assumed to terminate due to an
         error and an error report is issued using
         <CODE>error_logger:format/2</CODE>.</UL>
<P><A NAME="Module:code_change%3"><STRONG><CODE>Module:code_change(OldVsn, State, Extra) -&#62; {ok, NewState}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>OldVsn = undefined | term()</CODE></STRONG><BR>
<STRONG><CODE>State = NewState = term()</CODE></STRONG><BR>
<STRONG><CODE>Extra = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>This function is called by a gen_server when it should update its
         internal state due to code replacement, i.e. when the instruction
         <CODE>{update,Module,Change,PrePurge,PostPurge,Modules}</CODE> where
         <CODE>Change={advanced,Extra}</CODE> has been given to the release
         handler. See <STRONG>SASL User's Guide</STRONG> for more information.<P><CODE>OldVsn</CODE> is the <CODE>vsn</CODE> attribute of the old version of
         the callback module <CODE>Module</CODE>, or <CODE>undefined</CODE> if no
         such attribute is defined.<P><CODE>State</CODE> is the internal state of the gen_server.<P><CODE>Extra</CODE> is the same as in the <CODE>{advanced,Extra}</CODE> part
         of the update instruction.<P>The function should return the updated internal state.</UL>
<H3>SEE ALSO</H3>
<UL>
<P>supervisor(3), sys(3)</UL>
<H3>AUTHORS</H3>
<UL>
Gunilla Hugosson - support@erlang.ericsson.se<BR>
</UL>
<CENTER>
<HR>
<FONT SIZE=-1>stdlib 1.10<BR>
Copyright &copy; 1991-2001
<A HREF="http://www.erlang.se">Ericsson Utvecklings AB</A><BR>
<!--#include virtual="/ssi/otp_footer.html"-->
</FONT>
</CENTER>
</BODY>
</HTML>