From 25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 25 Feb 2023 12:09:11 +0100 Subject: Renamed algo/ccoro.h => algo/coroutine.h. Using cco_ as prefix. Changed ccoro_execute(c) => cco_begin(c); ... cco_end(); (was required). --- include/stc/algo/ccoro.h | 91 -------------------------------------------- include/stc/algo/coroutine.h | 91 ++++++++++++++++++++++++++++++++++++++++++++ misc/examples/coread.c | 18 ++++----- misc/examples/coroutines.c | 67 ++++++++++++++++++++++++++++++++ misc/examples/cosub.c | 65 ------------------------------- 5 files changed, 167 insertions(+), 165 deletions(-) delete mode 100644 include/stc/algo/ccoro.h create mode 100644 include/stc/algo/coroutine.h create mode 100644 misc/examples/coroutines.c delete mode 100644 misc/examples/cosub.c diff --git a/include/stc/algo/ccoro.h b/include/stc/algo/ccoro.h deleted file mode 100644 index 07f98bf4..00000000 --- a/include/stc/algo/ccoro.h +++ /dev/null @@ -1,91 +0,0 @@ -/* MIT License - * - * Copyright (c) 2023 Tyge Løvset - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef STC_CCORO_INCLUDED -#define STC_CCORO_INCLUDED -/* -#include -#include -#include - -struct iterate { - int max_x; - int max_y; - int ccoro_state; // required member - int x; - int y; -}; - -bool iterate(struct iterate* U) { - ccoro_execute(U, - for (U->x = 0; U->x < U->max_x; U->x++) - for (U->y = 0; U->y < U->max_y; U->y++) - ccoro_yield (true); - - ccoro_final: puts("final"); - ); - return false; -} - -int main(void) { - struct iterate it = {3, 3}; - int n = 0; - while (iterate(&it)) - { - printf("%d %d\n", it.x, it.y); - if (++n == 20) { iterate(ccoro_stop(&it)); break; } - } - return 0; -} -*/ -#include - -#define ccoro_execute(c, ...) \ - int* _state = &(c)->ccoro_state; \ - switch (*_state) { \ - case 0:; __VA_ARGS__ break; \ - default: assert(!"missing ccoro_finish: or illegal state"); \ - } \ - *_state = -2 - -#define ccoro_yield(ret) \ - do { \ - *_state = __LINE__; return ret; \ - case __LINE__:; \ - } while (0) - -#define ccoro_yield_call(...) c_MACRO_OVERLOAD(ccoro_yield_call, __VA_ARGS__) -#define ccoro_yield_call_2(c, subcoro) ccoro_yield_call_3(c, subcoro, ) -#define ccoro_yield_call_3(c, subcoro, ret) \ - do { \ - *_state = __LINE__; \ - c_PASTE(co, __LINE__): \ - subcoro; return ret; \ - case __LINE__:; \ - if (ccoro_alive(c)) goto c_PASTE(co, __LINE__); \ - } while (0) - -#define ccoro_final case -1 -#define ccoro_stop(c) ((c)->ccoro_state = (c)->ccoro_state > 0 ? -1 : -2, c) -#define ccoro_alive(c) ((c)->ccoro_state >= 0) - -#endif diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h new file mode 100644 index 00000000..b7ed895f --- /dev/null +++ b/include/stc/algo/coroutine.h @@ -0,0 +1,91 @@ +/* MIT License + * + * Copyright (c) 2023 Tyge Løvset + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef STC_COROUTINE_INCLUDED +#define STC_COROUTINE_INCLUDED +/* +#include +#include +#include + +struct iterate { + int max_x; + int max_y; + int cco_state; // required member + int x; + int y; +}; + +bool iterate(struct iterate* U) { + cco_begin(U); + for (U->x = 0; U->x < U->max_x; U->x++) + for (U->y = 0; U->y < U->max_y; U->y++) + cco_yield (true); + + cco_final: + puts("final"); + cco_end(U); + return false; +} + +int main(void) { + struct iterate it = {3, 3}; + int n = 0; + while (iterate(&it)) + { + printf("%d %d\n", it.x, it.y); + if (++n == 20) { iterate(cco_stop(&it)); break; } + } + return 0; +} +*/ +#include + +#define cco_begin(c) \ + int* _state = &(c)->cco_state; \ + switch (*_state) { \ + case 0: + +#define cco_end() \ + break; \ + default: assert(!"missing cco_final: or illegal state"); \ + } *_state = -2 + +#define cco_yield(retval) \ + do { \ + *_state = __LINE__; return retval; \ + case __LINE__:; \ + } while (0) + +#define cco_yield_coroutine(c, corocall, retval) \ + do { \ + *_state = __LINE__; cco_reset(c); \ + c_PASTE(cco, __LINE__): corocall; return retval; \ + case __LINE__:; if (cco_alive(c)) goto c_PASTE(cco, __LINE__); \ + } while (0) + +#define cco_final case -1 +#define cco_stop(c) ((c)->cco_state = (c)->cco_state > 0 ? -1 : -2, c) +#define cco_alive(c) ((c)->cco_state >= 0) +#define cco_reset(c) ((c)->cco_state = 0) + +#endif diff --git a/misc/examples/coread.c b/misc/examples/coread.c index a3eac7f9..6b0bfb37 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -1,30 +1,30 @@ #include -#include +#include #include // Read file line by line using coroutines: struct file_nextline { const char* filename; - int ccoro_state; + int cco_state; FILE* fp; cstr line; }; cstr file_nextline(struct file_nextline* U) { - ccoro_execute(U, + cco_begin(U) U->fp = fopen(U->filename, "r"); U->line = cstr_NULL; while (cstr_getline(&U->line, U->fp)) - ccoro_yield (cstr_clone(U->line)); + cco_yield(cstr_clone(U->line)); - ccoro_final: // ccoro_final is needed to support ccoro_stop. + cco_final: // cco_final is needed to support cco_stop. printf("finish\n"); cstr_drop(&U->line); fclose(U->fp); - ); + cco_end(); return cstr_NULL; } @@ -33,11 +33,11 @@ int main(void) { struct file_nextline z = {__FILE__}; int n = 0; do { - c_with (cstr line = file_nextline(&z), ccoro_alive(&z), cstr_drop(&line)) { + c_with (cstr line = file_nextline(&z), cco_alive(&z), cstr_drop(&line)) { printf("%3d %s\n", ++n, cstr_str(&line)); // stop after 15 lines: - if (n == 15) file_nextline(ccoro_stop(&z)); + if (n == 15) file_nextline(cco_stop(&z)); } - } while (ccoro_alive(&z)); + } while (cco_alive(&z)); } diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c new file mode 100644 index 00000000..d3295102 --- /dev/null +++ b/misc/examples/coroutines.c @@ -0,0 +1,67 @@ +#include +#include +#include + +// Use coroutine to create a fibonacci sequence generator: + +typedef long long intll_t; + +struct fibonacci { + int n; + int cco_state; + intll_t a, b, idx; +}; + +intll_t fibonacci(struct fibonacci* F) { + assert (F->n < 95); + + cco_begin(F); + F->a = 0; + F->b = 1; + for (F->idx = 0; F->idx < F->n; F->idx++) { + cco_yield(F->a); + intll_t sum = F->a + F->b; // NB! locals only lasts until next cco_yield! + F->a = F->b; + F->b = sum; + } + cco_final: + cco_end(); + + return -1; +} + +// Demonstrate to call another coroutine from a coroutine: +// Create a 2D iterator, and call fibonacci sequence when x,y = 1,1: + +struct iterate { + int max_x, max_y; + int cco_state; + int x, y; +}; + +bool iterate(struct iterate* I, struct fibonacci* F) { + 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++) { + if (I->x == 1 && I->y == 1) + cco_yield_coroutine(F, fibonacci(F), true); + else + cco_yield(true); + } + } + cco_final: + cco_end(); + return false; +} + + +int main(void) { + printf("Fibonacci numbers:\n"); + struct fibonacci fib = {.n = 14}; + struct iterate iter = {3, 3}; + + while (iterate(&iter, &fib)) + printf("%d %d. Fib: %lld\n", iter.x, iter.y, fib.a); + + puts(""); +} diff --git a/misc/examples/cosub.c b/misc/examples/cosub.c deleted file mode 100644 index 87dcb9a1..00000000 --- a/misc/examples/cosub.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include - -// Use coroutine to create a fibonacci sequence generator: - -typedef long long intll_t; - -struct fibonacci { - int n; - int ccoro_state; - intll_t a, b, idx; -}; - -intll_t fibonacci(struct fibonacci* F) { - assert (F->n < 95); - - ccoro_execute(F, - F->a = 0; - F->b = 1; - for (F->idx = 0; F->idx < F->n; F->idx++) { - ccoro_yield (F->a); - intll_t sum = F->a + F->b; // NB! locals only lasts until next ccoro_yield! - F->a = F->b; - F->b = sum; - } - ccoro_final: - ); - - return -1; -} - -// Demonstrate to call another coroutine from a coroutine: -// Create a 2D iterator, and call fibonacci sequence when x,y = 1,1: - -struct iterate { - int max_x, max_y; - int ccoro_state; - int x, y; -}; - -bool iterate(struct iterate* I, struct fibonacci* F) { - ccoro_execute(I, - for (I->x = 0; I->x < I->max_x; I->x++) - for (I->y = 0; I->y < I->max_y; I->y++) - if (I->x == 1 && I->y == 1) - ccoro_yield_call (F, fibonacci(F), true); - else - ccoro_yield (true); - ccoro_final: - ); - return false; -} - - -int main(void) { - printf("Fibonacci numbers:\n"); - struct fibonacci fib = {.n = 14}; - struct iterate iter = {3, 3}; - - while (iterate(&iter, &fib)) - printf("%d %d. Fib: %lld\n", iter.x, iter.y, fib.a); - - puts(""); -} -- cgit v1.2.3