diff options
| author | Tyge Løvset <[email protected]> | 2023-05-31 17:38:49 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-05-31 17:38:49 +0200 |
| commit | c23a90112ffc50ed5977874ec31cf4fd3d4afd9b (patch) | |
| tree | 16213ecba2a5a4af8a2b0b18fb293605be702232 | |
| parent | 7dd28530c93b907cc26064232c5498e45e838723 (diff) | |
| download | STC-modified-c23a90112ffc50ed5977874ec31cf4fd3d4afd9b.tar.gz STC-modified-c23a90112ffc50ed5977874ec31cf4fd3d4afd9b.zip | |
Simplified coroutine API. Removed unneeded cco_await_coro() and cco_yield_coro().
| -rw-r--r-- | docs/ccommon_api.md | 5 | ||||
| -rw-r--r-- | include/stc/algo/coroutine.h | 11 | ||||
| -rw-r--r-- | misc/examples/cointerleave.c | 14 |
3 files changed, 9 insertions, 21 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index beaad7e9..fc4f196b 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -373,17 +373,12 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c |:----------|:-------------------------------------|:----------------------------------------| | | `cco_final:` | Label for cleanup in coroutine | | | `cco_return` | Early return from the coroutine (no arg) | -| `bool` | `cco_suspended(co)` | Is coroutine in suspended state? | | `bool` | `cco_done(co)` | Is coroutine done? | | | `cco_routine(co) { ... }` | The coroutine closure | | | `cco_yield()` | Yield/suspend execution | | | `cco_yield(ret)` | Yield/suspend execution and return ret | -| | `cco_yield_coro(co, call)` | Yield at co call if it is suspended | -| | `cco_yield_coro(co, call, ret)` | Yield at co call with ret if suspended | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | -| | `cco_await_coro(co, call)` | Await for co call to finish | -| | `cco_await_coro(co, call, ret)` | Await for co call to finish with ret | | | Semaphores: | | | | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 7f2e1244..486f6e23 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -65,8 +65,6 @@ enum { }; #define cco_initial(co) ((co)->cco_state == 0) -#define cco_active(co) ((co)->cco_state >= 0) -#define cco_suspended(co) ((co)->cco_state > 0) #define cco_done(co) ((co)->cco_state == cco_state_done) #define cco_routine(co) \ @@ -79,11 +77,6 @@ enum { case __LINE__:; \ } while (0) -#define cco_yield_coro(...) c_MACRO_OVERLOAD(cco_yield_coro, __VA_ARGS__) -#define cco_yield_coro_2(co, call) cco_yield_coro_3(co, call, ) -#define cco_yield_coro_3(co, call, ret) \ - do { call; if (!cco_done(co)) cco_yield(ret); } while (0) - #define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__) #define cco_await_1(promise) cco_await_2(promise, ) #define cco_await_2(promise, ret) \ @@ -92,10 +85,6 @@ enum { case __LINE__: if (!(promise)) {return ret; goto _begin;} \ } while (0) -#define cco_await_coro(...) c_MACRO_OVERLOAD(cco_await_coro, __VA_ARGS__) -#define cco_await_coro_2(co, call) cco_await_2((call, cco_done(co)), ) -#define cco_await_coro_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) - #define cco_run(co, call) while (call, !cco_done(co)) #define cco_final \ diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 42bf1d32..51b9f39a 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -29,11 +29,15 @@ struct Generator { void interleaved(struct Generator* g) { cco_routine(g) { - while (!(cco_done(&g->x) & cco_done(&g->y))) - { - cco_yield_coro(&g->x, g->value = get_value(&g->x)); - cco_yield_coro(&g->y, g->value = get_value(&g->y)); - } + do { + g->value = get_value(&g->x); + if (!cco_done(&g->x)) + cco_yield(); + + g->value = get_value(&g->y); + if (!cco_done(&g->y)) + cco_yield(); + } while (!(cco_done(&g->x) & cco_done(&g->y))); } } |
