diff options
| author | Tyge Løvset <[email protected]> | 2023-02-27 16:37:13 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-27 16:37:13 +0100 |
| commit | 400e5bd8ad2b2daef411e5530b6fda6158672829 (patch) | |
| tree | 0473b8c629dc158dc632db2815a6c7f08adc5ce0 | |
| parent | d37820ddc211aec2876950cd4f2236cc9b92c9eb (diff) | |
| download | STC-modified-400e5bd8ad2b2daef411e5530b6fda6158672829.tar.gz STC-modified-400e5bd8ad2b2daef411e5530b6fda6158672829.zip | |
Enhanced c_with and c_scope macros. Improved coroutine.h and example.
| -rw-r--r-- | include/stc/algo/coroutine.h | 22 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 10 | ||||
| -rw-r--r-- | misc/examples/coroutines.c | 73 |
3 files changed, 71 insertions, 34 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index da15aa5b..150fa9ec 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -60,13 +60,20 @@ int main(void) { */ #include <stc/ccommon.h> +enum { + cco_state_final = -1, + cco_state_done = -2, + cco_state_illegal = -3, +}; + #define cco_begin(ctx) \ int *_state = &(ctx)->cco_state; \ switch (*_state) { \ - case 0: + case 0: \ + case cco_state_done:; #define cco_end(retval) \ - *_state = 0; break; \ + *_state = cco_state_done; break; \ default: assert(!"missing cco_final: or illegal state"); \ } \ return retval @@ -77,15 +84,18 @@ int main(void) { case __LINE__:; \ } while (0) -#define cco_yield_coroutine(ctx, corocall, retval) \ +#define cco_coroutine(corocall, ctx, retval) \ do { \ *_state = __LINE__; \ c_PASTE(cco, __LINE__): corocall; return retval; \ - case __LINE__:; if (cco_alive(ctx)) goto c_PASTE(cco, __LINE__); \ + case __LINE__: if (cco_alive(ctx)) goto c_PASTE(cco, __LINE__); \ } while (0) -#define cco_final case -1 +#define cco_final case cco_state_final +#define cco_stop(ctx) (((ctx)->cco_state = cco_alive(ctx) ? \ + cco_state_final : cco_state_illegal), ctx) +#define cco_reset(ctx) ((ctx)->cco_state = 0) +#define cco_done(ctx) ((ctx)->cco_state == cco_state_done) #define cco_alive(ctx) ((ctx)->cco_state > 0) -#define cco_stop(ctx) ((ctx)->cco_state = cco_alive(ctx) ? -1 : -2), ctx) #endif diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 711a9c6d..d163b4ab 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -209,10 +209,12 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, ; it.index < it.size; ++it.ref, ++it.index) #endif #define c_with(...) c_MACRO_OVERLOAD(c_with, __VA_ARGS__) -#define c_with_2(declvar, drop) for (declvar, **_c_i = NULL; !_c_i; ++_c_i, drop) -#define c_with_3(declvar, pred, drop) for (declvar, **_c_i = NULL; !_c_i && (pred); ++_c_i, drop) -#define c_scope(init, drop) for (int _c_i = (init, 1); _c_i; --_c_i, drop) -#define c_defer(...) for (int _c_i = 1; _c_i; --_c_i, __VA_ARGS__) +#define c_with_2(declvar, drop) for (declvar, *_i, **_ip = &_i; _ip; _ip = 0, drop) +#define c_with_3(declvar, pred, drop) for (declvar, *_i, **_ip = &_i; _ip && (pred); _ip = 0, drop) +#define c_scope(...) c_MACRO_OVERLOAD(c_scope, __VA_ARGS__) +#define c_scope_2(init, drop) for (int _i = (init, 1); _i; _i = 0, drop) +#define c_scope_3(init, pred, drop) for (int _i = (init, 1); _i && (pred); _i = 0, drop) +#define c_defer(...) for (int _i = 1; _i; _i = 0, __VA_ARGS__) #define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__) #define c_auto_2(C, a) \ diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 89b428ee..0588c2ea 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -3,41 +3,59 @@ #include <stdint.h> // Demonstrate to call another coroutine from a coroutine: -// First create a 2D iterator, then call fibonacci sequence: +// First create prime generator, then call fibonacci sequence: -struct iterate { - int max_x, max_y; +bool is_prime(int64_t i) { + for (int64_t j=2; j*j <= i; ++j) + if (i % j == 0) return false; + return true; +} + +struct prime { + int count, idx; + int64_t num, result; int cco_state; - int x, y; }; -bool iterate(struct iterate* I) { - cco_begin(I); - for (I->x = 0; I->x < I->max_x; I->x++) - for (I->y = 0; I->y < I->max_y; I->y++) +bool prime(struct prime* U) { + cco_begin(U); + if (U->num < 2) U->num = 2; + if (U->num == 2) { + ++U->idx; + U->result = U->num; + cco_yield(true); + } + U->num += !(U->num & 1); + for (; U->idx < U->count; U->num += 2) + if (is_prime(U->num)) { + ++U->idx; + U->result = U->num; cco_yield(true); + } cco_final: cco_end(false); } + // Use coroutine to create a fibonacci sequence generator: struct fibonacci { - int n; + int count, idx; + int64_t result, b; int cco_state; - int64_t a, b, idx; }; int64_t fibonacci(struct fibonacci* F) { - assert (F->n < 95); + assert (F->count < 94); cco_begin(F); - F->a = 0; + F->idx = 0; + F->result = 0; F->b = 1; - for (F->idx = 0; F->idx < F->n; F->idx++) { - cco_yield(F->a); - int64_t sum = F->a + F->b; // NB! locals only lasts until next cco_yield! - F->a = F->b; + for (F->idx = 1; F->idx < F->count; F->idx++) { + cco_yield(F->result); + int64_t sum = F->result + F->b; // NB! locals only lasts until next cco_yield! + F->result = F->b; F->b = sum; } cco_final: @@ -47,24 +65,31 @@ int64_t fibonacci(struct fibonacci* F) { // Combine struct combine { - struct iterate it; + struct prime prm; struct fibonacci fib; int cco_state; }; bool combine(struct combine* C) { cco_begin(C); - cco_yield_coroutine(&C->it, iterate(&C->it), true); - cco_yield_coroutine(&C->fib, fibonacci(&C->fib), true); - // May reuse the C->it context; state has been reset to 0. - C->it.max_x = 2; C->it.max_y = 2; - cco_yield_coroutine(&C->it, iterate(&C->it), true); + cco_coroutine(prime(&C->prm), &C->prm, true); + cco_coroutine(fibonacci(&C->fib), &C->fib, true); + + // Reuse the C->prm context and extend the count: + C->prm.count = 20; + cco_coroutine(prime(&C->prm), &C->prm, true); + cco_final: puts("final"); cco_end(false); } int main(void) { - struct combine comb = {.it={3, 3}, .fib={14}}; + struct combine comb = {.prm={.count=10}, .fib={14}}; while (combine(&comb)) - printf("Iter=(%d, %d). Fib=%lld\n", comb.it.x, comb.it.y, (long long)comb.fib.a); + if (cco_done(&comb.prm)) + cco_reset(&comb.prm); + else + printf("Prime(%d)=%lld, Fib(%d)=%lld\n", + comb.prm.idx, (long long)comb.prm.result, + comb.fib.idx, (long long)comb.fib.result); } |
