FreeBSD: add file descriptor tracking for _umtx_op
[valgrind.git] / none / tests / freebsd / swapcontext.c
blob41964e142502a576da6e386a8b17812ed7a6ba12
1 // obtained from https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.bpxbd00/rswctx.htm
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <ucontext.h>
6 #include <errno.h>
8 #define STACK_SIZE 2097152+16384 /* large enough value for AMODE 64 */
10 ucontext_t fcontext,mcontext;
11 int x = 0;
13 void func(int arg)
15 printf("function called with value %d\n",arg);
16 x++;
17 printf("function returning to main\n");
18 setcontext(&mcontext);
21 int main(void)
23 int value = 1;
25 getcontext(&fcontext);
26 if ((fcontext.uc_stack.ss_sp = (char *) malloc(STACK_SIZE)) != NULL)
28 fcontext.uc_stack.ss_size = STACK_SIZE;
29 fcontext.uc_stack.ss_flags = 0;
30 fcontext.uc_sigmask.__bits[3] = -1;
31 errno = 0;
32 makecontext(&fcontext,(void (*)(void))func,1,value);
33 if (errno != 0)
35 perror("Error reported by makecontext()");
36 return -1; /* Error occurred exit */
39 else
41 perror("not enough storage for stack");
42 abort();
45 printf("context has been built\n");
46 swapcontext(&mcontext,&fcontext);
48 if (!x)
50 perror("incorrect return from swapcontext");
51 abort();
53 else
55 printf("returned from function\n");
57 free(fcontext.uc_stack.ss_sp);