diff options
Diffstat (limited to 'context/main.c')
| -rw-r--r-- | context/main.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/context/main.c b/context/main.c new file mode 100644 index 0000000..f468311 --- /dev/null +++ b/context/main.c @@ -0,0 +1,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; +} |