kit

kit
git clone https://git.ryansepassi.com/git/kit.git
Log | Files | Refs | README

coro_runtime.c (856B)


      1 #include <kit/coro.h>
      2 #include <stdint.h>
      3 
      4 struct CoroState {
      5   int phase;
      6   int saw_self;
      7 };
      8 
      9 static uintptr_t coro_body(uintptr_t value) {
     10   struct CoroState* st = (struct CoroState*)value;
     11   st->saw_self = coro_self() != 0;
     12   st->phase = 1;
     13   st = (struct CoroState*)coro_yield(0);
     14   if (coro_self() == 0) st->phase = 101;
     15   st->phase = 2;
     16   return 0;
     17 }
     18 
     19 int test_main(void) {
     20   unsigned char* stack = __builtin_alloca(1024);
     21   struct CoroState st;
     22   coro_t co;
     23 
     24   st.phase = 0;
     25   st.saw_self = 0;
     26   coro_init(&co, coro_body, stack, 1024);
     27   if (coro_status(&co) != CORO_INIT) return 1;
     28 
     29   coro_resume(&co, (uintptr_t)&st);
     30   if (coro_status(&co) != CORO_SUSPENDED) return 2;
     31   if (st.phase != 1 || !st.saw_self) return 3;
     32 
     33   coro_resume(&co, (uintptr_t)&st);
     34   if (coro_status(&co) != CORO_DEAD) return 4;
     35   if (st.phase != 2) return 5;
     36   return 42;
     37 }