1 # This is a coroutine library that uses libaco (https://libaco.org)
3 # Lua programmers will recognize this as similar to Lua's stackful coroutines.
5 # Async/Await programmers will weep at its beauty and gnash their teeth and
6 # rend their garments in despair at what they could have had.
14 co := Coroutine(func()
15 say("I'm in the coroutine!")
17 say("I'm back in the coroutine!")
20 say("I'm in the main func")
22 say("I'm back in the main func")
24 say("I'm back in the main func again")
27 _main_co : @Memory? = none
28 _shared_stack : @Memory? = none
30 struct Coroutine(co:@Memory)
31 convert(fn:func() -> Coroutine)
36 shared_stack := _shared_stack
37 aco_ptr := C_code:@Memory`
38 aco_create(@main_co, @shared_stack, 0, (void*)@fn.fn, @fn.userdata)
40 return Coroutine(aco_ptr)
42 func is_finished(co:Coroutine->Bool; inline)
43 return C_code:Bool`((aco_t*)@(co.co))->is_finished`
45 func resume(co:Coroutine->Bool)
48 C_code `aco_resume(@co.co);`
53 aco_set_allocator(GC_malloc, NULL);
54 aco_thread_init(aco_exit_fn);
56 _main_co = C_code:@Memory`aco_create(NULL, NULL, 0, NULL, NULL)`
58 _shared_stack = C_code:@Memory`aco_shared_stack_new(0)`