Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4.2 |
both | POSIX(ON) |
#define _XOPEN_SOURCE_EXTENDED 1
#include <ucontext.h>
int swapcontext(ucontext_t *__restrict__ oucp, const ucontext_t *__restrict__ ucp);
The swapcontext() function saves the current user context in the context structure pointed to by oucp and restores the user context structure pointed to by ucp. swapcontext() is equivalent to getcontext() with the oucp argument followed by setcontext() with the ucp argument.
Control does not return from the initial invocation of swapcontext(). However, if the saved context is not modified using makecontext(), and a subsequent setcontext() or swapcontext() is issued using the saved context, swapcontext() returns with a 0 return value.
This function is supported only in a POSIX program.
mcontext_t uc_mcontext A machine-specific representation
of the saved context.
ucontext_t *uc_link Pointer to the context that will
be resumed when this context returns.
sigset_t uc_sigmask The set of signals that are blocked
when this context is active.
stack_t uc_stack The stack used by this context.
Special behavior for C++: If getcontext() and swapcontext() are used to transfer control in a z/OS® XL C++ program, the behavior in terms of the destruction of automatic objects is undefined. This applies to both z/OS XL C++ and z/OS XL C++ ILC modules. The use of getcontext() and swapcontext() in conjunction with try(), catch(), and throw() is also undefined.
Do not issue getcontext() in a C++ constructor or destructor, since the saved context would not be usable in a subsequent setcontext() or swapcontext() after the constructor or destructor returns.
If successful, swapcontext() does not return from the initial invocation. If the unmodified saved context is later restored, swapcontext() returns 0.
If unsuccessful, swapcontext() returns -1.
There are no errno values defined.
/* This example shows the usage of swapcontext(). */
#define _XOPEN_SOURCE_EXTENDED 1
#include <stdlib.h>
#include <stdio.h>
#include <ucontext.h>
#include <errno.h>
#ifdef _LP64
#define STACK_SIZE 2097152+16384 /* large enough value for AMODE 64 */
#else
#define STACK_SIZE 16384 /* AMODE 31 addressing*/
#endif
void func(int);
ucontext_t fcontext,mcontext;
int x = 0;
int main(void) {
int value = 1;
getcontext(&fcontext);
if ((fcontext.uc_stack.ss_sp = (char *) malloc(STACK_SIZE)) != NULL) {
fcontext.uc_stack.ss_size = STACK_SIZE;
fcontext.uc_stack.ss_flags = 0;
errno = 0;
makecontext(&fcontext,func,1,value);
if (errno != 0){
perror("Error reported by makecontext()");
return -1; /* Error occurred exit */
}
}
else {
perror("not enough storage for stack");
abort();
}
printf("context has been built\n");
swapcontext(&mcontext,&fcontext);
if (!x) {
perror("incorrect return from swapcontext");
abort();
}
else {
printf("returned from function\n");
}
}
void func(int arg) {
printf("function called with value %d\n",arg);
x++;
printf("function returning to main\n");
setcontext(&mcontext);
}
context has been built
function called with value 1
function returning to main
returned from function