setjmp.h is a header defined in the C standard library to provide “non-local jumps”: control flow that deviates from the usual subroutine call and return sequence.
The complementary functions setjmp
and longjmp
provide this functionality.
SYNOPSIS
#include <setjmp.h>
int setjmp(jmp_buf env);
int sigsetjmp(sigjmp_buf env, int savesigs);
setjmp() and longjmp() are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. setjmp() saves the stack context/environment in env for later use by longjmp(). The stack context will be invalidated if the function which called setjmp() returns.
setjmp
saves the current environment (the program state), at some point of program execution, into a platform-specific data structure (jmp_buf
) that can be used at some later point of program execution by longjmp
to restore the program state to that saved by setjmp
into jmp_buf
. This process can be imagined to be a “jump” back to the point of program execution where setjmp
saved the environment.
sigsetjmp() is similar to setjmp(). If, and only if, savesigs is nonzero, the process’s current signal mask is saved in env and will be restored if a sig?longjmp() is later performed with this env.