summaryrefslogtreecommitdiff
path: root/context/main.c
blob: f468311b120ae6f055aa25fcaf278d9af41b7dcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdbool.h>
#include <ucontext.h>
#include <stdlib.h>

int res;
ucontext_t c_main, c_worker;
char stack[1 << 12];
extern bool done;

void worker(void);

void outfunc(int i)
{
    printf("outfunc %d\n", i);
    res = i;
    swapcontext(&c_worker, &c_main);
}

int main()
{
    char *q = malloc(10);
    printf("ptr %p\n", q);
    getcontext(&c_main);
    c_worker = c_main;
    c_worker.uc_stack.ss_sp = stack;
    c_worker.uc_stack.ss_size = sizeof(stack);
    c_worker.uc_link = &c_main;
    makecontext(&c_worker, (void (*) (void)) worker, 1, q);
    swapcontext(&c_main, &c_worker);
    while (!done)
    {
        printf("Got %d\n", res);
        swapcontext(&c_main, &c_worker);
    }
    free(q);
    return 0;
}