Sun Dec 17 15:56:35 1995 Miles Bader <miles@gnu.ai.mit.edu>
[glibc.git] / manual / setjmp.texi
blobdfdac1c4cd297630014b2db297fa8a861074a05c
1 @node Non-Local Exits, Signal Handling, Date and Time, Top
2 @chapter Non-Local Exits
3 @cindex non-local exits
4 @cindex long jumps
6 Sometimes when your program detects an unusual situation inside a deeply
7 nested set of function calls, you would like to be able to immediately
8 return to an outer level of control.  This section describes how you can
9 do such @dfn{non-local exits} using the @code{setjmp} and @code{longjmp}
10 functions.
12 @menu
13 * Intro: Non-Local Intro.        When and how to use these facilities.
14 * Details: Non-Local Details.   Functions for nonlocal exits.
15 * Non-Local Exits and Signals::  Portability issues.
16 @end menu
18 @node Non-Local Intro, Non-Local Details,  , Non-Local Exits
19 @section Introduction to Non-Local Exits
21 As an example of a situation where a non-local exit can be useful,
22 suppose you have an interactive program that has a ``main loop'' that
23 prompts for and executes commands.  Suppose the ``read'' command reads
24 input from a file, doing some lexical analysis and parsing of the input
25 while processing it.  If a low-level input error is detected, it would
26 be useful to be able to return immediately to the ``main loop'' instead
27 of having to make each of the lexical analysis, parsing, and processing
28 phases all have to explicitly deal with error situations initially
29 detected by nested calls.
31 (On the other hand, if each of these phases has to do a substantial
32 amount of cleanup when it exits---such as closing files, deallocating
33 buffers or other data structures, and the like---then it can be more
34 appropriate to do a normal return and have each phase do its own
35 cleanup, because a non-local exit would bypass the intervening phases and
36 their associated cleanup code entirely.  Alternatively, you could use a
37 non-local exit but do the cleanup explicitly either before or after
38 returning to the ``main loop''.)
40 In some ways, a non-local exit is similar to using the @samp{return}
41 statement to return from a function.  But while @samp{return} abandons
42 only a single function call, transferring control back to the point at
43 which it was called, a non-local exit can potentially abandon many
44 levels of nested function calls.
46 You identify return points for non-local exits calling the function
47 @code{setjmp}.  This function saves information about the execution
48 environment in which the call to @code{setjmp} appears in an object of
49 type @code{jmp_buf}.  Execution of the program continues normally after
50 the call to @code{setjmp}, but if a exit is later made to this return
51 point by calling @code{longjmp} with the corresponding @w{@code{jmp_buf}}
52 object, control is transferred back to the point where @code{setjmp} was
53 called.  The return value from @code{setjmp} is used to distinguish
54 between an ordinary return and a return made by a call to
55 @code{longjmp}, so calls to @code{setjmp} usually appear in an @samp{if}
56 statement.
58 Here is how the example program described above might be set up:  
60 @smallexample
61 @include setjmp.c.texi
62 @end smallexample
64 The function @code{abort_to_main_loop} causes an immediate transfer of
65 control back to the main loop of the program, no matter where it is
66 called from.
68 The flow of control inside the @code{main} function may appear a little
69 mysterious at first, but it is actually a common idiom with
70 @code{setjmp}.  A normal call to @code{setjmp} returns zero, so the
71 ``else'' clause of the conditional is executed.  If
72 @code{abort_to_main_loop} is called somewhere within the execution of
73 @code{do_command}, then it actually appears as if the @emph{same} call
74 to @code{setjmp} in @code{main} were returning a second time with a value
75 of @code{-1}.
77 @need 250
78 So, the general pattern for using @code{setjmp} looks something like:
80 @smallexample
81 if (setjmp (@var{buffer}))
82   /* @r{Code to clean up after premature return.} */
83   @dots{}
84 else
85   /* @r{Code to be executed normally after setting up the return point.} */
86   @dots{}
87 @end smallexample
89 @node Non-Local Details, Non-Local Exits and Signals, Non-Local Intro, Non-Local Exits
90 @section Details of Non-Local Exits
92 Here are the details on the functions and data structures used for
93 performing non-local exits.  These facilities are declared in
94 @file{setjmp.h}.
95 @pindex setjmp.h
97 @comment setjmp.h
98 @comment ANSI
99 @deftp {Data Type} jmp_buf
100 Objects of type @code{jmp_buf} hold the state information to
101 be restored by a non-local exit.  The contents of a @code{jmp_buf}
102 identify a specific place to return to.
103 @end deftp
105 @comment setjmp.h
106 @comment ANSI
107 @deftypefn Macro int setjmp (jmp_buf @var{state})
108 When called normally, @code{setjmp} stores information about the
109 execution state of the program in @var{state} and returns zero.  If
110 @code{longjmp} is later used to perform a non-local exit to this
111 @var{state}, @code{setjmp} returns a nonzero value.
112 @end deftypefn
114 @comment setjmp.h
115 @comment ANSI
116 @deftypefun void longjmp (jmp_buf @var{state}, int @var{value}) 
117 This function restores current execution to the state saved in
118 @var{state}, and continues execution from the call to @code{setjmp} that
119 established that return point.  Returning from @code{setjmp} by means of
120 @code{longjmp} returns the @var{value} argument that was passed to
121 @code{longjmp}, rather than @code{0}.  (But if @var{value} is given as
122 @code{0}, @code{setjmp} returns @code{1}).@refill
123 @end deftypefun
125 There are a lot of obscure but important restrictions on the use of
126 @code{setjmp} and @code{longjmp}.  Most of these restrictions are
127 present because non-local exits require a fair amount of magic on the
128 part of the C compiler and can interact with other parts of the language
129 in strange ways.
131 The @code{setjmp} function is actually a macro without an actual
132 function definition, so you shouldn't try to @samp{#undef} it or take
133 its address.  In addition, calls to @code{setjmp} are safe in only the
134 following contexts:
136 @itemize @bullet
137 @item
138 As the test expression of a selection or iteration
139 statement (such as @samp{if}, @samp{switch}, or @samp{while}).
141 @item
142 As one operand of a equality or comparison operator that appears as the
143 test expression of a selection or iteration statement.  The other
144 operand must be an integer constant expression.
146 @item
147 As the operand of a unary @samp{!} operator, that appears as the
148 test expression of a selection or iteration statement.
150 @item
151 By itself as an expression statement.
152 @end itemize
154 Return points are valid only during the dynamic extent of the function
155 that called @code{setjmp} to establish them.  If you @code{longjmp} to
156 a return point that was established in a function that has already
157 returned, unpredictable and disastrous things are likely to happen.
159 You should use a nonzero @var{value} argument to @code{longjmp}.  While
160 @code{longjmp} refuses to pass back a zero argument as the return value
161 from @code{setjmp}, this is intended as a safety net against accidental
162 misuse and is not really good programming style.
164 When you perform a non-local exit, accessible objects generally retain
165 whatever values they had at the time @code{longjmp} was called.  The
166 exception is that the values of automatic variables local to the
167 function containing the @code{setjmp} call that have been changed since
168 the call to @code{setjmp} are indeterminate, unless you have declared
169 them @code{volatile}.
171 @node Non-Local Exits and Signals,, Non-Local Details, Non-Local Exits
172 @section Non-Local Exits and Signals
174 In BSD Unix systems, @code{setjmp} and @code{longjmp} also save and
175 restore the set of blocked signals; see @ref{Blocking Signals}.  However,
176 the POSIX.1 standard requires @code{setjmp} and @code{longjmp} not to
177 change the set of blocked signals, and provides an additional pair of
178 functions (@code{sigsetjmp} and @code{sigsetjmp}) to get the BSD
179 behavior.
181 The behavior of @code{setjmp} and @code{longjmp} in the GNU library is
182 controlled by feature test macros; see @ref{Feature Test Macros}.  The
183 default in the GNU system is the POSIX.1 behavior rather than the BSD
184 behavior.
186 The facilities in this section are declared in the header file
187 @file{setjmp.h}.
188 @pindex setjmp.h
190 @comment setjmp.h
191 @comment POSIX.1
192 @deftp {Data Type} sigjmp_buf
193 This is similar to @code{jmp_buf}, except that it can also store state
194 information about the set of blocked signals.
195 @end deftp
197 @comment setjmp.h
198 @comment POSIX.1
199 @deftypefun int sigsetjmp (sigjmp_buf @var{state}, int @var{savesigs})
200 This is similar to @code{setjmp}.  If @var{savesigs} is nonzero, the set
201 of blocked signals is saved in @var{state} and will be restored if a
202 @code{siglongjmp} is later performed with this @var{state}.
203 @end deftypefun
205 @comment setjmp.h
206 @comment POSIX.1
207 @deftypefun void siglongjmp (sigjmp_buf @var{state}, int @var{value})
208 This is similar to @code{longjmp} except for the type of its @var{state}
209 argument.  If the @code{sigsetjmp} call that set this @var{state} used a
210 nonzero @var{savesigs} flag, @code{siglongjmp} also restores the set of
211 blocked signals.
212 @end deftypefun