GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

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. A typical use of setjmp / longjmp is implementation of an exception mechanism that exploits the ability of longjmp to reestablish program or thread state, even across multiple levels of function calls. A less common use of setjmp is to create syntax similar to coroutines. Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

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. A typical use of setjmp / longjmp is implementation of an exception mechanism that exploits the ability of longjmp to reestablish program or thread state, even across multiple levels of function calls. A less common use of setjmp is to create syntax similar to coroutines. int setjmp(jmp_buf env) : Sets up the local jmp_buf buffer and initializes it for the jump. This routine saves the program's calling environment in the environment buffer specified by the env argument for later use by longjmp . If the return is from a direct invocation, setjmp returns 0. If the return is from a call to longjmp , setjmp returns a nonzero value. void longjmp(jmp_buf env, int value) : Restores the context of the environment buffer env that was saved by invocation of the setjmp routine in the same invocation of the program. Invoking longjmp from a nested signal handler is undefined. The value specified by value is passed from longjmp to setjmp . After longjmp is completed, program execution continues as if the corresponding invocation of setjmp had just returned. If the value passed to longjmp is 0, setjmp will behave as if it had returned 1; otherwise, it will behave as if it had returned value . 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. The (apparent) return value from setjmp indicates whether control reached that point normally (zero) or from a call to longjmp (nonzero). This leads to a common idiom: if (setjmp(x)) { /* handle longjmp(x) */ } . POSIX.1 does not specify whether setjmp and longjmp save and restore the current set of blocked signals; if a program employs signal handling it should use POSIX's sigsetjmp / siglongjmp . include include static jmp_buf buf; void second() { printf("second\n"); // prints longjmp(buf, 1); // jumps back to where setjmp was called - making setjmp now return 1 } void first() { second(); printf("first\n"); // does not print } int main() { if (!setjmp(buf)) { first(); // when executed, setjmp returned 0 } else { // when longjmp jumps back, setjmp returns 1 printf("main\n"); // prints } return 0; } When executed, the above program will output: second main Notice that although the first() subroutine gets called, " first " is never printed, as second() never returns control to first() . Instead, " main " is printed when the conditional statement if (!setjmp(buf)) is checked a second time. include include include include static void first(); static void second(); /* Use a file scoped static variable for the exception stack so we can access * it anywhere within this translation unit. */ static jmp_buf exception_env; static int exception_type; int main(void) { volatile char* mem_buffer = NULL; if (setjmp(exception_env)) { // if we get here there was an exception printf("first failed, exception type: %d\n", exception_type); } else { // Run code that may signal failure via longjmp. puts("calling first"); first(); mem_buffer = (char*)malloc(300); // allocate a resource printf("%s\n", strcpy(mem_buffer, "first succeeded")); // not reached } free(mem_buffer); // NULL can be passed to free, no operation is performed return 0; } static void first() { jmp_buf my_env; puts("entering first"); // reached memcpy(my_env, exception_env, sizeof my_env); // store value of exception_env in my_env since exception_env will be reused switch (setjmp(exception_env)) { case 3: // if we get here there was an exception. printf("second failed, exception type: 3; remapping to type 1"); exception_type = 1; default: // fall through memcpy(exception_env, my_env, sizeof exception_env); // restore exception stack longjmp(exception_env, exception_type); // continue handling the exception case 0: // normal, desired operation printf("calling second"); // reached second(); printf("second succeeded"); // not reached } memcpy(exception_env, my_env, sizeof exception_env); // restore exception stack puts("leaving first"); // never reached } static void second() { printf("entering second" ); // reached exception_type = 3; longjmp(exception_env, exception_type); // declare that the program has failed printf("leaving second"); // not reached } This program's output is: calling first entering first calling second entering second second failed, exception type: 3; remapping to type 1 first failed, exception type: 1 include include jmp_buf mainTask, childTask; void call_with_cushion(); void child(); int main() { if (!setjmp(mainTask)) { call_with_cushion(); // child never returns, yield } // execution resumes after this "}" after first time that child yields while (true) { printf("Parent\n"); if (!setjmp(mainTask)) { longjmp(childTask, 1); // yield - note that this is undefined under C99 } } } void call_with_cushion() { char space[1000]; // Reserve enough space for main to run space[999] = 1; // Do not optimize array out of existence child(); } void child() { while (true) { printf("Child loop begin\n"); if (!setjmp(childTask)) { longjmp(mainTask, 1); // yield - invalidates childTask in C99 } printf("Child loop end\n"); if (!setjmp(childTask)) { longjmp(mainTask, 1); // yield - invalidates childTask in C99 } } /* Don't return. Instead we should set a flag to indicate that main() should stop yielding to us and then longjmp(mainTask, 1) */ }

References

  1. setjmp.h. https://en.wikipedia.org/w/index.php?oldid=1374264355 · 1795438144.7002156
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"setjmp.h". 1789072352. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1374264355

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·