summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--context/main.c38
-rw-r--r--context/worker.c16
2 files changed, 54 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;
+}
diff --git a/context/worker.c b/context/worker.c
new file mode 100644
index 0000000..647c6f3
--- /dev/null
+++ b/context/worker.c
@@ -0,0 +1,16 @@
+#include <stdbool.h>
+#include <stdio.h>
+
+void *outfunc(int);
+bool done = false;
+
+void worker(char *q)
+{
+ printf("ptr %p\n", q);
+ for (int i = 0; i < 20; i++)
+ {
+ outfunc(i);
+ }
+
+ done = true;
+}