summaryrefslogtreecommitdiffhomepage
path: root/misc/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-05-24 16:21:22 +0200
committerTyge Løvset <[email protected]>2023-05-24 16:21:22 +0200
commit276b8110033aa275f58ce60d096f220ca050738c (patch)
tree76f5e2c069ecbe268c5497fafafb3b4cb7e66a51 /misc/examples
parent8a19b4d6ff098ec014244c86569a7bea2db65514 (diff)
downloadSTC-modified-276b8110033aa275f58ce60d096f220ca050738c.tar.gz
STC-modified-276b8110033aa275f58ce60d096f220ca050738c.zip
coroutine.h:
- Renamed Liigo's coroutine macro cco(x) => cco_routine(x). - Removed cco_begin(x), cco_end() macros. Replaced by cco_routine(x). - Replaced csleep_ms() with csleep_us(), using select() which is portable. - Updated all coroutine examples.
Diffstat (limited to 'misc/examples')
-rw-r--r--misc/examples/cointerleave.c11
-rw-r--r--misc/examples/coread.c3
-rw-r--r--misc/examples/coroutines.c14
-rw-r--r--misc/examples/generator.c21
-rw-r--r--misc/examples/scheduler.c10
-rw-r--r--misc/examples/triples.c5
6 files changed, 36 insertions, 28 deletions
diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c
index e11b2bf3..42bf1d32 100644
--- a/misc/examples/cointerleave.c
+++ b/misc/examples/cointerleave.c
@@ -1,6 +1,6 @@
// https://www.youtube.com/watch?v=8sEe-4tig_A
-#include <stc/calgo.h>
#include <stdio.h>
+#include <stc/calgo.h>
#define i_type IVec
#define i_val int
#include <stc/cvec.h>
@@ -13,10 +13,11 @@ struct GenValue {
static int get_value(struct GenValue* g)
{
- cco_begin(g);
+ cco_routine(g) {
for (g->it = IVec_begin(g->v); g->it.ref; IVec_next(&g->it))
cco_yield(*g->it.ref);
- cco_end(0);
+ }
+ return -1;
}
struct Generator {
@@ -27,13 +28,13 @@ struct Generator {
void interleaved(struct Generator* g)
{
- cco_begin(g);
+ cco_routine(g) {
while (!(cco_done(&g->x) & cco_done(&g->y)))
{
cco_yield_coro(&g->x, g->value = get_value(&g->x));
cco_yield_coro(&g->y, g->value = get_value(&g->y));
}
- cco_end();
+ }
}
void Use(void)
diff --git a/misc/examples/coread.c b/misc/examples/coread.c
index 1976231f..ef6cd6ee 100644
--- a/misc/examples/coread.c
+++ b/misc/examples/coread.c
@@ -1,3 +1,4 @@
+#define i_static
#include <stc/cstr.h>
#include <stc/algo/coroutine.h>
#include <errno.h>
@@ -13,7 +14,7 @@ struct file_read {
void file_read(struct file_read* g)
{
- cco(g) {
+ cco_routine(g) {
g->fp = fopen(g->filename, "r");
g->line = cstr_init();
diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c
index a7136993..040b8472 100644
--- a/misc/examples/coroutines.c
+++ b/misc/examples/coroutines.c
@@ -19,7 +19,7 @@ struct prime {
};
bool prime(struct prime* g) {
- cco_begin(g);
+ cco_routine(g) {
if (g->result < 2) g->result = 2;
if (g->result == 2) {
if (g->count-- == 0) cco_return;
@@ -37,7 +37,8 @@ bool prime(struct prime* g) {
}
cco_final:
printf("final prm\n");
- cco_end(true);
+ }
+ return true;
}
@@ -52,7 +53,7 @@ struct fibonacci {
bool fibonacci(struct fibonacci* g) {
assert(g->count < 94);
- cco_begin(g);
+ cco_routine(g) {
g->idx = 0;
g->result = 0;
g->b = 1;
@@ -69,7 +70,8 @@ bool fibonacci(struct fibonacci* g) {
}
cco_final:
printf("final fib\n");
- cco_end(true);
+ }
+ return true;
}
// Combine
@@ -82,7 +84,7 @@ struct combined {
void combined(struct combined* g) {
- cco_begin(g);
+ cco_routine(g) {
cco_await(prime(&g->prm));
cco_await(fibonacci(&g->fib));
@@ -93,7 +95,7 @@ void combined(struct combined* g) {
cco_final:
puts("final combined");
- cco_end();
+ }
}
int main(void)
diff --git a/misc/examples/generator.c b/misc/examples/generator.c
index 41dffafb..6b4b8407 100644
--- a/misc/examples/generator.c
+++ b/misc/examples/generator.c
@@ -15,13 +15,14 @@ typedef struct {
} Triple_iter;
void Triple_next(Triple_iter* it) {
- Triple_value* t = it->ref;
- cco_begin(it);
- for (t->c = 5; t->size; ++t->c) {
- for (t->a = 1; t->a < t->c; ++t->a) {
- for (t->b = t->a; t->b < t->c; ++t->b) {
- if (t->a*t->a + t->b*t->b == t->c*t->c) {
- if (it->count++ == t->size)
+ Triple_value* g = it->ref;
+ cco_routine(it)
+ {
+ for (g->c = 5; g->size; ++g->c) {
+ 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)
cco_return;
cco_yield();
}
@@ -30,11 +31,11 @@ void Triple_next(Triple_iter* it) {
}
cco_final:
it->ref = NULL;
- cco_end();
+ }
}
-Triple_iter Triple_begin(Triple* t) {
- Triple_iter it = {.ref=t};
+Triple_iter Triple_begin(Triple* g) {
+ Triple_iter it = {.ref=g};
Triple_next(&it);
return it;
}
diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c
index 14c85f56..ea1414c7 100644
--- a/misc/examples/scheduler.c
+++ b/misc/examples/scheduler.c
@@ -34,7 +34,7 @@ static bool push_task(const struct Task* task)
static bool taskA(struct Task* task)
{
- cco_begin(task);
+ cco_routine(task) {
puts("Hello, from task A");
cco_yield(push_task(task));
puts("A is back doing work");
@@ -42,18 +42,20 @@ static bool taskA(struct Task* task)
puts("A is back doing more work");
cco_yield(push_task(task));
puts("A is back doing even more work");
- cco_end(true);
+ }
+ return true;
}
static bool taskB(struct Task* task)
{
- cco_begin(task);
+ cco_routine(task) {
puts("Hello, from task B");
cco_yield(push_task(task));
puts("B is back doing work");
cco_yield(push_task(task));
puts("B is back doing more work");
- cco_end(true);
+ }
+ return true;
}
void Use(void)
diff --git a/misc/examples/triples.c b/misc/examples/triples.c
index 183b7389..06142916 100644
--- a/misc/examples/triples.c
+++ b/misc/examples/triples.c
@@ -33,7 +33,7 @@ struct triples {
};
bool triples_coro(struct triples* t) {
- cco_begin(t);
+ cco_routine(t) {
t->count = 0;
for (t->c = 5; t->size; ++t->c) {
for (t->a = 1; t->a < t->c; ++t->a) {
@@ -48,7 +48,8 @@ bool triples_coro(struct triples* t) {
}
cco_final:
puts("done");
- cco_end(true);
+ }
+ return true;
}
int main()