xxxxxxxxxx
// Async/await in CIR
// First define the types/decls we must provide to the library:
typedef unsigned promise_id;
// Create a new unresolved promise
promise_id promise_new(void);
// Resolve promise
void promise_resolve(promise_id);
// Register promise completion callback
void promise_then(promise_id, void (*cb)(void *), void *);
// Now include the library
#include <cirasync.h>
// Other stuff we use but not strictly needed by cirasync
// Returns a new promise that resolves after ms
promise_id wait_ms(int ms);
// Runs the event loop forever
void engine_run(void);
static promise_id f(int ms) {
promise_id p = wait_ms(ms);
@await(p);
puts("wait1 done");
p = wait_ms(ms);
puts("wait2 done");
}
@asyncTransform(f);
int main(void) {
f(1000);
f(2000);
engine_run();
return 0;