diff options
| author | Tyge Løvset <[email protected]> | 2023-02-25 12:09:11 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-25 12:09:11 +0100 |
| commit | 25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2 (patch) | |
| tree | e5af1d5d3872fe1380d6325dde6bf60a2917cc62 /include | |
| parent | 0befe31c0f7c2cc86ba872073af610b3e0d9c9fb (diff) | |
| download | STC-modified-25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2.tar.gz STC-modified-25d3a00cf37496f5be2f8a7f1aea42fc4d596ab2.zip | |
Renamed algo/ccoro.h => algo/coroutine.h. Using cco_ as prefix. Changed ccoro_execute(c) => cco_begin(c); ... cco_end(); (was required).
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/algo/coroutine.h (renamed from include/stc/algo/ccoro.h) | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/include/stc/algo/ccoro.h b/include/stc/algo/coroutine.h index 07f98bf4..b7ed895f 100644 --- a/include/stc/algo/ccoro.h +++ b/include/stc/algo/coroutine.h @@ -20,29 +20,30 @@ * 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 +#ifndef STC_COROUTINE_INCLUDED +#define STC_COROUTINE_INCLUDED /* #include <stdio.h> #include <stdbool.h> -#include <stc/algo/ccoro.h> +#include <stc/algo/coroutine.h> struct iterate { int max_x; int max_y; - int ccoro_state; // required member + int cco_state; // required member int x; int y; }; bool iterate(struct iterate* U) { - ccoro_execute(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++) - ccoro_yield (true); + cco_yield (true); - ccoro_final: puts("final"); - ); + cco_final: + puts("final"); + cco_end(U); return false; } @@ -52,40 +53,39 @@ int main(void) { while (iterate(&it)) { printf("%d %d\n", it.x, it.y); - if (++n == 20) { iterate(ccoro_stop(&it)); break; } + if (++n == 20) { iterate(cco_stop(&it)); break; } } return 0; } */ #include <stc/ccommon.h> -#define ccoro_execute(c, ...) \ - int* _state = &(c)->ccoro_state; \ +#define cco_begin(c) \ + int* _state = &(c)->cco_state; \ switch (*_state) { \ - case 0:; __VA_ARGS__ break; \ - default: assert(!"missing ccoro_finish: or illegal state"); \ - } \ - *_state = -2 + case 0: -#define ccoro_yield(ret) \ +#define cco_end() \ + break; \ + default: assert(!"missing cco_final: or illegal state"); \ + } *_state = -2 + +#define cco_yield(retval) \ do { \ - *_state = __LINE__; return ret; \ + *_state = __LINE__; return retval; \ 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) \ +#define cco_yield_coroutine(c, corocall, retval) \ do { \ - *_state = __LINE__; \ - c_PASTE(co, __LINE__): \ - subcoro; return ret; \ - case __LINE__:; \ - if (ccoro_alive(c)) goto c_PASTE(co, __LINE__); \ + *_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 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) +#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 |
