Ruby: wasm/tests/fiber_test.c Source File

Ruby 3.4.7p58 (2025-10-08 revision 7a5688e2a27668e48f8d6ff4af5b2208b98a2f5e)

fiber_test.c
1#include "wasm/fiber.h"
2#include "wasm/asyncify.h"
3#include <stdio.h>
4#include <stdlib.h>
5#include <assert.h>
6
7static rb_wasm_fiber_context fctx_main, fctx_func1, fctx_func2;
8
9static int counter = 0;
10
11static void func1(void *arg0, void *arg1) {
12 assert(counter == 2);
13 fprintf(stderr, "func1: started\n");
14 fprintf(stderr, "func1: swapcontext(&fctx_func1, &fctx_func2)\n");
15 counter++;
16 rb_wasm_swapcontext(&fctx_func1, &fctx_func2);
17
18 fprintf(stderr, "func1: returning\n");
19}
20
21static void func2(void *arg0, void *arg1) {
22 assert(counter == 1);
23 fprintf(stderr, "func2: started\n");
24 fprintf(stderr, "func2: swapcontext(&fctx_func2, &fctx_func1)\n");
25 counter++;
26 rb_wasm_swapcontext(&fctx_func2, &fctx_func1);
27
28 assert(counter == 3);
29 fprintf(stderr, "func2: swapcontext(&fctx_func2, &fctx_func2)\n");
30 counter++;
31 rb_wasm_swapcontext(&fctx_func2, &fctx_func2);
32
33 assert(counter == 4);
34 fprintf(stderr, "func2: swapcontext(&fctx_func2, &fctx_main)\n");
35 counter++;
36 rb_wasm_swapcontext(&fctx_func2, &fctx_main);
37
38 fprintf(stderr, "func2: returning\n");
39 assert(false && "unreachable");
40}
41
42// top level function should not be inlined to stop unwinding immediately after this function returns
43 __attribute__((noinline))
44int start(int argc, char **argv) {
45 rb_wasm_init_context(&fctx_main, NULL, NULL, NULL);
46 fctx_main.is_started = true;
47
48 rb_wasm_init_context(&fctx_func1, func1, NULL, NULL);
49
50 rb_wasm_init_context(&fctx_func2, func2, NULL, NULL);
51
52 counter++;
53 fprintf(stderr, "start: swapcontext(&uctx_main, &fctx_func2)\n");
54 rb_wasm_swapcontext(&fctx_main, &fctx_func2);
55 assert(counter == 5);
56
57 fprintf(stderr, "start: exiting\n");
58 return 42;
59}
60
61int main(int argc, char **argv) {
62 extern int rb_wasm_rt_start(int (main)(int argc, char **argv), int argc, char **argv);
63 int result = rb_wasm_rt_start(start, argc, argv);
64 assert(result == 42);
65 return 0;
66}
assert.h
rb_wasm_fiber_context
Definition fiber.h:17
__attribute__
Definition vm_insnhelper.c:2285