diff options
| author | Tyge Løvset <[email protected]> | 2023-06-20 18:26:35 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-06-20 18:26:35 +0200 |
| commit | d1a1ed75ee08ed1100748bdbdc6fb1c3136c4c6b (patch) | |
| tree | 659df2ea2f08feb6f6c9baa071890a74f441a825 | |
| parent | 3fed66750bed9c82a8bb12fd86b963d2f5ec0eb5 (diff) | |
| download | STC-modified-d1a1ed75ee08ed1100748bdbdc6fb1c3136c4c6b.tar.gz STC-modified-d1a1ed75ee08ed1100748bdbdc6fb1c3136c4c6b.zip | |
Added some more to coroutine.h
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | include/stc/algo/coroutine.h | 14 | ||||
| -rw-r--r-- | misc/examples/scheduler.c | 4 |
3 files changed, 16 insertions, 6 deletions
@@ -3,7 +3,7 @@ STC - Smart Template Containers =============================== -### [Version 4.3 RC](#version-history) +### [Version 4.3 RC2](#version-history) --- Description @@ -613,6 +613,8 @@ STC is generally very memory efficient. Memory usage for the different container # Version History ## Version 4.3 +- algo/coroutine.h much improved with new API and more features. +- New cdeq and cqueue implementation(s), using circular buffer. - Removed deprecated uppercase flow-control macro names. - Removed deprecated crandom.h. Use crand.h with new API. - Improved default string hash function. diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index e0952e1f..b92507b8 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -86,12 +86,20 @@ enum { #define cco_closure(Ret, Closure, ...) \ struct Closure { \ - Ret (*coroutine)(struct Closure*); \ - __VA_ARGS__ \ + Ret (*cco_fn)(struct Closure*); \ int cco_state; \ + __VA_ARGS__ \ } -#define cco_resume(closure) (closure)->coroutine(closure) +typedef struct cco_base { + void (*cco_fn)(struct cco_base*); + int cco_state; +} cco_base; + +#define cco_cast(closure) \ + ((cco_base *)(closure) + 0*sizeof((cco_resume(closure), (int*)0 == &(closure)->cco_state))) + +#define cco_resume(closure) (closure)->cco_fn(closure) #define cco_await_on(...) c_MACRO_OVERLOAD(cco_await_on, __VA_ARGS__) #define cco_await_on_1(closure) cco_await_2((cco_resume(closure), cco_done(closure)), ) #define cco_await_on_2(co, func) cco_await_2((func(co), cco_done(co)), ) diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index 54fefc47..59101b4e 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -58,8 +58,8 @@ static bool taskB(struct Task* task) void Use(void) { Scheduler scheduler = c_init(Scheduler, { - {taskA, &scheduler}, - {taskB, &scheduler}, + {.cco_fn=taskA, .sched=&scheduler}, + {.cco_fn=taskB, .sched=&scheduler}, }); while (schedule(&scheduler)) {} |
