-
-
Save george-hopkins/33f7783aa0c9ab5943348eacdefe530b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <ucontext.h> | |
#include <stdint.h> | |
#include <string.h> | |
static ucontext_t ctx[3]; | |
static void f1 (int a1, int a2, int a3, int a4) { | |
printf("start f1: %d %d %d %d\n", a1, a2, a3, a4); | |
swapcontext(&ctx[1], &ctx[2]); | |
printf("finish f1\n"); | |
} | |
static void f2 (void) { | |
printf("start f2\n"); | |
swapcontext(&ctx[2], &ctx[1]); | |
printf("finish f2\n"); | |
} | |
int main (int argc, const char *argv[]) { | |
char st1[8192]; | |
char st2[8192]; | |
/* poison each coroutine's stack memory for debugging purposes */ | |
memset(st1, 'A', sizeof st1); | |
memset(st2, 'B', sizeof st2); | |
getcontext(&ctx[1]); | |
ctx[1].uc_stack.ss_sp = st1; | |
ctx[1].uc_stack.ss_size = sizeof st1; | |
ctx[1].uc_link = &ctx[0]; | |
makecontext(&ctx[1], f1, 4, 1, 2, 3, 4); | |
getcontext(&ctx[2]); | |
ctx[2].uc_stack.ss_sp = st2; | |
ctx[2].uc_stack.ss_size = sizeof st2; | |
ctx[2].uc_link = &ctx[1]; | |
makecontext(&ctx[2], f2, 0); | |
swapcontext(&ctx[0], &ctx[2]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment