2018-01-30 03:53:04 +00:00
|
|
|
/*
|
|
|
|
* libucontext test program based on POSIX example program.
|
|
|
|
* Public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
2019-04-02 04:02:12 +00:00
|
|
|
#include <stdlib.h>
|
2018-01-30 03:53:04 +00:00
|
|
|
#include <string.h>
|
2020-12-06 09:32:09 +00:00
|
|
|
#include <libucontext/libucontext.h>
|
2018-01-30 03:53:04 +00:00
|
|
|
|
2023-08-30 20:33:48 +00:00
|
|
|
#define handle_error(msg) \
|
|
|
|
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
|
|
|
2020-12-06 09:32:09 +00:00
|
|
|
static libucontext_ucontext_t ctx[3];
|
2018-01-30 03:53:04 +00:00
|
|
|
|
|
|
|
|
2019-04-02 04:02:12 +00:00
|
|
|
static void check_arg(int actual, int expected) {
|
|
|
|
if (actual == expected) return;
|
|
|
|
fprintf(stderr, "argument has wrong value. got %d, expected %d.\n", actual, expected);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void f1 (int a, int b, int c, int d, int e, int f, int g, int h, int i, int j) {
|
2020-03-29 12:37:01 +00:00
|
|
|
printf("start f1\n");
|
|
|
|
|
|
|
|
printf("checking provided arguments to function f1\n");
|
2019-04-02 04:02:12 +00:00
|
|
|
check_arg(a, 1);
|
|
|
|
check_arg(b, 2);
|
|
|
|
check_arg(c, 3);
|
|
|
|
check_arg(d, 4);
|
|
|
|
check_arg(e, 5);
|
|
|
|
check_arg(f, 6);
|
|
|
|
check_arg(g, 7);
|
|
|
|
check_arg(h, 8);
|
|
|
|
check_arg(i, 9);
|
|
|
|
check_arg(j, 10);
|
2020-03-29 12:37:01 +00:00
|
|
|
printf("looks like all arguments are passed correctly\n");
|
2019-04-02 04:02:12 +00:00
|
|
|
|
2020-03-29 12:37:01 +00:00
|
|
|
printf("swap back to f2\n");
|
2023-08-30 20:33:48 +00:00
|
|
|
if (libucontext_swapcontext(&ctx[1], &ctx[2]) != 0)
|
|
|
|
handle_error("libucontext_swapcontext");
|
2018-01-30 03:53:04 +00:00
|
|
|
printf("finish f1\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void f2 (void) {
|
|
|
|
printf("start f2\n");
|
2020-03-29 12:37:01 +00:00
|
|
|
printf("swap to f1\n");
|
2023-08-30 20:33:48 +00:00
|
|
|
if (libucontext_swapcontext(&ctx[2], &ctx[1]) != 0)
|
|
|
|
handle_error("libucontext_swapcontext");
|
2020-03-29 15:10:17 +00:00
|
|
|
printf("finish f2, should swap to f1\n");
|
2018-01-30 03:53:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main (int argc, const char *argv[]) {
|
|
|
|
char st1[8192];
|
|
|
|
char st2[8192];
|
2019-04-02 04:03:21 +00:00
|
|
|
volatile int done = 0;
|
2018-01-30 03:53:04 +00:00
|
|
|
|
|
|
|
|
2018-01-31 23:23:45 +00:00
|
|
|
/* poison each coroutine's stack memory for debugging purposes */
|
2018-01-30 03:53:04 +00:00
|
|
|
memset(st1, 'A', sizeof st1);
|
|
|
|
memset(st2, 'B', sizeof st2);
|
|
|
|
|
|
|
|
|
2020-03-29 12:05:00 +00:00
|
|
|
printf("setting up context 1\n");
|
|
|
|
|
|
|
|
|
2023-08-30 20:33:48 +00:00
|
|
|
if (libucontext_getcontext(&ctx[1]) != 0)
|
|
|
|
handle_error("libucontext_getcontext");
|
2018-01-30 03:53:04 +00:00
|
|
|
ctx[1].uc_stack.ss_sp = st1;
|
|
|
|
ctx[1].uc_stack.ss_size = sizeof st1;
|
|
|
|
ctx[1].uc_link = &ctx[0];
|
2020-12-06 09:32:09 +00:00
|
|
|
libucontext_makecontext(&ctx[1], f1, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
|
2018-01-30 03:53:04 +00:00
|
|
|
|
|
|
|
|
2020-03-29 12:05:00 +00:00
|
|
|
printf("setting up context 2\n");
|
|
|
|
|
|
|
|
|
2020-12-06 09:32:09 +00:00
|
|
|
libucontext_getcontext(&ctx[2]);
|
2018-01-30 03:53:04 +00:00
|
|
|
ctx[2].uc_stack.ss_sp = st2;
|
|
|
|
ctx[2].uc_stack.ss_size = sizeof st2;
|
|
|
|
ctx[2].uc_link = &ctx[1];
|
2020-12-06 09:32:09 +00:00
|
|
|
libucontext_makecontext(&ctx[2], f2, 0);
|
2018-01-30 03:53:04 +00:00
|
|
|
|
|
|
|
|
2020-03-27 12:43:09 +00:00
|
|
|
printf("doing initial swapcontext\n");
|
|
|
|
|
|
|
|
|
2023-08-30 20:33:48 +00:00
|
|
|
if (libucontext_swapcontext(&ctx[0], &ctx[2]) != 0)
|
|
|
|
handle_error("libucontext_swapcontext");
|
2019-04-02 04:03:21 +00:00
|
|
|
|
|
|
|
|
2020-03-29 12:37:01 +00:00
|
|
|
printf("returned from initial swapcontext\n");
|
|
|
|
|
|
|
|
|
2019-04-02 04:03:21 +00:00
|
|
|
/* test ability to use getcontext/setcontext without makecontext */
|
2023-08-30 20:33:48 +00:00
|
|
|
if (libucontext_getcontext(&ctx[1]) != 0)
|
|
|
|
handle_error("libucontext_getcontext");
|
2019-04-02 04:03:21 +00:00
|
|
|
printf("done = %d\n", done);
|
2023-08-30 20:33:48 +00:00
|
|
|
if (done++ == 0)
|
|
|
|
if (libucontext_setcontext(&ctx[1]) != 0)
|
|
|
|
handle_error("libucontext_setcontext");
|
2019-04-02 04:03:21 +00:00
|
|
|
if (done != 2) {
|
|
|
|
fprintf(stderr, "wrong value for done. got %d, expected 2\n", done);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2018-01-30 03:53:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|