summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-08-08 12:28:15 +0200
committerTyge Løvset <[email protected]>2023-08-08 15:57:25 +0200
commitc27c266b6c4ae0e5e535b18c3790ee97416412b9 (patch)
tree0b73c9cf644486abbc9213f98c1c308b7511c7dc /misc/examples
parent9e13d34c82abfeeadcc8697331f9fd3e5e7f2bca (diff)
downloadSTC-modified-c27c266b6c4ae0e5e535b18c3790ee97416412b9.tar.gz
STC-modified-c27c266b6c4ae0e5e535b18c3790ee97416412b9.zip
Reverted cco_cleanup => cco_final. (cco_cleanup deprecated).
Updated generator.c example. Misc internal refactoring.
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/coroutines/coread.c2
-rw-r--r--misc/examples/coroutines/coroutines.c6
-rw-r--r--misc/examples/coroutines/cotasks1.c4
-rw-r--r--misc/examples/coroutines/cotasks2.c4
-rw-r--r--misc/examples/coroutines/dining_philosophers.c4
-rw-r--r--misc/examples/coroutines/filetask.c4
-rw-r--r--misc/examples/coroutines/generator.c28
-rw-r--r--misc/examples/coroutines/triples.c2
8 files changed, 33 insertions, 21 deletions
diff --git a/misc/examples/coroutines/coread.c b/misc/examples/coroutines/coread.c
index 359ca85d..6d3acdd7 100644
--- a/misc/examples/coroutines/coread.c
+++ b/misc/examples/coroutines/coread.c
@@ -21,7 +21,7 @@ int file_read(struct file_read* g)
cco_await(!cstr_getline(&g->line, g->fp));
- cco_cleanup:
+ cco_final:
printf("finish\n");
cstr_drop(&g->line);
if (g->fp) fclose(g->fp);
diff --git a/misc/examples/coroutines/coroutines.c b/misc/examples/coroutines/coroutines.c
index faeb71f6..802a976a 100644
--- a/misc/examples/coroutines/coroutines.c
+++ b/misc/examples/coroutines/coroutines.c
@@ -34,7 +34,7 @@ int prime(struct prime* g) {
cco_yield();
}
}
- cco_cleanup:
+ cco_final:
printf("final prm\n");
}
return 0;
@@ -68,7 +68,7 @@ int fibonacci(struct fibonacci* g) {
}
cco_yield();
}
- cco_cleanup:
+ cco_final:
printf("final fib\n");
}
return 0;
@@ -92,7 +92,7 @@ int combined(struct combined* g) {
cco_reset(&g->prm);
cco_await_call(prime(&g->prm));
- cco_cleanup:
+ cco_final:
puts("final combined");
}
return 0;
diff --git a/misc/examples/coroutines/cotasks1.c b/misc/examples/coroutines/cotasks1.c
index 230bd62b..7df4eb34 100644
--- a/misc/examples/coroutines/cotasks1.c
+++ b/misc/examples/coroutines/cotasks1.c
@@ -52,7 +52,7 @@ int produce_items(struct produce_items* p)
printf("produced %s\n", cstr_str(&p->str));
cco_yield();
}
- cco_cleanup:
+ cco_final:
cstr_drop(&p->str);
puts("done produce");
}
@@ -76,7 +76,7 @@ int consume_items(struct consume_items* c, struct produce_items* p)
print_time();
printf("consumed %s\n", cstr_str(&p->str));
}
- cco_cleanup:
+ cco_final:
puts("done consume");
}
return 0;
diff --git a/misc/examples/coroutines/cotasks2.c b/misc/examples/coroutines/cotasks2.c
index d77a28bc..f6257a7e 100644
--- a/misc/examples/coroutines/cotasks2.c
+++ b/misc/examples/coroutines/cotasks2.c
@@ -53,7 +53,7 @@ int produce_items(struct produce_items* p, cco_runtime* rt)
cco_yield();
}
- cco_cleanup:
+ cco_final:
cstr_drop(&p->str);
puts("done produce");
}
@@ -80,7 +80,7 @@ int consume_items(struct consume_items* c, cco_runtime* rt)
printf("consumed %s\n", cstr_str(&c->produce.str));
}
- cco_cleanup:
+ cco_final:
cco_stop(&c->produce);
cco_resume_task(&c->produce, rt);
puts("done consume");
diff --git a/misc/examples/coroutines/dining_philosophers.c b/misc/examples/coroutines/dining_philosophers.c
index e917c303..d353b3b9 100644
--- a/misc/examples/coroutines/dining_philosophers.c
+++ b/misc/examples/coroutines/dining_philosophers.c
@@ -48,7 +48,7 @@ int philosopher(struct Philosopher* p)
cco_sem_release(p->right_fork);
}
- cco_cleanup:
+ cco_final:
printf("Philosopher %d finished\n", p->id);
}
return 0;
@@ -76,7 +76,7 @@ int dining(struct Dining* d)
cco_yield(); // suspend, return control back to main
}
- cco_cleanup:
+ cco_final:
for (int i = 0; i < num_philosophers; ++i) {
cco_stop(&d->ph[i]);
philosopher(&d->ph[i]);
diff --git a/misc/examples/coroutines/filetask.c b/misc/examples/coroutines/filetask.c
index 0607442d..74388359 100644
--- a/misc/examples/coroutines/filetask.c
+++ b/misc/examples/coroutines/filetask.c
@@ -28,7 +28,7 @@ int file_read(struct file_read* co, cco_runtime* rt)
cco_yield();
}
- cco_cleanup:
+ cco_final:
fclose(co->fp);
cstr_drop(&co->line);
puts("done file_read");
@@ -56,7 +56,7 @@ int count_line(struct count_line* co, cco_runtime* rt)
cco_yield();
}
- cco_cleanup:
+ cco_final:
cstr_drop(&co->path);
puts("done count_line");
}
diff --git a/misc/examples/coroutines/generator.c b/misc/examples/coroutines/generator.c
index 3f51ce9c..96498498 100644
--- a/misc/examples/coroutines/generator.c
+++ b/misc/examples/coroutines/generator.c
@@ -2,12 +2,15 @@
#include <stdio.h>
#include <stc/coroutine.h>
+#include <stc/algorithm.h>
typedef struct {
- int size;
+ int max_triples;
int a, b, c;
} Triple;
+// Create an iterable generator over Triple with count items.
+// Requires coroutine Triple_next() and function Triple_begin() to be defined.
cco_iter_struct(Triple,
int count;
);
@@ -20,16 +23,15 @@ int Triple_next(Triple_iter* it) {
for (g->a = 1; g->a < g->c; ++g->a) {
for (g->b = g->a; g->b < g->c; ++g->b) {
if (g->a*g->a + g->b*g->b == g->c*g->c) {
- if (it->count++ == g->size)
+ if (it->count++ == g->max_triples)
cco_return;
cco_yield();
}
}
}
}
- cco_cleanup:
- it->ref = NULL;
- puts("done");
+ cco_final:
+ it->ref = NULL; // stop the iterator
}
return 0;
}
@@ -43,12 +45,22 @@ Triple_iter Triple_begin(Triple* g) {
int main(void)
{
- puts("Pythagorean triples; stops at 100 triples or c >= 100:");
- Triple triple = {.size=100};
+ puts("Pythagorean triples.\nGet max 200 triples with c < 50:");
+ Triple triple = {.max_triples=200};
+
c_foreach (i, Triple, triple) {
- if (i.ref->c < 100)
+ if (i.ref->c < 50)
printf("%u: (%d, %d, %d)\n", i.count, i.ref->a, i.ref->b, i.ref->c);
else
cco_stop(&i);
}
+
+ puts("\nGet the 10 first triples with odd a's and a <= 20:");
+ c_forfilter (i, Triple, triple,
+ i.ref->a <= 20 &&
+ (i.ref->a & 1) &&
+ c_flt_take(i, 10)
+ ){
+ printf("%d: (%d, %d, %d)\n", c_flt_getcount(i), i.ref->a, i.ref->b, i.ref->c);
+ }
}
diff --git a/misc/examples/coroutines/triples.c b/misc/examples/coroutines/triples.c
index 22914c2b..d6ce2791 100644
--- a/misc/examples/coroutines/triples.c
+++ b/misc/examples/coroutines/triples.c
@@ -40,7 +40,7 @@ int triples_coro(struct triples* t) {
}
}
}
- cco_cleanup:
+ cco_final:
puts("done");
}
return 0;