CIR Playground
Load example...
CIR on GitHub
Input
xxxxxxxxxx
 
1
// Async/await in CIR
2
// First define the types/decls we must provide to the library:
3
​
4
typedef unsigned promise_id;
5
​
6
// Create a new unresolved promise
7
promise_id promise_new(void);
8
​
9
// Resolve promise
10
void promise_resolve(promise_id);
11
​
12
// Register promise completion callback
13
void promise_then(promise_id, void (*cb)(void *), void *);
14
​
15
// Now include the library
16
#include <cirasync.h>
17
​
18
// Other stuff we use but not strictly needed by cirasync
19
​
20
// Returns a new promise that resolves after ms
21
promise_id wait_ms(int ms);
22
​
23
// Runs the event loop forever
24
void engine_run(void);
25
​
26
static promise_id f(int ms) {
27
    promise_id p = wait_ms(ms);
28
    @await(p);
29
    puts("wait1 done");
30
    p = wait_ms(ms);
31
    @await(p);
32
    puts("wait2 done");
33
}
34
@asyncTransform(f);
35
​
36
int main(void) {
37
    f(1000);
38
    f(2000);
39
    engine_run();
40
    return 0;
41
}
42
​
stdout
1
 
1
​
stderr
1
 
1
​
Load Example...
    00 - Compile-time fibonacci 01 - Inline Repeat 02 - Compile-time strtok 03 - Does a code call a function 04 - C Preprocessor identifiers may be accidentally shadowed 05 - CIR identifiers won't be accidentally shadowed 06 - C Preprocessor accidental capture of symbols 07 - CIR won't accidentally capture symbols 08 - bf-to-C (low level ver) 09 - bf-to-C (with quote-antiquote) 10 - async-await 11 - Embed an external file 12 - zlib