summaryrefslogtreecommitdiff
path: root/usr/src/man/man3ucb/setjmp.3ucb
blob: fec0256b8b6055aa6ee79bb9f8d539eef7566805 (plain)
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
'\" te
.\" Copyright (c) 2007, Sun Microsystems, Inc. All Rights Reserved.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
.\"  See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with
.\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH setjmp 3UCB "7 Apr 1993" "SunOS 5.11" "SunOS/BSD Compatibility Library Functions"
.SH NAME
setjmp, longjmp, _setjmp, _longjmp \- non-local goto
.SH SYNOPSIS
.LP
.nf
\fB/usr/ucb/cc\fR [ \fIflag\fR ... ] \fIfile\fR ...
#include <setjmp.h>

\fBint\fR \fBsetjmp\fR(\fIenv\fR)
\fBjmp_buf\fR \fIenv\fR;
.fi

.LP
.nf
\fBvoid\fR \fBlongjmp\fR(\fIenv\fR, \fIval\fR)
\fBjmp_buf\fR \fIenv\fR;
\fBint\fR \fIval\fR;
.fi

.LP
.nf
\fBint\fR \fB_setjmp\fR(\fIenv\fR)
\fBjmp_buf\fR \fIenv\fR;
.fi

.LP
.nf
\fBvoid\fR \fB_longjmp\fR(\fIenv\fR, \fIval\fR)
\fBjmp_buf\fR \fIenv\fR;
\fBint\fR \fIval\fR;
.fi

.SH DESCRIPTION
.sp
.LP
The \fBsetjmp()\fR and \fBlongjmp()\fR functions are useful for dealing with
errors and interrupts encountered in a low-level subroutine of a program.
.sp
.LP
The \fBsetjmp()\fR function saves its stack environment in \fIenv\fR for later
use by \fBlongjmp()\fR. A normal call to \fBsetjmp()\fR returns zero.
\fBsetjmp()\fR also saves the register environment.  If a \fBlongjmp()\fR call
will be made, the routine which called \fBsetjmp()\fR should not return until
after the \fBlongjmp()\fR has returned control (see below).
.sp
.LP
The \fBlongjmp()\fR function restores the environment saved by the last call of
\fBsetjmp()\fR, and then returns in such a way that execution continues as if
the call of \fBsetjmp()\fR had just returned the value \fIval\fR to the
function that invoked \fBsetjmp()\fR; however, if \fIval\fR were zero,
execution would continue as if the call of \fBsetjmp()\fR had returned one.
This ensures that a ``return'' from \fBsetjmp()\fR caused by a call to
\fBlongjmp()\fR can be distinguished from a regular return from \fBsetjmp()\fR.
The calling function must not itself have returned in the interim, otherwise
\fBlongjmp()\fR will be returning control to a possibly nonexistent
environment. All memory-bound data have values as of the time \fBlongjmp()\fR
was called.  The \fBCPU\fR and floating-point data registers are restored to
the values they had at the time that \fBsetjmp()\fR was called.  But, because
the \fBregister\fR storage class is only a hint to the C compiler, variables
declared as \fBregister\fR variables may not necessarily be assigned to machine
registers, so their values are unpredictable after a \fBlongjmp()\fR. This is
especially a problem for programmers trying to write machine-independent C
routines.
.sp
.LP
The \fBsetjmp()\fR and \fBlongjmp()\fR functions save and restore the signal
mask while \fB_setjmp()\fR and \fB_longjmp()\fR manipulate only the C stack and
registers.
.sp
.LP
None of these functions save or restore any floating-point status or control
registers.
.SH EXAMPLES
.LP
\fBExample 1 \fRExamples of \fBsetjmp()\fR and \fBlongjmp()\fR.
.sp
.LP
The following example uses both \fBsetjmp()\fR and \fBlongjmp()\fR to return
the flow of control to the appropriate instruction block:

.sp
.in +2
.nf
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#include <unistd.h>
jmp_buf env; static void signal_handler();
main(\|)  {
        int returned_from_longjump, processing = 1;
        unsigned int time_interval = 4;
        if ((returned_from_longjump = setjmp(env)) != 0)
            switch (returned_from_longjump)     {
              case SIGINT:
                printf("longjumped from interrupt %d\en",SIGINT);
                break;
              case SIGALRM:
                printf("longjumped from alarm %d\en",SIGALRM);
                break;
            }
        (void) signal(SIGINT, signal_handler);
        (void) signal(SIGALRM, signal_handler);
        alarm(time_interval);
        while (processing)        {
          printf(" waiting for you to INTERRUPT (cntrl-C) ...\en");
          sleep(1);
        }  /* end while forever loop */
}

static void signal_handler(sig)
int sig; {
        switch (sig)     {
          case SIGINT:         ...   /* process for interrupt */
                               longjmp(env,sig);
                                     /* break never reached */
          case SIGALRM:        ...   /* process for alarm */
                               longjmp(env,sig);
                                     /* break never reached */
          default:             exit(sig);
        }
}
.fi
.in -2

.sp
.LP
When this example is compiled and executed, and the user sends an interrupt
signal, the output will be:

.sp
.in +2
.nf
longjumped from interrupt
.fi
.in -2

.sp
.LP
Additionally, every 4 seconds the alarm will expire, signalling this process,
and the output will be:

.sp
.in +2
.nf
longjumped from alarm 
.fi
.in -2

.SH SEE ALSO
.sp
.LP
\fBsigvec\fR(3UCB), \fBsetjmp\fR(3C), \fBsignal\fR(3C)
.SH NOTES
.sp
.LP
Use of these interfaces should be restricted to only applications written on
BSD platforms.  Use of these interfaces with any of the system libraries or in
multi-thread applications is unsupported.
.SH BUGS
.sp
.LP
The \fBsetjmp()\fR function does not save the current notion of whether the
process is executing on the signal stack.  The result is that a \fBlongjmp()\fR
to some place on the signal stack leaves the signal stack state incorrect.
.sp
.LP
On some systems \fBsetjmp()\fR also saves the register environment. Therefore,
all data that are bound to registers are restored to the values they had at the
time that \fBsetjmp()\fR was called. All memory-bound data have values as of
the time \fBlongjmp()\fR was called.  However, because the \fBregister\fR
storage class is only a hint to the C compiler, variables declared as
\fBregister\fR variables may not necessarily be assigned to machine registers,
so their values are unpredictable after a \fBlongjmp()\fR. When using compiler
options that specify automatic register allocation, the compiler will not
attempt to assign variables to registers in routines that call \fBsetjmp()\fR.
.sp
.LP
The \fBlongjmp()\fR function never causes \fBsetjmp()\fR to return 0, so
programmers should not depend on \fBlongjmp()\fR being able to cause
\fBsetjmp()\fR to return 0.