summaryrefslogtreecommitdiffhomepage
path: root/misc
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-28 11:01:17 +0100
committerTyge Løvset <[email protected]>2023-02-28 11:01:17 +0100
commit158fbcd56a9684423f70e9579bdf2271f8c90b9f (patch)
tree0ee8b745dbec75d8101f34e7119a9e51b09906a3 /misc
parentd3c93c2cbb9ffe83a162d32b1021eb24ec703a9c (diff)
downloadSTC-modified-158fbcd56a9684423f70e9579bdf2271f8c90b9f.tar.gz
STC-modified-158fbcd56a9684423f70e9579bdf2271f8c90b9f.zip
Final cleanups on corotines.
Diffstat (limited to 'misc')
-rw-r--r--misc/examples/coread.c5
-rw-r--r--misc/examples/coroutines.c27
-rw-r--r--misc/examples/triples.c3
3 files changed, 17 insertions, 18 deletions
diff --git a/misc/examples/coread.c b/misc/examples/coread.c
index 4df80339..c5f85e08 100644
--- a/misc/examples/coread.c
+++ b/misc/examples/coread.c
@@ -15,12 +15,12 @@ bool file_nextline(struct file_nextline* U)
{
cco_begin(U)
U->fp = fopen(U->filename, "r");
- U->line = cstr_NULL;
+ U->line = cstr_init();
while (cstr_getline(&U->line, U->fp))
cco_yield(true);
- cco_final: // cco_final is needed to support cco_stop.
+ cco_final: // required label.
printf("finish\n");
cstr_drop(&U->line);
fclose(U->fp);
@@ -33,5 +33,4 @@ int main(void) {
while (file_nextline(&z)) {
printf("%3d %s\n", ++n, cstr_str(&z.line));
}
- printf("state %d\n", z.cco_state);
}
diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c
index 577ef8c1..2c9e6d5c 100644
--- a/misc/examples/coroutines.c
+++ b/misc/examples/coroutines.c
@@ -52,15 +52,18 @@ bool fibonacci(struct fibonacci* F) {
assert(F->count < 94);
cco_begin(F);
- F->idx = 1;
+ F->idx = 0;
F->result = 0;
F->b = 1;
- for (;; F->idx++) {
- if (F->count-- == 0) cco_return;
+ for (;;) {
+ if (F->count-- == 0)
+ cco_return;
+ if (++F->idx > 1) {
+ int64_t sum = F->result + F->b; // NB! locals only lasts until next cco_yield!
+ F->result = F->b;
+ F->b = sum;
+ }
cco_yield(true);
- int64_t sum = F->result + F->b; // NB! locals only lasts until next cco_yield!
- F->result = F->b;
- F->b = sum;
}
cco_final:
printf("final fib\n");
@@ -69,29 +72,29 @@ bool fibonacci(struct fibonacci* F) {
// Combine
-struct combine {
+struct combined {
struct prime prm;
struct fibonacci fib;
int cco_state;
};
-bool combine(struct combine* C) {
+bool combined(struct combined* C) {
cco_begin(C);
cco_yield(prime(&C->prm), &C->prm, true);
cco_yield(fibonacci(&C->fib), &C->fib, true);
// Reuse the C->prm context and extend the count:
- C->prm.count = 20;
+ C->prm.count = 8; C->prm.result += 2;
cco_yield(prime(&C->prm), &C->prm, true);
- cco_final: puts("final");
+ cco_final: puts("final comb");
cco_end(false);
}
int main(void) {
- struct combine comb = {.prm={.count=10}, .fib={14}};
+ struct combined comb = {.prm={.count=8}, .fib={14}};
if (true)
- while (combine(&comb))
+ while (combined(&comb))
printf("Prime(%d)=%lld, Fib(%d)=%lld\n",
comb.prm.idx, (long long)comb.prm.result,
comb.fib.idx, (long long)comb.fib.result);
diff --git a/misc/examples/triples.c b/misc/examples/triples.c
index 05e6fca2..0ce0eb37 100644
--- a/misc/examples/triples.c
+++ b/misc/examples/triples.c
@@ -23,8 +23,6 @@ struct tricoro {
int x, y, z;
};
-#include <stdlib.h>
-
bool triples_coro(struct tricoro* t) {
cco_begin(t);
for (t->z = 1;; ++t->z) {
@@ -38,7 +36,6 @@ bool triples_coro(struct tricoro* t) {
}
}
cco_final:
- puts("final");
cco_end(false);
}