summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-05-26 19:36:27 +0200
committerTyge Løvset <[email protected]>2023-05-26 19:36:27 +0200
commit8497b5497ecba2c2f1d368c9161ec52d4f03ae30 (patch)
tree86bd77ae8b1911a5db07b624089ba62b53dfa7cb
parent3f840ffe1c9d2df998e1cbcf6c87ea72fe23f97e (diff)
downloadSTC-modified-8497b5497ecba2c2f1d368c9161ec52d4f03ae30.tar.gz
STC-modified-8497b5497ecba2c2f1d368c9161ec52d4f03ae30.zip
Minor addition to coroutine API.
-rw-r--r--include/stc/algo/coroutine.h6
-rw-r--r--misc/examples/coread.c4
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)