forked from edubart/minicoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple.c
More file actions
37 lines (35 loc) · 1.31 KB
/
simple.c
File metadata and controls
37 lines (35 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define MINICORO_IMPL
#include "minicoro.h"
#include <stdio.h>
// Coroutine entry function.
void coro_entry(mco_coro* co) {
printf("coroutine 1\n");
mco_yield(co);
printf("coroutine 2\n");
}
int main() {
// First initialize a `desc` object through `mco_desc_init`.
mco_desc desc = mco_desc_init(coro_entry, 0);
// Configure `desc` fields when needed (e.g. customize user_data or allocation functions).
desc.user_data = NULL;
// Call `mco_create` with the output coroutine pointer and `desc` pointer.
mco_coro* co;
mco_result res = mco_create(&co, &desc);
assert(res == MCO_SUCCESS);
// The coroutine should be now in suspended state.
assert(mco_status(co) == MCO_SUSPENDED);
// Call `mco_resume` to start for the first time, switching to its context.
res = mco_resume(co); // Should print "coroutine 1".
assert(res == MCO_SUCCESS);
// We get back from coroutine context in suspended state (because it's unfinished).
assert(mco_status(co) == MCO_SUSPENDED);
// Call `mco_resume` to resume for a second time.
res = mco_resume(co); // Should print "coroutine 2".
assert(res == MCO_SUCCESS);
// The coroutine finished and should be now dead.
assert(mco_status(co) == MCO_DEAD);
// Call `mco_destroy` to destroy the coroutine.
res = mco_destroy(co);
assert(res == MCO_SUCCESS);
return 0;
}