1#ifndef COROUTINE_ASYNCIFY_CONTEXT_H
2#define COROUTINE_ASYNCIFY_CONTEXT_H
17#include "wasm/asyncify.h"
18#include "wasm/machine.h"
19#include "wasm/fiber.h"
21#define COROUTINE void __attribute__((__noreturn__))
23static const int ASYNCIFY_CORO_DEBUG = 0;
38COROUTINE coroutine_trampoline(
void * _start,
void * _context);
42 if (ASYNCIFY_CORO_DEBUG) fprintf(stderr,
"[%s] entry (context = %p)\n", __func__, context);
44 rb_wasm_init_context(&context->fc, NULL, NULL, NULL);
46 context->fc.is_started =
true;
49static inline void coroutine_initialize(
struct coroutine_context *context, coroutine_start start,
void *stack,
size_t size)
53 uintptr_t sp = ((uintptr_t)stack + size) & ~0xF;
54 if (ASYNCIFY_CORO_DEBUG) fprintf(stderr,
"[%s] entry (context = %p, stack = %p ... %p)\n", __func__, context, stack, (
char *)sp);
55 rb_wasm_init_context(&context->fc, coroutine_trampoline, start, context);
57 context->current_sp = (
char *)sp;
58 context->stack_base = stack;
64 if (ASYNCIFY_CORO_DEBUG) fprintf(stderr,
"[%s] entry (current = %p, target = %p)\n", __func__, current, target);
67 target->from = current;
68 if (ASYNCIFY_CORO_DEBUG) fprintf(stderr,
"[%s] current->current_sp = %p -> %p\n", __func__, current->current_sp, rb_wasm_get_stack_pointer());
70 current->current_sp = rb_wasm_get_stack_pointer();
74 rb_wasm_swapcontext(¤t->fc, &target->fc);
78 rb_wasm_set_stack_pointer(current->current_sp);
80 target->from = previous;
87 if (ASYNCIFY_CORO_DEBUG) fprintf(stderr,
"[%s] entry (context = %p)\n", __func__, context);
88 context->stack_base = NULL;