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 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 91 deletions(-) delete mode 100644 include/stc/algo/ccoro.h create mode 100644 include/stc/algo/coroutine.h (limited to 'include') 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 -- cgit v1.2.3