diff options
| author | Tyge Løvset <[email protected]> | 2023-08-01 16:47:38 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-08-01 18:09:40 +0200 |
| commit | 94391527ef50cbee073a4b427f6fe839c010ecb1 (patch) | |
| tree | f4d48b7efd6a11d94d08c4ef2ee599601b949dbb /include/stc | |
| parent | 6354a597892e84baa6c3a99b98f2c7acaf33a99d (diff) | |
| download | STC-modified-94391527ef50cbee073a4b427f6fe839c010ecb1.tar.gz STC-modified-94391527ef50cbee073a4b427f6fe839c010ecb1.zip | |
Last minute API change for coroutines before V4.3 release:
- Renamed cco_xxxx_await() => cco_await_xxxx() (call, task, sem, timer)
- Renamed cco_xxxx_blocking() => cco_blocking_xxxx() (call, task)
- Renamed cco_task_resume() => cco_resume_task()
- Simplified cco_blocking_call()
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/coroutine.h | 44 |
1 files changed, 21 insertions, 23 deletions
diff --git a/include/stc/coroutine.h b/include/stc/coroutine.h index 4a7fd6aa..0e592bae 100644 --- a/include/stc/coroutine.h +++ b/include/stc/coroutine.h @@ -91,19 +91,17 @@ typedef enum { case __LINE__: if (!(promise)) {return ret; goto _resume;} \ } while (0) -/* cco_call_await(): assumes coroutine returns a cco_result value (int) */ -#define cco_call_await(...) c_MACRO_OVERLOAD(cco_call_await, __VA_ARGS__) -#define cco_call_await_1(corocall) cco_call_await_2(corocall, CCO_DONE) -#define cco_call_await_2(corocall, awaitbits) \ +/* cco_await_call(): assumes coroutine returns a cco_result value (int) */ +#define cco_await_call(...) c_MACRO_OVERLOAD(cco_await_call, __VA_ARGS__) +#define cco_await_call_1(corocall) cco_await_call_2(corocall, CCO_DONE) +#define cco_await_call_2(corocall, awaitbits) \ do { \ *_state = __LINE__; \ case __LINE__: { int _r = corocall; if (_r & ~(awaitbits)) {return _r; goto _resume;} } \ } while (0) -/* cco_call_blocking(): assumes coroutine returns a cco_result value (int) */ -#define cco_call_blocking(...) c_MACRO_OVERLOAD(cco_call_blocking, __VA_ARGS__) -#define cco_call_blocking_1(corocall) while ((corocall) != CCO_DONE) -#define cco_call_blocking_2(corocall, result) while ((*(result) = (corocall)) != CCO_DONE) +/* cco_blocking_call(): assumes coroutine returns a cco_result value (int) */ +#define cco_blocking_call(corocall) while ((corocall) != CCO_DONE) #define cco_cleanup \ *_state = CCO_STATE_CLEANUP; case CCO_STATE_CLEANUP @@ -165,23 +163,23 @@ typedef struct cco_runtime { #define cco_cast_task(task) \ ((cco_task *)(task) + 0*sizeof((task)->cco_func(task, (cco_runtime*)0) + ((int*)0 == &(task)->cco_state))) -#define cco_task_resume(task, rt) \ +#define cco_resume_task(task, rt) \ (task)->cco_func(task, rt) -#define cco_task_await(...) c_MACRO_OVERLOAD(cco_task_await, __VA_ARGS__) -#define cco_task_await_2(task, rt) cco_task_await_3(task, rt, CCO_DONE) -#define cco_task_await_3(task, rt, awaitbits) \ +#define cco_await_task(...) c_MACRO_OVERLOAD(cco_await_task, __VA_ARGS__) +#define cco_await_task_2(task, rt) cco_await_task_3(task, rt, CCO_DONE) +#define cco_await_task_3(task, rt, awaitbits) \ do { \ cco_runtime* _rt = rt; \ (_rt->stack[++_rt->top] = cco_cast_task(task))->cco_expect = (awaitbits); \ cco_yield_v(CCO_AWAIT); \ } while (0) -#define cco_task_blocking(...) c_MACRO_OVERLOAD(cco_task_blocking, __VA_ARGS__) -#define cco_task_blocking_1(task) cco_task_blocking_3(task, _rt, 16) -#define cco_task_blocking_3(task, rt, STACKDEPTH) \ +#define cco_blocking_task(...) c_MACRO_OVERLOAD(cco_blocking_task, __VA_ARGS__) +#define cco_blocking_task_1(task) cco_blocking_task_3(task, _rt, 16) +#define cco_blocking_task_3(task, rt, STACKDEPTH) \ for (struct { int result, top; cco_task* stack[STACKDEPTH]; } rt = {.stack={cco_cast_task(task)}}; \ - (((rt.result = cco_task_resume(rt.stack[rt.top], (cco_runtime*)&rt)) & \ + (((rt.result = cco_resume_task(rt.stack[rt.top], (cco_runtime*)&rt)) & \ ~rt.stack[rt.top]->cco_expect) || --rt.top >= 0); ) /* @@ -190,9 +188,9 @@ typedef struct cco_runtime { typedef struct { intptr_t count; } cco_sem; -#define cco_sem_await(sem) cco_sem_await_and_return(sem, CCO_AWAIT) -#define cco_sem_await_v(sem) cco_sem_await_and_return(sem, ) -#define cco_sem_await_and_return(sem, ret) \ +#define cco_await_sem(sem) cco_await_sem_and_return(sem, CCO_AWAIT) +#define cco_await_sem_v(sem) cco_await_sem_and_return(sem, ) +#define cco_await_sem_and_return(sem, ret) \ do { \ cco_await_and_return((sem)->count > 0, ret); \ --(sem)->count; \ @@ -248,10 +246,10 @@ typedef struct { intptr_t count; } cco_sem; typedef struct { double interval, start; } cco_timer; -#define cco_timer_await(tm, sec) cco_timer_await_v_3(tm, sec, CCO_AWAIT) -#define cco_timer_await_v(...) c_MACRO_OVERLOAD(cco_timer_await_v, __VA_ARGS__) -#define cco_timer_await_v_2(tm, sec) cco_timer_await_v_3(tm, sec, ) -#define cco_timer_await_v_3(tm, sec, ret) \ +#define cco_await_timer(tm, sec) cco_await_timer_v_3(tm, sec, CCO_AWAIT) +#define cco_await_timer_v(...) c_MACRO_OVERLOAD(cco_await_timer_v, __VA_ARGS__) +#define cco_await_timer_v_2(tm, sec) cco_await_timer_v_3(tm, sec, ) +#define cco_await_timer_v_3(tm, sec, ret) \ do { \ cco_timer_start(tm, sec); \ cco_await_and_return(cco_timer_expired(tm), ret); \ |
