Skip to content

Commit

Permalink
Reverted from cco_await_with(promise, ret) to cco_await(promise, ret).
Browse files Browse the repository at this point in the history
  • Loading branch information
tylov committed May 3, 2023
1 parent e4efe2f commit 6b23e35
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
15 changes: 9 additions & 6 deletions include/stc/algo/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ int main(void) {
*/
#include <stc/ccommon.h>

enum cco_states {
enum {
cco_state_final = -1,
cco_state_done = -2,
};

#define cco_initial(ctx) ((ctx)->cco_state == 0)
#define cco_suspended(ctx) ((ctx)->cco_state > 0)
#define cco_done(ctx) ((ctx)->cco_state == cco_state_done)

Expand All @@ -82,8 +83,9 @@ enum cco_states {
case __LINE__:; \
} while (0)

#define cco_await(promise) cco_await_with(promise, )
#define cco_await_with(promise, retval) \
#define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__)
#define cco_await_1(promise) cco_await_2(promise, )
#define cco_await_2(promise, retval) \
do { \
*_state = __LINE__; \
case __LINE__: if (!(promise)) return retval; \
Expand Down Expand Up @@ -119,10 +121,11 @@ typedef struct {
* This macro carries out the "wait" operation on the semaphore,
* and causes the "thread" to block while the counter is zero.
*/
#define cco_await_sem(sem) cco_await_sem_with(sem, )
#define cco_await_sem_with(sem, retval) \
#define cco_await_sem(...) c_MACRO_OVERLOAD(cco_await_sem, __VA_ARGS__)
#define cco_await_sem_1(sem) cco_await_sem_2(sem, )
#define cco_await_sem_2(sem, retval) \
do { \
cco_await_with((sem)->count > 0, retval); \
cco_await_2((sem)->count > 0, retval); \
--(sem)->count; \
} while (0)

Expand Down
14 changes: 7 additions & 7 deletions misc/examples/coread.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ struct file_read {
cstr line;
};

bool file_read(struct file_read* U)
bool file_read(struct file_read* g)
{
cco_begin(U)
U->fp = fopen(U->filename, "r");
U->line = cstr_init();
cco_begin(g)
g->fp = fopen(g->filename, "r");
g->line = cstr_init();

while (cstr_getline(&U->line, U->fp))
while (cstr_getline(&g->line, g->fp))
cco_yield(false);

cco_final: // this label is required.
printf("finish\n");
cstr_drop(&U->line);
fclose(U->fp);
cstr_drop(&g->line);
fclose(g->fp);
cco_end(true);
}

Expand Down
26 changes: 14 additions & 12 deletions misc/examples/coroutines.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool prime(struct prime* g) {
}
}
cco_final:
printf("final prm\n");
printf("final prm\n");
cco_end(true);
}

Expand All @@ -60,14 +60,15 @@ bool fibonacci(struct fibonacci* g) {
if (g->count-- == 0)
cco_return;
if (++g->idx > 1) {
llong sum = g->result + g->b; // NB! locals lasts only until next cco_yield/cco_await!
// NB! locals lasts only until next cco_yield/cco_await!
llong sum = g->result + g->b;
g->result = g->b;
g->b = sum;
}
cco_yield(false);
}
cco_final:
printf("final fib\n");
printf("final fib\n");
cco_end(true);
}

Expand All @@ -80,17 +81,18 @@ struct combined {
};


bool combined(struct combined* C) {
cco_begin(C);
cco_await_with(prime(&C->prm), false);
cco_await_with(fibonacci(&C->fib), false);
bool combined(struct combined* g) {
cco_begin(g);
cco_await(prime(&g->prm), false);
cco_await(fibonacci(&g->fib), false);

// Reuse the C->prm context and extend the count:
C->prm.count = 8, C->prm.result += 2;
cco_reset(&C->prm);
cco_await_with(prime(&C->prm), false);
// Reuse the g->prm context and extend the count:
g->prm.count = 8, g->prm.result += 2;
cco_reset(&g->prm);
cco_await(prime(&g->prm), false);

cco_final: puts("final comb");
cco_final:
puts("final combined");
cco_end(true);
}

Expand Down

0 comments on commit 6b23e35

Please sign in to comment.