diff options
| -rw-r--r-- | include/stc/algo/coroutine.h | 6 | ||||
| -rw-r--r-- | misc/examples/coread.c | 4 |
2 files changed, 8 insertions, 2 deletions
diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 2e992e55..c786eb51 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -65,6 +65,7 @@ enum { }; #define cco_initial(co) ((co)->cco_state == 0) +#define cco_active(co) ((co)->cco_state >= 0) #define cco_suspended(co) ((co)->cco_state > 0) #define cco_done(co) ((co)->cco_state == cco_state_done) @@ -172,6 +173,11 @@ static inline void ctimer_start(ctimer* tm, long msec) { tm->start = clock(); } +static inline ctimer ctimer_with(long msec) { + ctimer tm = {msec*(CLOCKS_PER_SEC/1000), clock()}; + return tm; +} + static inline void ctimer_restart(ctimer* tm) { tm->start = clock(); } diff --git a/misc/examples/coread.c b/misc/examples/coread.c index ef6cd6ee..2585fb81 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -16,6 +16,7 @@ void file_read(struct file_read* g) { cco_routine(g) { g->fp = fopen(g->filename, "r"); + if (!g->fp) cco_return; g->line = cstr_init(); cco_await(!cstr_getline(&g->line, g->fp)); @@ -23,9 +24,8 @@ void file_read(struct file_read* g) cco_final: printf("finish\n"); cstr_drop(&g->line); - fclose(g->fp); + if (g->fp) fclose(g->fp); } - return; } int main(void) |
