blob: eb5a5bd3f8f2a71e514f69cd86de8f599f64851a [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH SETJMP 3
2.SH NAME
3setjmp, longjmp, notejmp \- non-local goto
4.SH SYNOPSIS
5.B #include <u.h>
6.br
7.B #include <libc.h>
8.PP
9.ta \w'\fLvoid 'u
10.B
11int setjmp(jmp_buf env)
12.PP
13.B
14void longjmp(jmp_buf env, int val)
15.PP
16.B
17void notejmp(void *uregs, jmp_buf env, int val)
18.SH DESCRIPTION
19These routines are useful for dealing with errors
20and interrupts encountered in
21a low-level subroutine of a program.
22.PP
23.I Setjmp
24saves its stack environment in
25.I env
26for later use by
27.IR longjmp .
28It returns value 0.
29.PP
30.I Longjmp
31restores the environment saved by the last call of
32.IR setjmp .
33It then causes execution to
34continue as if the call of
35.I setjmp
36had just returned with value
37.IR val .
38The invoker of
39.I setjmp
40must not itself have returned in the interim.
41All accessible data have values as of the time
42.I longjmp
43was called.
44.PP
45.I Notejmp
46is the same as
47.I longjmp
48except that it is to be called from within a note handler (see
rscbf8a59f2004-04-11 03:42:27 +000049.IR notify (3)).
rsccfa37a72004-04-10 18:53:55 +000050The
51.I uregs
52argument should be the first argument passed to the note handler.
53.PP
54.I Setjmp
55and
56.I longjmp
57can also be used to switch stacks.
58Defined in
59.B </$objtype/u.h>
60are several macros that can be used to build
61.B jmp_bufs
62by hand. The following code establishes a
63.B jmp_buf
64.i label
65that may be called by
66.I longjmp
67to begin execution in a function
68.BR f
69with 1024 bytes of stack:
70.IP
71.EX
72#include <u.h>
73#include <libc.h>
74
75jmp_buf label;
76#define NSTACK 1024
77char stack[NSTACK];
78
79void
80setlabel(void)
81{
82 label[JMPBUFPC] = ((ulong)f+JMPBUFDPC);
83 /* -2 leaves room for old pc and new pc in frame */
84 label[JMPBUFSP =
85 (ulong)(&stack[NSTACK-2*sizeof(ulong*)]);
86}
87.EE
88.SH SOURCE
89.B /sys/src/libc/$objtype/setjmp.s
90.br
91.B /sys/src/libc/$objtype/notejmp.c
92.SH SEE ALSO
rscbf8a59f2004-04-11 03:42:27 +000093.IR notify (3)
rsccfa37a72004-04-10 18:53:55 +000094.SH BUGS
95.PP
96.I Notejmp
97cannot recover from an address trap or bus error (page fault) on the 680x0
98architectures.