Need help about LLVM build coroutine

Dakota dakota at gmail.com
Sat Mar 2 12:11:10 UTC 2024


I am trying to build a llvm coroutine with clang without libcxx.  
  ( ref docs
https://github.com/llvm/llvm-project/blob/main/libcxx/include/__coroutine/noop_coroutine_handle.h )



Here is my code:

```c
#include <stdio.h>

typedef struct coro {
     void *handle;
     int done;
} coro;

void coro_func(coro *c) {
     printf("Coroutine started\n");
     __builtin_coro_resume(c->handle);
     printf("Coroutine resumed\n");
     __builtin_coro_resume(c->handle);
     printf("Coroutine finished\n");
     c->done = 1;
     __builtin_coro_destroy(c->handle);
}

int main() {
     coro c;
     c.done = 0;
     c.handle = __builtin_coro_noop();

     coro_func(&c);

     printf("in main is_done=%d\n", __builtin_coro_done(c.handle));

     if (!__builtin_coro_done(c.handle)) {
         __builtin_coro_resume(c.handle);
     }

     if (c.done) {
         printf("Coroutine has finished\n");
     } else {
         printf("Coroutine has not finished\n");
         __builtin_coro_destroy(c.handle);
     }

     return 0;
}

```

to build it with `clang++ -std=c++20 ./coro.cpp`


I am doing this because I try create a `extern(C)` stackless 
coroutine solution for D.


My example is so wrong, I am not sure how to fix it.


I need a example from main coroutine switch into new create 
coroutine, and from new create coroutine switch into main 
coroutine.


Any tips how to fix this simple code ?



More information about the digitalmars-d-ldc mailing list