From 8af055d464e0c0c7556b47708346acf3ce3ca0f7 Mon Sep 17 00:00:00 2001 From: Andrew Guschin Date: Mon, 4 Mar 2024 10:22:15 +0400 Subject: =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC=D0=B0?= =?UTF-8?q?=20=D1=81=20ucontext,=20=D0=BD=D0=B0=20=D0=BA=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=BE=D0=B9=20=D1=8F=20=D0=B2=D1=8B=D1=8F=D1=81=D0=BD?= =?UTF-8?q?=D1=8F=D0=BB=20=D1=87=D1=82=D0=BE=20=D0=BD=D0=B5=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20=D0=BD=D0=B0=20macos?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- context/main.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 context/main.c (limited to 'context/main.c') 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 +#include +#include +#include + +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; +} -- cgit v1.2.3