blob: d18353e9662c2b09cef42063e350f167f2b40231 [file] [log] [blame]
rjwalsh0140af52005-06-04 20:42:33 +00001#include <stdio.h>
2#include <stdlib.h>
3#include <ucontext.h>
4
5#include "valgrind.h"
6
7#define STACK_SIZE 4096
8
9struct ucontext ctx1, ctx2, oldc;
10int count;
11
12void hello(struct ucontext *newc)
13{
14 printf("hello, world: %d\n", count);
15 if (count++ == 2)
16 newc = &oldc;
17 setcontext(newc);
18}
19
20int init_context(struct ucontext *uc)
21{
22 void *stack;
23 int ret;
24
25 if (getcontext(uc) == -1) {
26 perror("getcontext");
27 exit(1);
28 }
29
30 if ((stack = malloc(STACK_SIZE)) == NULL) {
31 perror("malloc");
32 exit(1);
33 }
34
35 ret = VALGRIND_STACK_REGISTER(stack, stack + STACK_SIZE);
36
37 uc->uc_link = NULL;
38 uc->uc_stack.ss_sp = stack;
39 uc->uc_stack.ss_size = STACK_SIZE;
40 uc->uc_stack.ss_flags = 0;
41
42 return ret;
43}
44
45int main(int argc, char **argv)
46{
47 int c1 = init_context(&ctx1);
48 int c2 = init_context(&ctx2);
49
50 makecontext(&ctx1, (void (*)()) hello, 2, &ctx2);
51 makecontext(&ctx2, (void (*)()) hello, 2, &ctx1);
52
53 swapcontext(&oldc, &ctx1);
54
55 VALGRIND_STACK_DEREGISTER(c1);
56 free(ctx1.uc_stack.ss_sp);
57 VALGRIND_STACK_DEREGISTER(c2);
58 free(ctx2.uc_stack.ss_sp);
59
60 return 0;
61}