From 399eb8d0e1de2839d826a9e0cf123d90d00b0018 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 1 May 2023 23:11:45 +0200 Subject: Replaced cco_yield(corocall, ctx, retval) with cco_await(cond) and cco_await_while(cond). --- include/stc/algo/coroutine.h | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index b0ecd6b7..6c1d7d28 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -64,6 +64,7 @@ enum { #define cco_suspended(ctx) ((ctx)->cco_state > 0) #define cco_alive(ctx) ((ctx)->cco_state != cco_state_done) +#define cco_done(ctx) ((ctx)->cco_state == cco_state_done) #define cco_begin(ctx) \ int *_state = &(ctx)->cco_state; \ @@ -76,25 +77,20 @@ enum { } \ return retval -#define cco_yield(...) c_MACRO_OVERLOAD(cco_yield, __VA_ARGS__) -#define cco_yield_1(retval) \ +#define cco_yield(retval) \ do { \ *_state = __LINE__; return retval; \ case __LINE__:; \ } while (0) -#define cco_yield_2(corocall2, ctx2) \ - cco_yield_3(corocall2, ctx2, ) - -#define cco_yield_3(corocall2, ctx2, retval) \ +#define cco_await_while(cond, retval) \ do { \ *_state = __LINE__; \ - do { \ - corocall2; if (cco_suspended(ctx2)) return retval; \ - case __LINE__:; \ - } while (cco_alive(ctx2)); \ + case __LINE__: if (cond) return retval; \ } while (0) +#define cco_await(cond) cco_await_while(!(cond), true) + #define cco_final \ case cco_state_final: \ _cco_final_ -- cgit v1.2.3 From 2adea8b3b06ebe1b2152870862100f7e7985cfdf Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 2 May 2023 07:20:29 +0200 Subject: Improved coroutine.h, added new coro examples. --- docs/ccommon_api.md | 28 +++++++++------- include/stc/algo/coroutine.h | 49 ++++++++++++++++++++++------ misc/examples/cointerleave.c | 60 ++++++++++++++++++++++++++++++++++ misc/examples/coread.c | 14 ++++---- misc/examples/coroutines.c | 20 ++++++------ misc/examples/scheduler.c | 76 ++++++++++++++++++++++++++++++++++++++++++++ misc/examples/triples.c | 8 ++--- 7 files changed, 212 insertions(+), 43 deletions(-) create mode 100644 misc/examples/cointerleave.c create mode 100644 misc/examples/scheduler.c (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 549eff4e..27df13ce 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -302,13 +302,13 @@ struct triples { int cco_state; // required member }; -bool triples_next(struct triples* i) { // coroutine +bool triples(struct triples* i) { // coroutine cco_begin(i); for (i->c = 5; i->n; ++i->c) { for (i->a = 1; i->a < i->c; ++i->a) { for (i->b = i->a + 1; i->b < i->c; ++i->b) { if ((int64_t)i->a*i->a + (int64_t)i->b*i->b == (int64_t)i->c*i->c) { - cco_yield(true); + cco_yield(false); if (--i->n == 0) cco_return; } } @@ -316,7 +316,7 @@ bool triples_next(struct triples* i) { // coroutine } cco_final: // required label puts("done"); - cco_end(false); + cco_end(true); } int gcd(int a, int b) { // greatest common denominator @@ -333,7 +333,7 @@ int main() struct triples t = {.n=INT32_MAX}; int n = 0; - while (triples_next(&t)) { + while (!triples(&t)) { // Skip triples with GCD(a,b) > 1 if (gcd(t.a, t.b) > 1) continue; @@ -353,20 +353,24 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | Function / operator | Description | |:----------|:-------------------------------------|:----------------------------------------| | | `cco_final:` | Obligatory label in coroutine | -| | `cco_return;` | Early return from the coroutine | +| | `cco_return` | Early return from the coroutine (no arg) | | `bool` | `cco_alive(ctx)` | Is coroutine in initial or suspended state? | | `bool` | `cco_done(ctx)` | Is coroutine not alive? | | `bool` | `cco_suspended(ctx)` | Is coroutine in suspended state? | -| `void` | `cco_begin(ctx)` | Begin coroutine block | -| `rettype` | `cco_end(retval)` | End coroutine block and return retval | -| `void` | `cco_end()` | End coroutine block (return void) | -| `rettype` | `cco_yield(retval)` | Suspend execution and return retval | -| `void` | `cco_yield()` | Suspend execution (return void) | -| `rettype` | `cco_await_while(cond, retval)` | If cond, suspend execution and return retval | -| `bool` | `cco_await(cond)` | If not cond, suspend execution and return true | +| | `cco_begin(ctx)` | Begin coroutine block | +| | `cco_end(retval)` | End coroutine block and return retval | +| | `cco_end()` | End coroutine block (return void) | +| | `cco_yield(retval)` | Suspend execution and return retval | +| | `cco_yield()` | Suspend execution (return void) | +| | `cco_await(promise)` | Suspend and return false until promise is true | +| | `cco_await_while(cond, retval)` | Suspend and return retval while cond is true | | | From caller side: | | | `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | | `void` | `cco_reset(ctx)` | Reset state to initial (for reuse) | +| | Semaphores: | | +| | `cco_semaphore` | Semaphore type | +| | `cco_await_sem(sem)` | Await for the semaphore count > 0 | +| | `cco_signal_sem(sem)` | Signal the semaphore by increasing count| --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 6c1d7d28..4b7bee5f 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -26,38 +26,38 @@ #include #include -struct iterate { +struct coroutine { int max_x, max_y; int x, y; int cco_state; // required member }; -bool iterate(struct iterate* I) { +bool coroutine(struct coroutine* I) { cco_begin(I); for (I->x = 0; I->x < I->max_x; I->x++) for (I->y = 0; I->y < I->max_y; I->y++) - cco_yield(true); + cco_yield(false); cco_final: - puts("final"); - cco_end(false); + puts("final"); + cco_end(true); } int main(void) { - struct iterate it = {.max_x=3, .max_y=3}; + struct coroutine it = {.max_x=3, .max_y=3}; int n = 0; - while (iterate(&it)) + while (!coroutine(&it)) { printf("%d %d\n", it.x, it.y); // example of early stop: - if (++n == 20) cco_stop(&it); // signal to stop at next + if (++n == 7) cco_stop(&it); // signal to stop at next } return 0; } */ #include -enum { +enum cco_states { cco_state_final = -1, cco_state_done = -2, }; @@ -89,7 +89,7 @@ enum { case __LINE__: if (cond) return retval; \ } while (0) -#define cco_await(cond) cco_await_while(!(cond), true) +#define cco_await(promise) cco_await_while(!(promise), false) #define cco_final \ case cco_state_final: \ @@ -110,4 +110,33 @@ enum { if (*_state == cco_state_done) *_state = 0; \ } while (0) + +typedef struct { + int count; +} cco_semaphore; + +/** + * Wait for a semaphore + * + * This macro carries out the "wait" operation on the semaphore. The + * wait operation causes the "thread" to block while the counter is + * zero. When the counter reaches a value larger than zero, the + * protothread will continue. + */ +#define cco_await_sem(sem) \ + do { \ + cco_await((sem)->count > 0); \ + --(sem)->count; \ + } while (0) + +/** + * Signal a semaphore + * + * This macro carries out the "signal" operation on the semaphore. The + * signal operation increments the counter inside the semaphore, which + * eventually will cause waiting "threads" to continue executing. + */ +#define cco_signal_sem(sem) ++(sem)->count +#define cco_reset_sem(sem, value) ((sem)->count = value) + #endif diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c new file mode 100644 index 00000000..64473d96 --- /dev/null +++ b/misc/examples/cointerleave.c @@ -0,0 +1,60 @@ +// https://www.youtube.com/watch?v=8sEe-4tig_A +#include +#include +#define i_type IVec +#define i_val int +#include + +struct GenValue { + IVec *v; + IVec_iter it; + int cco_state; +}; + +static int next_value(struct GenValue* g) +{ + cco_begin(g); + for (g->it = IVec_begin(g->v); g->it.ref; IVec_next(&g->it)) + cco_yield(*g->it.ref); + cco_final: + cco_end(0); +} + +struct Generator { + struct GenValue x, y; + int cco_state; + int value; +}; + +bool interleaved(struct Generator* g) +{ + cco_begin(g); + while (cco_alive(&g->x) || cco_alive(&g->y)) + { + g->value = next_value(&g->x); + if (cco_alive(&g->x)) cco_yield(false); + + g->value = next_value(&g->y); + if (cco_alive(&g->y)) cco_yield(false); + } + cco_final: + cco_end(true); +} + +void Use(void) +{ + IVec a = c_make(IVec, {2, 4, 6, 8, 10, 11}); + IVec b = c_make(IVec, {3, 5, 7, 9}); + + struct Generator g = {{&a}, {&b}}; + + while (!interleaved(&g)) + printf("%d\n", g.value); + + c_drop(IVec, &a, &b); +} + +int main() +{ + Use(); +} diff --git a/misc/examples/coread.c b/misc/examples/coread.c index 0a7f4816..d5385a87 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -4,36 +4,36 @@ // Read file line by line using coroutines: -struct file_nextline { +struct file_read { const char* filename; int cco_state; FILE* fp; cstr line; }; -bool file_nextline(struct file_nextline* U) +bool file_read(struct file_read* U) { cco_begin(U) U->fp = fopen(U->filename, "r"); U->line = cstr_init(); while (cstr_getline(&U->line, U->fp)) - cco_yield(true); + cco_yield(false); cco_final: // this label is required. printf("finish\n"); cstr_drop(&U->line); fclose(U->fp); - cco_end(false); + cco_end(true); } int main(void) { - struct file_nextline it = {__FILE__}; + struct file_read g = {__FILE__}; int n = 0; - while (file_nextline(&it)) + while (!file_read(&g)) { - printf("%3d %s\n", ++n, cstr_str(&it.line)); + printf("%3d %s\n", ++n, cstr_str(&g.line)); //if (n == 10) cco_stop(&it); } } diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index af9fef81..3fddf913 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -23,7 +23,7 @@ bool prime(struct prime* U) { if (U->result == 2) { if (U->count-- == 0) cco_return; ++U->idx; - cco_yield(true); + cco_yield(false); } U->result += !(U->result & 1); for (U->pos = U->result; U->count > 0; U->pos += 2) { @@ -31,12 +31,12 @@ bool prime(struct prime* U) { --U->count; ++U->idx; U->result = U->pos; - cco_yield(true); + cco_yield(false); } } cco_final: printf("final prm\n"); - cco_end(false); + cco_end(true); } @@ -63,11 +63,11 @@ bool fibonacci(struct fibonacci* F) { F->result = F->b; F->b = sum; } - cco_yield(true); + cco_yield(false); } cco_final: printf("final fib\n"); - cco_end(false); + cco_end(true); } // Combine @@ -81,23 +81,23 @@ struct combined { bool combined(struct combined* C) { cco_begin(C); - cco_await(prime(&C->prm) == false); - cco_await(fibonacci(&C->fib) == false); + cco_await(prime(&C->prm)); + cco_await(fibonacci(&C->fib)); // Reuse the C->prm context and extend the count: C->prm.count = 8; C->prm.result += 2; cco_reset(&C->prm); - cco_await(prime(&C->prm) == false); + cco_await(prime(&C->prm)); cco_final: puts("final comb"); - cco_end(false); + cco_end(true); } int main(void) { struct combined comb = {.prm={.count=8}, .fib={14}}; - while (combined(&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/scheduler.c b/misc/examples/scheduler.c new file mode 100644 index 00000000..1809f2ec --- /dev/null +++ b/misc/examples/scheduler.c @@ -0,0 +1,76 @@ +// https://www.youtube.com/watch?v=8sEe-4tig_A +#include +#include + +struct Scheduler; +struct Task { + bool (*resume)(struct Task*); + struct Scheduler* sched; + int cco_state; +}; + +#define i_type Scheduler +#define i_val struct Task +#define i_no_cmp +#include + +static bool schedule(Scheduler* sched) +{ + struct Task task = *Scheduler_front(sched); + Scheduler_pop_front(sched); + + if (cco_alive(&task)) + task.resume(&task); + + return !Scheduler_empty(sched); +} + +static bool resume_task(const struct Task* task) +{ + Scheduler_push_back(task->sched, *task); + return false; +} + + +static bool taskA(struct Task* task) +{ + cco_begin(task); + puts("Hello, from task A"); + cco_yield(resume_task(task)); + puts("A is back doing work"); + cco_yield(resume_task(task)); + puts("A is back doing more work"); + cco_yield(resume_task(task)); + puts("A is back doing even more work"); + cco_final: + cco_end(true); +} + +static bool taskB(struct Task* task) +{ + cco_begin(task); + puts("Hello, from task B"); + cco_yield(resume_task(task)); + puts("B is back doing work"); + cco_yield(resume_task(task)); + puts("B is back doing more work"); + cco_final: + cco_end(true); +} + +void Use(void) +{ + Scheduler scheduler = c_make(Scheduler, { + {taskA, &scheduler}, + {taskB, &scheduler}, + }); + + while (schedule(&scheduler)) {} + + Scheduler_drop(&scheduler); +} + +int main() +{ + Use(); +} diff --git a/misc/examples/triples.c b/misc/examples/triples.c index 520bf012..2e0211c3 100644 --- a/misc/examples/triples.c +++ b/misc/examples/triples.c @@ -23,13 +23,13 @@ struct triples { int cco_state; }; -bool triples_next(struct triples* I) { +bool triples_coro(struct triples* I) { cco_begin(I); for (I->c = 5; I->n; ++I->c) { for (I->a = 1; I->a < I->c; ++I->a) { for (I->b = I->a + 1; I->b < I->c; ++I->b) { if ((int64_t)I->a*I->a + (int64_t)I->b*I->b == (int64_t)I->c*I->c) { - cco_yield(true); + cco_yield(false); if (--I->n == 0) cco_return; } } @@ -37,7 +37,7 @@ bool triples_next(struct triples* I) { } cco_final: puts("done"); - cco_end(false); + cco_end(true); } int gcd(int a, int b) { @@ -58,7 +58,7 @@ int main() struct triples t = {INT32_MAX}; int n = 0; - while (triples_next(&t)) { + while (!triples_coro(&t)) { if (gcd(t.a, t.b) > 1) continue; if (t.c < 100) -- cgit v1.2.3 From ab7a91c501fb3b7054e836a931754caae578c5f2 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 2 May 2023 13:09:16 +0200 Subject: Removed cco_alive(), was same as !cco_done() --- docs/ccommon_api.md | 3 +-- include/stc/algo/coroutine.h | 1 - misc/examples/cointerleave.c | 6 +++--- misc/examples/coroutines.c | 46 ++++++++++++++++++++++---------------------- misc/examples/scheduler.c | 2 +- 5 files changed, 28 insertions(+), 30 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 27df13ce..407ddac4 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -354,9 +354,8 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c |:----------|:-------------------------------------|:----------------------------------------| | | `cco_final:` | Obligatory label in coroutine | | | `cco_return` | Early return from the coroutine (no arg) | -| `bool` | `cco_alive(ctx)` | Is coroutine in initial or suspended state? | -| `bool` | `cco_done(ctx)` | Is coroutine not alive? | | `bool` | `cco_suspended(ctx)` | Is coroutine in suspended state? | +| `bool` | `cco_done(ctx)` | Is coroutine done? | | | `cco_begin(ctx)` | Begin coroutine block | | | `cco_end(retval)` | End coroutine block and return retval | | | `cco_end()` | End coroutine block (return void) | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 4b7bee5f..f6769162 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -63,7 +63,6 @@ enum cco_states { }; #define cco_suspended(ctx) ((ctx)->cco_state > 0) -#define cco_alive(ctx) ((ctx)->cco_state != cco_state_done) #define cco_done(ctx) ((ctx)->cco_state == cco_state_done) #define cco_begin(ctx) \ diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 64473d96..9ef7d561 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -29,13 +29,13 @@ struct Generator { bool interleaved(struct Generator* g) { cco_begin(g); - while (cco_alive(&g->x) || cco_alive(&g->y)) + while (!cco_done(&g->x) || !cco_done(&g->y)) { g->value = next_value(&g->x); - if (cco_alive(&g->x)) cco_yield(false); + if (!cco_done(&g->x)) cco_yield(false); g->value = next_value(&g->y); - if (cco_alive(&g->y)) cco_yield(false); + if (!cco_done(&g->y)) cco_yield(false); } cco_final: cco_end(true); diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 3fddf913..bbe85874 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -17,20 +17,20 @@ struct prime { int cco_state; }; -bool prime(struct prime* U) { - cco_begin(U); - if (U->result < 2) U->result = 2; - if (U->result == 2) { - if (U->count-- == 0) cco_return; - ++U->idx; +bool prime(struct prime* g) { + cco_begin(g); + if (g->result < 2) g->result = 2; + if (g->result == 2) { + if (g->count-- == 0) cco_return; + ++g->idx; cco_yield(false); } - U->result += !(U->result & 1); - for (U->pos = U->result; U->count > 0; U->pos += 2) { - if (is_prime(U->pos)) { - --U->count; - ++U->idx; - U->result = U->pos; + g->result += !(g->result & 1); + for (g->pos = g->result; g->count > 0; g->pos += 2) { + if (is_prime(g->pos)) { + --g->count; + ++g->idx; + g->result = g->pos; cco_yield(false); } } @@ -48,20 +48,20 @@ struct fibonacci { int cco_state; }; -bool fibonacci(struct fibonacci* F) { - assert(F->count < 94); +bool fibonacci(struct fibonacci* g) { + assert(g->count < 94); - cco_begin(F); - F->idx = 0; - F->result = 0; - F->b = 1; + cco_begin(g); + g->idx = 0; + g->result = 0; + g->b = 1; for (;;) { - if (F->count-- == 0) + if (g->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; + if (++g->idx > 1) { + int64_t sum = g->result + g->b; // NB! locals only lasts until next cco_yield! + g->result = g->b; + g->b = sum; } cco_yield(false); } diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index 1809f2ec..db9c2716 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -19,7 +19,7 @@ static bool schedule(Scheduler* sched) struct Task task = *Scheduler_front(sched); Scheduler_pop_front(sched); - if (cco_alive(&task)) + if (!cco_done(&task)) task.resume(&task); return !Scheduler_empty(sched); -- cgit v1.2.3 From 028b113df1e09cb56ac56b4ad60f633b8fabaded Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 2 May 2023 14:01:38 +0200 Subject: Add cco_await_void(). --- docs/ccommon_api.md | 7 ++++--- include/stc/algo/coroutine.h | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 407ddac4..f4a2c349 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -362,14 +362,15 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_yield(retval)` | Suspend execution and return retval | | | `cco_yield()` | Suspend execution (return void) | | | `cco_await(promise)` | Suspend and return false until promise is true | +| | `cco_await_void(promise)` | Suspend until promise is true (return void) | | | `cco_await_while(cond, retval)` | Suspend and return retval while cond is true | -| | From caller side: | | -| `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | -| `void` | `cco_reset(ctx)` | Reset state to initial (for reuse) | | | Semaphores: | | | | `cco_semaphore` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | | | `cco_signal_sem(sem)` | Signal the semaphore by increasing count| +| | From caller side: | | +| `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | +| `void` | `cco_reset(ctx)` | Reset state to initial (for reuse) | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index f6769162..5cadbc6a 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -89,6 +89,7 @@ enum cco_states { } while (0) #define cco_await(promise) cco_await_while(!(promise), false) +#define cco_await_void(promise) cco_await_while(!(promise), ) #define cco_final \ case cco_state_final: \ @@ -120,7 +121,7 @@ typedef struct { * This macro carries out the "wait" operation on the semaphore. The * wait operation causes the "thread" to block while the counter is * zero. When the counter reaches a value larger than zero, the - * protothread will continue. + * "thread" will continue. */ #define cco_await_sem(sem) \ do { \ @@ -136,6 +137,6 @@ typedef struct { * eventually will cause waiting "threads" to continue executing. */ #define cco_signal_sem(sem) ++(sem)->count -#define cco_reset_sem(sem, value) ((sem)->count = value) +#define cco_reset_sem(sem, value) ((sem)->count = (value)) #endif -- cgit v1.2.3 From e4efe2f9cc87e70e981ee75ec5c4d6db4cb60c49 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 2 May 2023 23:11:20 +0200 Subject: Changed cco_with_..() API again, final. --- docs/ccommon_api.md | 16 ++++++++-------- include/stc/algo/coroutine.h | 29 +++++++++++++---------------- misc/examples/cointerleave.c | 13 +++++++------ misc/examples/coroutines.c | 26 ++++++++++++++------------ 4 files changed, 42 insertions(+), 42 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index f4a2c349..7014def8 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -358,18 +358,18 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | `bool` | `cco_done(ctx)` | Is coroutine done? | | | `cco_begin(ctx)` | Begin coroutine block | | | `cco_end(retval)` | End coroutine block and return retval | -| | `cco_end()` | End coroutine block (return void) | +| | `cco_end()` | End coroutine block | | | `cco_yield(retval)` | Suspend execution and return retval | -| | `cco_yield()` | Suspend execution (return void) | -| | `cco_await(promise)` | Suspend and return false until promise is true | -| | `cco_await_void(promise)` | Suspend until promise is true (return void) | -| | `cco_await_while(cond, retval)` | Suspend and return retval while cond is true | +| | `cco_yield()` | Suspend execution | +| | `cco_await(promise)` | Suspend until promise is true | +| | `cco_await_with(promise, retval)` | Suspend with retval until promise is true | | | Semaphores: | | -| | `cco_semaphore` | Semaphore type | -| | `cco_await_sem(sem)` | Await for the semaphore count > 0 | +| | `cco_semaphore` | Semaphore type | +| | `cco_await_sem(sem)` | Await for the semaphore count > 0 | +| | `cco_await_sem_with(sem, retval)` | Await with retval for the semaphore | | | `cco_signal_sem(sem)` | Signal the semaphore by increasing count| | | From caller side: | | -| `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | +| `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | | `void` | `cco_reset(ctx)` | Reset state to initial (for reuse) | --- diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 5cadbc6a..ae9e4464 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -82,15 +82,13 @@ enum cco_states { case __LINE__:; \ } while (0) -#define cco_await_while(cond, retval) \ +#define cco_await(promise) cco_await_with(promise, ) +#define cco_await_with(promise, retval) \ do { \ *_state = __LINE__; \ - case __LINE__: if (cond) return retval; \ + case __LINE__: if (!(promise)) return retval; \ } while (0) -#define cco_await(promise) cco_await_while(!(promise), false) -#define cco_await_void(promise) cco_await_while(!(promise), ) - #define cco_final \ case cco_state_final: \ _cco_final_ @@ -118,22 +116,21 @@ typedef struct { /** * Wait for a semaphore * - * This macro carries out the "wait" operation on the semaphore. The - * wait operation causes the "thread" to block while the counter is - * zero. When the counter reaches a value larger than zero, the - * "thread" will continue. + * 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) \ - do { \ - cco_await((sem)->count > 0); \ - --(sem)->count; \ - } while (0) +#define cco_await_sem(sem) cco_await_sem_with(sem, ) +#define cco_await_sem_with(sem, retval) \ + do { \ + cco_await_with((sem)->count > 0, retval); \ + --(sem)->count; \ + } while (0) /** * Signal a semaphore * - * This macro carries out the "signal" operation on the semaphore. The - * signal operation increments the counter inside the semaphore, which + * This macro carries out the "signal" operation on the semaphore, + * and increments the counter inside the semaphore, which * eventually will cause waiting "threads" to continue executing. */ #define cco_signal_sem(sem) ++(sem)->count diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 9ef7d561..5bdbd257 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -26,19 +26,19 @@ struct Generator { int value; }; -bool interleaved(struct Generator* g) +void interleaved(struct Generator* g) { cco_begin(g); while (!cco_done(&g->x) || !cco_done(&g->y)) { g->value = next_value(&g->x); - if (!cco_done(&g->x)) cco_yield(false); + if (!cco_done(&g->x)) cco_yield(); g->value = next_value(&g->y); - if (!cco_done(&g->y)) cco_yield(false); + if (!cco_done(&g->y)) cco_yield(); } cco_final: - cco_end(true); + cco_end(); } void Use(void) @@ -48,9 +48,10 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - while (!interleaved(&g)) - printf("%d\n", g.value); + while (interleaved(&g), !cco_done(&g)) + printf("%d ", g.value); + puts(""); c_drop(IVec, &a, &b); } diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index bbe85874..00cedd84 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -4,16 +4,17 @@ // Demonstrate to call another coroutine from a coroutine: // First create prime generator, then call fibonacci sequence: +typedef long long llong; bool is_prime(int64_t i) { - for (int64_t j=2; j*j <= i; ++j) + for (llong j=2; j*j <= i; ++j) if (i % j == 0) return false; return true; } struct prime { int count, idx; - int64_t result, pos; + llong result, pos; int cco_state; }; @@ -44,7 +45,7 @@ bool prime(struct prime* g) { struct fibonacci { int count, idx; - int64_t result, b; + llong result, b; int cco_state; }; @@ -59,7 +60,7 @@ bool fibonacci(struct fibonacci* g) { if (g->count-- == 0) cco_return; if (++g->idx > 1) { - int64_t sum = g->result + g->b; // NB! locals only lasts until next cco_yield! + llong sum = g->result + g->b; // NB! locals lasts only until next cco_yield/cco_await! g->result = g->b; g->b = sum; } @@ -81,13 +82,13 @@ struct combined { bool combined(struct combined* C) { cco_begin(C); - cco_await(prime(&C->prm)); - cco_await(fibonacci(&C->fib)); + cco_await_with(prime(&C->prm), false); + cco_await_with(fibonacci(&C->fib), false); // Reuse the C->prm context and extend the count: - C->prm.count = 8; C->prm.result += 2; + C->prm.count = 8, C->prm.result += 2; cco_reset(&C->prm); - cco_await(prime(&C->prm)); + cco_await_with(prime(&C->prm), false); cco_final: puts("final comb"); cco_end(true); @@ -95,10 +96,11 @@ bool combined(struct combined* C) { int main(void) { - struct combined comb = {.prm={.count=8}, .fib={14}}; + struct combined c = {.prm={.count=8}, .fib={14}}; - while (!combined(&comb)) + while (!combined(&c)) { printf("Prime(%d)=%lld, Fib(%d)=%lld\n", - comb.prm.idx, (long long)comb.prm.result, - comb.fib.idx, (long long)comb.fib.result); + c.prm.idx, c.prm.result, + c.fib.idx, c.fib.result); + } } -- cgit v1.2.3 From 6b23e35287f26dad63abd755c5f365b443e025a3 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 3 May 2023 16:45:37 +0200 Subject: Reverted from cco_await_with(promise, ret) to cco_await(promise, ret). --- include/stc/algo/coroutine.h | 15 +++++++++------ misc/examples/coread.c | 14 +++++++------- misc/examples/coroutines.c | 26 ++++++++++++++------------ 3 files changed, 30 insertions(+), 25 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index ae9e4464..89dd27f0 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -57,11 +57,12 @@ int main(void) { */ #include -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) @@ -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; \ @@ -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) diff --git a/misc/examples/coread.c b/misc/examples/coread.c index d5385a87..38447c44 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -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); } diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 00cedd84..a5db3291 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -36,7 +36,7 @@ bool prime(struct prime* g) { } } cco_final: - printf("final prm\n"); + printf("final prm\n"); cco_end(true); } @@ -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); } @@ -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); } -- cgit v1.2.3 From b03148caa1d6fc660e6e7c5986dd6fd38779bedc Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 4 May 2023 11:51:56 +0200 Subject: Updates in coroutines.h: No longer *required* with cco_final:, but only when no cleanup is needed. --- docs/ccommon_api.md | 8 ++++---- include/stc/algo/coroutine.h | 14 ++++++-------- misc/examples/cointerleave.c | 8 ++++---- misc/examples/coread.c | 8 ++++---- misc/examples/coroutines.c | 6 +++--- misc/examples/generator.c | 32 +++++++++++++++--------------- misc/examples/scheduler.c | 2 -- misc/examples/triples.c | 46 +++++++++++++++++++++++--------------------- 8 files changed, 61 insertions(+), 63 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 7014def8..2319109b 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -314,7 +314,7 @@ bool triples(struct triples* i) { // coroutine } } } - cco_final: // required label + cco_final: // required for cleanup puts("done"); cco_end(true); } @@ -352,15 +352,15 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | Function / operator | Description | |:----------|:-------------------------------------|:----------------------------------------| -| | `cco_final:` | Obligatory label in coroutine | +| | `cco_final:` | Label for cleanup in coroutine | | | `cco_return` | Early return from the coroutine (no arg) | | `bool` | `cco_suspended(ctx)` | Is coroutine in suspended state? | | `bool` | `cco_done(ctx)` | Is coroutine done? | | | `cco_begin(ctx)` | Begin coroutine block | -| | `cco_end(retval)` | End coroutine block and return retval | | | `cco_end()` | End coroutine block | -| | `cco_yield(retval)` | Suspend execution and return retval | +| | `cco_end(retval)` | End coroutine block and return retval | | | `cco_yield()` | Suspend execution | +| | `cco_yield(retval)` | Suspend execution and return retval | | | `cco_await(promise)` | Suspend until promise is true | | | `cco_await_with(promise, retval)` | Suspend with retval until promise is true | | | Semaphores: | | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 89dd27f0..c9cb8fc0 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -38,7 +38,7 @@ bool coroutine(struct coroutine* I) { for (I->y = 0; I->y < I->max_y; I->y++) cco_yield(false); - cco_final: + cco_final: // required if there is cleanup code puts("final"); cco_end(true); } @@ -68,13 +68,12 @@ enum { #define cco_begin(ctx) \ int *_state = &(ctx)->cco_state; \ - switch (*_state) { \ + goto _begin; _begin: switch (*_state) { \ case 0: #define cco_end(retval) \ - *_state = cco_state_done; break; \ - case -99: goto _cco_final_; \ } \ + *_state = cco_state_done; \ return retval #define cco_yield(retval) \ @@ -92,11 +91,10 @@ enum { } while (0) #define cco_final \ - case cco_state_final: \ - _cco_final_ + case cco_state_final #define cco_return \ - goto _cco_final_ + do { *_state = cco_state_final; goto _begin; } while (0) #define cco_stop(ctx) \ do { \ @@ -112,7 +110,7 @@ enum { typedef struct { - int count; + intptr_t count; } cco_semaphore; /** diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 5bdbd257..4fe89316 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -16,7 +16,6 @@ static int next_value(struct GenValue* g) cco_begin(g); for (g->it = IVec_begin(g->v); g->it.ref; IVec_next(&g->it)) cco_yield(*g->it.ref); - cco_final: cco_end(0); } @@ -37,7 +36,6 @@ void interleaved(struct Generator* g) g->value = next_value(&g->y); if (!cco_done(&g->y)) cco_yield(); } - cco_final: cco_end(); } @@ -48,9 +46,11 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - while (interleaved(&g), !cco_done(&g)) + while (1) { + interleaved(&g); + if (cco_done(&g)) break; printf("%d ", g.value); - + } puts(""); c_drop(IVec, &a, &b); } diff --git a/misc/examples/coread.c b/misc/examples/coread.c index 38447c44..0073191b 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -20,10 +20,10 @@ bool file_read(struct file_read* g) while (cstr_getline(&g->line, g->fp)) cco_yield(false); - cco_final: // this label is required. - printf("finish\n"); - cstr_drop(&g->line); - fclose(g->fp); + cco_final: + printf("finish\n"); + cstr_drop(&g->line); + fclose(g->fp); cco_end(true); } diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index a5db3291..9071fee0 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -35,7 +35,7 @@ bool prime(struct prime* g) { cco_yield(false); } } - cco_final: + cco_final: printf("final prm\n"); cco_end(true); } @@ -67,7 +67,7 @@ bool fibonacci(struct fibonacci* g) { } cco_yield(false); } - cco_final: + cco_final: printf("final fib\n"); cco_end(true); } @@ -91,7 +91,7 @@ bool combined(struct combined* g) { cco_reset(&g->prm); cco_await(prime(&g->prm), false); - cco_final: + cco_final: puts("final combined"); cco_end(true); } diff --git a/misc/examples/generator.c b/misc/examples/generator.c index 2bccc489..41dffafb 100644 --- a/misc/examples/generator.c +++ b/misc/examples/generator.c @@ -4,37 +4,38 @@ #include typedef struct { - int n; + int size; int a, b, c; -} Triple_value, Triple; +} Triple, Triple_value; typedef struct { Triple_value* ref; + int count; int cco_state; } Triple_iter; -bool Triple_next(Triple_iter* it) { +void Triple_next(Triple_iter* it) { Triple_value* t = it->ref; cco_begin(it); - for (t->c = 1;; ++t->c) { + 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 (t->n-- == 0) cco_return; - cco_yield(true); + if (it->count++ == t->size) + cco_return; + cco_yield(); } } } } - cco_final: - it->ref = NULL; - cco_end(false); + cco_final: + it->ref = NULL; + cco_end(); } Triple_iter Triple_begin(Triple* t) { - Triple_iter it = {t}; - if (t->n > 0) Triple_next(&it); - else it.ref = NULL; + Triple_iter it = {.ref=t}; + Triple_next(&it); return it; } @@ -42,11 +43,10 @@ Triple_iter Triple_begin(Triple* t) { int main() { puts("Pythagorean triples with c < 100:"); - Triple t = {INT32_MAX}; - c_foreach (i, Triple, t) - { + Triple triple = {.size=30}; // max number of triples + c_foreach (i, Triple, triple) { if (i.ref->c < 100) - printf("%u: (%d, %d, %d)\n", INT32_MAX - i.ref->n + 1, i.ref->a, i.ref->b, i.ref->c); + printf("%u: (%d, %d, %d)\n", i.count, i.ref->a, i.ref->b, i.ref->c); else cco_stop(&i); } diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index db9c2716..04107d5e 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -42,7 +42,6 @@ static bool taskA(struct Task* task) puts("A is back doing more work"); cco_yield(resume_task(task)); puts("A is back doing even more work"); - cco_final: cco_end(true); } @@ -54,7 +53,6 @@ static bool taskB(struct Task* task) puts("B is back doing work"); cco_yield(resume_task(task)); puts("B is back doing more work"); - cco_final: cco_end(true); } diff --git a/misc/examples/triples.c b/misc/examples/triples.c index 2e0211c3..183b7389 100644 --- a/misc/examples/triples.c +++ b/misc/examples/triples.c @@ -3,12 +3,21 @@ #include #include +int gcd(int a, int b) { + while (b) { + int t = a % b; + a = b; + b = t; + } + return a; +} + void triples_vanilla(int n) { - for (int c = 5; n; ++c) { + for (int c = 5, i = 0; n; ++c) { for (int a = 1; a < c; ++a) { for (int b = a + 1; b < c; ++b) { - if ((int64_t)a*a + (int64_t)b*b == (int64_t)c*c) { - printf("{%d, %d, %d}\n", a, b, c); + if ((int64_t)a*a + (int64_t)b*b == (int64_t)c*c && gcd(a, b) == 1) { + printf("%d: {%d, %d, %d}\n", ++i, a, b, c); if (--n == 0) goto done; } } @@ -18,41 +27,34 @@ void triples_vanilla(int n) { } struct triples { - int n; + int size, count; int a, b, c; int cco_state; }; -bool triples_coro(struct triples* I) { - cco_begin(I); - for (I->c = 5; I->n; ++I->c) { - for (I->a = 1; I->a < I->c; ++I->a) { - for (I->b = I->a + 1; I->b < I->c; ++I->b) { - if ((int64_t)I->a*I->a + (int64_t)I->b*I->b == (int64_t)I->c*I->c) { +bool triples_coro(struct triples* t) { + cco_begin(t); + t->count = 0; + for (t->c = 5; t->size; ++t->c) { + for (t->a = 1; t->a < t->c; ++t->a) { + for (t->b = t->a + 1; t->b < t->c; ++t->b) { + if ((int64_t)t->a*t->a + (int64_t)t->b*t->b == (int64_t)t->c*t->c) { + if (t->count++ == t->size) + cco_return; cco_yield(false); - if (--I->n == 0) cco_return; } } } } - cco_final: + cco_final: puts("done"); cco_end(true); } -int gcd(int a, int b) { - while (b) { - int t = a % b; - a = b; - b = t; - } - return a; -} - int main() { puts("Vanilla triples:"); - triples_vanilla(6); + triples_vanilla(5); puts("\nCoroutine triples:"); struct triples t = {INT32_MAX}; -- cgit v1.2.3 From bca31bb8d85d6781f0c3d074eb1a25fa6de48e07 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 8 May 2023 07:40:41 +0200 Subject: Updated API for timer and semaphores in coroutine.h (ctimer, csem) --- docs/ccommon_api.md | 17 ++++++++--- include/stc/algo/coroutine.h | 68 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 19 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 2319109b..c20043f8 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -362,12 +362,21 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_yield()` | Suspend execution | | | `cco_yield(retval)` | Suspend execution and return retval | | | `cco_await(promise)` | Suspend until promise is true | -| | `cco_await_with(promise, retval)` | Suspend with retval until promise is true | +| | `cco_await(promise, retval)` | Suspend with retval until promise is true | | | Semaphores: | | -| | `cco_semaphore` | Semaphore type | +| | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | -| | `cco_await_sem_with(sem, retval)` | Await with retval for the semaphore | -| | `cco_signal_sem(sem)` | Signal the semaphore by increasing count| +| | `cco_await_sem(sem, retval)` | Await with retval for the semaphore | +| | `csem_set(sem, long value)` | Set semaphore | +| | `csem_signal(sem)` | Signal the semaphore | +| | Timers: | | +| | `ctimer` | Timer type | +| | `cco_await_timer(tm)` | Await for timer to expire | +| | `cco_await_timer(tm, retval)` | Await with retval for timer to expire | +| | `ctimer_start(tm, long msecs)` | Start timer for milliseconds | +| | `ctimer_restart(tm)` | Restart timer with same duration | +| `bool` | `ctimer_expired(tm)` | Return true if timer is expired | +| `long` | `ctimer_remaining(tm)` | Return milliseconds remaining | | | From caller side: | | | `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | | `void` | `cco_reset(ctx)` | Reset state to initial (for reuse) | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index c9cb8fc0..80b96bbd 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -108,17 +108,14 @@ enum { if (*_state == cco_state_done) *_state = 0; \ } while (0) +/* + * Semaphore + */ typedef struct { intptr_t count; -} cco_semaphore; +} csem; -/** - * Wait for a semaphore - * - * 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(...) 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) \ @@ -127,14 +124,55 @@ typedef struct { --(sem)->count; \ } while (0) -/** - * Signal a semaphore - * - * This macro carries out the "signal" operation on the semaphore, - * and increments the counter inside the semaphore, which - * eventually will cause waiting "threads" to continue executing. +#define csem_signal(sem) ++(sem)->count +#define csem_set(sem, value) ((sem)->count = (value)) + +/* + * Timer */ -#define cco_signal_sem(sem) ++(sem)->count -#define cco_reset_sem(sem, value) ((sem)->count = (value)) + +#include + +#ifdef _WIN32 + static inline void csleep_ms(long msecs) { + extern void Sleep(unsigned long); + Sleep((unsigned long)msecs); + } +#elif _POSIX_C_SOURCE >= 199309L + static inline void csleep_ms(long msecs) { + struct timespec ts = {msecs/1000, 1000000*(msecs % 1000)}; + nanosleep(&ts, NULL); + } +#endif + +typedef struct { + clock_t start; + clock_t interval; +} ctimer; + +#define cco_await_timer(...) c_MACRO_OVERLOAD(cco_await_timer, __VA_ARGS__) +#define cco_await_timer_2(t, msecs) cco_await_timer_3(t, msecs, ) +#define cco_await_timer_3(t, msecs, ret) \ + do { \ + ctimer_start(t, msecs); \ + cco_await_2(ctimer_expired(t), ret); \ + } while (0) + +static inline void ctimer_start(ctimer* t, long msecs) { + t->interval = msecs*(CLOCKS_PER_SEC/1000); + t->start = clock(); +} + +static inline void ctimer_restart(ctimer* t) { + t->start = clock(); +} + +static inline bool ctimer_expired(ctimer* t) { + return clock() - t->start >= t->interval; +} + +static inline long ctimer_remaining(ctimer* t) { + return (long)((double)(t->start + t->interval - clock())*(1000.0/CLOCKS_PER_SEC)); +} #endif -- cgit v1.2.3 From b909bee0e400fa12908bc3d9bca447ea2a71864b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 8 May 2023 11:14:03 +0200 Subject: More coroutine updates. --- docs/ccommon_api.md | 7 ++++-- include/stc/algo/coroutine.h | 53 ++++++++++++++++++++++++++------------------ misc/examples/coroutines.c | 12 +++++----- 3 files changed, 42 insertions(+), 30 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index c20043f8..daf21e56 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -314,7 +314,7 @@ bool triples(struct triples* i) { // coroutine } } } - cco_final: // required for cleanup + cco_final: // tear down puts("done"); cco_end(true); } @@ -363,6 +363,8 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_yield(retval)` | Suspend execution and return retval | | | `cco_await(promise)` | Suspend until promise is true | | | `cco_await(promise, retval)` | Suspend with retval until promise is true | +| | `cco_call(ctx, corocall)` | Call coro async, suspend while not done | +| | `cco_call(ctx, corocall, retval)` | Call coro async, return retval on suspend | | | Semaphores: | | | | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | @@ -373,13 +375,14 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `ctimer` | Timer type | | | `cco_await_timer(tm)` | Await for timer to expire | | | `cco_await_timer(tm, retval)` | Await with retval for timer to expire | -| | `ctimer_start(tm, long msecs)` | Start timer for milliseconds | +| | `ctimer_start(tm, long msecs)` | Start timer msecs milliseconds | | | `ctimer_restart(tm)` | Restart timer with same duration | | `bool` | `ctimer_expired(tm)` | Return true if timer is expired | | `long` | `ctimer_remaining(tm)` | Return milliseconds remaining | | | From caller side: | | | `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | | `void` | `cco_reset(ctx)` | Reset state to initial (for reuse) | +| `void` | `cco_run_blocked(ctx, corocall) { }` | Call coro blocked until done | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 80b96bbd..942abc5a 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -62,12 +62,12 @@ enum { 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) +#define cco_initial(co) ((co)->cco_state == 0) +#define cco_suspended(co) ((co)->cco_state > 0) +#define cco_done(co) ((co)->cco_state == cco_state_done) -#define cco_begin(ctx) \ - int *_state = &(ctx)->cco_state; \ +#define cco_begin(co) \ + int *_state = &(co)->cco_state; \ goto _begin; _begin: switch (*_state) { \ case 0: @@ -90,21 +90,30 @@ enum { case __LINE__: if (!(promise)) return retval; \ } while (0) +#define cco_call(...) c_MACRO_OVERLOAD(cco_call, __VA_ARGS__) +#define cco_call_2(co, call) cco_call_3(co, call, ) +#define cco_call_3(co, call, retval) cco_await_2((call, cco_done(co)), retval) + +#define cco_run_blocked(co, call) while (call, !cco_done(co)) + #define cco_final \ case cco_state_final #define cco_return \ do { *_state = cco_state_final; goto _begin; } while (0) -#define cco_stop(ctx) \ +#define cco_return_v(value) \ + return (*_state = cco_state_final, value) + +#define cco_stop(co) \ do { \ - int* _state = &(ctx)->cco_state; \ + int* _state = &(co)->cco_state; \ if (*_state > 0) *_state = cco_state_final; \ } while (0) -#define cco_reset(ctx) \ +#define cco_reset(co) \ do { \ - int* _state = &(ctx)->cco_state; \ + int* _state = &(co)->cco_state; \ if (*_state == cco_state_done) *_state = 0; \ } while (0) @@ -151,28 +160,28 @@ typedef struct { } ctimer; #define cco_await_timer(...) c_MACRO_OVERLOAD(cco_await_timer, __VA_ARGS__) -#define cco_await_timer_2(t, msecs) cco_await_timer_3(t, msecs, ) -#define cco_await_timer_3(t, msecs, ret) \ +#define cco_await_timer_2(tm, msecs) cco_await_timer_3(tm, msecs, ) +#define cco_await_timer_3(tm, msecs, ret) \ do { \ - ctimer_start(t, msecs); \ - cco_await_2(ctimer_expired(t), ret); \ + ctimer_start(tm, msecs); \ + cco_await_2(ctimer_expired(tm), ret); \ } while (0) -static inline void ctimer_start(ctimer* t, long msecs) { - t->interval = msecs*(CLOCKS_PER_SEC/1000); - t->start = clock(); +static inline void ctimer_start(ctimer* tm, long msecs) { + tm->interval = msecs*(CLOCKS_PER_SEC/1000); + tm->start = clock(); } -static inline void ctimer_restart(ctimer* t) { - t->start = clock(); +static inline void ctimer_restart(ctimer* tm) { + tm->start = clock(); } -static inline bool ctimer_expired(ctimer* t) { - return clock() - t->start >= t->interval; +static inline bool ctimer_expired(ctimer* tm) { + return clock() - tm->start >= tm->interval; } -static inline long ctimer_remaining(ctimer* t) { - return (long)((double)(t->start + t->interval - clock())*(1000.0/CLOCKS_PER_SEC)); +static inline long ctimer_remaining(ctimer* tm) { + return (long)((double)(tm->start + tm->interval - clock())*(1000.0/CLOCKS_PER_SEC)); } #endif diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 9071fee0..7f255dda 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -81,26 +81,26 @@ struct combined { }; -bool combined(struct combined* g) { +void combined(struct combined* g) { cco_begin(g); - cco_await(prime(&g->prm), false); - cco_await(fibonacci(&g->fib), false); + cco_await(prime(&g->prm)); + cco_await(fibonacci(&g->fib)); // 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_await(prime(&g->prm)); cco_final: puts("final combined"); - cco_end(true); + cco_end(); } int main(void) { struct combined c = {.prm={.count=8}, .fib={14}}; - while (!combined(&c)) { + cco_run_blocked(&c, combined(&c)) { printf("Prime(%d)=%lld, Fib(%d)=%lld\n", c.prm.idx, c.prm.result, c.fib.idx, c.fib.result); -- cgit v1.2.3 From f8f544d8f5b805b9749f1e06fd7c1875b6115d48 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 8 May 2023 13:23:05 +0200 Subject: Final coroutine API updates. --- docs/ccommon_api.md | 34 ++++++++++++++++++---------------- include/stc/algo/coroutine.h | 27 ++++++++++++++++----------- misc/examples/cointerleave.c | 13 ++++--------- 3 files changed, 38 insertions(+), 36 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index daf21e56..fb29d642 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -354,35 +354,37 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c |:----------|:-------------------------------------|:----------------------------------------| | | `cco_final:` | Label for cleanup in coroutine | | | `cco_return` | Early return from the coroutine (no arg) | -| `bool` | `cco_suspended(ctx)` | Is coroutine in suspended state? | -| `bool` | `cco_done(ctx)` | Is coroutine done? | -| | `cco_begin(ctx)` | Begin coroutine block | +| `bool` | `cco_suspended(co)` | Is coroutine in suspended state? | +| `bool` | `cco_done(co)` | Is coroutine done? | +| | `cco_begin(co)` | Begin coroutine block | | | `cco_end()` | End coroutine block | -| | `cco_end(retval)` | End coroutine block and return retval | -| | `cco_yield()` | Suspend execution | -| | `cco_yield(retval)` | Suspend execution and return retval | -| | `cco_await(promise)` | Suspend until promise is true | -| | `cco_await(promise, retval)` | Suspend with retval until promise is true | -| | `cco_call(ctx, corocall)` | Call coro async, suspend while not done | -| | `cco_call(ctx, corocall, retval)` | Call coro async, return retval on suspend | +| | `cco_end(ret)` | End coroutine block and return ret | +| | `cco_yield()` | Yield/suspend execution | +| | `cco_yield(ret)` | Yield/suspend execution and return ret | +| | `cco_yield_sub(subco, call)` | Yield if subco not done after call | +| | `cco_yield_sub(subco, call, ret)` | Yield with ret if subco alive after call | +| | `cco_await(promise)` | Wait/suspend until promise is true | +| | `cco_await(promise, ret)` | Wait/suspend with ret value | +| | `cco_await_sub(subco, call)` | Wait/suspend until subco call is done | +| | `cco_await_sub(subco, call, ret)` | Wait/suspend with ret on subco call done | | | Semaphores: | | | | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | -| | `cco_await_sem(sem, retval)` | Await with retval for the semaphore | -| | `csem_set(sem, long value)` | Set semaphore | +| | `cco_await_sem(sem, ret)` | Await with ret on the semaphore | +| | `csem_set(sem, long value)` | Set semaphore value | | | `csem_signal(sem)` | Signal the semaphore | | | Timers: | | | | `ctimer` | Timer type | | | `cco_await_timer(tm)` | Await for timer to expire | -| | `cco_await_timer(tm, retval)` | Await with retval for timer to expire | +| | `cco_await_timer(tm, ret)` | Await with ret for timer to expire | | | `ctimer_start(tm, long msecs)` | Start timer msecs milliseconds | | | `ctimer_restart(tm)` | Restart timer with same duration | | `bool` | `ctimer_expired(tm)` | Return true if timer is expired | | `long` | `ctimer_remaining(tm)` | Return milliseconds remaining | | | From caller side: | | -| `void` | `cco_stop(ctx)` | Next call of coroutine returns `cco_end()` | -| `void` | `cco_reset(ctx)` | Reset state to initial (for reuse) | -| `void` | `cco_run_blocked(ctx, corocall) { }` | Call coro blocked until done | +| `void` | `cco_stop(co)` | Next call of coroutine returns `cco_end()` | +| `void` | `cco_reset(co)` | Reset state to initial (for reuse) | +| `void` | `cco_run_blocked(co, corocall) { }` | Call coro blocked until done | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 942abc5a..83814605 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -71,28 +71,33 @@ enum { goto _begin; _begin: switch (*_state) { \ case 0: -#define cco_end(retval) \ +#define cco_end(ret) \ } \ *_state = cco_state_done; \ - return retval + return ret -#define cco_yield(retval) \ +#define cco_yield(ret) \ do { \ - *_state = __LINE__; return retval; \ + *_state = __LINE__; return ret; \ case __LINE__:; \ } while (0) +#define cco_yield_sub(...) c_MACRO_OVERLOAD(cco_yield_sub, __VA_ARGS__) +#define cco_yield_sub_2(co, call) cco_yield_sub_3(co, call, ) +#define cco_yield_sub_3(co, call, ret) \ + do { call; if (!cco_done(co)) cco_yield(ret); } while (0) + #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) \ +#define cco_await_2(promise, ret) \ do { \ *_state = __LINE__; \ - case __LINE__: if (!(promise)) return retval; \ + case __LINE__: if (!(promise)) return ret; \ } while (0) -#define cco_call(...) c_MACRO_OVERLOAD(cco_call, __VA_ARGS__) -#define cco_call_2(co, call) cco_call_3(co, call, ) -#define cco_call_3(co, call, retval) cco_await_2((call, cco_done(co)), retval) +#define cco_await_sub(...) c_MACRO_OVERLOAD(cco_await_sub, __VA_ARGS__) +#define cco_await_sub_2(co, call) cco_await_sub_3(co, call, ) +#define cco_await_sub_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) #define cco_run_blocked(co, call) while (call, !cco_done(co)) @@ -127,9 +132,9 @@ typedef struct { #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) \ +#define cco_await_sem_2(sem, ret) \ do { \ - cco_await_2((sem)->count > 0, retval); \ + cco_await_2((sem)->count > 0, ret); \ --(sem)->count; \ } while (0) diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 4fe89316..0ccf9ad7 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -11,7 +11,7 @@ struct GenValue { int cco_state; }; -static int next_value(struct GenValue* g) +static int get_value(struct GenValue* g) { cco_begin(g); for (g->it = IVec_begin(g->v); g->it.ref; IVec_next(&g->it)) @@ -30,11 +30,8 @@ void interleaved(struct Generator* g) cco_begin(g); while (!cco_done(&g->x) || !cco_done(&g->y)) { - g->value = next_value(&g->x); - if (!cco_done(&g->x)) cco_yield(); - - g->value = next_value(&g->y); - if (!cco_done(&g->y)) cco_yield(); + cco_yield_sub(&g->x, g->value = get_value(&g->x)); + cco_yield_sub(&g->y, g->value = get_value(&g->y)); } cco_end(); } @@ -46,9 +43,7 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - while (1) { - interleaved(&g); - if (cco_done(&g)) break; + cco_run_blocked(&g, interleaved(&g)) { printf("%d ", g.value); } puts(""); -- cgit v1.2.3 From 2f11c7cf36690a1493344189b6a011c26ee58a9b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 8 May 2023 18:45:24 +0200 Subject: coroutine cco_await_* and cco_yield_* changes. --- docs/ccommon_api.md | 12 ++++++------ include/stc/algo/coroutine.h | 12 ++++++------ misc/examples/cointerleave.c | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index fb29d642..512aeb80 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -361,12 +361,12 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_end(ret)` | End coroutine block and return ret | | | `cco_yield()` | Yield/suspend execution | | | `cco_yield(ret)` | Yield/suspend execution and return ret | -| | `cco_yield_sub(subco, call)` | Yield if subco not done after call | -| | `cco_yield_sub(subco, call, ret)` | Yield with ret if subco alive after call | -| | `cco_await(promise)` | Wait/suspend until promise is true | -| | `cco_await(promise, ret)` | Wait/suspend with ret value | -| | `cco_await_sub(subco, call)` | Wait/suspend until subco call is done | -| | `cco_await_sub(subco, call, ret)` | Wait/suspend with ret on subco call done | +| | `cco_yield_at(co, call)` | Yield next in co call, if not done | +| | `cco_yield_at(co, call, ret)` | Yield next in co call with ret | +| | `cco_await(promise)` | Await/suspend until promise is true | +| | `cco_await(promise, ret)` | Await/suspend with ret value | +| | `cco_await_done(co, call)` | Await for co call to finish | +| | `cco_await_done(co, call, ret)` | Await for co call to finish with ret | | | Semaphores: | | | | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 83814605..979e05bb 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -82,9 +82,9 @@ enum { case __LINE__:; \ } while (0) -#define cco_yield_sub(...) c_MACRO_OVERLOAD(cco_yield_sub, __VA_ARGS__) -#define cco_yield_sub_2(co, call) cco_yield_sub_3(co, call, ) -#define cco_yield_sub_3(co, call, ret) \ +#define cco_yield_at(...) c_MACRO_OVERLOAD(cco_yield_at, __VA_ARGS__) +#define cco_yield_at_2(co, call) cco_yield_at_3(co, call, ) +#define cco_yield_at_3(co, call, ret) \ do { call; if (!cco_done(co)) cco_yield(ret); } while (0) #define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__) @@ -95,9 +95,9 @@ enum { case __LINE__: if (!(promise)) return ret; \ } while (0) -#define cco_await_sub(...) c_MACRO_OVERLOAD(cco_await_sub, __VA_ARGS__) -#define cco_await_sub_2(co, call) cco_await_sub_3(co, call, ) -#define cco_await_sub_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) +#define cco_await_done(...) c_MACRO_OVERLOAD(cco_await_done, __VA_ARGS__) +#define cco_await_done_2(co, call) cco_await_done_3(co, call, ) +#define cco_await_done_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) #define cco_run_blocked(co, call) while (call, !cco_done(co)) diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 0ccf9ad7..0854a741 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -28,10 +28,10 @@ struct Generator { void interleaved(struct Generator* g) { cco_begin(g); - while (!cco_done(&g->x) || !cco_done(&g->y)) + while (!(cco_done(&g->x) & cco_done(&g->y))) { - cco_yield_sub(&g->x, g->value = get_value(&g->x)); - cco_yield_sub(&g->y, g->value = get_value(&g->y)); + cco_yield_at(&g->x, g->value = get_value(&g->x)); + cco_yield_at(&g->y, g->value = get_value(&g->y)); } cco_end(); } -- cgit v1.2.3 From 99d94309f31f082b505180d2cb7c1c6c2215e9f0 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Tue, 9 May 2023 04:40:24 +0200 Subject: reverted cco_await_done => cco_await_at. --- docs/ccommon_api.md | 10 +++++----- include/stc/algo/coroutine.h | 8 ++++---- include/stc/cpque.h | 5 +++-- misc/examples/cointerleave.c | 2 +- misc/examples/coread.c | 11 +++++------ misc/examples/coroutines.c | 2 +- misc/examples/scheduler.c | 12 ++++++------ 7 files changed, 25 insertions(+), 25 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 512aeb80..93ad2bb7 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -361,12 +361,12 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_end(ret)` | End coroutine block and return ret | | | `cco_yield()` | Yield/suspend execution | | | `cco_yield(ret)` | Yield/suspend execution and return ret | -| | `cco_yield_at(co, call)` | Yield next in co call, if not done | -| | `cco_yield_at(co, call, ret)` | Yield next in co call with ret | +| | `cco_yield_at(co, call)` | Yield at co call if it is suspended | +| | `cco_yield_at(co, call, ret)` | Yield at co call with ret if suspended | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | -| | `cco_await_done(co, call)` | Await for co call to finish | -| | `cco_await_done(co, call, ret)` | Await for co call to finish with ret | +| | `cco_await_at(co, call)` | Await for co call to finish | +| | `cco_await_at(co, call, ret)` | Await for co call to finish with ret | | | Semaphores: | | | | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | @@ -384,7 +384,7 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine returns `cco_end()` | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | -| `void` | `cco_run_blocked(co, corocall) { }` | Call coro blocked until done | +| `void` | `cco_run(co, corocall) { }` | Run blocking until coro is done | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 979e05bb..2ea7122b 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -95,11 +95,11 @@ enum { case __LINE__: if (!(promise)) return ret; \ } while (0) -#define cco_await_done(...) c_MACRO_OVERLOAD(cco_await_done, __VA_ARGS__) -#define cco_await_done_2(co, call) cco_await_done_3(co, call, ) -#define cco_await_done_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) +#define cco_await_at(...) c_MACRO_OVERLOAD(cco_await_at, __VA_ARGS__) +#define cco_await_at_2(co, call) cco_await_at_3(co, call, ) +#define cco_await_at_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) -#define cco_run_blocked(co, call) while (call, !cco_done(co)) +#define cco_run(co, call) while (call, !cco_done(co)) #define cco_final \ case cco_state_final diff --git a/include/stc/cpque.h b/include/stc/cpque.h index c76621cd..85002c67 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -36,7 +36,7 @@ typedef i_keyraw _cx_raw; STC_API void _cx_memb(_make_heap)(_cx_self* self); STC_API void _cx_memb(_erase_at)(_cx_self* self, intptr_t idx); -STC_API void _cx_memb(_push)(_cx_self* self, _cx_value value); +STC_API _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value value); STC_INLINE _cx_self _cx_memb(_init)(void) { return c_LITERAL(_cx_self){NULL}; } @@ -144,7 +144,7 @@ _cx_memb(_erase_at)(_cx_self* self, const intptr_t idx) { _cx_memb(_sift_down_)(self, idx + 1, n); } -STC_DEF void +STC_DEF _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value value) { if (self->_len == self->_cap) _cx_memb(_reserve)(self, self->_len*3/2 + 4); @@ -153,6 +153,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value value) { for (; c > 1 && (i_less((&arr[c/2]), (&value))); c /= 2) arr[c] = arr[c/2]; arr[c] = value; + return arr + c; } #endif diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 0854a741..d725989f 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -43,7 +43,7 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - cco_run_blocked(&g, interleaved(&g)) { + cco_run(&g, interleaved(&g)) { printf("%d ", g.value); } puts(""); diff --git a/misc/examples/coread.c b/misc/examples/coread.c index 0073191b..e60fb31c 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -11,29 +11,28 @@ struct file_read { cstr line; }; -bool file_read(struct file_read* g) +void file_read(struct file_read* g) { cco_begin(g) g->fp = fopen(g->filename, "r"); g->line = cstr_init(); - while (cstr_getline(&g->line, g->fp)) - cco_yield(false); + cco_await(!cstr_getline(&g->line, g->fp)); cco_final: printf("finish\n"); cstr_drop(&g->line); fclose(g->fp); - cco_end(true); + cco_end(); } int main(void) { struct file_read g = {__FILE__}; int n = 0; - while (!file_read(&g)) + cco_run(&g, file_read(&g)) { printf("%3d %s\n", ++n, cstr_str(&g.line)); - //if (n == 10) cco_stop(&it); + //if (n == 10) cco_stop(&g); } } diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 7f255dda..a7136993 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -100,7 +100,7 @@ int main(void) { struct combined c = {.prm={.count=8}, .fib={14}}; - cco_run_blocked(&c, combined(&c)) { + cco_run(&c, combined(&c)) { printf("Prime(%d)=%lld, Fib(%d)=%lld\n", c.prm.idx, c.prm.result, c.fib.idx, c.fib.result); diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index 04107d5e..bad5201b 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -25,7 +25,7 @@ static bool schedule(Scheduler* sched) return !Scheduler_empty(sched); } -static bool resume_task(const struct Task* task) +static bool push_task(const struct Task* task) { Scheduler_push_back(task->sched, *task); return false; @@ -36,11 +36,11 @@ static bool taskA(struct Task* task) { cco_begin(task); puts("Hello, from task A"); - cco_yield(resume_task(task)); + cco_yield(push_task(task)); puts("A is back doing work"); - cco_yield(resume_task(task)); + cco_yield(push_task(task)); puts("A is back doing more work"); - cco_yield(resume_task(task)); + cco_yield(push_task(task)); puts("A is back doing even more work"); cco_end(true); } @@ -49,9 +49,9 @@ static bool taskB(struct Task* task) { cco_begin(task); puts("Hello, from task B"); - cco_yield(resume_task(task)); + cco_yield(push_task(task)); puts("B is back doing work"); - cco_yield(resume_task(task)); + cco_yield(push_task(task)); puts("B is back doing more work"); cco_end(true); } -- cgit v1.2.3 From 992950d20096848f606525dffc96c74bda52c543 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Tue, 9 May 2023 16:30:08 +0200 Subject: Renaming. --- docs/ccommon_api.md | 8 ++++---- include/stc/algo/coroutine.h | 14 +++++++------- misc/examples/cointerleave.c | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 93ad2bb7..e4c881dd 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -361,12 +361,12 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_end(ret)` | End coroutine block and return ret | | | `cco_yield()` | Yield/suspend execution | | | `cco_yield(ret)` | Yield/suspend execution and return ret | -| | `cco_yield_at(co, call)` | Yield at co call if it is suspended | -| | `cco_yield_at(co, call, ret)` | Yield at co call with ret if suspended | +| | `cco_yield_coro(co, call)` | Yield at co call if it is suspended | +| | `cco_yield_coro(co, call, ret)` | Yield at co call with ret if suspended | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | -| | `cco_await_at(co, call)` | Await for co call to finish | -| | `cco_await_at(co, call, ret)` | Await for co call to finish with ret | +| | `cco_await_coro(co, call)` | Await for co call to finish | +| | `cco_await_coro(co, call, ret)` | Await for co call to finish with ret | | | Semaphores: | | | | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 2ea7122b..8d3b79b5 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -82,12 +82,12 @@ enum { case __LINE__:; \ } while (0) -#define cco_yield_at(...) c_MACRO_OVERLOAD(cco_yield_at, __VA_ARGS__) -#define cco_yield_at_2(co, call) cco_yield_at_3(co, call, ) -#define cco_yield_at_3(co, call, ret) \ +#define cco_yield_coro(...) c_MACRO_OVERLOAD(cco_yield_coro, __VA_ARGS__) +#define cco_yield_coro_2(co, call) cco_yield_coro_3(co, call, ) +#define cco_yield_coro_3(co, call, ret) \ do { call; if (!cco_done(co)) cco_yield(ret); } while (0) -#define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__) +#define cco_await(...) c_MACRO_OVERLOAD(cco_awaitcoro, __VA_ARGS__) #define cco_await_1(promise) cco_await_2(promise, ) #define cco_await_2(promise, ret) \ do { \ @@ -95,9 +95,9 @@ enum { case __LINE__: if (!(promise)) return ret; \ } while (0) -#define cco_await_at(...) c_MACRO_OVERLOAD(cco_await_at, __VA_ARGS__) -#define cco_await_at_2(co, call) cco_await_at_3(co, call, ) -#define cco_await_at_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) +#define cco_await_coro(...) c_MACRO_OVERLOAD(cco_await_coro, __VA_ARGS__) +#define cco_await_coro_2(co, call) cco_await_2((call, cco_done(co)), ) +#define cco_await_coro_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) #define cco_run(co, call) while (call, !cco_done(co)) diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index d725989f..aa57808e 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -30,9 +30,9 @@ void interleaved(struct Generator* g) cco_begin(g); while (!(cco_done(&g->x) & cco_done(&g->y))) { - cco_yield_at(&g->x, g->value = get_value(&g->x)); - cco_yield_at(&g->y, g->value = get_value(&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(); } -- cgit v1.2.3 From 98d131bfc1bcc8b82cb27b30ad6da176d1fed117 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Tue, 9 May 2023 16:36:46 +0200 Subject: fixed typo. --- include/stc/algo/coroutine.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 8d3b79b5..c3f36ac2 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -87,7 +87,7 @@ enum { #define cco_yield_coro_3(co, call, ret) \ do { call; if (!cco_done(co)) cco_yield(ret); } while (0) -#define cco_await(...) c_MACRO_OVERLOAD(cco_awaitcoro, __VA_ARGS__) +#define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__) #define cco_await_1(promise) cco_await_2(promise, ) #define cco_await_2(promise, ret) \ do { \ -- cgit v1.2.3 From 6973d104999f47e0baad4b23d36642f86775be90 Mon Sep 17 00:00:00 2001 From: Liigo Zhuang Date: Thu, 18 May 2023 14:18:10 +0800 Subject: add `cco` macro, block style coroutine definition, to replace cco_begin(co) && cco_end() --- include/stc/algo/coroutine.h | 6 +++++- misc/examples/coread.c | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index c3f36ac2..78dc80c6 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -76,9 +76,13 @@ enum { *_state = cco_state_done; \ return ret +#define cco(co) \ + for (int *_state = &(co)->cco_state, _once=1; _once; *_state = cco_state_done, _once=0) \ + _begin: switch (*_state) case 0: + #define cco_yield(ret) \ do { \ - *_state = __LINE__; return ret; \ + *_state = __LINE__; return ret; goto _begin; \ case __LINE__:; \ } while (0) diff --git a/misc/examples/coread.c b/misc/examples/coread.c index e60fb31c..1976231f 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -13,7 +13,7 @@ struct file_read { void file_read(struct file_read* g) { - cco_begin(g) + cco(g) { g->fp = fopen(g->filename, "r"); g->line = cstr_init(); @@ -23,7 +23,8 @@ void file_read(struct file_read* g) printf("finish\n"); cstr_drop(&g->line); fclose(g->fp); - cco_end(); + } + return; } int main(void) -- cgit v1.2.3 From 276b8110033aa275f58ce60d096f220ca050738c Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 24 May 2023 16:21:22 +0200 Subject: 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. --- docs/ccommon_api.md | 11 +++++----- include/stc/algo/coroutine.h | 49 +++++++++++++++----------------------------- misc/examples/cointerleave.c | 11 +++++----- misc/examples/coread.c | 3 ++- misc/examples/coroutines.c | 14 +++++++------ misc/examples/generator.c | 21 ++++++++++--------- misc/examples/scheduler.c | 10 +++++---- misc/examples/triples.c | 5 +++-- 8 files changed, 58 insertions(+), 66 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 5f6c82ed..beaad7e9 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -321,7 +321,7 @@ struct triples { }; bool triples(struct triples* i) { // coroutine - cco_begin(i); + cco_routine(i) { for (i->c = 5; i->n; ++i->c) { for (i->a = 1; i->a < i->c; ++i->a) { for (i->b = i->a + 1; i->b < i->c; ++i->b) { @@ -332,9 +332,10 @@ bool triples(struct triples* i) { // coroutine } } } - cco_final: // tear down + cco_final: puts("done"); - cco_end(true); + } + return true; } int gcd(int a, int b) { // greatest common denominator @@ -374,9 +375,7 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_return` | Early return from the coroutine (no arg) | | `bool` | `cco_suspended(co)` | Is coroutine in suspended state? | | `bool` | `cco_done(co)` | Is coroutine done? | -| | `cco_begin(co)` | Begin coroutine block | -| | `cco_end()` | End coroutine block | -| | `cco_end(ret)` | End coroutine block and return ret | +| | `cco_routine(co) { ... }` | The coroutine closure | | | `cco_yield()` | Yield/suspend execution | | | `cco_yield(ret)` | Yield/suspend execution and return ret | | | `cco_yield_coro(co, call)` | Yield at co call if it is suspended | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 78dc80c6..ebfed613 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -26,31 +26,32 @@ #include #include -struct coroutine { +struct iterpair { int max_x, max_y; int x, y; int cco_state; // required member }; -bool coroutine(struct coroutine* I) { - cco_begin(I); +bool iterpair(struct iterpair* I) { + cco_routine(I) { for (I->x = 0; I->x < I->max_x; I->x++) for (I->y = 0; I->y < I->max_y; I->y++) cco_yield(false); - cco_final: // required if there is cleanup code + cco_final: // required if there is cleanup code puts("final"); - cco_end(true); + } + return true; // finished } int main(void) { - struct coroutine it = {.max_x=3, .max_y=3}; + struct iterpair it = {.max_x=3, .max_y=3}; int n = 0; - while (!coroutine(&it)) + while (!iterpair(&it)) { printf("%d %d\n", it.x, it.y); // example of early stop: - if (++n == 7) cco_stop(&it); // signal to stop at next + if (++n == 7) cco_stop(&it); // signal to stop/finalize in next } return 0; } @@ -66,19 +67,9 @@ enum { #define cco_suspended(co) ((co)->cco_state > 0) #define cco_done(co) ((co)->cco_state == cco_state_done) -#define cco_begin(co) \ - int *_state = &(co)->cco_state; \ - goto _begin; _begin: switch (*_state) { \ - case 0: - -#define cco_end(ret) \ - } \ - *_state = cco_state_done; \ - return ret - -#define cco(co) \ +#define cco_routine(co) \ for (int *_state = &(co)->cco_state, _once=1; _once; *_state = cco_state_done, _once=0) \ - _begin: switch (*_state) case 0: + _begin: switch (*_state) case 0: // thanks, @liigo! #define cco_yield(ret) \ do { \ @@ -96,7 +87,7 @@ enum { #define cco_await_2(promise, ret) \ do { \ *_state = __LINE__; \ - case __LINE__: if (!(promise)) return ret; \ + case __LINE__: if (!(promise)) {return ret; goto _begin;} \ } while (0) #define cco_await_coro(...) c_MACRO_OVERLOAD(cco_await_coro, __VA_ARGS__) @@ -150,18 +141,12 @@ typedef struct { */ #include +#include -#ifdef _WIN32 - static inline void csleep_ms(long msecs) { - extern void Sleep(unsigned long); - Sleep((unsigned long)msecs); - } -#elif _POSIX_C_SOURCE >= 199309L - static inline void csleep_ms(long msecs) { - struct timespec ts = {msecs/1000, 1000000*(msecs % 1000)}; - nanosleep(&ts, NULL); - } -#endif +static inline void csleep_us(int64_t usec) { + struct timeval tv = {.tv_sec=(int)(usec/1000000), .tv_usec=usec % 1000000}; + select(0, NULL, NULL, NULL, &tv); +} typedef struct { clock_t start; 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 #include +#include #define i_type IVec #define i_val int #include @@ -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 #include #include @@ -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() -- cgit v1.2.3 From c3e01470f45f19215fac339302d87b5d73a323e8 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 24 May 2023 19:47:16 +0200 Subject: More coro adjustments. --- include/stc/algo/coroutine.h | 17 +++++++++++------ include/stc/csview.h | 31 +++++++++++++++---------------- misc/examples/make.sh | 17 ++++++++--------- misc/examples/prime.c | 4 ++-- 4 files changed, 36 insertions(+), 33 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index ebfed613..fdce2629 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -141,12 +141,17 @@ typedef struct { */ #include -#include - -static inline void csleep_us(int64_t usec) { - struct timeval tv = {.tv_sec=(int)(usec/1000000), .tv_usec=usec % 1000000}; - select(0, NULL, NULL, NULL, &tv); -} +#if defined _WIN32 && !defined __GNUC__ + static inline void csleep_ms(long msec) { + extern void Sleep(unsigned long); + Sleep((unsigned long)msec); + } +#else + static inline void csleep_ms(long msec) { + struct timespec ts = {msec/1000, 1000000*(msec % 1000)}; + nanosleep(&ts, NULL); + } +#endif typedef struct { clock_t start; diff --git a/include/stc/csview.h b/include/stc/csview.h index 571bb278..e8a3ad9b 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -33,7 +33,12 @@ #define csview_lit(literal) c_sv_1(literal) #define csview_from_n(str, n) c_sv_2(str, n) -STC_API intptr_t csview_find_sv(csview sv, csview search); +STC_API csview_iter csview_advance(csview_iter it, intptr_t pos); +STC_API intptr_t csview_find_sv(csview sv, csview search); +STC_API uint64_t csview_hash(const csview *self); +STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); +STC_API csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n); +STC_API csview csview_token(csview sv, const char* sep, intptr_t* start); STC_INLINE csview csview_from(const char* str) { return c_LITERAL(csview){str, c_strlen(str)}; } @@ -87,15 +92,6 @@ STC_INLINE void csview_next(csview_iter* it) { it->u8.chr.size = utf8_chr_size(it->ref); if (it->ref == it->u8.end) it->ref = NULL; } -STC_INLINE csview_iter csview_advance(csview_iter it, intptr_t pos) { - int inc = -1; - if (pos > 0) pos = -pos, inc = 1; - while (pos && it.ref != it.u8.end) pos += (*(it.ref += inc) & 0xC0) != 0x80; - it.u8.chr.size = utf8_chr_size(it.ref); - if (it.ref == it.u8.end) it.ref = NULL; - return it; -} - /* utf8 */ STC_INLINE intptr_t csview_u8_size(csview sv) @@ -110,10 +106,6 @@ STC_INLINE csview csview_u8_substr(csview sv, intptr_t bytepos, intptr_t u8len) STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c { return utf8_valid_n(sv.str, sv.size); } -STC_API csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n); -STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); -STC_API csview csview_token(csview sv, const char* sep, intptr_t* start); - #define c_fortoken_sv(it, inputsv, sep) \ for (struct { csview _inp, token, *ref; const char *_sep; intptr_t pos; } \ it = {._inp=inputsv, .token=it._inp, .ref=&it.token, ._sep=sep} \ @@ -155,11 +147,18 @@ STC_INLINE int csview_icmp(const csview* x, const csview* y) STC_INLINE bool csview_eq(const csview* x, const csview* y) { return x->size == y->size && !c_memcmp(x->str, y->str, x->size); } -STC_API uint64_t csview_hash(const csview *self); - /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) +STC_DEF csview_iter csview_advance(csview_iter it, intptr_t pos) { + int inc = -1; + if (pos > 0) pos = -pos, inc = 1; + while (pos && it.ref != it.u8.end) pos += (*(it.ref += inc) & 0xC0) != 0x80; + it.u8.chr.size = utf8_chr_size(it.ref); + if (it.ref == it.u8.end) it.ref = NULL; + return it; +} + STC_DEF intptr_t csview_find_sv(csview sv, csview search) { char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); return res ? (res - sv.str) : c_NPOS; diff --git a/misc/examples/make.sh b/misc/examples/make.sh index 5c81c4d3..d58ed0cb 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -6,15 +6,14 @@ if [ "$(uname)" = 'Linux' ]; then oflag='-o ' fi -cc=gcc; cflags="-DSTC_STATIC -s -O3 -std=c99 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-implicit-fallthrough -Wno-maybe-uninitialized -Wno-missing-field-initializers" -#cc=gcc; cflags="-DSTC_STATIC -g -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" -#cc=tcc; cflags="-DSTC_STATIC -Wall -std=c99" -#cc=clang; cflags="-DSTC_STATIC -s -O3 -std=c99 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers" -#cc=gcc; cflags="-DSTC_STATIC -x c++ -s -O2 -Wall -std=c++20" -#cc=g++; cflags="-DSTC_STATIC -x c++ -s -O2 -Wall" -#cc=cl; cflags="-DSTC_STATIC -O2 -nologo -W3 -MD" -#cc=cl; cflags="-DSTC_STATIC -nologo -TP" -#cc=cl; cflags="-DSTC_STATIC -nologo -std:c11" +cc=gcc; cflags="-DSTC_STATIC -std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-maybe-uninitialized -Wno-implicit-fallthrough -Wno-missing-field-initializers" +#cc=gcc; cflags="-DSTC_STATIC -std=c99 -g -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" +#cc=tcc; cflags="-DSTC_STATIC -std=c99 -Wall" +#cc=clang; cflags="-DSTC_STATIC -std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers" +#cc=gcc; cflags="-DSTC_STATIC -x c++ -std=c++20 -O2 -s -Wall" +#cc=cl; cflags="-DSTC_STATIC -nologo -O2 -MD -W3 -wd4003" +#cc=cl; cflags="-DSTC_STATIC -nologo -TP -wd4003" +#cc=cl; cflags="-DSTC_STATIC -nologo -std:c11 -wd4003" if [ "$cc" = "cl" ]; then oflag='/Fe:' diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 7efa26ff..cb0f8926 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -32,9 +32,8 @@ int main(void) clock_t t = clock(); cbits primes = sieveOfEratosthenes(n + 1); int np = (int)cbits_count(&primes); - t = t - clock(); + t = clock() - t; - printf("Number of primes: %d, time: %f\n\n", np, (double)t/CLOCKS_PER_SEC); puts("Show all the primes in the range [2, 1000):"); printf("2"); c_forrange (i, 3, 1000, 2) @@ -49,6 +48,7 @@ int main(void) printf("%lld ", *i.ref); if (c_flt_getcount(i) % 10 == 0) puts(""); } + printf("Number of primes: %d, time: %.2f\n\n", np, (double)t/CLOCKS_PER_SEC); cbits_drop(&primes); } -- cgit v1.2.3 From 3f840ffe1c9d2df998e1cbcf6c87ea72fe23f97e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 24 May 2023 22:19:26 +0200 Subject: Fixed portability for csleep_ms() function. --- include/stc/algo/coroutine.h | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index fdce2629..2e992e55 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -56,6 +56,7 @@ int main(void) { return 0; } */ +#include #include enum { @@ -140,34 +141,34 @@ typedef struct { * Timer */ -#include -#if defined _WIN32 && !defined __GNUC__ - static inline void csleep_ms(long msec) { - extern void Sleep(unsigned long); - Sleep((unsigned long)msec); - } -#else - static inline void csleep_ms(long msec) { - struct timespec ts = {msec/1000, 1000000*(msec % 1000)}; - nanosleep(&ts, NULL); - } -#endif - typedef struct { clock_t start; clock_t interval; } ctimer; #define cco_await_timer(...) c_MACRO_OVERLOAD(cco_await_timer, __VA_ARGS__) -#define cco_await_timer_2(tm, msecs) cco_await_timer_3(tm, msecs, ) -#define cco_await_timer_3(tm, msecs, ret) \ +#define cco_await_timer_2(tm, msec) cco_await_timer_3(tm, msec, ) +#define cco_await_timer_3(tm, msec, ret) \ do { \ - ctimer_start(tm, msecs); \ + ctimer_start(tm, msec); \ cco_await_2(ctimer_expired(tm), ret); \ } while (0) -static inline void ctimer_start(ctimer* tm, long msecs) { - tm->interval = msecs*(CLOCKS_PER_SEC/1000); +#if defined _WIN32 + static inline void csleep_ms(long msec) { + extern void Sleep(unsigned long); + Sleep((unsigned long)msec); + } +#else + #include + static inline void csleep_ms(long msec) { + struct timeval tv = {.tv_sec=msec/1000, .tv_usec=1000*(msec % 1000)}; + select(0, NULL, NULL, NULL, &tv); + } +#endif + +static inline void ctimer_start(ctimer* tm, long msec) { + tm->interval = msec*(CLOCKS_PER_SEC/1000); tm->start = clock(); } -- cgit v1.2.3 From 8497b5497ecba2c2f1d368c9161ec52d4f03ae30 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 26 May 2023 19:36:27 +0200 Subject: Minor addition to coroutine API. --- include/stc/algo/coroutine.h | 6 ++++++ misc/examples/coread.c | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'include/stc/algo/coroutine.h') 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) -- cgit v1.2.3 From 7dd28530c93b907cc26064232c5498e45e838723 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 30 May 2023 20:23:09 +0200 Subject: Changed extern to dllimport for Sleep() function. --- include/stc/algo/coroutine.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index c786eb51..7f2e1244 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -157,7 +157,7 @@ typedef struct { #if defined _WIN32 static inline void csleep_ms(long msec) { - extern void Sleep(unsigned long); + __declspec(dllimport) void Sleep(unsigned long); Sleep((unsigned long)msec); } #else -- cgit v1.2.3 From c23a90112ffc50ed5977874ec31cf4fd3d4afd9b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 31 May 2023 17:38:49 +0200 Subject: Simplified coroutine API. Removed unneeded cco_await_coro() and cco_yield_coro(). --- docs/ccommon_api.md | 5 ----- include/stc/algo/coroutine.h | 11 ----------- misc/examples/cointerleave.c | 14 +++++++++----- 3 files changed, 9 insertions(+), 21 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index beaad7e9..fc4f196b 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -373,17 +373,12 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c |:----------|:-------------------------------------|:----------------------------------------| | | `cco_final:` | Label for cleanup in coroutine | | | `cco_return` | Early return from the coroutine (no arg) | -| `bool` | `cco_suspended(co)` | Is coroutine in suspended state? | | `bool` | `cco_done(co)` | Is coroutine done? | | | `cco_routine(co) { ... }` | The coroutine closure | | | `cco_yield()` | Yield/suspend execution | | | `cco_yield(ret)` | Yield/suspend execution and return ret | -| | `cco_yield_coro(co, call)` | Yield at co call if it is suspended | -| | `cco_yield_coro(co, call, ret)` | Yield at co call with ret if suspended | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | -| | `cco_await_coro(co, call)` | Await for co call to finish | -| | `cco_await_coro(co, call, ret)` | Await for co call to finish with ret | | | Semaphores: | | | | `csem` | Semaphore type | | | `cco_await_sem(sem)` | Await for the semaphore count > 0 | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 7f2e1244..486f6e23 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -65,8 +65,6 @@ 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) #define cco_routine(co) \ @@ -79,11 +77,6 @@ enum { case __LINE__:; \ } while (0) -#define cco_yield_coro(...) c_MACRO_OVERLOAD(cco_yield_coro, __VA_ARGS__) -#define cco_yield_coro_2(co, call) cco_yield_coro_3(co, call, ) -#define cco_yield_coro_3(co, call, ret) \ - do { call; if (!cco_done(co)) cco_yield(ret); } while (0) - #define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__) #define cco_await_1(promise) cco_await_2(promise, ) #define cco_await_2(promise, ret) \ @@ -92,10 +85,6 @@ enum { case __LINE__: if (!(promise)) {return ret; goto _begin;} \ } while (0) -#define cco_await_coro(...) c_MACRO_OVERLOAD(cco_await_coro, __VA_ARGS__) -#define cco_await_coro_2(co, call) cco_await_2((call, cco_done(co)), ) -#define cco_await_coro_3(co, call, ret) cco_await_2((call, cco_done(co)), ret) - #define cco_run(co, call) while (call, !cco_done(co)) #define cco_final \ diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 42bf1d32..51b9f39a 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -29,11 +29,15 @@ struct Generator { void interleaved(struct Generator* 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)); - } + do { + g->value = get_value(&g->x); + if (!cco_done(&g->x)) + cco_yield(); + + g->value = get_value(&g->y); + if (!cco_done(&g->y)) + cco_yield(); + } while (!(cco_done(&g->x) & cco_done(&g->y))); } } -- cgit v1.2.3 From c82dffc657faedba4c7af75792aa26287d9cf9bc Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 1 Jun 2023 00:14:20 +0200 Subject: Changed API for cco_timer and cco_sem. --- docs/ccommon_api.md | 25 ++++++++++++----------- include/stc/algo/coroutine.h | 47 +++++++++++++++++++++++--------------------- 2 files changed, 38 insertions(+), 34 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index fc4f196b..4d18120a 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -379,20 +379,21 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_yield(ret)` | Yield/suspend execution and return ret | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | +| | `cco_sleep(long msec)` | Sleep for milliseconds | | | Semaphores: | | -| | `csem` | Semaphore type | -| | `cco_await_sem(sem)` | Await for the semaphore count > 0 | -| | `cco_await_sem(sem, ret)` | Await with ret on the semaphore | -| | `csem_set(sem, long value)` | Set semaphore value | -| | `csem_signal(sem)` | Signal the semaphore | +| | `cco_sem` | Semaphore type | +| | `cco_sem_await(sem)` | Await for the semaphore count > 0 | +| | `cco_sem_await(sem, ret)` | Await with ret on the semaphore | +| cco_sem | `cco_sem_init(long value)` | Set semaphore value | +| | `cco_sem_release(sem)` | Signal the semaphore | | | Timers: | | -| | `ctimer` | Timer type | -| | `cco_await_timer(tm)` | Await for timer to expire | -| | `cco_await_timer(tm, ret)` | Await with ret for timer to expire | -| | `ctimer_start(tm, long msecs)` | Start timer msecs milliseconds | -| | `ctimer_restart(tm)` | Restart timer with same duration | -| `bool` | `ctimer_expired(tm)` | Return true if timer is expired | -| `long` | `ctimer_remaining(tm)` | Return milliseconds remaining | +| | `cco_timer` | Timer type | +| | `cco_timer_await(tm)` | Await for timer to expire | +| | `cco_timer_await(tm, ret)` | Await with ret for timer to expire | +| | `cco_timer_start(tm, long msecs)` | Start timer msecs milliseconds | +| | `cco_timer_restart(tm)` | Restart timer with same duration | +| `bool` | `cco_timer_expired(tm)` | Return true if timer is expired | +| `long` | `cco_timer_remaining(tm)` | Return milliseconds remaining | | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine returns `cco_end()` | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 486f6e23..d3f73229 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -114,18 +114,18 @@ enum { typedef struct { intptr_t count; -} csem; +} cco_sem; -#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, ret) \ +#define cco_sem_await(...) c_MACRO_OVERLOAD(cco_sem_await, __VA_ARGS__) +#define cco_sem_await_1(sem) cco_sem_await_2(sem, ) +#define cco_sem_await_2(sem, ret) \ do { \ cco_await_2((sem)->count > 0, ret); \ --(sem)->count; \ } while (0) -#define csem_signal(sem) ++(sem)->count -#define csem_set(sem, value) ((sem)->count = (value)) +#define cco_sem_release(sem) ++(sem)->count +#define cco_sem_with(value) ((cco_sem){value}) /* * Timer @@ -134,48 +134,51 @@ typedef struct { typedef struct { clock_t start; clock_t interval; -} ctimer; +} cco_timer; -#define cco_await_timer(...) c_MACRO_OVERLOAD(cco_await_timer, __VA_ARGS__) -#define cco_await_timer_2(tm, msec) cco_await_timer_3(tm, msec, ) -#define cco_await_timer_3(tm, msec, ret) \ +#define cco_timer_await(...) c_MACRO_OVERLOAD(cco_timer_await, __VA_ARGS__) +#define cco_timer_await_2(tm, msec) cco_timer_await_3(tm, msec, ) +#define cco_timer_await_3(tm, msec, ret) \ do { \ - ctimer_start(tm, msec); \ - cco_await_2(ctimer_expired(tm), ret); \ + cco_timer_start(tm, msec); \ + cco_await_2(cco_timer_expired(tm), ret); \ } while (0) -#if defined _WIN32 - static inline void csleep_ms(long msec) { - __declspec(dllimport) void Sleep(unsigned long); +#ifdef _WIN32 + #ifdef __cplusplus + extern "C" + #endif + __declspec(dllimport) void __stdcall Sleep(unsigned long); + static inline void cco_sleep(long msec) { Sleep((unsigned long)msec); } #else #include - static inline void csleep_ms(long msec) { + static inline void cco_sleep(long msec) { struct timeval tv = {.tv_sec=msec/1000, .tv_usec=1000*(msec % 1000)}; select(0, NULL, NULL, NULL, &tv); } #endif -static inline void ctimer_start(ctimer* tm, long msec) { +static inline void cco_timer_start(cco_timer* tm, long msec) { tm->interval = msec*(CLOCKS_PER_SEC/1000); tm->start = clock(); } -static inline ctimer ctimer_with(long msec) { - ctimer tm = {msec*(CLOCKS_PER_SEC/1000), clock()}; +static inline cco_timer cco_timer_with(long msec) { + cco_timer tm = {msec*(CLOCKS_PER_SEC/1000), clock()}; return tm; } -static inline void ctimer_restart(ctimer* tm) { +static inline void cco_timer_restart(cco_timer* tm) { tm->start = clock(); } -static inline bool ctimer_expired(ctimer* tm) { +static inline bool cco_timer_expired(cco_timer* tm) { return clock() - tm->start >= tm->interval; } -static inline long ctimer_remaining(ctimer* tm) { +static inline long cco_timer_remaining(cco_timer* tm) { return (long)((double)(tm->start + tm->interval - clock())*(1000.0/CLOCKS_PER_SEC)); } -- cgit v1.2.3 From 2d3250d2d35dda415840d8403b7b8957ca40914a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 4 Jun 2023 22:16:38 +0200 Subject: Added dining_philosophers.c coroutine example. Fixed cco_stop() when in state 0. Renamed cco_timer_with(msec) => cco_timer_from(msec) Renamed cco_sem_with(val) => cco_sem_from(val) --- include/stc/algo/coroutine.h | 15 +++--- misc/examples/dining_philosophers.c | 99 +++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 misc/examples/dining_philosophers.c (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index d3f73229..05307b08 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -98,15 +98,13 @@ enum { #define cco_stop(co) \ do { \ - int* _state = &(co)->cco_state; \ - if (*_state > 0) *_state = cco_state_final; \ + int* _s = &(co)->cco_state; \ + if (*_s > 0) *_s = cco_state_final; \ + else if (*_s == 0) *_s = cco_state_done; \ } while (0) #define cco_reset(co) \ - do { \ - int* _state = &(co)->cco_state; \ - if (*_state == cco_state_done) *_state = 0; \ - } while (0) + (void)((co)->cco_state = 0) /* * Semaphore @@ -125,7 +123,8 @@ typedef struct { } while (0) #define cco_sem_release(sem) ++(sem)->count -#define cco_sem_with(value) ((cco_sem){value}) +#define cco_sem_from(value) ((cco_sem){value}) +#define cco_sem_set(sem, value) ((sem)->count = value) /* * Timer @@ -165,7 +164,7 @@ static inline void cco_timer_start(cco_timer* tm, long msec) { tm->start = clock(); } -static inline cco_timer cco_timer_with(long msec) { +static inline cco_timer cco_timer_from(long msec) { cco_timer tm = {msec*(CLOCKS_PER_SEC/1000), clock()}; return tm; } diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c new file mode 100644 index 00000000..0bf421c6 --- /dev/null +++ b/misc/examples/dining_philosophers.c @@ -0,0 +1,99 @@ +// https://en.wikipedia.org/wiki/Dining_philosophers_problem +#include +#include +#include + +// Define the number of philosophers and forks +enum { + num_philosophers = 5, + num_forks = num_philosophers, +}; + +struct Philosopher { + int id; + cco_timer tm; + cco_sem* left_fork; + cco_sem* right_fork; + int cco_state; // required +}; + +struct Dining { + // Define semaphores for the forks + cco_sem forks[num_forks]; + struct Philosopher ph[num_philosophers]; + int ph_idx; + int cco_state; // required +}; + + +// Philosopher coroutine +void philosopher(struct Philosopher* p) +{ + cco_routine(p) { + while (1) { + int duration = (int)(1000 + crand() % 2000); // 1-3 seconds + printf("Philosopher %d is thinking for %d minutes...\n", p->id, duration/100); + cco_timer_await(&p->tm, duration); + + printf("Philosopher %d is hungry...\n", p->id); + cco_sem_await(p->left_fork); + cco_sem_await(p->right_fork); + + duration = (int)(500 + crand() % 1000); + printf("Philosopher %d is eating for %d minutes...\n", p->id, duration/100); + cco_timer_await(&p->tm, duration); + + cco_sem_release(p->left_fork); + cco_sem_release(p->right_fork); + } + + cco_final: + printf("Philosopher %d finished\n", p->id); + } +} + + +// Dining coroutine +void dining(struct Dining* d) +{ + cco_routine(d) { + for (int i = 0; i < num_forks; ++i) + cco_sem_set(&d->forks[i], 1); // all forks available + for (int i = 0; i < num_philosophers; ++i) { + cco_reset(&d->ph[i]); + d->ph[i].id = i + 1; + d->ph[i].left_fork = &d->forks[i]; + d->ph[i].right_fork = &d->forks[(i + 1) % num_forks]; + } + + while (1) { + for (d->ph_idx = 0; d->ph_idx < num_philosophers; ++d->ph_idx) { + philosopher(&d->ph[d->ph_idx]); + cco_yield(); + } + } + + cco_final: + for (int i = 0; i < num_philosophers; ++i) { + cco_stop(&d->ph[i]); + philosopher(&d->ph[i]); + } + puts("Dining finished"); + } +} + + +int main() +{ + struct Dining dine; + cco_reset(&dine); + cco_timer tm = cco_timer_from(10000); + csrand((uint64_t)time(NULL)); + + while (!cco_done(&dine)) { + if (cco_timer_expired(&tm)) + cco_stop(&dine); + dining(&dine); + cco_sleep(1); + } +} -- cgit v1.2.3 From 4b8f8d2a97ca1411ad6dccdeac6195574edac852 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 5 Jun 2023 19:02:03 +0200 Subject: Updated timers and time functions to microseconds (from milli). --- docs/ccommon_api.md | 12 +++--- include/stc/algo/coroutine.h | 79 +++++++++++++++++++++++-------------- misc/examples/dining_philosophers.c | 12 +++--- 3 files changed, 64 insertions(+), 39 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 4d18120a..6276494b 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -379,25 +379,27 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_yield(ret)` | Yield/suspend execution and return ret | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | -| | `cco_sleep(long msec)` | Sleep for milliseconds | | | Semaphores: | | | | `cco_sem` | Semaphore type | | | `cco_sem_await(sem)` | Await for the semaphore count > 0 | | | `cco_sem_await(sem, ret)` | Await with ret on the semaphore | -| cco_sem | `cco_sem_init(long value)` | Set semaphore value | +| `cco_sem` | `cco_sem_init(long value)` | Set semaphore value | | | `cco_sem_release(sem)` | Signal the semaphore | | | Timers: | | | | `cco_timer` | Timer type | | | `cco_timer_await(tm)` | Await for timer to expire | | | `cco_timer_await(tm, ret)` | Await with ret for timer to expire | -| | `cco_timer_start(tm, long msecs)` | Start timer msecs milliseconds | +| | `cco_timer_start(tm, long usec)` | Start timer for usec microseconds | | | `cco_timer_restart(tm)` | Restart timer with same duration | | `bool` | `cco_timer_expired(tm)` | Return true if timer is expired | -| `long` | `cco_timer_remaining(tm)` | Return milliseconds remaining | +|`long long`| `cco_timer_remaining(tm)` | Return microseconds remaining | | | From caller side: | | -| `void` | `cco_stop(co)` | Next call of coroutine returns `cco_end()` | +| `void` | `cco_stop(co)` | Next call of coroutine finalizes | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | | `void` | `cco_run(co, corocall) { }` | Run blocking until coro is done | +| | Time functions: | | +|`long long`| `cco_utime(void)` | Return microseconds since Epoch | +| | `cco_usleep(long long usec)` | Sleep for microseconds | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 05307b08..d0c9ad86 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -56,7 +56,6 @@ int main(void) { return 0; } */ -#include #include enum { @@ -130,55 +129,77 @@ typedef struct { * Timer */ -typedef struct { - clock_t start; - clock_t interval; -} cco_timer; - -#define cco_timer_await(...) c_MACRO_OVERLOAD(cco_timer_await, __VA_ARGS__) -#define cco_timer_await_2(tm, msec) cco_timer_await_3(tm, msec, ) -#define cco_timer_await_3(tm, msec, ret) \ - do { \ - cco_timer_start(tm, msec); \ - cco_await_2(cco_timer_expired(tm), ret); \ - } while (0) - #ifdef _WIN32 #ifdef __cplusplus - extern "C" + #define _c_LINKC extern "C" __declspec(dllimport) + #else + #define _c_LINKC __declspec(dllimport) #endif - __declspec(dllimport) void __stdcall Sleep(unsigned long); - static inline void cco_sleep(long msec) { - Sleep((unsigned long)msec); + struct _FILETIME; struct _SECURITY_ATTRIBUTES; union _LARGE_INTEGER; + _c_LINKC void GetSystemTimePreciseAsFileTime(struct _FILETIME*); + _c_LINKC void* CreateWaitableTimerW(struct _SECURITY_ATTRIBUTES*, int, const wchar_t*); + _c_LINKC int SetWaitableTimer(void*, const union _LARGE_INTEGER*, long, void(*)(void*, unsigned long, unsigned long), void*, int); + _c_LINKC unsigned long WaitForSingleObject(void*, unsigned long); + _c_LINKC int CloseHandle(void*); + + static inline long long cco_utime(void) { + static const long long epoch_offset = 11644473600000000LL; /* microseconds betweeen Jan 1,1601 - Jan 1,1970 */ + unsigned long long quad; /* 64-bit value, 100-nanosecond intervals since January 1, 1601 00:00 UTC */ + GetSystemTimePreciseAsFileTime((struct _FILETIME*)&quad); + return (long long)quad/10 - epoch_offset; /* microseconds since epoch */ } + + static inline void cco_usleep(long long usec) { + unsigned long long ft = -10*usec; + void* timer = CreateWaitableTimerW(NULL, true, NULL); + SetWaitableTimer(timer, (const union _LARGE_INTEGER*)&ft, 0, NULL, NULL, 0); + WaitForSingleObject(timer, ~0UL); + CloseHandle(timer); + } #else #include - static inline void cco_sleep(long msec) { - struct timeval tv = {.tv_sec=msec/1000, .tv_usec=1000*(msec % 1000)}; + static inline long long cco_utime(void) { + struct timeval tv; + gettimeofday(&tv, NULL); + return tv.tv_sec*1000000LL + tv.tv_usec; + } + + static inline void cco_usleep(long long usec) { + struct timeval tv = {.tv_sec=(time_t)(usec/1000000), .tv_usec=(suseconds_t)(usec % 1000000)}; select(0, NULL, NULL, NULL, &tv); } #endif -static inline void cco_timer_start(cco_timer* tm, long msec) { - tm->interval = msec*(CLOCKS_PER_SEC/1000); - tm->start = clock(); +typedef struct { long long interval, start; } cco_timer; + +#define cco_timer_await(...) c_MACRO_OVERLOAD(cco_timer_await, __VA_ARGS__) +#define cco_timer_await_2(tm, usec) cco_timer_await_3(tm, usec, ) +#define cco_timer_await_3(tm, usec, ret) \ + do { \ + cco_timer_start(tm, usec); \ + cco_await_2(cco_timer_expired(tm), ret); \ + } while (0) + +static inline void cco_timer_start(cco_timer* tm, long long usec) { + tm->interval = usec; + tm->start = cco_utime(); } -static inline cco_timer cco_timer_from(long msec) { - cco_timer tm = {msec*(CLOCKS_PER_SEC/1000), clock()}; +static inline cco_timer cco_timer_from(long long usec) { + cco_timer tm = {.interval=usec, .start=cco_utime()}; return tm; } static inline void cco_timer_restart(cco_timer* tm) { - tm->start = clock(); + tm->start = cco_utime(); } static inline bool cco_timer_expired(cco_timer* tm) { - return clock() - tm->start >= tm->interval; + return cco_utime() - tm->start >= tm->interval; } -static inline long cco_timer_remaining(cco_timer* tm) { - return (long)((double)(tm->start + tm->interval - clock())*(1000.0/CLOCKS_PER_SEC)); +static inline long long cco_timer_remaining(cco_timer* tm) { + return tm->start + tm->interval - cco_utime(); } #endif diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c index 0bf421c6..cc6e5fd2 100644 --- a/misc/examples/dining_philosophers.c +++ b/misc/examples/dining_philosophers.c @@ -1,5 +1,6 @@ // https://en.wikipedia.org/wiki/Dining_philosophers_problem #include +#include #include #include @@ -33,7 +34,7 @@ void philosopher(struct Philosopher* p) while (1) { int duration = (int)(1000 + crand() % 2000); // 1-3 seconds printf("Philosopher %d is thinking for %d minutes...\n", p->id, duration/100); - cco_timer_await(&p->tm, duration); + cco_timer_await(&p->tm, duration*1000); printf("Philosopher %d is hungry...\n", p->id); cco_sem_await(p->left_fork); @@ -41,7 +42,7 @@ void philosopher(struct Philosopher* p) duration = (int)(500 + crand() % 1000); printf("Philosopher %d is eating for %d minutes...\n", p->id, duration/100); - cco_timer_await(&p->tm, duration); + cco_timer_await(&p->tm, duration*1000); cco_sem_release(p->left_fork); cco_sem_release(p->right_fork); @@ -67,10 +68,11 @@ void dining(struct Dining* d) } while (1) { + // per-"frame" logic update of all philosophers states for (d->ph_idx = 0; d->ph_idx < num_philosophers; ++d->ph_idx) { philosopher(&d->ph[d->ph_idx]); - cco_yield(); } + cco_yield(); // suspend, return control back to main } cco_final: @@ -87,13 +89,13 @@ int main() { struct Dining dine; cco_reset(&dine); - cco_timer tm = cco_timer_from(10000); + cco_timer tm = cco_timer_from(10*1000000); // microseconds csrand((uint64_t)time(NULL)); while (!cco_done(&dine)) { if (cco_timer_expired(&tm)) cco_stop(&dine); dining(&dine); - cco_sleep(1); + cco_usleep(100); } } -- cgit v1.2.3 From f2d90c87590133547e474da4ea9d5dd1b834043e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 6 Jun 2023 01:36:54 +0200 Subject: Switched to double for times in cco_timer and cco_time, etc. Reverted to just use Sleep on win32 - same effect. --- docs/ccommon_api.md | 8 ++--- include/stc/algo/coroutine.h | 63 +++++++++++++++++-------------------- misc/examples/dining_philosophers.c | 20 ++++++------ 3 files changed, 44 insertions(+), 47 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 6276494b..de421f2c 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -389,17 +389,17 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_timer` | Timer type | | | `cco_timer_await(tm)` | Await for timer to expire | | | `cco_timer_await(tm, ret)` | Await with ret for timer to expire | -| | `cco_timer_start(tm, long usec)` | Start timer for usec microseconds | +| | `cco_timer_start(tm, double sec)` | Start timer for sec seconds | | | `cco_timer_restart(tm)` | Restart timer with same duration | | `bool` | `cco_timer_expired(tm)` | Return true if timer is expired | -|`long long`| `cco_timer_remaining(tm)` | Return microseconds remaining | +| `double` | `cco_timer_remaining(tm)` | Return seconds remaining | | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine finalizes | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | | `void` | `cco_run(co, corocall) { }` | Run blocking until coro is done | | | Time functions: | | -|`long long`| `cco_utime(void)` | Return microseconds since Epoch | -| | `cco_usleep(long long usec)` | Sleep for microseconds | +| `double` | `cco_time(void)` | Return secs with usec prec. since Epoch | +| | `cco_sleep(double sec)` | Sleep for seconds | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index d0c9ad86..79819c55 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -135,71 +135,66 @@ typedef struct { #else #define _c_LINKC __declspec(dllimport) #endif - struct _FILETIME; struct _SECURITY_ATTRIBUTES; union _LARGE_INTEGER; + struct _FILETIME; _c_LINKC void GetSystemTimePreciseAsFileTime(struct _FILETIME*); - _c_LINKC void* CreateWaitableTimerW(struct _SECURITY_ATTRIBUTES*, int, const wchar_t*); - _c_LINKC int SetWaitableTimer(void*, const union _LARGE_INTEGER*, long, void(*)(void*, unsigned long, unsigned long), void*, int); - _c_LINKC unsigned long WaitForSingleObject(void*, unsigned long); - _c_LINKC int CloseHandle(void*); - - static inline long long cco_utime(void) { - static const long long epoch_offset = 11644473600000000LL; /* microseconds betweeen Jan 1,1601 - Jan 1,1970 */ - unsigned long long quad; /* 64-bit value, 100-nanosecond intervals since January 1, 1601 00:00 UTC */ + _c_LINKC void Sleep(unsigned long); + + static inline double cco_time(void) { + static const unsigned long long epoch_offset = 116444736000000000ULL; /* 1/10th usecs betweeen Jan 1,1601 - Jan 1,1970 */ + unsigned long long quad; /* 64-bit value, 100-nanosecond intervals since January 1, 1601 00:00 UTC */ GetSystemTimePreciseAsFileTime((struct _FILETIME*)&quad); - return (long long)quad/10 - epoch_offset; /* microseconds since epoch */ + return (double)(quad - epoch_offset)*1e-7; /* usecs since epoch */ } - static inline void cco_usleep(long long usec) { - unsigned long long ft = -10*usec; - void* timer = CreateWaitableTimerW(NULL, true, NULL); - SetWaitableTimer(timer, (const union _LARGE_INTEGER*)&ft, 0, NULL, NULL, 0); - WaitForSingleObject(timer, ~0UL); - CloseHandle(timer); - } + static inline void cco_sleep(double sec) { + Sleep((unsigned long)(sec*1000.0)); + } #else #include - static inline long long cco_utime(void) { + static inline double cco_time(void) { struct timeval tv; gettimeofday(&tv, NULL); - return tv.tv_sec*1000000LL + tv.tv_usec; + return tv.tv_sec + tv.tv_usec*1e-6; } - static inline void cco_usleep(long long usec) { - struct timeval tv = {.tv_sec=(time_t)(usec/1000000), .tv_usec=(suseconds_t)(usec % 1000000)}; + static inline void cco_sleep(double sec) { + struct timeval tv; + tv.tv_sec = (time_t)sec; + tv.tv_usec = (suseconds_t)(1e6*(sec - tv.tv_sec)); select(0, NULL, NULL, NULL, &tv); } #endif -typedef struct { long long interval, start; } cco_timer; +typedef struct { double interval, start; } cco_timer; #define cco_timer_await(...) c_MACRO_OVERLOAD(cco_timer_await, __VA_ARGS__) -#define cco_timer_await_2(tm, usec) cco_timer_await_3(tm, usec, ) -#define cco_timer_await_3(tm, usec, ret) \ +#define cco_timer_await_2(tm, sec) cco_timer_await_3(tm, sec, ) +#define cco_timer_await_3(tm, sec, ret) \ do { \ - cco_timer_start(tm, usec); \ + cco_timer_start(tm, sec); \ cco_await_2(cco_timer_expired(tm), ret); \ } while (0) -static inline void cco_timer_start(cco_timer* tm, long long usec) { - tm->interval = usec; - tm->start = cco_utime(); +static inline void cco_timer_start(cco_timer* tm, double sec) { + tm->interval = sec; + tm->start = cco_time(); } -static inline cco_timer cco_timer_from(long long usec) { - cco_timer tm = {.interval=usec, .start=cco_utime()}; +static inline cco_timer cco_timer_from(double sec) { + cco_timer tm = {.interval=sec, .start=cco_time()}; return tm; } static inline void cco_timer_restart(cco_timer* tm) { - tm->start = cco_utime(); + tm->start = cco_time(); } static inline bool cco_timer_expired(cco_timer* tm) { - return cco_utime() - tm->start >= tm->interval; + return cco_time() - tm->start >= tm->interval; } -static inline long long cco_timer_remaining(cco_timer* tm) { - return tm->start + tm->interval - cco_utime(); +static inline double cco_timer_remaining(cco_timer* tm) { + return tm->start + tm->interval - cco_time(); } #endif diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c index cc6e5fd2..57fcef56 100644 --- a/misc/examples/dining_philosophers.c +++ b/misc/examples/dining_philosophers.c @@ -32,17 +32,17 @@ void philosopher(struct Philosopher* p) { cco_routine(p) { while (1) { - int duration = (int)(1000 + crand() % 2000); // 1-3 seconds - printf("Philosopher %d is thinking for %d minutes...\n", p->id, duration/100); - cco_timer_await(&p->tm, duration*1000); + double duration = 1.0 + crandf()*2.0; + printf("Philosopher %d is thinking for %.0f minutes...\n", p->id, duration*10); + cco_timer_await(&p->tm, duration); printf("Philosopher %d is hungry...\n", p->id); cco_sem_await(p->left_fork); cco_sem_await(p->right_fork); - duration = (int)(500 + crand() % 1000); - printf("Philosopher %d is eating for %d minutes...\n", p->id, duration/100); - cco_timer_await(&p->tm, duration*1000); + duration = 0.5 + crandf(); + printf("Philosopher %d is eating for %.0f minutes...\n", p->id, duration*10); + cco_timer_await(&p->tm, duration); cco_sem_release(p->left_fork); cco_sem_release(p->right_fork); @@ -84,18 +84,20 @@ void dining(struct Dining* d) } } - int main() { struct Dining dine; cco_reset(&dine); - cco_timer tm = cco_timer_from(10*1000000); // microseconds + int n=0; + cco_timer tm = cco_timer_from(10.0); // seconds csrand((uint64_t)time(NULL)); while (!cco_done(&dine)) { if (cco_timer_expired(&tm)) cco_stop(&dine); dining(&dine); - cco_usleep(100); + cco_sleep(0.001); + ++n; } + printf("n=%d\n", n); } -- cgit v1.2.3 From 8e6e1d2b266e46e3920edf7cc6eaad33c1312880 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 6 Jun 2023 11:42:18 +0200 Subject: Warning fixes and docs update. --- docs/ccommon_api.md | 4 ++-- include/stc/algo/coroutine.h | 10 +++++----- include/stc/algo/sort.h | 10 +++++----- 3 files changed, 12 insertions(+), 12 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index de421f2c..7aa94c50 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -389,7 +389,7 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_timer` | Timer type | | | `cco_timer_await(tm)` | Await for timer to expire | | | `cco_timer_await(tm, ret)` | Await with ret for timer to expire | -| | `cco_timer_start(tm, double sec)` | Start timer for sec seconds | +| | `cco_timer_start(tm, double sec)` | Start timer for sec seconds (usec prec.)| | | `cco_timer_restart(tm)` | Restart timer with same duration | | `bool` | `cco_timer_expired(tm)` | Return true if timer is expired | | `double` | `cco_timer_remaining(tm)` | Return seconds remaining | @@ -399,7 +399,7 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | `void` | `cco_run(co, corocall) { }` | Run blocking until coro is done | | | Time functions: | | | `double` | `cco_time(void)` | Return secs with usec prec. since Epoch | -| | `cco_sleep(double sec)` | Sleep for seconds | +| | `cco_sleep(double sec)` | Sleep for seconds (msec or usec prec.) | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 79819c55..1ac30fff 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -139,11 +139,11 @@ typedef struct { _c_LINKC void GetSystemTimePreciseAsFileTime(struct _FILETIME*); _c_LINKC void Sleep(unsigned long); - static inline double cco_time(void) { + static inline double cco_time(void) { /* seconds since epoch */ static const unsigned long long epoch_offset = 116444736000000000ULL; /* 1/10th usecs betweeen Jan 1,1601 - Jan 1,1970 */ unsigned long long quad; /* 64-bit value, 100-nanosecond intervals since January 1, 1601 00:00 UTC */ GetSystemTimePreciseAsFileTime((struct _FILETIME*)&quad); - return (double)(quad - epoch_offset)*1e-7; /* usecs since epoch */ + return (double)(quad - epoch_offset)*1e-7; } static inline void cco_sleep(double sec) { @@ -151,16 +151,16 @@ typedef struct { } #else #include - static inline double cco_time(void) { + static inline double cco_time(void) { /* seconds since epoch */ struct timeval tv; gettimeofday(&tv, NULL); - return tv.tv_sec + tv.tv_usec*1e-6; + return (double)tv.tv_sec + (double)tv.tv_usec*1e-6; } static inline void cco_sleep(double sec) { struct timeval tv; tv.tv_sec = (time_t)sec; - tv.tv_usec = (suseconds_t)(1e6*(sec - tv.tv_sec)); + tv.tv_usec = (suseconds_t)((sec - (double)(long)sec)*1e6); select(0, NULL, NULL, NULL, &tv); } #endif diff --git a/include/stc/algo/sort.h b/include/stc/algo/sort.h index 20b7e1b3..bbd58427 100644 --- a/include/stc/algo/sort.h +++ b/include/stc/algo/sort.h @@ -24,7 +24,7 @@ template params: #define i_val - value type [required] #define i_less - less function. default: *x < *y -#define i_tag name - define namearray_qsort(). i_tag defaults {i_val} +#define i_type name - define {{name}}_sort_n(), else {{i_val}}array_sort_n(). // test: #include @@ -32,12 +32,12 @@ template params: #include int main() { - int arr[] = {23, 321, 5434, 25, 245, 1, 654, 33, 543, 21}; + int nums[] = {23, 321, 5434, 25, 245, 1, 654, 33, 543, 21}; - intarray_qsort(arr, c_arraylen(arr)); + intarray_sort_n(nums, c_arraylen(arr)); - for (int i = 0; i < c_arraylen(arr); i++) - printf(" %d", arr[i]); + for (int i = 0; i < c_arraylen(nums); i++) + printf(" %d", nums[i]); puts(""); } */ -- cgit v1.2.3 From 7c57f4fb7edf33d030975a04160f183f71c48ecd Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 6 Jun 2023 18:13:18 +0200 Subject: Fixed some logic in coroutines. Changed API in c11/print.h (not officially part of STC as it is C11). --- docs/ccommon_api.md | 3 +- docs/cspan_api.md | 13 ++++--- include/c11/print.h | 87 ++++++++++++++++++++++---------------------- include/stc/algo/coroutine.h | 12 ++++-- 4 files changed, 61 insertions(+), 54 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 7aa94c50..1fd8af75 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -372,13 +372,14 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | Function / operator | Description | |:----------|:-------------------------------------|:----------------------------------------| | | `cco_final:` | Label for cleanup in coroutine | -| | `cco_return` | Early return from the coroutine (no arg) | | `bool` | `cco_done(co)` | Is coroutine done? | | | `cco_routine(co) { ... }` | The coroutine closure | | | `cco_yield()` | Yield/suspend execution | | | `cco_yield(ret)` | Yield/suspend execution and return ret | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | +| | `cco_return` | Execute final cleanup, enter done-state | +| | `cco_return_v(val)` | Yield final value, enter final-state | | | Semaphores: | | | | `cco_sem` | Semaphore type | | | `cco_sem_await(sem)` | Await for the semaphore count > 0 | diff --git a/docs/cspan_api.md b/docs/cspan_api.md index ec203460..f0c6babd 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -136,6 +136,7 @@ int main() { ## Example 2 Slicing cspan without and with reducing the rank: ```c +#define i_implement #include #include @@ -154,7 +155,7 @@ int main() puts("\niterate span2 flat:"); c_foreach (i, Span2, span2) - print(" {}", *i.ref); + fmt_print(" {}", *i.ref); puts(""); // slice without reducing rank: @@ -164,8 +165,8 @@ int main() c_forrange (i, ss3.shape[0]) { c_forrange (j, ss3.shape[1]) { c_forrange (k, ss3.shape[2]) - print(" {:2}", *cspan_at(&ss3, i, j, k)); - print(" |"); + fmt_print(" {:2}", *cspan_at(&ss3, i, j, k)); + fmt_print(" |"); } } // slice and reduce rank: @@ -174,13 +175,13 @@ int main() puts("\niterate ss2 by dimensions:"); c_forrange (i, ss2.shape[0]) { c_forrange (j, ss2.shape[1]) - print(" {:2}", *cspan_at(&ss2, i, j)); - print(" |"); + fmt_print(" {:2}", *cspan_at(&ss2, i, j)); + fmt_print(" |"); } puts("\niterate ss2 flat:"); c_foreach (i, Span2, ss2) - print(" {:2}", *i.ref); + fmt_print(" {:2}", *i.ref); puts(""); } ``` diff --git a/include/c11/print.h b/include/c11/print.h index 7c155875..ee0d8151 100644 --- a/include/c11/print.h +++ b/include/c11/print.h @@ -1,7 +1,7 @@ #ifndef FMT_H_INCLUDED #define FMT_H_INCLUDED /* -VER 2.1: NEW API: +VER 2.2: NEW API: void print(fmt, ...); void println(fmt, ...); void printd(dest, fmt, ...); @@ -9,28 +9,31 @@ void printd(dest, fmt, ...); void fmt_print(fmt, ...); void fmt_println(fmt, ...); void fmt_printd(dest, fmt, ...); -void fmt_destroy(fmt_buffer* buf); +void fmt_close(fmt_stream* ss); dest - destination, one of: FILE* fp Write to a file char* strbuf Write to a pre-allocated string buffer - fmt_buffer* buf Auto realloc the needed memory (safe). - Set buf->stream=1 for stream-mode. - Call fmt_destroy(buf) after usage. + fmt_stream* ss Write to a string-stream (auto allocated). + Set ss->overwrite=1 for overwrite-mode. + Call fmt_close(ss) after usage. fmt - format string {} Auto-detected format. If :MOD is not specified, float will use ".8g" format, and double ".16g". - {:MOD} Format modifiers: < left align (replaces -), default for char*, char. - > right align, default for numbers. - Other than that MOD can be normal printf format modifiers. - {{, }} Print chars {, and }. (note: a single % prints %). + {:MODS} Format modifiers: '<' left align (replaces '-'). Default for char* and char. + '>' right align. Default for numbers. + Other than that MODS can be regular printf() format modifiers. + {{ }} % Print the '{', '}', and '%' characters. * C11 or higher required. * MAX 255 chars fmt string by default. MAX 12 arguments after fmt string. -* Static linking by default, shared symbols by defining FMT_HEADER / FMT_IMPLEMENT. +* Define FMT_IMPLEMENT or i_implement prior to #include in one translation unit. +* Define FMT_SHORTS to define print(), println() and printd() macros, without fmt_ prefix. * (c) operamint, 2022, MIT License. ----------------------------------------------------------------------------------- +#define FMT_IMPLEMENT +#define FMT_SHORTS #include "c11/print.h" int main() { @@ -50,15 +53,15 @@ int main() { printd(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World"); printd(stderr, "100%: {:<20} {:.*} {}\n", string, 4, pi, x); printd(buffer, "Precision: {} {:.10} {}", string, pi, x); - println("{}", buffer); - println("Vector: ({}, {}, {})", 3.2, 3.3, pi); + fmt_println("{}", buffer); + fmt_println("Vector: ({}, {}, {})", 3.2, 3.3, pi); - fmt_buffer out[1] = {{.stream=1}}; - printd(out, "{} {}", "Pi is:", pi); - print("{}, len={}, cap={}\n", out->data, out->len, out->cap); - printd(out, "{} {}", ", Pi squared is:", pi*pi); - print("{}, len={}, cap={}\n", out->data, out->len, out->cap); - fmt_destroy(out); + fmt_stream ss[1] = {0}; + printd(ss, "{} {}", "Pi is:", pi); + print("{}, len={}, cap={}\n", ss->data, ss->len, ss->cap); + printd(ss, "{} {}", ", Pi squared is:", pi*pi); + print("{}, len={}, cap={}\n", ss->data, ss->len, ss->cap); + fmt_close(ss); } */ #include @@ -77,11 +80,6 @@ int main() { #define _fmt_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, \ _14, _15, _16, N, ...) N -#if defined FMT_HEADER || defined FMT_IMPLEMENT -# define FMT_API -#else -# define FMT_API static inline -#endif #if defined FMT_NDEBUG || defined NDEBUG # define fmt_OK(exp) (void)(exp) #else @@ -91,25 +89,25 @@ int main() { typedef struct { char* data; intptr_t cap, len; - _Bool stream; -} fmt_buffer; + _Bool overwrite; +} fmt_stream; -FMT_API void fmt_destroy(fmt_buffer* buf); -FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...); -FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); +void fmt_close(fmt_stream* ss); +int _fmt_parse(char* p, int nargs, const char *fmt, ...); +void _fmt_bprint(fmt_stream*, const char* fmt, ...); #ifndef FMT_MAX #define FMT_MAX 256 #endif -#ifndef FMT_NOSHORTS +#ifdef FMT_SHORTS #define print(...) fmt_printd(stdout, __VA_ARGS__) -#define println(...) fmt_printd((fmt_buffer*)0, __VA_ARGS__) +#define println(...) fmt_printd((fmt_stream*)0, __VA_ARGS__) #define printd fmt_printd #endif #define fmt_print(...) fmt_printd(stdout, __VA_ARGS__) -#define fmt_println(...) fmt_printd((fmt_buffer*)0, __VA_ARGS__) +#define fmt_println(...) fmt_printd((fmt_stream*)0, __VA_ARGS__) #define fmt_printd(...) fmt_OVERLOAD(fmt_printd, __VA_ARGS__) /* Primary function. */ @@ -161,7 +159,7 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); #define _fmt_fn(x) _Generic ((x), \ FILE*: fprintf, \ char*: sprintf, \ - fmt_buffer*: _fmt_bprint) + fmt_stream*: _fmt_bprint) #if defined(_MSC_VER) && !defined(__clang__) # define _signed_char_hhd @@ -192,38 +190,38 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...); const wchar_t*: "ls", \ const void*: "p") -#if defined FMT_IMPLEMENT || !(defined FMT_HEADER || defined FMT_IMPLEMENT) +#if defined FMT_IMPLEMENT || defined i_implement #include #include #include -FMT_API void fmt_destroy(fmt_buffer* buf) { - free(buf->data); +void fmt_close(fmt_stream* ss) { + free(ss->data); } -FMT_API void _fmt_bprint(fmt_buffer* buf, const char* fmt, ...) { +void _fmt_bprint(fmt_stream* ss, const char* fmt, ...) { va_list args, args2; va_start(args, fmt); - if (buf == NULL) { + if (ss == NULL) { vprintf(fmt, args); putchar('\n'); goto done1; } va_copy(args2, args); const int n = vsnprintf(NULL, 0U, fmt, args); if (n < 0) goto done2; - const intptr_t pos = buf->stream ? buf->len : 0; - buf->len = pos + n; - if (buf->len > buf->cap) { - buf->cap = buf->len + buf->cap/2; - buf->data = (char*)realloc(buf->data, (size_t)buf->cap + 1U); + const intptr_t pos = ss->overwrite ? 0 : ss->len; + ss->len = pos + n; + if (ss->len > ss->cap) { + ss->cap = ss->len + ss->cap/2; + ss->data = (char*)realloc(ss->data, (size_t)ss->cap + 1U); } - vsprintf(buf->data + pos, fmt, args2); + vsprintf(ss->data + pos, fmt, args2); done2: va_end(args2); done1: va_end(args); } -FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...) { +int _fmt_parse(char* p, int nargs, const char *fmt, ...) { char *arg, *p0, ch; int n = 0, empty; va_list args; @@ -273,3 +271,4 @@ FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...) { } #endif #endif +#undef i_implement \ No newline at end of file diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 1ac30fff..f03fc836 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -87,13 +87,19 @@ enum { #define cco_run(co, call) while (call, !cco_done(co)) #define cco_final \ - case cco_state_final + *_state = cco_state_done; case cco_state_final #define cco_return \ - do { *_state = cco_state_final; goto _begin; } while (0) + do { \ + *_state = *_state < 0 ? cco_state_done : cco_state_final; \ + goto _begin; \ + } while (0) #define cco_return_v(value) \ - return (*_state = cco_state_final, value) + do { \ + *_state = *_state < 0 ? cco_state_done : cco_state_final; \ + return value; \ + } while (0) #define cco_stop(co) \ do { \ -- cgit v1.2.3 From 56469c2738effe6d44a3a0c44e821c0ff18ce28e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 7 Jun 2023 10:35:38 +0200 Subject: cco: Minor internal cleanup + added cco_timer_elapsed(). --- docs/ccommon_api.md | 9 +++++---- include/stc/algo/coroutine.h | 27 ++++++++++++++------------- misc/examples/dining_philosophers.c | 7 +++---- 3 files changed, 22 insertions(+), 21 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 1fd8af75..930b8881 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -385,14 +385,15 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_sem_await(sem)` | Await for the semaphore count > 0 | | | `cco_sem_await(sem, ret)` | Await with ret on the semaphore | | `cco_sem` | `cco_sem_init(long value)` | Set semaphore value | -| | `cco_sem_release(sem)` | Signal the semaphore | +| | `cco_sem_release(sem)` | Signal the semaphore (count += 1) | | | Timers: | | | | `cco_timer` | Timer type | -| | `cco_timer_await(tm)` | Await for timer to expire | -| | `cco_timer_await(tm, ret)` | Await with ret for timer to expire | -| | `cco_timer_start(tm, double sec)` | Start timer for sec seconds (usec prec.)| +| | `cco_timer_await(tm, double sec)` | Await secs for timer to expire (usec prec.)| +| | `cco_timer_await(tm, double sec, ret)`| Await secs for timer with ret value | +| | `cco_timer_start(tm, double sec)` | Start timer for secs duration | | | `cco_timer_restart(tm)` | Restart timer with same duration | | `bool` | `cco_timer_expired(tm)` | Return true if timer is expired | +| `double` | `cco_timer_elapsed(tm)` | Return seconds elapsed | | `double` | `cco_timer_remaining(tm)` | Return seconds remaining | | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine finalizes | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index f03fc836..81c75aa1 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -67,12 +67,12 @@ enum { #define cco_done(co) ((co)->cco_state == cco_state_done) #define cco_routine(co) \ - for (int *_state = &(co)->cco_state, _once=1; _once; *_state = cco_state_done, _once=0) \ - _begin: switch (*_state) case 0: // thanks, @liigo! + for (int *_state = &(co)->cco_state; *_state != cco_state_done; *_state = cco_state_done) \ + _resume: switch (*_state) case 0: // thanks, @liigo! #define cco_yield(ret) \ do { \ - *_state = __LINE__; return ret; goto _begin; \ + *_state = __LINE__; return ret; goto _resume; \ case __LINE__:; \ } while (0) @@ -81,7 +81,7 @@ enum { #define cco_await_2(promise, ret) \ do { \ *_state = __LINE__; \ - case __LINE__: if (!(promise)) {return ret; goto _begin;} \ + case __LINE__: if (!(promise)) {return ret; goto _resume;} \ } while (0) #define cco_run(co, call) while (call, !cco_done(co)) @@ -91,13 +91,13 @@ enum { #define cco_return \ do { \ - *_state = *_state < 0 ? cco_state_done : cco_state_final; \ - goto _begin; \ + *_state = *_state >= 0 ? cco_state_final : cco_state_done; \ + goto _resume; \ } while (0) #define cco_return_v(value) \ do { \ - *_state = *_state < 0 ? cco_state_done : cco_state_final; \ + *_state = *_state >= 0 ? cco_state_final : cco_state_done; \ return value; \ } while (0) @@ -115,9 +115,7 @@ enum { * Semaphore */ -typedef struct { - intptr_t count; -} cco_sem; +typedef struct { intptr_t count; } cco_sem; #define cco_sem_await(...) c_MACRO_OVERLOAD(cco_sem_await, __VA_ARGS__) #define cco_sem_await_1(sem) cco_sem_await_2(sem, ) @@ -146,10 +144,9 @@ typedef struct { _c_LINKC void Sleep(unsigned long); static inline double cco_time(void) { /* seconds since epoch */ - static const unsigned long long epoch_offset = 116444736000000000ULL; /* 1/10th usecs betweeen Jan 1,1601 - Jan 1,1970 */ - unsigned long long quad; /* 64-bit value, 100-nanosecond intervals since January 1, 1601 00:00 UTC */ + unsigned long long quad; /* 64-bit value representing 1/10th usecs since Jan 1 1601, 00:00 UTC */ GetSystemTimePreciseAsFileTime((struct _FILETIME*)&quad); - return (double)(quad - epoch_offset)*1e-7; + return (double)(quad - 116444736000000000ULL)*1e-7; /* time diff Jan 1 1601-Jan 1 1970 in 1/10th usecs */ } static inline void cco_sleep(double sec) { @@ -199,6 +196,10 @@ static inline bool cco_timer_expired(cco_timer* tm) { return cco_time() - tm->start >= tm->interval; } +static inline double cco_timer_elapsed(cco_timer* tm) { + return cco_time() - tm->start; +} + static inline double cco_timer_remaining(cco_timer* tm) { return tm->start + tm->interval - cco_time(); } diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c index 57fcef56..f9c05e71 100644 --- a/misc/examples/dining_philosophers.c +++ b/misc/examples/dining_philosophers.c @@ -22,7 +22,6 @@ struct Dining { // Define semaphores for the forks cco_sem forks[num_forks]; struct Philosopher ph[num_philosophers]; - int ph_idx; int cco_state; // required }; @@ -43,7 +42,7 @@ void philosopher(struct Philosopher* p) duration = 0.5 + crandf(); printf("Philosopher %d is eating for %.0f minutes...\n", p->id, duration*10); cco_timer_await(&p->tm, duration); - + cco_sem_release(p->left_fork); cco_sem_release(p->right_fork); } @@ -69,8 +68,8 @@ void dining(struct Dining* d) while (1) { // per-"frame" logic update of all philosophers states - for (d->ph_idx = 0; d->ph_idx < num_philosophers; ++d->ph_idx) { - philosopher(&d->ph[d->ph_idx]); + for (int i = 0; i < num_philosophers; ++i) { + philosopher(&d->ph[i]); } cco_yield(); // suspend, return control back to main } -- cgit v1.2.3 From abd3b4372dee2291a81271f02588228279139960 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Thu, 8 Jun 2023 06:55:42 +0200 Subject: More small adjustments. --- docs/ccommon_api.md | 2 +- include/stc/algo/coroutine.h | 2 +- include/stc/cstr.h | 3 ++- misc/benchmarks/plotbench/plot.py | 4 ++-- misc/examples/triples.c | 10 +++++----- 5 files changed, 11 insertions(+), 10 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 930b8881..cd9be505 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -378,7 +378,7 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_yield(ret)` | Yield/suspend execution and return ret | | | `cco_await(promise)` | Await/suspend until promise is true | | | `cco_await(promise, ret)` | Await/suspend with ret value | -| | `cco_return` | Execute final cleanup, enter done-state | +| | `cco_return` | Replaces return. Jump to cco_final: if exist| | | `cco_return_v(val)` | Yield final value, enter final-state | | | Semaphores: | | | | `cco_sem` | Semaphore type | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 81c75aa1..67ea5a40 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -87,7 +87,7 @@ enum { #define cco_run(co, call) while (call, !cco_done(co)) #define cco_final \ - *_state = cco_state_done; case cco_state_final + *_state = cco_state_final; case cco_state_final #define cco_return \ do { \ diff --git a/include/stc/cstr.h b/include/stc/cstr.h index d496b85e..03eefd2f 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -504,7 +504,8 @@ STC_DEF char* cstr_reserve(cstr* self, const intptr_t cap) { if (cap > cstr_s_cap) { char* data = (char *)c_malloc(cap + 1); const intptr_t len = cstr_s_size(self); - c_memcpy(data, self->sml.data, cstr_s_cap + 1); + /* copy full short buffer to emulate realloc() */ + c_memcpy(data, self->sml.data, cstr_s_cap + 2); self->lon.data = data; self->lon.size = (size_t)len; cstr_l_set_cap(self, cap); diff --git a/misc/benchmarks/plotbench/plot.py b/misc/benchmarks/plotbench/plot.py index 0ba92264..e65631b7 100644 --- a/misc/benchmarks/plotbench/plot.py +++ b/misc/benchmarks/plotbench/plot.py @@ -12,8 +12,8 @@ df = df[df.Method != 'total'] if n > 0: df = df[df.Compiler == comp[n]] -g = sns.catplot(data=df, x='Method', y='Seconds', hue='Library', col='C', kind='bar', - ci=68, legend=False, col_wrap=2, sharex=False, aspect=1.4, height=3.1) +g = sns.catplot(data=df, x='Method', y='Seconds', hue='Library', col='C', kind='bar', orient='v', + errorbar=('ci', 68), legend=False, col_wrap=2, sharex=False, aspect=1.4, height=3.1) g.set_xlabels('') g.add_legend(bbox_to_anchor=(0.75, 0.2), borderaxespad=0.) diff --git a/misc/examples/triples.c b/misc/examples/triples.c index 06142916..17e3d40b 100644 --- a/misc/examples/triples.c +++ b/misc/examples/triples.c @@ -32,7 +32,7 @@ struct triples { int cco_state; }; -bool triples_coro(struct triples* t) { +void triples_coro(struct triples* t) { cco_routine(t) { t->count = 0; for (t->c = 5; t->size; ++t->c) { @@ -41,15 +41,14 @@ bool triples_coro(struct triples* t) { if ((int64_t)t->a*t->a + (int64_t)t->b*t->b == (int64_t)t->c*t->c) { if (t->count++ == t->size) cco_return; - cco_yield(false); + cco_yield(); } } } } - cco_final: + cco_final: puts("done"); } - return true; } int main() @@ -61,7 +60,8 @@ int main() struct triples t = {INT32_MAX}; int n = 0; - while (!triples_coro(&t)) { + while (!cco_done(&t)) { + triples_coro(&t); if (gcd(t.a, t.b) > 1) continue; if (t.c < 100) -- cgit v1.2.3 From b564ef6bdfcd2437f1b4997f42054c45ccdedbb1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 11 Jun 2023 14:03:16 +0200 Subject: Added priv/linkage.h and renamed priv/template2.h => priv/template_undef.h. Make all examples c++ compatible, except those using cspan.h Removed: crange_obj() Renamed: crange_make() => crange_init() Renamed: cspan_make() => cspan_init() Renamed: cstr_NULL => cstr_null Renamed: csview_NULL => csview_null --- docs/carc_api.md | 2 +- docs/cbox_api.md | 2 +- docs/ccommon_api.md | 14 +++++----- docs/cmap_api.md | 2 +- docs/cspan_api.md | 2 +- docs/cstr_api.md | 4 +-- docs/csview_api.md | 2 +- include/stc/algo/coroutine.h | 2 +- include/stc/algo/crange.h | 18 ++++++------- include/stc/algo/filter.h | 2 +- include/stc/algo/sort.h | 1 + include/stc/carc.h | 5 ++-- include/stc/cbox.h | 5 ++-- include/stc/ccommon.h | 51 +++++++++++++++---------------------- include/stc/clist.h | 3 ++- include/stc/cmap.h | 3 ++- include/stc/cpque.h | 3 ++- include/stc/cqueue.h | 3 ++- include/stc/crand.h | 3 ++- include/stc/cregex.h | 4 ++- include/stc/csmap.h | 3 ++- include/stc/cspan.h | 9 ++++--- include/stc/cstack.h | 3 ++- include/stc/cstr.h | 20 +++++++-------- include/stc/csview.h | 18 ++++++------- include/stc/cvec.h | 3 ++- include/stc/priv/linkage.h | 40 +++++++++++++++++++++++++++++ include/stc/utf8.h | 34 ++++++++++++++++++++----- misc/examples/astar.c | 2 +- misc/examples/box.c | 2 +- misc/examples/box2.c | 6 ++--- misc/examples/csmap_find.c | 12 ++++----- misc/examples/csmap_insert.c | 12 ++++----- misc/examples/dining_philosophers.c | 5 ++-- misc/examples/forfilter.c | 5 ++-- misc/examples/make.sh | 14 +++++----- misc/examples/music_arc.c | 2 +- misc/examples/new_list.c | 2 +- misc/examples/new_sptr.c | 3 ++- misc/examples/new_vec.c | 8 +++--- misc/examples/person_arc.c | 3 ++- misc/examples/prime.c | 4 ++- misc/examples/printspan.c | 8 +++--- misc/examples/read.c | 2 +- misc/examples/shape.c | 2 +- misc/examples/vikings.c | 10 ++++---- src/cregex.c | 32 +++++++++++------------ src/utf8code.c | 41 +++++++++++++++-------------- 48 files changed, 254 insertions(+), 182 deletions(-) create mode 100644 include/stc/priv/linkage.h (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/carc_api.md b/docs/carc_api.md index 9f3d8cb9..22e6bac2 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -67,7 +67,7 @@ bool carc_X_value_eq(const i_val* x, const i_val* y); | Type name | Type definition | Used to represent... | |:------------------|:--------------------------------------------------|:-----------------------| -| `carc_NULL` | `{NULL, NULL}` | Init nullptr const | +| `carc_null` | `{0}` | Init nullptr const | | `carc_X` | `struct { carc_X_value* get; long* use_count; }` | The carc type | | `carc_X_value` | `i_val` | The carc element type | | `carc_X_raw` | `i_valraw` | Convertion type | diff --git a/docs/cbox_api.md b/docs/cbox_api.md index 5914a5ad..9151f56d 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -64,7 +64,7 @@ bool cbox_X_value_eq(const i_val* x, const i_val* y); | Type name | Type definition | Used to represent... | |:-------------------|:--------------------------------|:------------------------| -| `cbox_NULL` | `{NULL}` | Init nullptr const | +| `cbox_null` | `{0}` | Init nullptr const | | `cbox_X` | `struct { cbox_X_value* get; }` | The cbox type | | `cbox_X_value` | `i_val` | The cbox element type | diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index cd9be505..1f0847da 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -82,17 +82,16 @@ c_forrange (i, 30, 0, -5) printf(" %lld", i); ### crange A number sequence generator type, similar to [boost::irange](https://www.boost.org/doc/libs/release/libs/range/doc/html/range/reference/ranges/irange.html). The **crange_value** type is `long long`. Below *start*, *stop*, and *step* are of type *crange_value*: ```c -crange& crange_obj(...) // create a compound literal crange object -crange crange_make(stop); // will generate 0, 1, ..., stop-1 -crange crange_make(start, stop); // will generate start, start+1, ... stop-1 -crange crange_make(start, stop, step); // will generate start, start+step, ... upto-not-including stop +crange crange_init(stop); // will generate 0, 1, ..., stop-1 +crange crange_init(start, stop); // will generate start, start+1, ... stop-1 +crange crange_init(start, stop, step); // will generate start, start+step, ... upto-not-including stop // note that step may be negative. crange_iter crange_begin(crange* self); crange_iter crange_end(crange* self); void crange_next(crange_iter* it); // 1. All primes less than 32: -crange r1 = crange_make(3, 32, 2); +crange r1 = crange_init(3, 32, 2); printf("2"); // first prime c_forfilter (i, crange, r1, isPrime(*i.ref)) printf(" %lld", *i.ref); @@ -100,7 +99,8 @@ c_forfilter (i, crange, r1, isPrime(*i.ref)) // 2. The first 11 primes: printf("2"); -c_forfilter (i, crange, crange_obj(3, INT64_MAX, 2), +crange range = crange_init(3, INT64_MAX, 2); +c_forfilter (i, crange, range, isPrime(*i.ref) && c_flt_take(10) ){ @@ -140,7 +140,7 @@ bool isPrime(long long i) { int main() { // Get 10 prime numbers starting from 1000. Skip the first 15 primes, // then select every 25th prime (including the initial). - crange R = crange_make(1001, INT64_MAX, 2); // 1001, 1003, ... + crange R = crange_init(1001, INT64_MAX, 2); // 1001, 1003, ... c_forfilter (i, crange, R, isPrime(*i.ref) && diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 69e547a0..8ef322e6 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -277,7 +277,7 @@ typedef struct { cstr country; } Viking; -#define Viking_init() ((Viking){cstr_NULL, cstr_NULL}) +#define Viking_init() ((Viking){cstr_null, cstr_null}) static inline int Viking_cmp(const Viking* a, const Viking* b) { int c = cstr_cmp(&a->name, &b->name); diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 3a811ebf..c78bb8a0 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -26,7 +26,7 @@ i.e., it may be expanded multiple times. However, all integer arguments are safe `cspan_at(&ms3, i++, j++, k++)` is allowed. If the number of arguments does not match the span rank, a compile error is issued. Runtime bounds checks are enabled by default (define `STC_NDEBUG` or `NDEBUG` to disable). ```c -SpanType cspan_make(T SpanType, {v1, v2, ...}); // make a 1-d cspan from values +SpanType cspan_init(T SpanType, {v1, v2, ...}); // make a 1-d cspan from values SpanType cspan_from(STCContainer* cnt); // make a 1-d cspan from compatible STC container SpanType cspan_from_array(ValueType array[]); // make a 1-d cspan from C array SpanTypeN cspan_md(ValueType* data, intptr_t xdim, ...); // make a multi-dimensional cspan diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 438dbf27..c7d19e0c 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -18,7 +18,7 @@ All cstr definitions and prototypes are available by including a single header f ## Methods ```c -cstr cstr_init(void); // constructor; same as cstr_NULL. +cstr cstr_init(void); // constructor; same as cstr_null. cstr cstr_lit(const char literal_only[]); // cstr from literal; no strlen() call. cstr cstr_from(const char* str); // constructor using strlen() cstr cstr_from_n(const char* str, intptr_t n); // constructor with n first bytes of str @@ -153,7 +153,7 @@ char* cstrnstrn(const char* str, const char* search, intptr_t slen, intpt | Name | Value | |:------------------|:------------------| | `c_NPOS` | `INTPTR_MAX` | -| `cstr_NULL` | cstr null value | +| `cstr_null` | empty cstr value | ## Example ```c diff --git a/docs/csview_api.md b/docs/csview_api.md index 879822d3..a02b007a 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -112,7 +112,7 @@ uint64_t csview_hash(const csview* x); | Name | Value | Usage | |:---------------|:---------------------|:---------------------------------------------| -| `csview_NULL` | same as `c_sv("")` | `sview = csview_NULL;` | +| `csview_null` | same as `c_sv("")` | `sview = csview_null;` | | `c_SV(sv)` | printf argument | `printf("sv: %.*s\n", c_SV(sv));` | ## Example diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 67ea5a40..5cd6d68f 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -56,7 +56,7 @@ int main(void) { return 0; } */ -#include +#include "../ccommon.h" enum { cco_state_final = -1, diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h index 56c317da..34ed541b 100644 --- a/include/stc/algo/crange.h +++ b/include/stc/algo/crange.h @@ -27,14 +27,15 @@ int main() { - crange r1 = crange_make(80, 90); + crange r1 = crange_init(80, 90); c_foreach (i, crange, r1) printf(" %lld", *i.ref); puts(""); // use a temporary crange object. int a = 100, b = INT32_MAX; - c_forfilter (i, crange, crange_obj(a, b, 8), + crange r2 = crange_init(a, b, 8); + c_forfilter (i, crange, r2, c_flt_skip(i, 10) && c_flt_take(i, 3)) printf(" %lld", *i.ref); @@ -44,20 +45,17 @@ int main() #ifndef STC_CRANGE_H_INCLUDED #define STC_CRANGE_H_INCLUDED -#include - -#define crange_obj(...) \ - (*(crange[]){crange_make(__VA_ARGS__)}) +#include "../ccommon.h" typedef long long crange_value; typedef struct { crange_value start, end, step, value; } crange; typedef struct { crange_value *ref, end, step; } crange_iter; -#define crange_make(...) c_MACRO_OVERLOAD(crange_make, __VA_ARGS__) -#define crange_make_1(stop) crange_make_3(0, stop, 1) -#define crange_make_2(start, stop) crange_make_3(start, stop, 1) +#define crange_init(...) c_MACRO_OVERLOAD(crange_init, __VA_ARGS__) +#define crange_init_1(stop) crange_init_3(0, stop, 1) +#define crange_init_2(start, stop) crange_init_3(start, stop, 1) -STC_INLINE crange crange_make_3(crange_value start, crange_value stop, crange_value step) +STC_INLINE crange crange_init_3(crange_value start, crange_value stop, crange_value step) { crange r = {start, stop - (step > 0), step}; return r; } STC_INLINE crange_iter crange_begin(crange* self) diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h index 8dc1ad74..f5de1811 100644 --- a/include/stc/algo/filter.h +++ b/include/stc/algo/filter.h @@ -47,7 +47,7 @@ int main() #ifndef STC_FILTER_H_INCLUDED #define STC_FILTER_H_INCLUDED -#include +#include "../ccommon.h" // c_forfilter: diff --git a/include/stc/algo/sort.h b/include/stc/algo/sort.h index bbd58427..2e73b0fb 100644 --- a/include/stc/algo/sort.h +++ b/include/stc/algo/sort.h @@ -42,6 +42,7 @@ int main() { } */ #include "../ccommon.h" + #ifndef i_type #define i_at(arr, idx) (&arr[idx]) #ifndef i_tag diff --git a/include/stc/carc.h b/include/stc/carc.h index 756b604f..749b1fc1 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -49,10 +49,11 @@ int main() { c_drop(ArcPers, &p, &q); } */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CARC_H_INCLUDED #define CARC_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include @@ -72,7 +73,7 @@ int main() { #define c_atomic_dec_and_test(v) (atomic_fetch_sub(v, 1) == 1) #endif -#define carc_NULL {NULL, NULL} +#define carc_null {0} #endif // CARC_H_INCLUDED #define _i_prefix carc_ diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 699b32ac..d7f6246d 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -57,15 +57,16 @@ int main() { } } */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CBOX_H_INCLUDED #define CBOX_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include #include -#define cbox_NULL {NULL} +#define cbox_null {0} #endif // CBOX_H_INCLUDED #define _i_prefix cbox_ diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index e491a567..5f280218 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -117,10 +117,6 @@ /* Function macros and others */ -#define c_init(C, ...) \ - C##_from_n((C##_raw[])__VA_ARGS__, c_sizeof((C##_raw[])__VA_ARGS__)/c_sizeof(C##_raw)) -#define c_make(C, ...) c_init(C, __VA_ARGS__) // [deprecated] - #define c_litstrlen(literal) (c_sizeof("" literal) - 1) #define c_arraylen(a) (intptr_t)(sizeof(a)/sizeof 0[a]) @@ -210,16 +206,23 @@ STC_INLINE intptr_t cnextpow2(intptr_t n) { ; (_inc > 0) ^ (i > _end); i += _inc) #ifndef __cplusplus + #define c_init(C, ...) \ + C##_from_n((C##_raw[])__VA_ARGS__, c_sizeof((C##_raw[])__VA_ARGS__)/c_sizeof(C##_raw)) #define c_forlist(it, T, ...) \ - for (struct {T* ref; int size, index;} \ - it = {.ref=(T[])__VA_ARGS__, .size=(int)(sizeof((T[])__VA_ARGS__)/sizeof(T))} \ - ; it.index < it.size; ++it.ref, ++it.index) + for (struct {T* ref; int size, index;} \ + it = {.ref=(T[])__VA_ARGS__, .size=(int)(sizeof((T[])__VA_ARGS__)/sizeof(T))} \ + ; it.index < it.size; ++it.ref, ++it.index) #else - #include - #define c_forlist(it, T, ...) \ - for (struct {std::initializer_list _il; std::initializer_list::iterator ref; size_t size, index;} \ - it = {._il=__VA_ARGS__, .ref=it._il.begin(), .size=it._il.size()} \ - ; it.index < it.size; ++it.ref, ++it.index) + #include + template + inline C _from_n(C (*func)(const T[], intptr_t), std::initializer_list il) + { return func(&*il.begin(), il.size()); } + + #define c_init(C, ...) _from_n(C##_from_n, __VA_ARGS__) + #define c_forlist(it, T, ...) \ + for (struct {std::initializer_list _il; std::initializer_list::iterator ref; size_t size, index;} \ + it = {._il=__VA_ARGS__, .ref=it._il.begin(), .size=it._il.size()} \ + ; it.index < it.size; ++it.ref, ++it.index) #endif #define c_drop(C, ...) \ @@ -236,23 +239,9 @@ STC_INLINE intptr_t cnextpow2(intptr_t n) { #define c_umul128(a, b, lo, hi) \ asm("mulq %3" : "=a"(*(lo)), "=d"(*(hi)) : "a"(a), "rm"(b)) #endif -#endif // CCOMMON_H_INCLUDED -#undef STC_API -#undef STC_DEF - -#ifdef i_extern -# define i_import -#endif -#if !defined(i_static) && !defined(STC_STATIC) && (defined(i_header) || defined(STC_HEADER) || \ - defined(i_implement) || defined(STC_IMPLEMENT)) - #define STC_API extern - #define STC_DEF -#else - #define i_static - #define STC_API static inline - #define STC_DEF static inline -#endif -#if defined(STC_IMPLEMENT) || defined(i_import) - #define i_implement -#endif +// [deprecated]: +#define c_make(...) c_init(__VA_ARGS__) +#define cspan_make(...) cspan_init(__VA_ARGS__) +#define crange_make(...) crange_init(__VA_ARGS__) +#endif // CCOMMON_H_INCLUDED diff --git a/include/stc/clist.h b/include/stc/clist.h index 310db204..4d05a3d1 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -51,9 +51,10 @@ } } */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CLIST_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include #include diff --git a/include/stc/cmap.h b/include/stc/cmap.h index f6c3eb07..2e234fb5 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -47,9 +47,10 @@ int main(void) { cmap_ichar_drop(&m); } */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CMAP_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include #include diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 31a53ece..b66c7735 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -20,9 +20,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CPQUE_H_INCLUDED +#include "ccommon.h" #include #include "forward.h" #endif diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index 28515877..3adc1bcb 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -20,9 +20,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CQUEUE_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include #include diff --git a/include/stc/crand.h b/include/stc/crand.h index 95a65fb0..89b681cd 100644 --- a/include/stc/crand.h +++ b/include/stc/crand.h @@ -20,10 +20,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CRAND_H_INCLUDED #define CRAND_H_INCLUDED +#include "ccommon.h" /* // crand: Pseudo-random number generator #include "stc/crand.h" diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 43a7fcbf..1d1d441f 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -22,6 +22,8 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "priv/linkage.h" + #ifndef CREGEX_H_INCLUDED #define CREGEX_H_INCLUDED /* @@ -157,6 +159,7 @@ cstr cregex_replace_pattern_6(const char* pattern, const char* input, const char /* destroy regex */ void cregex_drop(cregex* re); +#endif // CREGEX_H_INCLUDED #if defined i_implement # include "../../src/cregex.c" @@ -164,7 +167,6 @@ void cregex_drop(cregex* re); #if defined i_import # include "../../src/utf8code.c" #endif -#endif // CREGEX_H_INCLUDED #undef i_opt #undef i_header #undef i_static diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 7638b8f2..28598f0a 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -49,9 +49,10 @@ int main(void) { csmap_sx_drop(&m); } */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CSMAP_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include #include diff --git a/include/stc/cspan.h b/include/stc/cspan.h index dd6cb1c0..d7a72267 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -60,6 +60,7 @@ int demo2() { #ifndef STC_CSPAN_H_INCLUDED #define STC_CSPAN_H_INCLUDED +#include "priv/linkage.h" #include "ccommon.h" #define using_cspan(...) c_MACRO_OVERLOAD(using_cspan, __VA_ARGS__) @@ -80,7 +81,7 @@ int demo2() { return (Self){.data=raw, .shape={(int32_t)n}}; \ } \ STC_INLINE Self Self##_slice_(Self##_value* v, const int32_t shape[], const int32_t stri[], \ - const int rank, const int32_t a[][2]) { \ + const int rank, const int32_t a[][2]) { \ Self s = {.data=v}; int outrank; \ s.data += _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \ c_ASSERT(outrank == RANK); \ @@ -115,8 +116,8 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define cspan_md(array, ...) \ {.data=array, .shape={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} -/* For static initialization, use cspan_make(). c_init() for non-static only. */ -#define cspan_make(SpanType, ...) \ +/* For static initialization, use cspan_init(). c_init() for non-static only. */ +#define cspan_init(SpanType, ...) \ {.data=(SpanType##_value[])__VA_ARGS__, .shape={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} #define cspan_slice(OutSpan, parent, ...) \ @@ -210,6 +211,7 @@ STC_API intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t shape[], co STC_API intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, const int32_t shape[], const int32_t stri[], int rank, const int32_t a[][2]); +#endif // STC_CSPAN_H_INCLUDED /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) || defined(i_static) @@ -260,7 +262,6 @@ STC_DEF intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, return off; } #endif -#endif #undef i_opt #undef i_header #undef i_implement diff --git a/include/stc/cstack.h b/include/stc/cstack.h index fa0fab2b..fb4eae4b 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -20,10 +20,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CSTACK_H_INCLUDED #define CSTACK_H_INCLUDED +#include "ccommon.h" #include #include "forward.h" #endif // CSTACK_H_INCLUDED diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 0c5b67d8..bdfee39b 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -24,7 +24,7 @@ /* A string type with short string optimization in C99 with good small-string * optimization (22 characters with 24 bytes string). */ -#define _i_no_undef +#define _i_nested #include "utf8.h" #ifndef CSTR_H_INCLUDED @@ -67,7 +67,7 @@ extern char* _cstr_internal_move(cstr* self, intptr_t pos1, intptr_t pos2); /**************************** PUBLIC API **********************************/ #define cstr_lit(literal) cstr_from_n(literal, c_litstrlen(literal)) -#define cstr_NULL (c_LITERAL(cstr){{{0}, 0}}) +#define cstr_null (c_LITERAL(cstr){0}) #define cstr_toraw(self) cstr_str(self) extern char* cstr_reserve(cstr* self, intptr_t cap); @@ -97,7 +97,7 @@ STC_INLINE csview cstr_sv(const cstr* s) { } STC_INLINE cstr cstr_init(void) - { return cstr_NULL; } + { return cstr_null; } STC_INLINE cstr cstr_from_n(const char* str, const intptr_t len) { cstr s; @@ -132,7 +132,7 @@ STC_INLINE cstr* cstr_take(cstr* self, const cstr s) { STC_INLINE cstr cstr_move(cstr* self) { cstr tmp = *self; - *self = cstr_NULL; + *self = cstr_null; return tmp; } @@ -440,8 +440,8 @@ cstr cstr_tocase(csview sv, int k) { #endif // i_import /* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined i_import || (defined i_implement && !defined _i_nested) #ifndef CSTR_C_INCLUDED -#if defined i_import || (defined i_implement && !defined _i_no_undef) #define CSTR_C_INCLUDED uint64_t cstr_hash(const cstr *self) { @@ -573,7 +573,7 @@ bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { cstr cstr_replace_sv(csview in, csview search, csview repl, int32_t count) { - cstr out = cstr_NULL; + cstr out = cstr_null; intptr_t from = 0; char* res; if (!count) count = INT32_MAX; if (search.size) @@ -625,7 +625,7 @@ intptr_t cstr_vfmt(cstr* self, intptr_t start, const char* fmt, va_list args) { #endif cstr cstr_from_fmt(const char* fmt, ...) { - cstr s = cstr_NULL; + cstr s = cstr_null; va_list args; va_start(args, fmt); cstr_vfmt(&s, 0, fmt, args); @@ -649,17 +649,17 @@ intptr_t cstr_printf(cstr* self, const char* fmt, ...) { va_end(args); return n; } -#endif // i_implement #endif // CSTR_C_INCLUDED +#endif // i_implement #if defined __GNUC__ && !defined __clang__ # pragma GCC diagnostic pop #endif -#ifndef _i_no_undef +#ifndef _i_nested #undef i_opt #undef i_header #undef i_static #undef i_implement #undef i_import #endif -#undef _i_no_undef +#undef _i_nested diff --git a/include/stc/csview.h b/include/stc/csview.h index a1893063..c16f58bc 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -20,14 +20,14 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#define _i_no_undef +#define _i_nested #include "utf8.h" #ifndef CSVIEW_H_INCLUDED #define CSVIEW_H_INCLUDED -#define csview_NULL c_sv_1("") -#define csview_init() csview_NULL +#define csview_null c_sv_1("") +#define csview_init() csview_null #define csview_drop(p) c_default_drop(p) #define csview_clone(sv) c_default_clone(sv) #define csview_lit(literal) c_sv_1(literal) @@ -42,7 +42,7 @@ extern csview csview_token(csview sv, const char* sep, intptr_t* start); STC_INLINE csview csview_from(const char* str) { return c_LITERAL(csview){str, c_strlen(str)}; } -STC_INLINE void csview_clear(csview* self) { *self = csview_NULL; } +STC_INLINE void csview_clear(csview* self) { *self = csview_null; } STC_INLINE intptr_t csview_size(csview sv) { return sv.size; } STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; } @@ -150,8 +150,8 @@ STC_INLINE csview cstr_u8_substr(const cstr* self , intptr_t bytepos, intptr_t u #endif /* -------------------------- IMPLEMENTATION ------------------------- */ +#if defined i_import || (defined i_implement && !defined _i_nested) #ifndef CSVIEW_C_INCLUDED -#if defined i_import || (defined i_implement && !defined _i_no_undef) #define CSVIEW_C_INCLUDED csview_iter csview_advance(csview_iter it, intptr_t pos) { @@ -201,13 +201,13 @@ csview csview_token(csview sv, const char* sep, intptr_t* start) { *start += tok.size + sep_size; return tok; } -#endif -#endif -#ifndef _i_no_undef +#endif // CSVIEW_C_INCLUDED +#endif // i_implement +#ifndef _i_nested #undef i_static #undef i_header #undef i_implement #undef i_import #undef i_opt #endif -#undef _i_no_undef +#undef _i_nested diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 747c654d..874f4f47 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -58,9 +58,10 @@ int main() { cvec_str_drop(&svec); } */ -#include "ccommon.h" +#include "priv/linkage.h" #ifndef CVEC_H_INCLUDED +#include "ccommon.h" #include "forward.h" #include #include diff --git a/include/stc/priv/linkage.h b/include/stc/priv/linkage.h new file mode 100644 index 00000000..7f63f5f1 --- /dev/null +++ b/include/stc/priv/linkage.h @@ -0,0 +1,40 @@ +/* MIT License + * + * Copyright (c) 2023 Tyge Løvset + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#undef STC_API +#undef STC_DEF + +#ifdef i_extern // [deprecated] +# define i_import +#endif +#if !defined(i_static) && !defined(STC_STATIC) && (defined(i_header) || defined(STC_HEADER) || \ + defined(i_implement) || defined(STC_IMPLEMENT)) + #define STC_API extern + #define STC_DEF +#else + #define i_static + #define STC_API static inline + #define STC_DEF static inline +#endif +#if defined(STC_IMPLEMENT) || defined(i_import) + #define i_implement +#endif diff --git a/include/stc/utf8.h b/include/stc/utf8.h index d6c759eb..190cc7f3 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -1,9 +1,31 @@ - -#include "ccommon.h" +/* MIT License + * + * Copyright (c) 2023 Tyge Løvset + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "priv/linkage.h" #ifndef UTF8_H_INCLUDED #define UTF8_H_INCLUDED +#include "ccommon.h" #include #include "forward.h" @@ -53,9 +75,9 @@ STC_INLINE bool utf8_isspace(uint32_t c) { /* decode next utf8 codepoint. https://bjoern.hoehrmann.de/utf-8/decoder/dfa */ typedef struct { uint32_t state, codep; } utf8_decode_t; +extern const uint8_t utf8_dtab[]; /* utf8code.c */ STC_INLINE uint32_t utf8_decode(utf8_decode_t* d, const uint32_t byte) { - extern const uint8_t utf8_dtab[]; /* utf8code.c */ const uint32_t type = utf8_dtab[byte]; d->codep = d->state ? (byte & 0x3fu) | (d->codep << 6) : (0xffU >> type) & byte; @@ -116,14 +138,14 @@ STC_INLINE intptr_t utf8_pos(const char* s, intptr_t index) { return (intptr_t)(utf8_at(s, index) - s); } #endif // UTF8_H_INCLUDED -#if defined i_import || (defined i_implement && !defined _i_no_undef) +#if defined i_import || (defined i_implement && !defined _i_nested) # include "../../src/utf8code.c" #endif -#ifndef _i_no_undef +#ifndef _i_nested #undef i_static #undef i_header #undef i_implement #undef i_import #undef i_opt #endif -#undef _i_no_undef +#undef _i_nested diff --git a/misc/examples/astar.c b/misc/examples/astar.c index db6bbd70..1b3876aa 100644 --- a/misc/examples/astar.c +++ b/misc/examples/astar.c @@ -21,7 +21,7 @@ point; point point_init(int x, int y, int width) { - return (point) { x, y, 0, width }; + return c_LITERAL(point){ x, y, 0, width }; } int diff --git a/misc/examples/box.c b/misc/examples/box.c index e352aa2b..a9131afa 100644 --- a/misc/examples/box.c +++ b/misc/examples/box.c @@ -5,7 +5,7 @@ typedef struct { cstr name, last; } Person; Person Person_make(const char* name, const char* last) { - return (Person){.name = cstr_from(name), .last = cstr_from(last)}; + return c_LITERAL(Person){.name = cstr_from(name), .last = cstr_from(last)}; } uint64_t Person_hash(const Person* a) { diff --git a/misc/examples/box2.c b/misc/examples/box2.c index 33212ef4..963a3815 100644 --- a/misc/examples/box2.c +++ b/misc/examples/box2.c @@ -29,12 +29,12 @@ typedef struct { #include // BoxBoxPoint Point origin(void) { - return (Point){ .x=1.0, .y=2.0 }; + return c_LITERAL(Point){ .x=1.0, .y=2.0 }; } cbox_Point boxed_origin(void) { // Allocate this point on the heap, and return a pointer to it - return cbox_Point_make((Point){ .x=1.0, .y=2.0 }); + return cbox_Point_make(c_LITERAL(Point){ .x=1.0, .y=2.0 }); } @@ -47,7 +47,7 @@ int main(void) { }; // Heap allocated rectangle - cbox_Rectangle boxed_rectangle = cbox_Rectangle_make((Rectangle){ + cbox_Rectangle boxed_rectangle = cbox_Rectangle_make(c_LITERAL(Rectangle){ .top_left = origin(), .bottom_right = { .x=3.0, .y=-4.0 } }); diff --git a/misc/examples/csmap_find.c b/misc/examples/csmap_find.c index a8928410..c123e398 100644 --- a/misc/examples/csmap_find.c +++ b/misc/examples/csmap_find.c @@ -50,12 +50,12 @@ int main() print_collection_csmap_istr(&m1); typedef cvec_istr_value pair; - cvec_istr_push(&v, (pair){43, "Tc"}); - cvec_istr_push(&v, (pair){41, "Nb"}); - cvec_istr_push(&v, (pair){46, "Pd"}); - cvec_istr_push(&v, (pair){42, "Mo"}); - cvec_istr_push(&v, (pair){44, "Ru"}); - cvec_istr_push(&v, (pair){44, "Ru"}); // attempt a duplicate + cvec_istr_push(&v, c_LITERAL(pair){43, "Tc"}); + cvec_istr_push(&v, c_LITERAL(pair){41, "Nb"}); + cvec_istr_push(&v, c_LITERAL(pair){46, "Pd"}); + cvec_istr_push(&v, c_LITERAL(pair){42, "Mo"}); + cvec_istr_push(&v, c_LITERAL(pair){44, "Ru"}); + cvec_istr_push(&v, c_LITERAL(pair){44, "Ru"}); // attempt a duplicate puts("Inserting the following vector data into m1:"); print_collection_cvec_istr(&v); diff --git a/misc/examples/csmap_insert.c b/misc/examples/csmap_insert.c index f96cc08f..18a88ec3 100644 --- a/misc/examples/csmap_insert.c +++ b/misc/examples/csmap_insert.c @@ -34,7 +34,7 @@ int main() // insert single values csmap_ii m1 = {0}; csmap_ii_insert(&m1, 1, 10); - csmap_ii_push(&m1, (csmap_ii_value){2, 20}); + csmap_ii_push(&m1, c_LITERAL(csmap_ii_value){2, 20}); puts("The original key and mapped values of m1 are:"); print_ii(m1); @@ -61,11 +61,11 @@ int main() csmap_ii m2 = {0}; cvec_ii v = {0}; typedef cvec_ii_value ipair; - cvec_ii_push(&v, (ipair){43, 294}); - cvec_ii_push(&v, (ipair){41, 262}); - cvec_ii_push(&v, (ipair){45, 330}); - cvec_ii_push(&v, (ipair){42, 277}); - cvec_ii_push(&v, (ipair){44, 311}); + cvec_ii_push(&v, c_LITERAL(ipair){43, 294}); + cvec_ii_push(&v, c_LITERAL(ipair){41, 262}); + cvec_ii_push(&v, c_LITERAL(ipair){45, 330}); + cvec_ii_push(&v, c_LITERAL(ipair){42, 277}); + cvec_ii_push(&v, c_LITERAL(ipair){44, 311}); puts("Inserting the following vector data into m2:"); c_foreach (e, cvec_ii, v) diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c index f9c05e71..e13eb055 100644 --- a/misc/examples/dining_philosophers.c +++ b/misc/examples/dining_philosophers.c @@ -29,9 +29,10 @@ struct Dining { // Philosopher coroutine void philosopher(struct Philosopher* p) { + double duration; cco_routine(p) { while (1) { - double duration = 1.0 + crandf()*2.0; + duration = 1.0 + crandf()*2.0; printf("Philosopher %d is thinking for %.0f minutes...\n", p->id, duration*10); cco_timer_await(&p->tm, duration); @@ -46,7 +47,7 @@ void philosopher(struct Philosopher* p) cco_sem_release(p->left_fork); cco_sem_release(p->right_fork); } - + cco_final: printf("Philosopher %d finished\n", p->id); } diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index 94a84065..d39693b5 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -55,7 +55,8 @@ fn main() { void demo2(void) { IVec vector = {0}; - c_forfilter (x, crange, crange_obj(INT64_MAX), + crange r = crange_init(INT64_MAX); + c_forfilter (x, crange, r, c_flt_skipwhile(x, *x.ref != 11) && (*x.ref % 2) != 0 && c_flt_take(x, 5) @@ -124,7 +125,7 @@ void demo5(void) { #define flt_even(i) ((*i.ref & 1) == 0) #define flt_mid_decade(i) ((*i.ref % 10) != 0) - crange R = crange_make(1963, INT32_MAX); + crange R = crange_init(1963, INT32_MAX); c_forfilter (i, crange, R, c_flt_skip(i,15) && diff --git a/misc/examples/make.sh b/misc/examples/make.sh index 61d9f879..cf224950 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -7,13 +7,13 @@ if [ "$(uname)" = 'Linux' ]; then fi cc=gcc; cflags="-std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-maybe-uninitialized -Wno-implicit-fallthrough -Wno-missing-field-initializers" -#cc=gcc; cflags="-DSTC_STATIC -std=c99 -g -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" -#cc=tcc; cflags="-DSTC_STATIC -std=c99 -Wall" -#cc=clang; cflags="-DSTC_STATIC -std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers" -#cc=gcc; cflags="-DSTC_STATIC -x c++ -std=c++20 -O2 -s -Wall" -#cc=cl; cflags="-DSTC_STATIC -nologo -O2 -MD -W3 -wd4003" -#cc=cl; cflags="-DSTC_STATIC -nologo -TP -wd4003" -#cc=cl; cflags="-DSTC_STATIC -nologo -std:c11 -wd4003" +#cc=gcc; cflags="-std=c99 -g -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" +#cc=tcc; cflags="-std=c99 -Wall" +#cc=clang; cflags="-std=c99 -s -O3 -Wall -Wextra -Wpedantic -Wconversion -Wwrite-strings -Wdouble-promotion -Wno-unused-parameter -Wno-unused-function -Wno-implicit-fallthrough -Wno-missing-field-initializers" +#cc=gcc; cflags="-x c++ -std=c++20 -O2 -s -Wall" +#cc=cl; cflags="-nologo -O2 -MD -W3 -wd4003" +#cc=cl; cflags="-nologo -TP -std:c++20 -wd4003" +#cc=cl; cflags="-nologo -std:c11 -wd4003" if [ "$cc" = "cl" ]; then oflag='/Fe:' diff --git a/misc/examples/music_arc.c b/misc/examples/music_arc.c index 9c7173ef..18ea30c0 100644 --- a/misc/examples/music_arc.c +++ b/misc/examples/music_arc.c @@ -13,7 +13,7 @@ int Song_cmp(const Song* x, const Song* y) { return cstr_cmp(&x->title, &y->title); } Song Song_make(const char* artist, const char* title) - { return (Song){cstr_from(artist), cstr_from(title)}; } + { return c_LITERAL(Song){cstr_from(artist), cstr_from(title)}; } void Song_drop(Song* s) { printf("drop: %s\n", cstr_str(&s->title)); diff --git a/misc/examples/new_list.c b/misc/examples/new_list.c index 993f1aac..382943bb 100644 --- a/misc/examples/new_list.c +++ b/misc/examples/new_list.c @@ -45,7 +45,7 @@ int main() { MyStruct my = {0}; clist_i32_push_back(&my.intlst, 123); - clist_pnt_push_back(&my.pntlst, (Point){123, 456}); + clist_pnt_push_back(&my.pntlst, c_LITERAL(Point){123, 456}); MyStruct_drop(&my); clist_pnt plst = c_init(clist_pnt, {{42, 14}, {32, 94}, {62, 81}}); diff --git a/misc/examples/new_sptr.c b/misc/examples/new_sptr.c index aa8dd175..36a61f9c 100644 --- a/misc/examples/new_sptr.c +++ b/misc/examples/new_sptr.c @@ -30,7 +30,8 @@ uint64_t Person_hash(const Person* p); Person Person_make(const char* name, const char* last) { - return (Person){.name = cstr_from(name), .last = cstr_from(last)}; + Person p = {.name = cstr_from(name), .last = cstr_from(last)}; + return p; } int Person_cmp(const Person* a, const Person* b) { diff --git a/misc/examples/new_vec.c b/misc/examples/new_vec.c index d4b66883..e10910d9 100644 --- a/misc/examples/new_vec.c +++ b/misc/examples/new_vec.c @@ -26,10 +26,10 @@ int main() { MyStruct my = {0}; - cvec_pnt_push(&my.pntvec, (Point){42, 14}); - cvec_pnt_push(&my.pntvec, (Point){32, 94}); - cvec_pnt_push(&my.pntvec, (Point){62, 81}); - cvec_pnt_push(&my.pntvec, (Point){32, 91}); + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){42, 14}); + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){32, 94}); + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){62, 81}); + cvec_pnt_push(&my.pntvec, c_LITERAL(Point){32, 91}); cvec_pnt_sort(&my.pntvec); diff --git a/misc/examples/person_arc.c b/misc/examples/person_arc.c index b4b926da..3614c02d 100644 --- a/misc/examples/person_arc.c +++ b/misc/examples/person_arc.c @@ -6,7 +6,8 @@ typedef struct { cstr name, last; } Person; Person Person_make(const char* name, const char* last) { - return (Person){.name = cstr_from(name), .last = cstr_from(last)}; + Person p = {.name = cstr_from(name), .last = cstr_from(last)}; + return p; } int Person_cmp(const Person* a, const Person* b) { diff --git a/misc/examples/prime.c b/misc/examples/prime.c index cb0f8926..34fa144c 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -41,7 +41,9 @@ int main(void) puts("\n"); puts("Show the last 50 primes using a temporary crange generator:"); - c_forfilter (i, crange, crange_obj(n - 1, 0, -2), + crange range = crange_init(n - 1, 0, -2); + + c_forfilter (i, crange, range, cbits_test(&primes, *i.ref/2) && c_flt_take(i, 50) ){ diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c index b5099ed5..7564bd88 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/printspan.c @@ -22,22 +22,22 @@ void printMe(intspan container) { int main() { - intspan sp1 = cspan_make(intspan, {1, 2}); + intspan sp1 = cspan_init(intspan, {1, 2}); printMe( sp1 ); printMe( c_init(intspan, {1, 2, 3}) ); int arr[] = {1, 2, 3, 4, 5, 6}; intspan sp2 = cspan_from_array(arr); - printMe( (intspan)cspan_subspan(&sp2, 1, 4) ); + printMe( c_LITERAL(intspan)cspan_subspan(&sp2, 1, 4) ); cvec_int vec = c_init(cvec_int, {1, 2, 3, 4, 5}); - printMe( (intspan)cspan_from(&vec) ); + printMe( c_LITERAL(intspan)cspan_from(&vec) ); printMe( sp2 ); cstack_int stk = c_init(cstack_int, {1, 2, 3, 4, 5, 6, 7}); - printMe( (intspan)cspan_from(&stk) ); + printMe( c_LITERAL(intspan)cspan_from(&stk) ); csset_str set = c_init(csset_str, {"5", "7", "4", "3", "8", "2", "1", "9", "6"}); printf("%d:", (int)csset_str_size(&set)); diff --git a/misc/examples/read.c b/misc/examples/read.c index 545d706a..3c1cadf6 100644 --- a/misc/examples/read.c +++ b/misc/examples/read.c @@ -9,7 +9,7 @@ cvec_str read_file(const char* name) { cvec_str vec = cvec_str_init(); c_with (FILE* f = fopen(name, "r"), fclose(f)) - c_with (cstr line = cstr_NULL, cstr_drop(&line)) + c_with (cstr line = cstr_null, cstr_drop(&line)) while (cstr_getline(&line, f)) cvec_str_push(&vec, cstr_clone(line)); return vec; diff --git a/misc/examples/shape.c b/misc/examples/shape.c index 22e993db..1d9fe5c5 100644 --- a/misc/examples/shape.c +++ b/misc/examples/shape.c @@ -137,7 +137,7 @@ int main(void) { Shapes shapes = {0}; - Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20})); + Triangle* tri1 = c_new(Triangle, Triangle_from(c_LITERAL(Point){5, 7}, c_LITERAL(Point){12, 7}, c_LITERAL(Point){12, 20})); Polygon* pol1 = c_new(Polygon, Polygon_init()); Polygon* pol2 = c_new(Polygon, Polygon_init()); diff --git a/misc/examples/vikings.c b/misc/examples/vikings.c index cf087119..d9024052 100644 --- a/misc/examples/vikings.c +++ b/misc/examples/vikings.c @@ -44,12 +44,12 @@ static inline RViking Viking_toraw(const Viking* vp) { int main() { Vikings vikings = {0}; - Vikings_emplace(&vikings, (RViking){"Einar", "Norway"}, 20); - Vikings_emplace(&vikings, (RViking){"Olaf", "Denmark"}, 24); - Vikings_emplace(&vikings, (RViking){"Harald", "Iceland"}, 12); - Vikings_emplace(&vikings, (RViking){"Björn", "Sweden"}, 10); + Vikings_emplace(&vikings, c_LITERAL(RViking){"Einar", "Norway"}, 20); + Vikings_emplace(&vikings, c_LITERAL(RViking){"Olaf", "Denmark"}, 24); + Vikings_emplace(&vikings, c_LITERAL(RViking){"Harald", "Iceland"}, 12); + Vikings_emplace(&vikings, c_LITERAL(RViking){"Björn", "Sweden"}, 10); - Vikings_value* v = Vikings_get_mut(&vikings, (RViking){"Einar", "Norway"}); + Vikings_value* v = Vikings_get_mut(&vikings, c_LITERAL(RViking){"Einar", "Norway"}); v->second += 3; // add 3 hp points to Einar c_forpair (vk, hp, Vikings, vikings) { diff --git a/src/cregex.c b/src/cregex.c index 1af719b4..9b7179b6 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -28,20 +28,20 @@ THE SOFTWARE. #include #ifdef i_import -# define _i_extern +# define _i_import #endif #ifndef CREGEX_H_INCLUDED # include "../include/stc/cregex.h" #endif -#ifdef _i_extern +#ifdef _i_import # include "utf8code.c" #endif -#ifdef _i_extern +#ifdef _i_import # define i_implement #else # undef i_implement #endif -#undef _i_extern +#undef _i_import #include "../include/stc/cstr.h" typedef uint32_t _Rune; /* Utf8 code point */ @@ -944,14 +944,14 @@ _runematch(_Rune s, _Rune r) case ASC_LO: inv = 1; case ASC_lo: return inv ^ (islower((int)r) != 0); case ASC_UP: inv = 1; case ASC_up: return inv ^ (isupper((int)r) != 0); case ASC_XD: inv = 1; case ASC_xd: return inv ^ (isxdigit((int)r) != 0); - case UTF_AN: inv = 1; case UTF_an: return inv ^ utf8_isalnum(r); - case UTF_BL: inv = 1; case UTF_bl: return inv ^ utf8_isblank(r); - case UTF_SP: inv = 1; case UTF_sp: return inv ^ utf8_isspace(r); - case UTF_LL: inv = 1; case UTF_ll: return inv ^ utf8_islower(r); - case UTF_LU: inv = 1; case UTF_lu: return inv ^ utf8_isupper(r); - case UTF_LC: inv = 1; case UTF_lc: return inv ^ utf8_iscased(r); - case UTF_AL: inv = 1; case UTF_al: return inv ^ utf8_isalpha(r); - case UTF_WR: inv = 1; case UTF_wr: return inv ^ utf8_isword(r); + case UTF_AN: inv = 1; case UTF_an: return inv ^ (int)utf8_isalnum(r); + case UTF_BL: inv = 1; case UTF_bl: return inv ^ (int)utf8_isblank(r); + case UTF_SP: inv = 1; case UTF_sp: return inv ^ (int)utf8_isspace(r); + case UTF_LL: inv = 1; case UTF_ll: return inv ^ (int)utf8_islower(r); + case UTF_LU: inv = 1; case UTF_lu: return inv ^ (int)utf8_isupper(r); + case UTF_LC: inv = 1; case UTF_lc: return inv ^ (int)utf8_iscased(r); + case UTF_AL: inv = 1; case UTF_al: return inv ^ (int)utf8_isalpha(r); + case UTF_WR: inv = 1; case UTF_wr: return inv ^ (int)utf8_isword(r); case UTF_cc: case UTF_CC: case UTF_lt: case UTF_LT: case UTF_nd: case UTF_ND: @@ -972,7 +972,7 @@ _runematch(_Rune s, _Rune r) case UTF_latin: case UTF_LATIN: n = (int)s - UTF_GRP; inv = n & 1; - return inv ^ utf8_isgroup(n / 2, r); + return inv ^ (int)utf8_isgroup(n / 2, r); } return s == r; } @@ -1220,7 +1220,7 @@ _build_subst(const char* replace, int nmatch, const csview match[], cstr_buf buf = cstr_buffer(subst); intptr_t len = 0, cap = buf.cap; char* dst = buf.data; - cstr mstr = cstr_NULL; + cstr mstr = cstr_null; while (*replace != '\0') { if (*replace == '$') { @@ -1293,8 +1293,8 @@ cregex_find_pattern_4(const char* pattern, const char* input, cstr cregex_replace_sv_6(const cregex* re, csview input, const char* replace, int count, bool (*mfun)(int, csview, cstr*), int rflags) { - cstr out = cstr_NULL; - cstr subst = cstr_NULL; + cstr out = cstr_null; + cstr subst = cstr_null; csview match[CREG_MAX_CAPTURES]; int nmatch = cregex_captures(re) + 1; if (!count) count = INT32_MAX; diff --git a/src/utf8code.c b/src/utf8code.c index 6a133050..4abf10ea 100644 --- a/src/utf8code.c +++ b/src/utf8code.c @@ -461,28 +461,31 @@ static const URange16 Latin_range16[] = { #define UNI_ENTRY(Code) \ { Code##_range16, sizeof(Code##_range16)/sizeof(URange16) } -#ifndef __cplusplus +#ifdef __cplusplus +#define _e_arg(k, v) v +#else +#define _e_arg(k, v) [k] = v static #endif const UGroup _utf8_unicode_groups[U8G_SIZE] = { - [U8G_Cc] = UNI_ENTRY(Cc), - [U8G_Lt] = UNI_ENTRY(Lt), - [U8G_Nd] = UNI_ENTRY(Nd), - [U8G_Nl] = UNI_ENTRY(Nl), - [U8G_Pc] = UNI_ENTRY(Pc), - [U8G_Pd] = UNI_ENTRY(Pd), - [U8G_Pf] = UNI_ENTRY(Pf), - [U8G_Pi] = UNI_ENTRY(Pi), - [U8G_Sc] = UNI_ENTRY(Sc), - [U8G_Zl] = UNI_ENTRY(Zl), - [U8G_Zp] = UNI_ENTRY(Zp), - [U8G_Zs] = UNI_ENTRY(Zs), - [U8G_Arabic] = UNI_ENTRY(Arabic), - [U8G_Cyrillic] = UNI_ENTRY(Cyrillic), - [U8G_Devanagari] = UNI_ENTRY(Devanagari), - [U8G_Greek] = UNI_ENTRY(Greek), - [U8G_Han] = UNI_ENTRY(Han), - [U8G_Latin] = UNI_ENTRY(Latin), + _e_arg(U8G_Cc, UNI_ENTRY(Cc)), + _e_arg(U8G_Lt, UNI_ENTRY(Lt)), + _e_arg(U8G_Nd, UNI_ENTRY(Nd)), + _e_arg(U8G_Nl, UNI_ENTRY(Nl)), + _e_arg(U8G_Pc, UNI_ENTRY(Pc)), + _e_arg(U8G_Pd, UNI_ENTRY(Pd)), + _e_arg(U8G_Pf, UNI_ENTRY(Pf)), + _e_arg(U8G_Pi, UNI_ENTRY(Pi)), + _e_arg(U8G_Sc, UNI_ENTRY(Sc)), + _e_arg(U8G_Zl, UNI_ENTRY(Zl)), + _e_arg(U8G_Zp, UNI_ENTRY(Zp)), + _e_arg(U8G_Zs, UNI_ENTRY(Zs)), + _e_arg(U8G_Arabic, UNI_ENTRY(Arabic)), + _e_arg(U8G_Cyrillic, UNI_ENTRY(Cyrillic)), + _e_arg(U8G_Devanagari, UNI_ENTRY(Devanagari)), + _e_arg(U8G_Greek, UNI_ENTRY(Greek)), + _e_arg(U8G_Han, UNI_ENTRY(Han)), + _e_arg(U8G_Latin, UNI_ENTRY(Latin)), }; #endif -- cgit v1.2.3 From 06f3ae1d904d776aea8a78113c16fc30309817ed Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 13 Jun 2023 18:21:04 +0200 Subject: Added cco_closue(Ret, Closure) to define coroutine closure. Added back cco_await_on(child) calls. --- docs/ccommon_api.md | 6 +++++- include/stc/algo/coroutine.h | 16 +++++++++++++++- include/stc/cstr.h | 7 ++----- include/stc/csview.h | 7 ++----- include/stc/utf8.h | 6 +++--- misc/examples/cointerleave.c | 2 +- misc/examples/coread.c | 2 +- 7 files changed, 29 insertions(+), 17 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 1f0847da..89a98cbb 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -380,6 +380,9 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | `cco_await(promise, ret)` | Await/suspend with ret value | | | `cco_return` | Replaces return. Jump to cco_final: if exist| | | `cco_return_v(val)` | Yield final value, enter final-state | +| | `cco_closure(Ret, Closure, ...)` | Define coroutine closure struct. | +| `void` | `cco_await_on(closure) { }` | Await on closure to finish | +| `void` | `cco_await_on(co, func) { }` | Await on func(co) to finish | | | Semaphores: | | | | `cco_sem` | Semaphore type | | | `cco_sem_await(sem)` | Await for the semaphore count > 0 | @@ -398,7 +401,8 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine finalizes | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | -| `void` | `cco_run(co, corocall) { }` | Run blocking until coro is done | +| `void` | `cco_block_on(closure) { }` | Run blocking until closure is finished | +| `void` | `cco_block_on(co, func) { }` | Run blocking until func is finished | | | Time functions: | | | `double` | `cco_time(void)` | Return secs with usec prec. since Epoch | | | `cco_sleep(double sec)` | Sleep for seconds (msec or usec prec.) | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 5cd6d68f..61581f64 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -84,7 +84,21 @@ enum { case __LINE__: if (!(promise)) {return ret; goto _resume;} \ } while (0) -#define cco_run(co, call) while (call, !cco_done(co)) +#define cco_closure(Ret, Closure, ...) \ + struct Closure { \ + Ret (*coroutine)(struct Closure*); \ + __VA_ARGS__ \ + int cco_state; \ + } + +#define cco_resume(closure) (closure)->coroutine(closure) +#define cco_await_on(...) c_MACRO_OVERLOAD(cco_await_on, __VA_ARGS__) +#define cco_await_on_1(closure) cco_await_on_2(closure, cco_resume(closure)) +#define cco_await_on_2(co, func) cco_await_1((func(co), !cco_done(co))) + +#define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__) +#define cco_block_on_1(closure) while (cco_resume(closure), !cco_done(closure)) +#define cco_block_on_2(co, func) while (func(co), !cco_done(co)) #define cco_final \ *_state = cco_state_final; case cco_state_final diff --git a/include/stc/cstr.h b/include/stc/cstr.h index bdfee39b..b9b066ad 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -24,7 +24,7 @@ /* A string type with short string optimization in C99 with good small-string * optimization (22 characters with 24 bytes string). */ -#define _i_nested +#define _i_inc_utf8 #include "utf8.h" #ifndef CSTR_H_INCLUDED @@ -440,7 +440,7 @@ cstr cstr_tocase(csview sv, int k) { #endif // i_import /* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined i_import || (defined i_implement && !defined _i_nested) +#if defined i_import || defined i_implement #ifndef CSTR_C_INCLUDED #define CSTR_C_INCLUDED @@ -655,11 +655,8 @@ intptr_t cstr_printf(cstr* self, const char* fmt, ...) { #if defined __GNUC__ && !defined __clang__ # pragma GCC diagnostic pop #endif -#ifndef _i_nested #undef i_opt #undef i_header #undef i_static #undef i_implement #undef i_import -#endif -#undef _i_nested diff --git a/include/stc/csview.h b/include/stc/csview.h index c16f58bc..07ab4059 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -20,7 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#define _i_nested +#define _i_inc_utf8 #include "utf8.h" #ifndef CSVIEW_H_INCLUDED @@ -150,7 +150,7 @@ STC_INLINE csview cstr_u8_substr(const cstr* self , intptr_t bytepos, intptr_t u #endif /* -------------------------- IMPLEMENTATION ------------------------- */ -#if defined i_import || (defined i_implement && !defined _i_nested) +#if defined i_import || defined i_implement #ifndef CSVIEW_C_INCLUDED #define CSVIEW_C_INCLUDED @@ -203,11 +203,8 @@ csview csview_token(csview sv, const char* sep, intptr_t* start) { } #endif // CSVIEW_C_INCLUDED #endif // i_implement -#ifndef _i_nested #undef i_static #undef i_header #undef i_implement #undef i_import #undef i_opt -#endif -#undef _i_nested diff --git a/include/stc/utf8.h b/include/stc/utf8.h index 190cc7f3..6d12856f 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -138,14 +138,14 @@ STC_INLINE intptr_t utf8_pos(const char* s, intptr_t index) { return (intptr_t)(utf8_at(s, index) - s); } #endif // UTF8_H_INCLUDED -#if defined i_import || (defined i_implement && !defined _i_nested) +#if defined i_import || (defined i_implement && !defined _i_inc_utf8) # include "../../src/utf8code.c" #endif -#ifndef _i_nested +#ifndef _i_inc_utf8 #undef i_static #undef i_header #undef i_implement #undef i_import #undef i_opt #endif -#undef _i_nested +#undef _i_inc_utf8 diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 51b9f39a..1ba7b861 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -48,7 +48,7 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - cco_run(&g, interleaved(&g)) { + cco_block_on(&g, interleaved) { printf("%d ", g.value); } puts(""); diff --git a/misc/examples/coread.c b/misc/examples/coread.c index 63162ba3..622228c0 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -32,7 +32,7 @@ int main(void) { struct file_read g = {__FILE__}; int n = 0; - cco_run(&g, file_read(&g)) + cco_block_on(&g, file_read) { printf("%3d %s\n", ++n, cstr_str(&g.line)); //if (n == 10) cco_stop(&g); -- cgit v1.2.3 From 3f919a3b38a88e1c96399cd6096dec16060802a1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 14 Jun 2023 13:24:29 +0200 Subject: Fixed a bug in cco_await_on(), and added _pull() function to random access containers (moves element out of container, ie no drop). --- include/stc/algo/coroutine.h | 4 ++-- include/stc/cdeq.h | 7 +++++++ include/stc/cqueue.h | 9 ++++++++- include/stc/cstack.h | 5 ++++- include/stc/cvec.h | 4 +++- misc/examples/scheduler.c | 9 +++------ 6 files changed, 27 insertions(+), 11 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 61581f64..e0952e1f 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -93,8 +93,8 @@ enum { #define cco_resume(closure) (closure)->coroutine(closure) #define cco_await_on(...) c_MACRO_OVERLOAD(cco_await_on, __VA_ARGS__) -#define cco_await_on_1(closure) cco_await_on_2(closure, cco_resume(closure)) -#define cco_await_on_2(co, func) cco_await_1((func(co), !cco_done(co))) +#define cco_await_on_1(closure) cco_await_2((cco_resume(closure), cco_done(closure)), ) +#define cco_await_on_2(co, func) cco_await_2((func(co), cco_done(co)), ) #define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__) #define cco_block_on_1(closure) while (cco_resume(closure), !cco_done(closure)) diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 8bb62602..a406c2b0 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -22,6 +22,7 @@ */ #define _i_prefix cdeq_ #define _pop _pop_front +#define _pull _pull_front #ifdef i_more #include "cqueue.h" #define i_more @@ -55,6 +56,12 @@ _cx_memb(_pop_back)(_cx_self* self) { i_keydrop((self->data + self->end)); } +STC_INLINE _cx_value _cx_memb(_pull_back)(_cx_self* self) { // move back out of deq + assert(!_cx_memb(_empty)(self)); + self->end = (self->end - 1) & self->capmask; + return self->data[self->end]; +} + STC_INLINE _cx_iter _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, const _cx_value val) { intptr_t idx = _cdeq_toidx(self, it.pos); diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index 3adc1bcb..aa3d7384 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -93,6 +93,13 @@ STC_INLINE void _cx_memb(_pop)(_cx_self* self) { // pop_front self->start = (self->start + 1) & self->capmask; } +STC_INLINE _cx_value _cx_memb(_pull)(_cx_self* self) { // move front out of queue + assert(!_cx_memb(_empty)(self)); + intptr_t s = self->start; + self->start = (s + 1) & self->capmask; + return self->data[s]; +} + STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) { if (self->data == other->data) return; _cx_memb(_drop)(self); @@ -162,7 +169,7 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t n) { if (!data) return false; intptr_t head = oldcap - self->start; - if (self->start < self->end || self->start == 0) + if (self->start <= self->end) ; else if (head < self->end) { self->start = newcap - head; diff --git a/include/stc/cstack.h b/include/stc/cstack.h index fb4eae4b..5f0ffe2b 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -129,7 +129,10 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) { } STC_INLINE void _cx_memb(_pop)(_cx_self* self) - { assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } + { assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } + +STC_INLINE _cx_value _cx_memb(_pull)(_cx_self* self) + { assert(self->_len); return self->data[--self->_len]; } STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 874f4f47..71787733 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -133,7 +133,9 @@ STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->da STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self) { return self->data + self->_len - 1; } STC_INLINE void _cx_memb(_pop)(_cx_self* self) - { assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } + { assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } +STC_INLINE _cx_value _cx_memb(_pull)(_cx_self* self) + { assert(self->_len); return self->data[--self->_len]; } STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) { return _cx_memb(_push)(self, value); } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); } diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index aecb1a26..54fefc47 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -2,12 +2,9 @@ #include #include -struct Scheduler; -struct Task { - bool (*resume)(struct Task*); +cco_closure(bool, Task, struct Scheduler* sched; - int cco_state; -}; +); #define i_type Scheduler #define i_val struct Task @@ -20,7 +17,7 @@ static bool schedule(Scheduler* sched) Scheduler_pop(sched); if (!cco_done(&task)) - task.resume(&task); + cco_resume(&task); return !Scheduler_empty(sched); } -- cgit v1.2.3 From d1a1ed75ee08ed1100748bdbdc6fb1c3136c4c6b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 20 Jun 2023 18:26:35 +0200 Subject: Added some more to coroutine.h --- README.md | 4 +++- include/stc/algo/coroutine.h | 14 +++++++++++--- misc/examples/scheduler.c | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/README.md b/README.md index f14858e7..3628ecd1 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ STC - Smart Template Containers =============================== -### [Version 4.3 RC](#version-history) +### [Version 4.3 RC2](#version-history) --- Description @@ -613,6 +613,8 @@ STC is generally very memory efficient. Memory usage for the different container # Version History ## Version 4.3 +- algo/coroutine.h much improved with new API and more features. +- New cdeq and cqueue implementation(s), using circular buffer. - Removed deprecated uppercase flow-control macro names. - Removed deprecated crandom.h. Use crand.h with new API. - Improved default string hash function. diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index e0952e1f..b92507b8 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -86,12 +86,20 @@ enum { #define cco_closure(Ret, Closure, ...) \ struct Closure { \ - Ret (*coroutine)(struct Closure*); \ - __VA_ARGS__ \ + Ret (*cco_fn)(struct Closure*); \ int cco_state; \ + __VA_ARGS__ \ } -#define cco_resume(closure) (closure)->coroutine(closure) +typedef struct cco_base { + void (*cco_fn)(struct cco_base*); + int cco_state; +} cco_base; + +#define cco_cast(closure) \ + ((cco_base *)(closure) + 0*sizeof((cco_resume(closure), (int*)0 == &(closure)->cco_state))) + +#define cco_resume(closure) (closure)->cco_fn(closure) #define cco_await_on(...) c_MACRO_OVERLOAD(cco_await_on, __VA_ARGS__) #define cco_await_on_1(closure) cco_await_2((cco_resume(closure), cco_done(closure)), ) #define cco_await_on_2(co, func) cco_await_2((func(co), cco_done(co)), ) diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index 54fefc47..59101b4e 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -58,8 +58,8 @@ static bool taskB(struct Task* task) void Use(void) { Scheduler scheduler = c_init(Scheduler, { - {taskA, &scheduler}, - {taskB, &scheduler}, + {.cco_fn=taskA, .sched=&scheduler}, + {.cco_fn=taskB, .sched=&scheduler}, }); while (schedule(&scheduler)) {} -- cgit v1.2.3 From 3aa8a04f4fe38d828006b4713beb390ef526cea7 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Wed, 21 Jun 2023 00:31:59 +0200 Subject: Added cco_switch(x) {}, cco_case(val) {}, and cco_default {} to use inside coroutines. --- include/stc/algo/coroutine.h | 5 +++++ misc/examples/regex2.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index b92507b8..727ce2a8 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -66,6 +66,11 @@ enum { #define cco_initial(co) ((co)->cco_state == 0) #define cco_done(co) ((co)->cco_state == cco_state_done) +/* Always use { } after cco_switch(x), cco_case(val), and cco_default. */ +#define cco_switch(x) for (intmax_t _sw = (intmax_t)(x), _once=1; _once; _once=0) +#define cco_case(val) if (_sw == (intmax_t)(val)) +#define cco_default else + #define cco_routine(co) \ for (int *_state = &(co)->cco_state; *_state != cco_state_done; *_state = cco_state_done) \ _resume: switch (*_state) case 0: // thanks, @liigo! diff --git a/misc/examples/regex2.c b/misc/examples/regex2.c index 5718a1d8..734190cb 100644 --- a/misc/examples/regex2.c +++ b/misc/examples/regex2.c @@ -26,7 +26,7 @@ int main() printf("\ninput: %s\n", s[i].input); c_formatch (j, &re, s[i].input) { - c_forrange (k, cregex_captures(&re)) + c_forrange (k, cregex_captures(&re) + 1) printf(" submatch %lld: %.*s\n", k, c_SV(j.match[k])); } } -- cgit v1.2.3 From bc6b781548c10935ec6e72e42ece905f46105ec3 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Wed, 21 Jun 2023 00:35:30 +0200 Subject: Minor fix. --- include/stc/algo/coroutine.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 727ce2a8..a2496d25 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -69,7 +69,7 @@ enum { /* Always use { } after cco_switch(x), cco_case(val), and cco_default. */ #define cco_switch(x) for (intmax_t _sw = (intmax_t)(x), _once=1; _once; _once=0) #define cco_case(val) if (_sw == (intmax_t)(val)) -#define cco_default else +#define cco_default #define cco_routine(co) \ for (int *_state = &(co)->cco_state; *_state != cco_state_done; *_state = cco_state_done) \ -- cgit v1.2.3 From 08f75cf88252a3a95ee8eb8464295bcd177ec74f Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Wed, 21 Jun 2023 09:11:49 +0200 Subject: Fixed cco_switch: emulation of switch inside coroutines. --- include/stc/algo/coroutine.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index a2496d25..e0cf5488 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -66,9 +66,9 @@ enum { #define cco_initial(co) ((co)->cco_state == 0) #define cco_done(co) ((co)->cco_state == cco_state_done) -/* Always use { } after cco_switch(x), cco_case(val), and cco_default. */ -#define cco_switch(x) for (intmax_t _sw = (intmax_t)(x), _once=1; _once; _once=0) -#define cco_case(val) if (_sw == (intmax_t)(val)) +/* Emulate switch in coro: always use { } after cco_case(val) and cco_default. */ +#define cco_switch(x) for (long long _sw = (long long)(x), _b=0; !_b; _b=1) +#define cco_case(val) if (_b |= _sw == (val)) #define cco_default #define cco_routine(co) \ -- cgit v1.2.3 From e27a51708bf1ee4b22842b4a0924b3ded26f630c Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 21 Jun 2023 13:10:18 +0200 Subject: Update of cstr_append_uninit(). --- include/stc/algo/coroutine.h | 2 +- include/stc/cstr.h | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index e0cf5488..e20fd8ad 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -101,7 +101,7 @@ typedef struct cco_base { int cco_state; } cco_base; -#define cco_cast(closure) \ +#define cco_base_cast(closure) \ ((cco_base *)(closure) + 0*sizeof((cco_resume(closure), (int*)0 == &(closure)->cco_state))) #define cco_resume(closure) (closure)->cco_fn(closure) diff --git a/include/stc/cstr.h b/include/stc/cstr.h index b9b066ad..ce42cf8d 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -77,6 +77,7 @@ extern intptr_t cstr_find_at(const cstr* self, intptr_t pos, const char* sea extern intptr_t cstr_find_sv(const cstr* self, csview search); extern char* cstr_assign_n(cstr* self, const char* str, intptr_t len); extern char* cstr_append_n(cstr* self, const char* str, intptr_t len); +extern char* cstr_append_uninit(cstr *self, intptr_t len); extern bool cstr_getdelim(cstr *self, int delim, FILE *fp); extern void cstr_erase(cstr* self, intptr_t pos, intptr_t len); extern void cstr_u8_erase(cstr* self, intptr_t bytepos, intptr_t u8len); @@ -244,14 +245,6 @@ STC_INLINE cstr_iter cstr_advance(cstr_iter it, intptr_t pos) { STC_INLINE void cstr_clear(cstr* self) { _cstr_set_size(self, 0); } -STC_INLINE char* cstr_append_uninit(cstr *self, intptr_t len) { - intptr_t sz = cstr_size(self); - char* d = cstr_reserve(self, sz + len); - if (!d) return NULL; - _cstr_set_size(self, sz + len); - return d + sz; -} - STC_INLINE int cstr_cmp(const cstr* s1, const cstr* s2) { return strcmp(cstr_str(s1), cstr_str(s2)); } @@ -551,6 +544,14 @@ char* cstr_append_n(cstr* self, const char* str, const intptr_t len) { return r.data; } +char* cstr_append_uninit(cstr *self, intptr_t len) { + cstr_buf r = cstr_buffer(self); + if (r.size + len > r.cap && !(r.data = cstr_reserve(self, r.size*3/2 + len))) + return NULL; + _cstr_set_size(self, r.size + len); + return r.data + r.size; +} + bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { int c = fgetc(fp); if (c == EOF) -- cgit v1.2.3 From e25dec033773ab713a7593a923e2c83745be0b9a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 2 Jul 2023 22:01:46 +0200 Subject: Update in coroutine API. cco_yield, cco_await, cco_await_on, cco_block_on has changed. cco_final: renamed => cco_cleanup: Reverted i_retain template param back to => i_more. --- docs/ccommon_api.md | 62 +++++++++++------- include/stc/algo/coroutine.h | 120 ++++++++++++++++++++-------------- include/stc/algo/sort.h | 2 +- include/stc/cdeq.h | 6 +- include/stc/priv/template2.h | 4 +- misc/benchmarks/various/csort_bench.c | 2 +- misc/examples/cointerleave.c | 11 ++-- misc/examples/coread.c | 7 +- misc/examples/coroutines.c | 54 +++++++-------- misc/examples/dining_philosophers.c | 16 +++-- misc/examples/generator.c | 5 +- misc/examples/scheduler.c | 34 +++++----- misc/examples/triples.c | 10 +-- 13 files changed, 189 insertions(+), 144 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index c9c65156..b30bdda6 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -222,11 +222,11 @@ int main() { } ``` Containers with random access may also be sorted. Even sorting cdeq/cqueue (with ring buffer) is -possible and very fast. Note that `i_retain` must be defined to retain specified template parameters for use by sort: +possible and very fast. Note that `i_more` must be defined to retain specified template parameters for use by sort: ```c #define i_type MyDeq #define i_val int -#define i_retain +#define i_more #include // deque #include #include @@ -313,6 +313,7 @@ the gcd() function. It also ensures that it stops when the diagonal size >= 100: [ [Run this code](https://godbolt.org/z/coqqrfbd5) ] ```c #include +#include struct triples { int n; // input: max number of triples to be generated. @@ -320,22 +321,23 @@ struct triples { int cco_state; // required member }; -bool triples(struct triples* i) { // coroutine +int triples(struct triples* i) { // coroutine cco_routine(i) { for (i->c = 5; i->n; ++i->c) { for (i->a = 1; i->a < i->c; ++i->a) { for (i->b = i->a + 1; i->b < i->c; ++i->b) { if ((int64_t)i->a*i->a + (int64_t)i->b*i->b == (int64_t)i->c*i->c) { - cco_yield(false); - if (--i->n == 0) cco_return; + cco_yield(); + if (--i->n == 0) + cco_return; } } } } - cco_final: + cco_cleanup: puts("done"); } - return true; + return 0; } int gcd(int a, int b) { // greatest common denominator @@ -352,7 +354,7 @@ int main() struct triples t = {.n=INT32_MAX}; int n = 0; - while (!triples(&t)) { + while (triples(&t)) { // Skip triples with GCD(a,b) > 1 if (gcd(t.a, t.b) > 1) continue; @@ -366,28 +368,36 @@ int main() } ``` ### Coroutine API -**Note**: *cco_yield()* may not be called inside a `switch` statement. Use `if-else-if` constructs instead. -To resume the coroutine from where it was suspended with *cco_yield()*, simply call the coroutine again. +To resume the coroutine from where it was suspended with *cco_yield()*: call the coroutine again. + +**Note**: *cco_yield()* / *cco_await()* may not be called inside a `switch` statement; either use +`if-else-if` constructs, or `cco_switch / cco_case / cco_default` for switch-emulation instead. | | Function / operator | Description | |:----------|:-------------------------------------|:----------------------------------------| -| | `cco_final:` | Label for cleanup in coroutine | +| | Function / 'keywords': | | +|`cco_result` | Enum `CCO_DONE=0`, `CCO_YIELD`, `CCO_AWAIT` | Recommended return values in coroutines | +| | Function / 'keywords': | | +| | `cco_cleanup:` | Label for cleanup position in coroutine | | `bool` | `cco_done(co)` | Is coroutine done? | -| | `cco_routine(co) { ... }` | The coroutine closure | -| | `cco_yield()` | Yield/suspend execution | -| | `cco_yield(ret)` | Yield/suspend execution and return ret | -| | `cco_await(promise)` | Await/suspend until promise is true | -| | `cco_await(promise, ret)` | Await/suspend with ret value | -| | `cco_return` | Replaces return. Jump to cco_final: if exist| -| | `cco_return_v(val)` | Yield final value, enter final-state | -| | `cco_closure(Ret, Closure, ...)` | Define coroutine closure struct. | -| `void` | `cco_await_on(closure) { }` | Await on closure to finish | -| `void` | `cco_await_on(co, func) { }` | Await on func(co) to finish | +| | `cco_routine(co) { }` | The coroutine scope | +| | `cco_yield();` | Yield/suspend execution (return CCO_YIELD)| +| | `cco_yield_v();` | Yield/suspend execution (return void) | +| | `cco_yield_v(ret);` | Yield/suspend execution (return ret) | +| | `cco_yield_final();` | Yield final time, enables cleanup-state | +| | `cco_yield_final(val);` | Yield a final value (e.g. CCO_ERROR) | +| | `cco_await(condition);` | Suspend until condition is true (return CCO_AWAIT)| +| | `cco_await_v(condition);` | Suspend until condition is true (return void) | +| | `cco_await_v(condition, ret);` | Suspend until condition is true (return ret)| +| | `cco_await_on(cocall);` | Await on sub-coroutine to finish | +| | `cco_return;` | Return from coroutine (inside cco_routine) | +| | `cco_closure(Closure, ...);` | Define a coroutine closure struct (optional) | | | Semaphores: | | | | `cco_sem` | Semaphore type | +| `cco_sem` | `cco_sem_from(long value)` | Create semaphore | +| | `cco_sem_set(sem, long value)` | Set semaphore value | | | `cco_sem_await(sem)` | Await for the semaphore count > 0 | | | `cco_sem_await(sem, ret)` | Await with ret on the semaphore | -| `cco_sem` | `cco_sem_init(long value)` | Set semaphore value | | | `cco_sem_release(sem)` | Signal the semaphore (count += 1) | | | Timers: | | | | `cco_timer` | Timer type | @@ -401,11 +411,15 @@ To resume the coroutine from where it was suspended with *cco_yield()*, simply c | | From caller side: | | | `void` | `cco_stop(co)` | Next call of coroutine finalizes | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | -| `void` | `cco_block_on(closure) { }` | Run blocking until closure is finished | -| `void` | `cco_block_on(co, func) { }` | Run blocking until func is finished | +| `void` | `cco_block_on(cocall) { }` | Run blocking until cocall is finished | +| `void` | `cco_block_on(cocall, int *result) { }`| Run blocking until cocall is finished | | | Time functions: | | | `double` | `cco_time(void)` | Return secs with usec prec. since Epoch | | | `cco_sleep(double sec)` | Sleep for seconds (msec or usec prec.) | +| | Emulate switch: | | +| | `cco_switch(x) { }` | Like switch syntax | +| | `cco_case(val) { }` | Braces are required. Fall-through if no break; | +| | `cco_default { }` | Default action | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index e20fd8ad..198b0439 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -32,22 +32,22 @@ struct iterpair { int cco_state; // required member }; -bool iterpair(struct iterpair* I) { +int iterpair(struct iterpair* I) { cco_routine(I) { for (I->x = 0; I->x < I->max_x; I->x++) for (I->y = 0; I->y < I->max_y; I->y++) - cco_yield(false); + cco_yield(); - cco_final: // required if there is cleanup code + cco_cleanup: // required if there is cleanup code puts("final"); } - return true; // finished + return 0; // CCO_DONE } int main(void) { struct iterpair it = {.max_x=3, .max_y=3}; int n = 0; - while (!iterpair(&it)) + while (iterpair(&it)) { printf("%d %d\n", it.x, it.y); // example of early stop: @@ -59,12 +59,19 @@ int main(void) { #include "../ccommon.h" enum { - cco_state_final = -1, - cco_state_done = -2, + CCO_STATE_CLEANUP = -1, + CCO_STATE_DONE = -2, }; +typedef enum { + CCO_DONE = 0, + CCO_YIELD = 1, + CCO_AWAIT = 2, + CCO_ERROR = -1, +} cco_result; #define cco_initial(co) ((co)->cco_state == 0) -#define cco_done(co) ((co)->cco_state == cco_state_done) +#define cco_suspended(co) ((co)->cco_state > 0) +#define cco_done(co) ((co)->cco_state == CCO_STATE_DONE) /* Emulate switch in coro: always use { } after cco_case(val) and cco_default. */ #define cco_switch(x) for (long long _sw = (long long)(x), _b=0; !_b; _b=1) @@ -72,83 +79,97 @@ enum { #define cco_default #define cco_routine(co) \ - for (int *_state = &(co)->cco_state; *_state != cco_state_done; *_state = cco_state_done) \ + for (int *_state = &(co)->cco_state; *_state != CCO_STATE_DONE; *_state = CCO_STATE_DONE) \ _resume: switch (*_state) case 0: // thanks, @liigo! -#define cco_yield(ret) \ +#define cco_yield() cco_yield_v(CCO_YIELD) +#define cco_yield_v(ret) \ do { \ *_state = __LINE__; return ret; goto _resume; \ case __LINE__:; \ } while (0) -#define cco_await(...) c_MACRO_OVERLOAD(cco_await, __VA_ARGS__) -#define cco_await_1(promise) cco_await_2(promise, ) -#define cco_await_2(promise, ret) \ +#define cco_await(promise) cco_await_v_2(promise, CCO_AWAIT) +#define cco_await_v(...) c_MACRO_OVERLOAD(cco_await_v, __VA_ARGS__) +#define cco_await_v_1(promise) cco_await_v_2(promise, ) +#define cco_await_v_2(promise, ret) \ do { \ *_state = __LINE__; \ case __LINE__: if (!(promise)) {return ret; goto _resume;} \ } while (0) -#define cco_closure(Ret, Closure, ...) \ - struct Closure { \ - Ret (*cco_fn)(struct Closure*); \ - int cco_state; \ - __VA_ARGS__ \ - } - -typedef struct cco_base { - void (*cco_fn)(struct cco_base*); - int cco_state; -} cco_base; - -#define cco_base_cast(closure) \ - ((cco_base *)(closure) + 0*sizeof((cco_resume(closure), (int*)0 == &(closure)->cco_state))) - -#define cco_resume(closure) (closure)->cco_fn(closure) -#define cco_await_on(...) c_MACRO_OVERLOAD(cco_await_on, __VA_ARGS__) -#define cco_await_on_1(closure) cco_await_2((cco_resume(closure), cco_done(closure)), ) -#define cco_await_on_2(co, func) cco_await_2((func(co), cco_done(co)), ) +/* cco_await_on(): assumes coroutine returns a cco_result value (int) */ +#define cco_await_on(corocall) \ + do { \ + *_state = __LINE__; \ + case __LINE__: { int _r = corocall; if (_r != CCO_DONE) {return _r; goto _resume;} } \ + } while (0) +/* cco_block_on(): assumes coroutine returns a cco_result value (int) */ #define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__) -#define cco_block_on_1(closure) while (cco_resume(closure), !cco_done(closure)) -#define cco_block_on_2(co, func) while (func(co), !cco_done(co)) +#define cco_block_on_1(corocall) while (corocall != CCO_DONE) +#define cco_block_on_2(corocall, res) while ((*(res) = (corocall)) != CCO_DONE) -#define cco_final \ - *_state = cco_state_final; case cco_state_final +#define cco_cleanup \ + *_state = CCO_STATE_CLEANUP; case CCO_STATE_CLEANUP #define cco_return \ do { \ - *_state = *_state >= 0 ? cco_state_final : cco_state_done; \ + *_state = *_state >= 0 ? CCO_STATE_CLEANUP : CCO_STATE_DONE; \ goto _resume; \ } while (0) -#define cco_return_v(value) \ +#define cco_yield_final() cco_yield_final_v(CCO_YIELD) +#define cco_yield_final_v(value) \ do { \ - *_state = *_state >= 0 ? cco_state_final : cco_state_done; \ + *_state = *_state >= 0 ? CCO_STATE_CLEANUP : CCO_STATE_DONE; \ return value; \ } while (0) #define cco_stop(co) \ do { \ int* _s = &(co)->cco_state; \ - if (*_s > 0) *_s = cco_state_final; \ - else if (*_s == 0) *_s = cco_state_done; \ + if (*_s > 0) *_s = CCO_STATE_CLEANUP; \ + else if (*_s == 0) *_s = CCO_STATE_DONE; \ } while (0) #define cco_reset(co) \ (void)((co)->cco_state = 0) +/* + * Closure (optional) + */ + +#define cco_closure(Name, ...) \ + struct Name { \ + int (*cco_fn)(struct Name*); \ + int cco_state; \ + __VA_ARGS__ \ + } + +typedef struct cco_base { + int (*cco_fn)(struct cco_base*); + int cco_state; +} cco_base; + +#define cco_resume(closure) \ + (closure)->cco_fn(closure) + +#define cco_cast(closure) \ + ((cco_base *)(closure) + 0*sizeof((cco_resume(closure), (int*)0 == &(closure)->cco_state))) + /* * Semaphore */ typedef struct { intptr_t count; } cco_sem; -#define cco_sem_await(...) c_MACRO_OVERLOAD(cco_sem_await, __VA_ARGS__) -#define cco_sem_await_1(sem) cco_sem_await_2(sem, ) -#define cco_sem_await_2(sem, ret) \ +#define cco_sem_await(sem) cco_sem_await_v_2(sem, CCO_AWAIT) +#define cco_sem_await_v(...) c_MACRO_OVERLOAD(cco_sem_await_v, __VA_ARGS__) +#define cco_sem_await_v_1(sem) cco_sem_await_v_2(sem, ) +#define cco_sem_await_v_2(sem, ret) \ do { \ - cco_await_2((sem)->count > 0, ret); \ + cco_await_v_2((sem)->count > 0, ret); \ --(sem)->count; \ } while (0) @@ -197,12 +218,13 @@ typedef struct { intptr_t count; } cco_sem; typedef struct { double interval, start; } cco_timer; -#define cco_timer_await(...) c_MACRO_OVERLOAD(cco_timer_await, __VA_ARGS__) -#define cco_timer_await_2(tm, sec) cco_timer_await_3(tm, sec, ) -#define cco_timer_await_3(tm, sec, ret) \ +#define cco_timer_await(tm, sec) cco_timer_await_v_3(tm, sec, CCO_AWAIT) +#define cco_timer_await_v(...) c_MACRO_OVERLOAD(cco_timer_await_v, __VA_ARGS__) +#define cco_timer_await_v_2(tm, sec) cco_timer_await_v_3(tm, sec, ) +#define cco_timer_await_v_3(tm, sec, ret) \ do { \ cco_timer_start(tm, sec); \ - cco_await_2(cco_timer_expired(tm), ret); \ + cco_await_v_2(cco_timer_expired(tm), ret); \ } while (0) static inline void cco_timer_start(cco_timer* tm, double sec) { diff --git a/include/stc/algo/sort.h b/include/stc/algo/sort.h index 002e6499..8365ccc5 100644 --- a/include/stc/algo/sort.h +++ b/include/stc/algo/sort.h @@ -44,7 +44,7 @@ int main() { // ex2: #define i_val int #define i_type IDeq -#define i_retain // retain input template params to be reused by sort.h +#define i_more // retain input template params to be reused by sort.h #include #include diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 2db040f1..bac40f90 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -23,11 +23,11 @@ #define _i_prefix cdeq_ #define _pop _pop_front #define _pull _pull_front -#ifdef i_retain +#ifdef i_more #include "cqueue.h" - #define i_retain + #define i_more #else - #define i_retain + #define i_more #include "cqueue.h" #endif #undef _pop diff --git a/include/stc/priv/template2.h b/include/stc/priv/template2.h index bd8bc5fc..def5d01e 100644 --- a/include/stc/priv/template2.h +++ b/include/stc/priv/template2.h @@ -20,8 +20,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifdef i_retain -#undef i_retain +#ifdef i_more +#undef i_more #else #undef i_type #undef i_tag diff --git a/misc/benchmarks/various/csort_bench.c b/misc/benchmarks/various/csort_bench.c index 93034dde..d434693f 100644 --- a/misc/benchmarks/various/csort_bench.c +++ b/misc/benchmarks/various/csort_bench.c @@ -8,7 +8,7 @@ #define NDEBUG #define i_type Ints #define i_val int -#define i_retain +#define i_more #include #include diff --git a/misc/examples/cointerleave.c b/misc/examples/cointerleave.c index 1ba7b861..61562a5f 100644 --- a/misc/examples/cointerleave.c +++ b/misc/examples/cointerleave.c @@ -15,7 +15,7 @@ static int get_value(struct GenValue* g) { cco_routine(g) { for (g->it = IVec_begin(g->v); g->it.ref; IVec_next(&g->it)) - cco_yield(*g->it.ref); + cco_yield_v(*g->it.ref); } return -1; } @@ -26,10 +26,10 @@ struct Generator { int value; }; -void interleaved(struct Generator* g) +cco_result interleaved(struct Generator* g) { cco_routine(g) { - do { + while (!(cco_done(&g->x) & cco_done(&g->y))) { g->value = get_value(&g->x); if (!cco_done(&g->x)) cco_yield(); @@ -37,8 +37,9 @@ void interleaved(struct Generator* g) g->value = get_value(&g->y); if (!cco_done(&g->y)) cco_yield(); - } while (!(cco_done(&g->x) & cco_done(&g->y))); + } } + return CCO_DONE; } void Use(void) @@ -48,7 +49,7 @@ void Use(void) struct Generator g = {{&a}, {&b}}; - cco_block_on(&g, interleaved) { + cco_block_on(interleaved(&g)) { printf("%d ", g.value); } puts(""); diff --git a/misc/examples/coread.c b/misc/examples/coread.c index 622228c0..a13f6be5 100644 --- a/misc/examples/coread.c +++ b/misc/examples/coread.c @@ -12,7 +12,7 @@ struct file_read { cstr line; }; -void file_read(struct file_read* g) +int file_read(struct file_read* g) { cco_routine(g) { g->fp = fopen(g->filename, "r"); @@ -21,18 +21,19 @@ void file_read(struct file_read* g) cco_await(!cstr_getline(&g->line, g->fp)); - cco_final: + cco_cleanup: printf("finish\n"); cstr_drop(&g->line); if (g->fp) fclose(g->fp); } + return 0; } int main(void) { struct file_read g = {__FILE__}; int n = 0; - cco_block_on(&g, file_read) + cco_block_on(file_read(&g)) { printf("%3d %s\n", ++n, cstr_str(&g.line)); //if (n == 10) cco_stop(&g); diff --git a/misc/examples/coroutines.c b/misc/examples/coroutines.c index 1e900fa1..b8dfaa13 100644 --- a/misc/examples/coroutines.c +++ b/misc/examples/coroutines.c @@ -4,27 +4,26 @@ // Demonstrate to call another coroutine from a coroutine: // First create prime generator, then call fibonacci sequence: -typedef long long llong; -bool is_prime(int64_t i) { - for (llong j=2; j*j <= i; ++j) +bool is_prime(long long i) { + for (long long j=2; j*j <= i; ++j) if (i % j == 0) return false; return true; } struct prime { int count, idx; - llong result, pos; + long long result, pos; int cco_state; }; -bool prime(struct prime* g) { +int prime(struct prime* g) { cco_routine(g) { if (g->result < 2) g->result = 2; if (g->result == 2) { if (g->count-- == 0) cco_return; ++g->idx; - cco_yield(false); + cco_yield(); } g->result += !(g->result & 1); for (g->pos = g->result; g->count > 0; g->pos += 2) { @@ -32,13 +31,13 @@ bool prime(struct prime* g) { --g->count; ++g->idx; g->result = g->pos; - cco_yield(false); + cco_yield(); } } - cco_final: + cco_cleanup: printf("final prm\n"); } - return true; + return 0; } @@ -46,13 +45,14 @@ bool prime(struct prime* g) { struct fibonacci { int count, idx; - llong result, b; + long long result, b; int cco_state; }; -bool fibonacci(struct fibonacci* g) { +int fibonacci(struct fibonacci* g) { assert(g->count < 94); + long long sum; cco_routine(g) { g->idx = 0; g->result = 0; @@ -61,17 +61,17 @@ bool fibonacci(struct fibonacci* g) { if (g->count-- == 0) cco_return; if (++g->idx > 1) { - // NB! locals lasts only until next cco_yield/cco_await! - llong sum = g->result + g->b; + // NB! locals lasts only until next yield/await! + sum = g->result + g->b; g->result = g->b; g->b = sum; } - cco_yield(false); + cco_yield(); } - cco_final: + cco_cleanup: printf("final fib\n"); } - return true; + return 0; } // Combine @@ -82,29 +82,31 @@ struct combined { int cco_state; }; - -void combined(struct combined* g) { +int combined(struct combined* g) { cco_routine(g) { - cco_await(prime(&g->prm)); - cco_await(fibonacci(&g->fib)); + cco_await_on(prime(&g->prm)); + cco_await_on(fibonacci(&g->fib)); // 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)); + cco_await_on(prime(&g->prm)); - cco_final: + cco_cleanup: puts("final combined"); } + return 0; } int main(void) { struct combined c = {.prm={.count=8}, .fib={14}}; + int res; - cco_block_on(&c, combined) { - printf("Prime(%d)=%lld, Fib(%d)=%lld\n", - c.prm.idx, c.prm.result, - c.fib.idx, c.fib.result); + cco_block_on(combined(&c), &res) { + if (res == CCO_YIELD) + printf("Prime(%d)=%lld, Fib(%d)=%lld\n", + c.prm.idx, c.prm.result, + c.fib.idx, c.fib.result); } } diff --git a/misc/examples/dining_philosophers.c b/misc/examples/dining_philosophers.c index e13eb055..61fe67fb 100644 --- a/misc/examples/dining_philosophers.c +++ b/misc/examples/dining_philosophers.c @@ -27,7 +27,7 @@ struct Dining { // Philosopher coroutine -void philosopher(struct Philosopher* p) +int philosopher(struct Philosopher* p) { double duration; cco_routine(p) { @@ -48,14 +48,15 @@ void philosopher(struct Philosopher* p) cco_sem_release(p->right_fork); } - cco_final: + cco_cleanup: printf("Philosopher %d finished\n", p->id); } + return 0; } // Dining coroutine -void dining(struct Dining* d) +int dining(struct Dining* d) { cco_routine(d) { for (int i = 0; i < num_forks; ++i) @@ -68,20 +69,21 @@ void dining(struct Dining* d) } while (1) { - // per-"frame" logic update of all philosophers states + // per-"frame" logic resume each philosopher for (int i = 0; i < num_philosophers; ++i) { philosopher(&d->ph[i]); } cco_yield(); // suspend, return control back to main } - cco_final: + cco_cleanup: for (int i = 0; i < num_philosophers; ++i) { cco_stop(&d->ph[i]); philosopher(&d->ph[i]); } puts("Dining finished"); } + return 0; } int main() @@ -89,13 +91,13 @@ int main() struct Dining dine; cco_reset(&dine); int n=0; - cco_timer tm = cco_timer_from(10.0); // seconds + cco_timer tm = cco_timer_from(15.0); // seconds csrand((uint64_t)time(NULL)); while (!cco_done(&dine)) { if (cco_timer_expired(&tm)) cco_stop(&dine); - dining(&dine); + dining(&dine); // resume cco_sleep(0.001); ++n; } diff --git a/misc/examples/generator.c b/misc/examples/generator.c index 6b4b8407..3ff7a645 100644 --- a/misc/examples/generator.c +++ b/misc/examples/generator.c @@ -14,7 +14,7 @@ typedef struct { int cco_state; } Triple_iter; -void Triple_next(Triple_iter* it) { +int Triple_next(Triple_iter* it) { Triple_value* g = it->ref; cco_routine(it) { @@ -29,9 +29,10 @@ void Triple_next(Triple_iter* it) { } } } - cco_final: + cco_cleanup: it->ref = NULL; } + return 0; } Triple_iter Triple_begin(Triple* g) { diff --git a/misc/examples/scheduler.c b/misc/examples/scheduler.c index 04f7ba4a..c1168850 100644 --- a/misc/examples/scheduler.c +++ b/misc/examples/scheduler.c @@ -2,9 +2,11 @@ #include #include -cco_closure(bool, Task, +struct Task { + int (*fn)(struct Task*); + int cco_state; struct Scheduler* sched; -); +}; #define i_type Scheduler #define i_val struct Task @@ -16,49 +18,49 @@ static bool schedule(Scheduler* sched) Scheduler_pop(sched); if (!cco_done(&task)) - cco_resume(&task); + task.fn(&task); return !Scheduler_empty(sched); } -static bool push_task(const struct Task* task) +static int push_task(const struct Task* task) { Scheduler_push(task->sched, *task); - return false; + return CCO_YIELD; } -static bool taskA(struct Task* task) +static int taskA(struct Task* task) { cco_routine(task) { puts("Hello, from task A"); - cco_yield(push_task(task)); + cco_yield_v(push_task(task)); puts("A is back doing work"); - cco_yield(push_task(task)); + cco_yield_v(push_task(task)); puts("A is back doing more work"); - cco_yield(push_task(task)); + cco_yield_v(push_task(task)); puts("A is back doing even more work"); } - return true; + return 0; } -static bool taskB(struct Task* task) +static int taskB(struct Task* task) { cco_routine(task) { puts("Hello, from task B"); - cco_yield(push_task(task)); + cco_yield_v(push_task(task)); puts("B is back doing work"); - cco_yield(push_task(task)); + cco_yield_v(push_task(task)); puts("B is back doing more work"); } - return true; + return 0; } void Use(void) { Scheduler scheduler = c_init(Scheduler, { - {.cco_fn=taskA, .sched=&scheduler}, - {.cco_fn=taskB, .sched=&scheduler}, + {.fn=taskA, .sched=&scheduler}, + {.fn=taskB, .sched=&scheduler}, }); while (schedule(&scheduler)) {} diff --git a/misc/examples/triples.c b/misc/examples/triples.c index 17e3d40b..a8ca6b47 100644 --- a/misc/examples/triples.c +++ b/misc/examples/triples.c @@ -32,7 +32,7 @@ struct triples { int cco_state; }; -void triples_coro(struct triples* t) { +int triples_coro(struct triples* t) { cco_routine(t) { t->count = 0; for (t->c = 5; t->size; ++t->c) { @@ -46,9 +46,10 @@ void triples_coro(struct triples* t) { } } } - cco_final: + cco_cleanup: puts("done"); } + return 0; } int main() @@ -57,11 +58,10 @@ int main() triples_vanilla(5); puts("\nCoroutine triples:"); - struct triples t = {INT32_MAX}; + struct triples t = {.size=INT32_MAX}; int n = 0; - while (!cco_done(&t)) { - triples_coro(&t); + while (triples_coro(&t)) { if (gcd(t.a, t.b) > 1) continue; if (t.c < 100) -- cgit v1.2.3 From e63a3dd6545261f5236a3d7e1c2be6571871b689 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 4 Jul 2023 09:35:00 +0200 Subject: Removed cco_switch; won't work without state. --- docs/ccommon_api.md | 8 ++------ include/stc/algo/coroutine.h | 9 ++------- 2 files changed, 4 insertions(+), 13 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index b30bdda6..d39f6de6 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -370,8 +370,8 @@ int main() ### Coroutine API To resume the coroutine from where it was suspended with *cco_yield()*: call the coroutine again. -**Note**: *cco_yield()* / *cco_await()* may not be called inside a `switch` statement; either use -`if-else-if` constructs, or `cco_switch / cco_case / cco_default` for switch-emulation instead. +**Note**: *cco_yield()* / *cco_await()* may not be called inside a `switch` statement; use +`if-else-if` constructs instead. | | Function / operator | Description | |:----------|:-------------------------------------|:----------------------------------------| @@ -416,10 +416,6 @@ To resume the coroutine from where it was suspended with *cco_yield()*: call the | | Time functions: | | | `double` | `cco_time(void)` | Return secs with usec prec. since Epoch | | | `cco_sleep(double sec)` | Sleep for seconds (msec or usec prec.) | -| | Emulate switch: | | -| | `cco_switch(x) { }` | Like switch syntax | -| | `cco_case(val) { }` | Braces are required. Fall-through if no break; | -| | `cco_default { }` | Default action | --- ## RAII scope macros diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 198b0439..3a5382f3 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -73,11 +73,6 @@ typedef enum { #define cco_suspended(co) ((co)->cco_state > 0) #define cco_done(co) ((co)->cco_state == CCO_STATE_DONE) -/* Emulate switch in coro: always use { } after cco_case(val) and cco_default. */ -#define cco_switch(x) for (long long _sw = (long long)(x), _b=0; !_b; _b=1) -#define cco_case(val) if (_b |= _sw == (val)) -#define cco_default - #define cco_routine(co) \ for (int *_state = &(co)->cco_state; *_state != CCO_STATE_DONE; *_state = CCO_STATE_DONE) \ _resume: switch (*_state) case 0: // thanks, @liigo! @@ -107,8 +102,8 @@ typedef enum { /* cco_block_on(): assumes coroutine returns a cco_result value (int) */ #define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__) -#define cco_block_on_1(corocall) while (corocall != CCO_DONE) -#define cco_block_on_2(corocall, res) while ((*(res) = (corocall)) != CCO_DONE) +#define cco_block_on_1(corocall) while ((corocall) != CCO_DONE) +#define cco_block_on_2(corocall, result) while ((*(result) = (corocall)) != CCO_DONE) #define cco_cleanup \ *_state = CCO_STATE_CLEANUP; case CCO_STATE_CLEANUP -- cgit v1.2.3 From 313c1d7bb9b92e75801429c1f7f132589860292e Mon Sep 17 00:00:00 2001 From: tylov Date: Tue, 18 Jul 2023 01:36:51 +0200 Subject: Renamed i_native_cmp => i_cmp_native Added c_all_of(), c_any_of(), c_none_of() to algo/filter.h --- README.md | 4 ++-- docs/carc_api.md | 24 +++++++++++++-------- docs/cbox_api.md | 29 +++++++++++++++----------- docs/cdeq_api.md | 23 +++++++++++---------- docs/clist_api.md | 21 ++++++++++--------- docs/cmap_api.md | 39 +++++++++++++++++------------------ docs/cpque_api.md | 18 ++++++++-------- docs/cqueue_api.md | 16 +++++++------- docs/cset_api.md | 26 +++++++++++------------ docs/csmap_api.md | 36 ++++++++++++++++---------------- docs/csset_api.md | 18 ++++++++-------- docs/cstack_api.md | 16 +++++++------- docs/cvec_api.md | 23 +++++++++++---------- include/stc/algo/coroutine.h | 13 ++++++++---- include/stc/algo/filter.h | 16 +++++++++++++- include/stc/priv/template.h | 8 +++---- include/stc/priv/template2.h | 2 +- misc/benchmarks/various/csort_bench.c | 2 +- misc/examples/arc_demo.c | 2 +- misc/examples/arcvec_erase.c | 2 +- misc/examples/demos.c | 2 +- misc/examples/intrusive.c | 2 +- misc/examples/list.c | 2 +- misc/examples/lower_bound.c | 2 +- misc/examples/new_list.c | 2 +- misc/examples/new_sptr.c | 2 +- 26 files changed, 191 insertions(+), 159 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/README.md b/README.md index b7e06790..6bbe50ee 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ struct Point { float x, y; }; #include // cvec_pnt: vector of struct Point #define i_key int -#define i_native_cmp // enable sort/search. Use native `<` and `==` operators +#define i_cmp_native // enable sort/search. Use native `<` and `==` operators #include // clist_int: singly linked list #define i_key int @@ -619,7 +619,7 @@ STC is generally very memory efficient. Memory usage for the different container - cspan: Support for column-major (fortran order) multidim spans and transposed views. - Removed default comparison for clist, cvec and cdeq (as with cstack and cqueue). - Using i_key_str, i_keyclass, i_keyboxed still expects comparisons defined. - - Define i_native_cmp to enable built-in i_key types comparisons (<, ==). + - Define i_cmp_native to enable built-in i_key types comparisons (<, ==). - cstr and csview are now shared linked by default. Static linking by defining i_static. - New cdeq and cqueue implementation(s), using circular buffer. - Renamed i_extern => i_import. diff --git a/docs/carc_api.md b/docs/carc_api.md index 8b7b67a1..fb79019a 100644 --- a/docs/carc_api.md +++ b/docs/carc_api.md @@ -20,15 +20,21 @@ See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory ## Header file and declaration ```c -#define i_type // full typename of the carc -#define i_key // element type: REQUIRED - -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyto // convertion func i_key* => i_keyraw: REQUIRED IF i_keyraw defined. -#define i_keyfrom // convertion func i_keyraw => i_key - -#define i_opt c_no_atomic // Non-atomic reference counting, like Rust Rc. -#define i_tag // alternative typename: carc_{i_tag}. i_tag defaults to i_key +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // carc container type name +#define i_cmp // three-way compareison. REQUIRED IF i_key is a non-integral type + // Note that containers of carcs will "inherit" i_cmp + // when using carc in containers with i_valboxed MyArc - ie. the i_type. +#define i_cmp_native // define instead of i_cmp only when i_key is an integral/native-type. +#define i_keydrop // destroy element func - defaults to empty destruct +#define i_keyclone // REQUIRED if i_keydrop is defined, unless 'i_opt c_no_clone' is defined. + +#define i_keyraw // convertion type (lookup): default to {i_key} +#define i_keyto // convertion func i_key* => i_keyraw: REQUIRED IF i_keyraw defined. +#define i_keyfrom // from-raw func. + +#define i_opt c_no_atomic // Non-atomic reference counting, like Rust Rc. +#define i_tag // alternative typename: carc_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/cbox_api.md b/docs/cbox_api.md index b6c76d2f..0e6fca64 100644 --- a/docs/cbox_api.md +++ b/docs/cbox_api.md @@ -14,18 +14,23 @@ See similar c++ class [std::unique_ptr](https://en.cppreference.com/w/cpp/memory ## Header file and declaration ```c -#define i_type // full typename of the cbox -#define i_key // element type: REQUIRED -#define i_cmp // three-way compare two i_key* : REQUIRED IF i_key is a non-integral type -#define i_keydrop // destroy element func - defaults to empty destruct -#define i_keyclone // REQUIRED if i_keydrop is defined, unless 'i_opt c_no_clone' is defined. - -#define i_keyraw // convertion type (lookup): default to {i_key} -#define i_keyto // convertion func i_key* => i_keyraw: REQUIRED IF i_keyraw defined. -#define i_keyfrom // from-raw func. - -#define i_keyclass // alt. to i_key: REQUIRES that {i_key}_clone, {i_key}_drop, {i_keyraw}_cmp exist. -#define i_tag // alternative typename: cbox_{i_tag}. i_tag defaults to i_key +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // cbox container type name +#define i_cmp // three-way compareison. REQUIRED IF i_key is a non-integral type + // Note that containers of carcs will "inherit" i_cmp + // when using carc in containers with i_valboxed MyArc - ie. the i_type. +#define i_cmp_native // define instead of i_cmp only when i_key is an integral/native-type. +#define i_keydrop // destroy element func - defaults to empty destruct +#define i_keyclone // REQUIRED if i_keydrop is defined, unless 'i_opt c_no_clone' is defined. + +#define i_keyraw // convertion type (lookup): default to {i_key} +#define i_keyto // convertion func i_key* => i_keyraw: REQUIRED IF i_keyraw defined. +#define i_keyfrom // from-raw func. + +#define i_tag // alternative typename: cbox_{i_tag}. i_tag defaults to i_key +#define i_keyclass // Use instead of i_key when functions {i_key}_clone, + // {i_key}_drop and {i_keyraw}_cmp exist. +#define i_keyboxed // Use instead of i_key when key is a carc- or a cbox-type. #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index c6de6cd6..38de7f66 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -10,17 +10,18 @@ See the c++ class [std::deque](https://en.cppreference.com/w/cpp/container/deque ## Header file and declaration ```c -#define i_type // full typename of the container -#define i_key // value: REQUIRED -#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type -#define i_keydrop // destroy value func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined - -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_keyto // convertion func i_key* => i_keyraw - -#define i_tag // alternative typename: cdeq_{i_tag}. i_tag defaults to i_key +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // cdeq container type name +#define i_cmp // three-way compare of two i_keyraw*. +#define i_cmp_native // define instead of i_cmp only when i_key is an integral/native-type. +#define i_keydrop // destroy value func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop is defined + +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_keyto // convertion func i_key* => i_keyraw + +#define i_tag // alternative typename: cdeq_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/clist_api.md b/docs/clist_api.md index 3d785789..d8d614c2 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -22,16 +22,17 @@ See the c++ class [std::list](https://en.cppreference.com/w/cpp/container/list) ## Header file and declaration ```c -#define i_type // container type name (default: clist_{i_key}) -#define i_key // value: REQUIRED -#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type -#define i_keydrop // destroy value func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined - -#define i_keyraw // convertion "raw" type (default: {i_key}) -#define i_keyto // convertion func i_key* => i_keyraw -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_tag // alternative typename: cpque_{i_tag}. i_tag defaults to i_key +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // clist container type name +#define i_cmp // three-way compare two i_keyraw* +#define i_cmp_native // define instead of i_cmp only when i_key is an integral/native-type. +#define i_keydrop // destroy value func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop defined + +#define i_keyraw // convertion "raw" type (default: {i_key}) +#define i_keyto // convertion func i_key* => i_keyraw +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_tag // alternative typename: cpque_{i_tag}. i_tag defaults to i_key #include ``` diff --git a/docs/cmap_api.md b/docs/cmap_api.md index eca350b4..17f27662 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -17,26 +17,25 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain ## Header file and declaration ```c -#define i_type // container type name (default: cmap_{i_key}) -#define i_key // hash key: REQUIRED -#define i_val // map value: REQUIRED -#define i_hash // hash func i_keyraw*: REQUIRED IF i_keyraw is non-pod type -#define i_eq // equality comparison two i_keyraw*: REQUIRED IF i_keyraw is a - // non-integral type. Three-way i_cmp may alternatively be specified. -#define i_keydrop // destroy key func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_keyto // convertion func i_key* => i_keyraw - -#define i_valdrop // destroy value func - defaults to empty destruct -#define i_valclone // REQUIRED IF i_valdrop defined -#define i_valraw // convertion "raw" type - defaults to i_val -#define i_valfrom // convertion func i_valraw => i_val -#define i_valto // convertion func i_val* => i_valraw - -#define i_tag // alternative typename: cmap_{i_tag}. i_tag defaults to i_val -#define i_expandby // default 1. If 2, table expand 2x (else 1.5x) +#define i_key // key type: REQUIRED. +#define i_val // mapped value type: REQUIRED. +#define i_type // container type name (default: cmap_{i_key}) +#define i_hash // hash func i_keyraw*: REQUIRED IF i_keyraw is non-pod type +#define i_eq // equality comparison two i_keyraw*: REQUIRED IF i_keyraw is a + // non-integral type. Three-way i_cmp may alternatively be specified. +#define i_keydrop // destroy key func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop defined +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_keyto // convertion func i_key* => i_keyraw + +#define i_valdrop // destroy value func - defaults to empty destruct +#define i_valclone // REQUIRED IF i_valdrop defined +#define i_valraw // convertion "raw" type - defaults to i_val +#define i_valfrom // convertion func i_valraw => i_val +#define i_valto // convertion func i_val* => i_valraw + +#define i_tag // alternative typename: cmap_{i_tag}. i_tag defaults to i_val #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 5b63dfd1..4cde927b 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -8,17 +8,17 @@ See the c++ class [std::priority_queue](https://en.cppreference.com/w/cpp/contai ## Header file and declaration ```c -#define i_type // define type name of the container (default cpque_{i_key}) -#define i_key // value: REQUIRED -#define i_less // compare two i_key* : REQUIRED IF i_key/i_keyraw is a non-integral type -#define i_keydrop // destroy value func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // cpque container type name +#define i_less // compare two i_key* : REQUIRED IF i_key/i_keyraw is a non-integral type +#define i_keydrop // destroy value func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop defined -#define i_keyraw // convertion type -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_keyto // convertion func i_key* => i_keyraw. +#define i_keyraw // convertion type +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_keyto // convertion func i_key* => i_keyraw. -#define i_tag // alternative typename: cpque_{i_tag}. i_tag defaults to i_key +#define i_tag // alternative typename: cpque_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/cqueue_api.md b/docs/cqueue_api.md index b324e5fc..1834baf9 100644 --- a/docs/cqueue_api.md +++ b/docs/cqueue_api.md @@ -7,16 +7,16 @@ See the c++ class [std::queue](https://en.cppreference.com/w/cpp/container/queue ## Header file and declaration ```c -#define i_type // container type name (default: cqueue_{i_key}) -#define i_key // value: REQUIRED -#define i_keydrop // destroy value func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // cqueue container type name +#define i_keydrop // destroy value func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop defined -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_keyto // convertion func i_key* => i_keyraw +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_keyto // convertion func i_key* => i_keyraw -#define i_tag // alternative typename: cqueue_{i_tag}. i_tag defaults to i_key +#define i_tag // alternative typename: cqueue_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/cset_api.md b/docs/cset_api.md index e894ad4f..928d63a8 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -7,19 +7,19 @@ A **cset** is an associative container that contains a set of unique objects of ## Header file and declaration ```c -#define i_type // container type name (default: cset_{i_key}) -#define i_key // hash key: REQUIRED. -#define i_hash // hash func: REQUIRED IF i_keyraw is a non-pod type. -#define i_eq // equality comparison two i_keyraw*: !i_cmp is used if not defined. -#define i_keydrop // destroy key func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined - -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy -#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy - -#define i_tag // alternative typename: cmap_{i_tag}. i_tag defaults to i_key -#define i_expandby // default 1. If 2, table expand 2x (else 1.5x) +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // container type name +#define i_hash // hash func i_keyraw*: REQUIRED IF i_keyraw is non-pod type +#define i_eq // equality comparison two i_keyraw*: REQUIRED IF i_keyraw is a + // non-integral type. Three-way i_cmp may alternatively be specified. +#define i_keydrop // destroy key func: defaults to empty destruct +#define i_keyclone // clone func: REQUIRED IF i_keydrop defined + +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy +#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy + +#define i_tag // alternative typename: cmap_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 099d7dfc..afaf49f3 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -15,24 +15,24 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo ## Header file and declaration ```c -#define i_type // container type name (default: cmap_{i_key}) -#define i_key // key: REQUIRED -#define i_val // value: REQUIRED -#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type - -#define i_keydrop // destroy key func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_valdrop defined -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_keyto // convertion func i_key* => i_keyraw - -#define i_valdrop // destroy value func - defaults to empty destruct -#define i_valclone // REQUIRED IF i_valdrop defined -#define i_valraw // convertion "raw" type - defaults to i_val -#define i_valfrom // convertion func i_valraw => i_val -#define i_valto // convertion func i_val* => i_valraw - -#define i_tag // alternative typename: csmap_{i_tag}. i_tag defaults to i_val +#define i_key // key type: REQUIRED. +#define i_val // mapped value type: REQUIRED. +#define i_type // container type name (default: cmap_{i_key}) +#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type + +#define i_keydrop // destroy key func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_valdrop defined +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_keyto // convertion func i_key* => i_keyraw + +#define i_valdrop // destroy value func - defaults to empty destruct +#define i_valclone // REQUIRED IF i_valdrop defined +#define i_valraw // convertion "raw" type - defaults to i_val +#define i_valfrom // convertion func i_valraw => i_val +#define i_valto // convertion func i_val* => i_valraw + +#define i_tag // alternative typename: csmap_{i_tag}. i_tag defaults to i_val #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/csset_api.md b/docs/csset_api.md index aef3af3c..21e38f61 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -8,17 +8,17 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo ## Header file and declaration ```c -#define i_type // full typename of the container -#define i_key // key: REQUIRED -#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type -#define i_keydrop // destroy key func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // container type name +#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type +#define i_keydrop // destroy key func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop defined -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy -#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key - defaults to plain copy +#define i_keyto // convertion func i_key* => i_keyraw - defaults to plain copy -#define i_tag // alternative typename: csset_{i_tag}. i_tag defaults to i_key +#define i_tag // alternative typename: csset_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/cstack_api.md b/docs/cstack_api.md index e799b152..fb629392 100644 --- a/docs/cstack_api.md +++ b/docs/cstack_api.md @@ -8,16 +8,16 @@ See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack ## Header file and declaration ```c -#define i_type // full typename of the container -#define i_key // value: REQUIRED -#define i_keydrop // destroy value func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_type // container type name +#define i_keydrop // destroy value func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop defined -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_keyto // convertion func i_key* => i_keyraw +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_keyto // convertion func i_key* => i_keyraw -#define i_tag // alternative typename: cstack_{i_tag}. i_tag defaults to i_key +#define i_tag // alternative typename: cstack_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/docs/cvec_api.md b/docs/cvec_api.md index d38ef23f..9cba74b5 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -12,17 +12,18 @@ See the c++ class [std::vector](https://en.cppreference.com/w/cpp/container/vect ## Header file and declaration ```c -#define i_type // full typename of the container -#define i_key // value: REQUIRED -#define i_cmp // three-way compare two i_keyraw* : REQUIRED IF i_keyraw is a non-integral type -#define i_keydrop // destroy value func - defaults to empty destruct -#define i_keyclone // REQUIRED IF i_keydrop defined - -#define i_keyraw // convertion "raw" type - defaults to i_key -#define i_keyfrom // convertion func i_keyraw => i_key -#define i_keyto // convertion func i_key* => i_keyraw - -#define i_tag // alternative typename: cvec_{i_tag}. i_tag defaults to i_key +#define i_type // container type name +#define i_key // element type: REQUIRED. Note: i_val* may be specified instead of i_key*. +#define i_cmp // three-way compare two i_keyraw* +#define i_cmp_native // define instead of i_cmp only when i_key is an integral/native-type. +#define i_keydrop // destroy value func - defaults to empty destruct +#define i_keyclone // REQUIRED IF i_keydrop defined + +#define i_keyraw // convertion "raw" type - defaults to i_key +#define i_keyfrom // convertion func i_keyraw => i_key +#define i_keyto // convertion func i_key* => i_keyraw + +#define i_tag // alternative typename: cvec_{i_tag}. i_tag defaults to i_key #include ``` `X` should be replaced by the value of `i_tag` in all of the following documentation. diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index 3a5382f3..e4c0915c 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -178,17 +178,22 @@ typedef struct { intptr_t count; } cco_sem; #ifdef _WIN32 #ifdef __cplusplus - #define _c_LINKC extern "C" __declspec(dllimport) + #define _c_LINKC extern "C" __declspec(dllimport) #else - #define _c_LINKC __declspec(dllimport) + #define _c_LINKC __declspec(dllimport) + #endif + #if _WIN32_WINNT < _WIN32_WINNT_WIN8 || defined __TINYC__ + #define _c_getsystime GetSystemTimeAsFileTime + #else + #define _c_getsystime GetSystemTimePreciseAsFileTime #endif struct _FILETIME; - _c_LINKC void GetSystemTimePreciseAsFileTime(struct _FILETIME*); + _c_LINKC void _c_getsystime(struct _FILETIME*); _c_LINKC void Sleep(unsigned long); static inline double cco_time(void) { /* seconds since epoch */ unsigned long long quad; /* 64-bit value representing 1/10th usecs since Jan 1 1601, 00:00 UTC */ - GetSystemTimePreciseAsFileTime((struct _FILETIME*)&quad); + _c_getsystime((struct _FILETIME*)&quad); return (double)(quad - 116444736000000000ULL)*1e-7; /* time diff Jan 1 1601-Jan 1 1970 in 1/10th usecs */ } diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h index 4a227927..1a62c3e1 100644 --- a/include/stc/algo/filter.h +++ b/include/stc/algo/filter.h @@ -85,6 +85,21 @@ int main(void) if (it.ref == _endref) it.ref = NULL; \ } while (0) +#define c_all_of(boolptr, it, C, cnt, pred) do { \ + C##_iter it; \ + c_find_if_4(it, C, cnt, !(pred)); \ + *(boolptr) = it.ref == NULL; \ +} while (0) +#define c_any_of(boolptr, it, C, cnt, pred) do { \ + C##_iter it; \ + c_find_if_4(it, C, cnt, pred); \ + *(boolptr) = it.ref != NULL; \ +} while (0) +#define c_none_of(boolptr, it, C, cnt, pred) do { \ + C##_iter it; \ + c_find_if_4(it, C, cnt, pred); \ + *(boolptr) = it.ref == NULL; \ +} while (0) // Use with: clist, cmap, cset, csmap, csset: #define c_erase_if(it, C, cnt, pred) do { \ @@ -95,7 +110,6 @@ int main(void) } \ } while (0) - // Use with: cstack, cvec, cdeq, cqueue: #define c_eraseremove_if(it, C, cnt, pred) do { \ C* _cnt = &cnt; \ diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h index 30ed5732..ccdce718 100644 --- a/include/stc/priv/template.h +++ b/include/stc/priv/template.h @@ -103,7 +103,7 @@ #define i_no_emplace #endif #if c_option(c_native_cmp) - #define i_native_cmp + #define i_cmp_native #endif #if c_option(c_no_clone) || defined _i_carc #define i_no_clone @@ -203,10 +203,10 @@ #endif #ifndef i_no_cmp - #if defined i_cmp || defined i_less || defined i_native_cmp + #if defined i_cmp || defined i_less || defined i_cmp_native #define _i_has_cmp #endif - #if defined i_eq || defined i_native_cmp + #if defined i_eq || defined i_cmp_native #define _i_has_eq #endif @@ -228,7 +228,7 @@ #endif #endif -#if !defined i_hash && (!(defined _i_cbox || defined _i_carc) || defined i_native_cmp) +#if !defined i_hash && (!(defined _i_cbox || defined _i_carc) || defined i_cmp_native) #define i_hash c_default_hash #endif diff --git a/include/stc/priv/template2.h b/include/stc/priv/template2.h index def5d01e..351defde 100644 --- a/include/stc/priv/template2.h +++ b/include/stc/priv/template2.h @@ -67,7 +67,7 @@ #undef i_realloc #undef i_free -#undef i_native_cmp +#undef i_cmp_native #undef i_no_cmp #undef i_no_hash #undef i_no_clone diff --git a/misc/benchmarks/various/csort_bench.c b/misc/benchmarks/various/csort_bench.c index f6b7f1db..793a0503 100644 --- a/misc/benchmarks/various/csort_bench.c +++ b/misc/benchmarks/various/csort_bench.c @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) { Ints a = Ints_with_capacity(size); for (i = 0; i < size; i++) - *Ints_push(&a, romutrio(s) & (1U << 30) - 1); + Ints_push(&a, romutrio(s) & (1U << 30) - 1); testsort(&a, size, "random"); for (i = 0; i < 20; i++) printf(" %d", (int)*Ints_at(&a, i)); diff --git a/misc/examples/arc_demo.c b/misc/examples/arc_demo.c index 87d64e67..929a48a1 100644 --- a/misc/examples/arc_demo.c +++ b/misc/examples/arc_demo.c @@ -11,7 +11,7 @@ void int_drop(int* x) { #define i_type Arc // set type name to be defined (instead of 'carc_int') #define i_key int #define i_keydrop int_drop // optional, just to display the elements destroyed -#define i_native_cmp // use int comparison (x < y, x == y). +#define i_cmp_native // use int comparison (x < y, x == y). #include // Arc #define i_keyboxed Arc // note: use i_keyboxed instead of i_key for carc/cbox elements diff --git a/misc/examples/arcvec_erase.c b/misc/examples/arcvec_erase.c index addef8b7..ba54c1c7 100644 --- a/misc/examples/arcvec_erase.c +++ b/misc/examples/arcvec_erase.c @@ -5,7 +5,7 @@ void show_drop(int* x) { printf("drop: %d\n", *x); } #define i_type Arc #define i_key int #define i_keydrop show_drop -#define i_native_cmp // enable sort/search for int type +#define i_cmp_native // enable sort/search for int type #include // Shared pointer to int #define i_type Vec diff --git a/misc/examples/demos.c b/misc/examples/demos.c index ecc89f2e..1a604d9f 100644 --- a/misc/examples/demos.c +++ b/misc/examples/demos.c @@ -74,7 +74,7 @@ void vectordemo2(void) #define i_key int #define i_tag ix -#define i_native_cmp +#define i_cmp_native #include void listdemo1(void) diff --git a/misc/examples/intrusive.c b/misc/examples/intrusive.c index 4fca654b..c22ed260 100644 --- a/misc/examples/intrusive.c +++ b/misc/examples/intrusive.c @@ -4,7 +4,7 @@ #define i_type List #define i_key int -#define i_native_cmp +#define i_cmp_native #include void printList(List list) { diff --git a/misc/examples/list.c b/misc/examples/list.c index fa33305a..ad8bebb8 100644 --- a/misc/examples/list.c +++ b/misc/examples/list.c @@ -5,7 +5,7 @@ #define i_type DList #define i_key double -#define i_native_cmp +#define i_cmp_native #include int main(void) { diff --git a/misc/examples/lower_bound.c b/misc/examples/lower_bound.c index e5d816e9..bea828f2 100644 --- a/misc/examples/lower_bound.c +++ b/misc/examples/lower_bound.c @@ -1,7 +1,7 @@ #include #define i_key int -#define i_native_cmp +#define i_cmp_native #include #define i_key int diff --git a/misc/examples/new_list.c b/misc/examples/new_list.c index 9676e7b4..2112bf1f 100644 --- a/misc/examples/new_list.c +++ b/misc/examples/new_list.c @@ -27,7 +27,7 @@ int point_cmp(const Point* a, const Point* b) { #include #define i_key float -#define i_native_cmp // use < and == operators for comparison +#define i_cmp_native // use < and == operators for comparison #include void MyStruct_drop(MyStruct* s); diff --git a/misc/examples/new_sptr.c b/misc/examples/new_sptr.c index 7fef5d1f..3c6fa16c 100644 --- a/misc/examples/new_sptr.c +++ b/misc/examples/new_sptr.c @@ -15,7 +15,7 @@ uint64_t Person_hash(const Person* p); #define i_type IPtr #define i_key int #define i_keydrop(x) printf("drop: %d\n", *x) -#define i_native_cmp +#define i_cmp_native #include #define i_type IPStack -- cgit v1.2.3 From 224a04f7fa7549ed94d2a1415eb25829e39a7cca Mon Sep 17 00:00:00 2001 From: tylov Date: Thu, 20 Jul 2023 14:42:00 +0200 Subject: Added Task-object to coroutines and true stackless execution. --- docs/ccommon_api.md | 5 ++++- docs/cspan_api.md | 14 ++++++------- include/stc/algo/coroutine.h | 50 ++++++++++++++++++++++++++++++-------------- include/stc/cspan.h | 12 +++++------ include/stc/cstr.h | 34 +++++++++++++++--------------- 5 files changed, 68 insertions(+), 47 deletions(-) (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index e053f743..1e1ae1aa 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -390,7 +390,10 @@ cco_routine scope; Use `if-else-if` constructs instead. | | `cco_await_v(condition, ret);` | Suspend until condition is true (return ret)| | | `cco_await_on(cocall);` | Await on sub-coroutine to finish (return its ret) | | | `cco_return;` | Return from coroutine (inside cco_routine) | -| | `cco_closure(Closure, ...);` | Define a coroutine closure struct (optional) | +| | Task objects: | | +| | `cco_task_struct(Name, ...);` | Define a coroutine task struct | +| | `cco_await_task(task, ...);` | Await for task to finish or optionally yield a value | +| | `cco_block_task(task);` | Run blocking until task is finished (stackless) | | | Semaphores: | | | | `cco_sem` | Semaphore type | | `cco_sem` | `cco_sem_from(long value)` | Create semaphore | diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 09821450..e1c92bbf 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -26,14 +26,14 @@ i.e., it may be expanded multiple times. However, all index arguments are safe, `cspan_at(&ms3, i++, j++, k++)` is allowed. If the number of arguments does not match the span rank, a compile error is issued. Runtime bounds checks are enabled by default (define `STC_NDEBUG` or `NDEBUG` to disable). ```c -SpanType cspan_init(T SpanType, {v1, v2, ...}); // make a 1-d cspan from values -SpanType cspan_from(STCContainer* cnt); // make a 1-d cspan from compatible STC container -SpanType cspan_from_array(ValueType array[]); // make a 1-d cspan from C array - +SpanType cspan_init(TYPE SpanType, {v1, v2, ...}); // make a 1-d cspan from values +SpanType cspan_from(STCContainer* cnt); // make a 1-d cspan from a cvec, cstack, cpque (heap) +SpanType cspan_from_array(ValueType array[]); // make a 1-d cspan from a C array + intptr_t cspan_size(const SpanTypeN* self); // return number of elements intptr_t cspan_rank(const SpanTypeN* self); // dimensions; compile time constant intptr_t cspan_index(const SpanTypeN* self, intptr_t x, ..); // index of element - + ValueType* cspan_at(const SpanTypeN* self, intptr_t x, ...); // #args must match input span rank ValueType* cspan_front(const SpanTypeN* self); ValueType* cspan_back(const SpanTypeN* self); @@ -55,13 +55,13 @@ SpanType2 cspan_subspan2(const SpanType2* span, intptr_t offset, intptr_t SpanType3 cspan_subspan3(const SpanType3* span, intptr_t offset, intptr_t count); // create a sub md span of lower rank. Like e.g. cspan_slice(Span2, &ms4, {x}, {y}, {c_ALL}, {c_ALL}); -OutSpan1 cspan_submd2(const SpanType2* parent, intptr_t x); // return a 1d subspan from a 2d span. +OutSpan cspan_submd2(const SpanType2* parent, intptr_t x); // return a 1d subspan from a 2d span. OutSpanN cspan_submd3(const SpanType3* parent, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. OutSpanN cspan_submd4(const SpanType4* parent, intptr_t x, ...); // number of args decides rank of output span. // general slicing of an md span. // {i}: reduce rank. {i,c_END}: slice to end. {c_ALL}: use full extent. -OutSpanN cspan_slice(TYPE OutSpanN, const SpanTypeN* parent, {x0,x1}, {y0,y1}.., {N0,N1}); +OutSpanN cspan_slice(TYPE OutSpanN, const SpanTypeM* parent, {x0,x1}, {y0,y1}.., {N0,N1}); ``` ## TypesPd | Type name | Type definition / usage | Used to represent... | diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h index e4c0915c..7c6989c3 100644 --- a/include/stc/algo/coroutine.h +++ b/include/stc/algo/coroutine.h @@ -64,9 +64,8 @@ enum { }; typedef enum { CCO_DONE = 0, - CCO_YIELD = 1, - CCO_AWAIT = 2, - CCO_ERROR = -1, + CCO_AWAIT = 1<<0, + CCO_YIELD = 1<<1, } cco_result; #define cco_initial(co) ((co)->cco_state == 0) @@ -132,26 +131,45 @@ typedef enum { (void)((co)->cco_state = 0) /* - * Closure (optional) + * Tasks (optional) */ -#define cco_closure(Name, ...) \ +struct cco_runtime; + +#define cco_task_struct(Name, ...) \ struct Name { \ - int (*cco_fn)(struct Name*); \ - int cco_state; \ + int (*cco_fn)(struct Name*, struct cco_runtime*); \ + int cco_state, cco_expect; \ __VA_ARGS__ \ } -typedef struct cco_base { - int (*cco_fn)(struct cco_base*); - int cco_state; -} cco_base; +typedef cco_task_struct(cco_task, /**/) cco_task; + +typedef struct cco_runtime { + int result, top; + cco_task* stack[]; +} cco_runtime; + +#define cco_cast_task(task) \ + ((cco_task *)(task) + 0*sizeof((task)->cco_fn(task, (cco_runtime*)0) + ((int*)0 == &(task)->cco_state))) -#define cco_resume(closure) \ - (closure)->cco_fn(closure) +#define cco_resume(task, rt) \ + (task)->cco_fn(task, rt) -#define cco_cast(closure) \ - ((cco_base *)(closure) + 0*sizeof((cco_resume(closure), (int*)0 == &(closure)->cco_state))) +#define cco_block_task(...) c_MACRO_OVERLOAD(cco_block_task, __VA_ARGS__) +#define cco_block_task_1(task) cco_block_task_3(task, rt, 16) +#define cco_block_task_3(task, rt, STACKDEPTH) \ + for (struct { int result, top; cco_task* stack[STACKDEPTH]; } rt = {.stack={cco_cast_task(task)}}; \ + (((rt.result = cco_resume(rt.stack[rt.top], (cco_runtime*)&rt)) & rt.stack[rt.top]->cco_expect) || --rt.top >= 0); ) + +#define cco_await_task(...) c_MACRO_OVERLOAD(cco_await_task, __VA_ARGS__) +#define cco_await_task_2(task, rt) cco_await_task_3(task, rt, CCO_DONE) +#define cco_await_task_3(task, rt, resultbits) \ + do { \ + cco_runtime* _rt = rt; \ + (_rt->stack[++_rt->top] = cco_cast_task(task))->cco_expect = ~(resultbits); \ + cco_yield_v(CCO_AWAIT); \ + } while (0) /* * Semaphore @@ -182,7 +200,7 @@ typedef struct { intptr_t count; } cco_sem; #else #define _c_LINKC __declspec(dllimport) #endif - #if _WIN32_WINNT < _WIN32_WINNT_WIN8 || defined __TINYC__ + #if 1 // _WIN32_WINNT < _WIN32_WINNT_WIN8 || defined __TINYC__ #define _c_getsystime GetSystemTimeAsFileTime #else #define _c_getsystime GetSystemTimePreciseAsFileTime diff --git a/include/stc/cspan.h b/include/stc/cspan.h index dcb02961..08045010 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -80,19 +80,19 @@ int demo2() { STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \ return (Self){.data=raw, .shape={(int32_t)n}}; \ } \ - STC_INLINE Self Self##_slice_(Self##_value* v, const int32_t shape[], const int32_t stri[], \ + STC_INLINE Self Self##_slice_(Self##_value* d, const int32_t shape[], const int32_t stri[], \ const int rank, const int32_t a[][2]) { \ - Self s; s.data = v; int outrank; \ - s.data += _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \ + Self s; int outrank; \ + s.data = d + _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \ c_assert(outrank == RANK); \ return s; \ } \ STC_INLINE Self##_iter Self##_begin(const Self* self) { \ - Self##_iter it = {.ref=self->data, .pos={0}, ._s=self}; \ + Self##_iter it = {.ref=self->data, ._s=self}; \ return it; \ } \ STC_INLINE Self##_iter Self##_end(const Self* self) { \ - Self##_iter it = {.ref=NULL}; \ + Self##_iter it = {0}; \ return it; \ } \ STC_INLINE void Self##_next(Self##_iter* it) { \ @@ -120,7 +120,7 @@ using_cspan_tuple(7); using_cspan_tuple(8); #define cspan_init(SpanType, ...) \ {.data=(SpanType##_value[])__VA_ARGS__, .shape={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}, .stride={.d={1}}} -/* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ +/* create a cspan from a cvec, cstack, or cpque (heap) */ #define cspan_from(container) \ {.data=(container)->data, .shape={(int32_t)(container)->_len}, .stride={.d={1}}} diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 17943ad5..2648e267 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -170,14 +170,14 @@ STC_INLINE intptr_t cstr_capacity(const cstr* self) // utf8 methods defined in/depending on src/utf8code.c: -STC_API cstr cstr_casefold_sv(csview sv); -STC_API cstr cstr_tolower_sv(csview sv); -STC_API cstr cstr_toupper_sv(csview sv); -STC_API cstr cstr_tolower(const char* str); -STC_API cstr cstr_toupper(const char* str); -STC_API void cstr_lowercase(cstr* self); -STC_API void cstr_uppercase(cstr* self); -STC_API bool cstr_valid_utf8(const cstr* self); +extern cstr cstr_casefold_sv(csview sv); +extern cstr cstr_tolower_sv(csview sv); +extern cstr cstr_toupper_sv(csview sv); +extern cstr cstr_tolower(const char* str); +extern cstr cstr_toupper(const char* str); +extern void cstr_lowercase(cstr* self); +extern void cstr_uppercase(cstr* self); +extern bool cstr_valid_utf8(const cstr* self); // other utf8 @@ -394,7 +394,7 @@ fn_tocase[] = {{tolower, utf8_casefold}, {tolower, utf8_tolower}, {toupper, utf8_toupper}}; -STC_DEF cstr cstr_tocase(csview sv, int k) { +static cstr cstr_tocase(csview sv, int k) { cstr out = cstr_init(); char *buf = cstr_reserve(&out, sv.size*3/2); const char *end = sv.str + sv.size; @@ -415,28 +415,28 @@ STC_DEF cstr cstr_tocase(csview sv, int k) { return out; } -STC_DEF cstr cstr_casefold_sv(csview sv) +cstr cstr_casefold_sv(csview sv) { return cstr_tocase(sv, 0); } -STC_DEF cstr cstr_tolower_sv(csview sv) +cstr cstr_tolower_sv(csview sv) { return cstr_tocase(sv, 1); } -STC_DEF cstr cstr_toupper_sv(csview sv) +cstr cstr_toupper_sv(csview sv) { return cstr_tocase(sv, 2); } -STC_DEF cstr cstr_tolower(const char* str) +cstr cstr_tolower(const char* str) { return cstr_tolower_sv(c_sv(str, c_strlen(str))); } -STC_DEF cstr cstr_toupper(const char* str) +cstr cstr_toupper(const char* str) { return cstr_toupper_sv(c_sv(str, c_strlen(str))); } -STC_DEF void cstr_lowercase(cstr* self) +void cstr_lowercase(cstr* self) { cstr_take(self, cstr_tolower_sv(cstr_sv(self))); } -STC_DEF void cstr_uppercase(cstr* self) +void cstr_uppercase(cstr* self) { cstr_take(self, cstr_toupper_sv(cstr_sv(self))); } -STC_DEF bool cstr_valid_utf8(const cstr* self) +bool cstr_valid_utf8(const cstr* self) { return utf8_valid(cstr_str(self)); } #endif // i_import -- cgit v1.2.3 From 2d67f4040f6eecd41f1b864b43c62823ed75aff0 Mon Sep 17 00:00:00 2001 From: tylov Date: Fri, 21 Jul 2023 00:37:28 +0200 Subject: Renamed badly abbreviated names in crand.h. Moved coroutine.h from algo subfolder to stc. Updated coroutine.h and docs. --- docs/ccommon_api.md | 7 +- docs/cpque_api.md | 4 +- docs/crandom_api.md | 47 +++-- include/stc/algo/coroutine.h | 274 ------------------------- include/stc/algorithm.h | 8 + include/stc/calgo.h | 9 - include/stc/coroutine.h | 273 ++++++++++++++++++++++++ include/stc/crand.h | 43 ++-- misc/examples/algorithms/forfilter.c | 3 +- misc/examples/algorithms/forloops.c | 2 +- misc/examples/algorithms/random.c | 20 +- misc/examples/bitsets/prime.c | 22 +- misc/examples/coroutines/cointerleave.c | 2 +- misc/examples/coroutines/coread.c | 2 +- misc/examples/coroutines/coroutines.c | 2 +- misc/examples/coroutines/cotasks1.c | 2 +- misc/examples/coroutines/cotasks2.c | 12 +- misc/examples/coroutines/dining_philosophers.c | 2 +- misc/examples/coroutines/generator.c | 2 +- misc/examples/coroutines/scheduler.c | 2 +- misc/examples/coroutines/triples.c | 2 +- misc/examples/linkedlists/list.c | 2 +- misc/examples/make.sh | 8 +- misc/examples/priorityqueues/priority.c | 6 +- misc/examples/queues/new_queue.c | 6 +- misc/examples/queues/queue.c | 8 +- misc/examples/sortedmaps/gauss2.c | 4 +- misc/examples/spans/mdspan.c | 51 +++++ 28 files changed, 436 insertions(+), 389 deletions(-) delete mode 100644 include/stc/algo/coroutine.h create mode 100644 include/stc/algorithm.h delete mode 100644 include/stc/calgo.h create mode 100644 include/stc/coroutine.h create mode 100644 misc/examples/spans/mdspan.c (limited to 'include/stc/algo/coroutine.h') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 1e1ae1aa..9189d7e8 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -392,8 +392,7 @@ cco_routine scope; Use `if-else-if` constructs instead. | | `cco_return;` | Return from coroutine (inside cco_routine) | | | Task objects: | | | | `cco_task_struct(Name, ...);` | Define a coroutine task struct | -| | `cco_await_task(task, ...);` | Await for task to finish or optionally yield a value | -| | `cco_block_task(task);` | Run blocking until task is finished (stackless) | +| | `cco_task_await(task, ...);` | Await for task to finish or optionally yield a value | | | Semaphores: | | | | `cco_sem` | Semaphore type | | `cco_sem` | `cco_sem_from(long value)` | Create semaphore | @@ -414,7 +413,9 @@ cco_routine scope; Use `if-else-if` constructs instead. | `void` | `cco_stop(co)` | Next call of coroutine finalizes | | `void` | `cco_reset(co)` | Reset state to initial (for reuse) | | `void` | `cco_block_on(cocall) { }` | Run blocking until cocall is finished | -| `void` | `cco_block_on(cocall, int *result) { }`| Run blocking until cocall is finished | +| `void` | `cco_block_on(cocall, int *result) {}`| Run blocking until cocall is finished | +| | `cco_task_block_on(task) {}` | Run blocking until task is finished | +| | `cco_task_block_on(task, rt, STACKSZ) {}`| Run blocking until task is finished | | | Time functions: | | | `double` | `cco_time(void)` | Return secs with usec prec. since Epoch | | | `cco_sleep(double sec)` | Sleep for seconds (msec or usec prec.) | diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 4cde927b..247424b4 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -72,14 +72,14 @@ int main(void) { intptr_t N = 10000000; crand_t rng = crand_init(1234); - crand_unif_t dist = crand_unif_init(0, N * 10); + crand_uniform_t dist = crand_uniform_init(0, N * 10); // Define heap cpque_i heap = {0}; // Push ten million random numbers to priority queue. c_forrange (N) - cpque_i_push(&heap, crand_unif(&rng, &dist)); + cpque_i_push(&heap, crand_uniform(&rng, &dist)); // Add some negative ones. int nums[] = {-231, -32, -873, -4, -343}; diff --git a/docs/crandom_api.md b/docs/crandom_api.md index 22a4f4dd..88924784 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -1,30 +1,29 @@ # STC [crand](../include/stc/crand.h): Pseudo Random Number Generator ![Random](pics/random.jpg) -This features a *64-bit PRNG* named **stc64**, and can generate bounded uniform and normal +This features a *64-bit PRNG* named **crand64**, and can generate bounded uniform and normal distributed random numbers. See [random](https://en.cppreference.com/w/cpp/header/random) for similar c++ functionality. ## Description -**stc64** is a novel, extremely fast PRNG by Tyge Løvset, suited for parallel usage. It features -Weyl-sequences as part of its state. It is inspired on *sfc64*, but has a different output function +**crand64** is a novel, very fast PRNG, suited for parallel usage. It features a +Weyl-sequence as part of its state. It is based on *sfc64*, but has a different output function and state size. -**sfc64** is the fastest among *pcg*, *xoshiro`**`*, and *lehmer*. It is equally fast as *sfc64* on -most platforms. *wyrand* is faster on platforms with fast 128-bit multiplication, and has 2^64 period -length (https://github.com/lemire/SwiftWyhash/issues/10). However, *wyrand* is not suited for massive -parallel usage due to its limited total minimal period length. +**sfc64** is the fastest among *pcg*, *xoshiro`**`*, and *lehmer*. It is equally fast or faster than +*sfc64* on most platforms. *wyrand* is faster on platforms with fast 128-bit multiplication, and has +2^64 period (https://github.com/lemire/SwiftWyhash/issues/10). *wyrand* is not suited for massive +parallel usage due to its limited minimal period. -**stc64** does not require multiplication or 128-bit integer operations. It has 320 bit state, -but updates only 256 bit per generated number. +**crand64** does not require multiplication or 128-bit integer operations. It has 320 bit state, +where 64-bits are constant per prng instance created. There is no *jump function*, but each odd number Weyl-increment (state[4]) starts a new -unique 2^64 *minimum* length period. For a single thread, a minimum period of 2^127 is generated -when the Weyl-increment is incremented by 2 every 2^64 output. +unique 2^64 *minimum* length period, i.e. virtually unlimitied number of unique threads. -**stc64** passes *PractRand* (tested up to 8TB output), Vigna's Hamming weight test, and simple +**crand64** passes *PractRand* (tested up to 8TB output), Vigna's Hamming weight test, and simple correlation tests, i.e. *n* interleaved streams with only one-bit differences in initial state. Also 32-bit and 16-bit versions passes PractRand up to their size limits. @@ -41,27 +40,27 @@ All crand definitions and prototypes are available by including a single header ## Methods ```c -void csrand(uint64_t seed); // seed global stc64 prng +void csrand(uint64_t seed); // seed global crand64 prng uint64_t crand(void); // global crand_u64(rng) double crandf(void); // global crand_f64(rng) -crand_t crand_init(uint64_t seed); // stc64_init(s) is deprecated +crand_t crand_init(uint64_t seed); uint64_t crand_u64(crand_t* rng); // range [0, 2^64 - 1] double crand_f64(crand_t* rng); // range [0.0, 1.0) -crand_unif_t crand_unif_init(int64_t low, int64_t high); // uniform-distribution -int64_t crand_unif(crand_t* rng, crand_unif_t* dist); // range [low, high] +crand_uniform_t crand_uniform_init(int64_t low, int64_t high); // uniform-distribution range +int64_t crand_uniform(crand_t* rng, crand_uniform_t* dist); -crand_norm_t crand_norm_init(double mean, double stddev); // normal-distribution -double crand_norm(crand_t* rng, crand_norm_t* dist); +crand_normal_t crand_normal_init(double mean, double stddev); // normal-gauss distribution +double crand_normal(crand_t* rng, crand_normal_t* dist); ``` ## Types | Name | Type definition | Used to represent... | |:-------------------|:------------------------------------------|:-----------------------------| | `crand_t` | `struct {uint64_t state[4];}` | The PRNG engine type | -| `crand_unif_t` | `struct {int64_t lower; uint64_t range;}` | Integer uniform distribution | -| `crand_norm_t` | `struct {double mean, stddev;}` | Normal distribution type | +| `crand_uniform_t` | `struct {int64_t lower; uint64_t range;}` | Integer uniform distribution | +| `crand_normal_t` | `struct {double mean, stddev;}` | Normal distribution type | ## Example ```c @@ -86,17 +85,17 @@ int main(void) // Setup random engine with normal distribution. uint64_t seed = time(NULL); crand_t rng = crand_init(seed); - crand_norm_t dist = crand_norm_init(Mean, StdDev); + crand_normal_t dist = crand_normal_init(Mean, StdDev); // Create histogram map - csmap_i mhist = csmap_i_init(); + csmap_i mhist = {0}; c_forrange (N) { - int index = (int)round(crand_norm(&rng, &dist)); + int index = (int)round(crand_normal(&rng, &dist)); csmap_i_emplace(&mhist, index, 0).ref->second += 1; } // Print the gaussian bar chart - cstr bar = cstr_init(); + cstr bar = {0}; c_foreach (i, csmap_i, mhist) { int n = (int)(i.ref->second * StdDev * Scale * 2.5 / N); if (n > 0) { diff --git a/include/stc/algo/coroutine.h b/include/stc/algo/coroutine.h deleted file mode 100644 index 7c6989c3..00000000 --- a/include/stc/algo/coroutine.h +++ /dev/null @@ -1,274 +0,0 @@ -/* MIT License - * - * Copyright (c) 2023 Tyge Løvset - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#ifndef STC_COROUTINE_INCLUDED -#define STC_COROUTINE_INCLUDED -/* -#include -#include - -struct iterpair { - int max_x, max_y; - int x, y; - int cco_state; // required member -}; - -int iterpair(struct iterpair* I) { - cco_routine(I) { - for (I->x = 0; I->x < I->max_x; I->x++) - for (I->y = 0; I->y < I->max_y; I->y++) - cco_yield(); - - cco_cleanup: // required if there is cleanup code - puts("final"); - } - return 0; // CCO_DONE -} - -int main(void) { - struct iterpair it = {.max_x=3, .max_y=3}; - int n = 0; - while (iterpair(&it)) - { - printf("%d %d\n", it.x, it.y); - // example of early stop: - if (++n == 7) cco_stop(&it); // signal to stop/finalize in next - } - return 0; -} -*/ -#include "../ccommon.h" - -enum { - CCO_STATE_CLEANUP = -1, - CCO_STATE_DONE = -2, -}; -typedef enum { - CCO_DONE = 0, - CCO_AWAIT = 1<<0, - CCO_YIELD = 1<<1, -} cco_result; - -#define cco_initial(co) ((co)->cco_state == 0) -#define cco_suspended(co) ((co)->cco_state > 0) -#define cco_done(co) ((co)->cco_state == CCO_STATE_DONE) - -#define cco_routine(co) \ - for (int *_state = &(co)->cco_state; *_state != CCO_STATE_DONE; *_state = CCO_STATE_DONE) \ - _resume: switch (*_state) case 0: // thanks, @liigo! - -#define cco_yield() cco_yield_v(CCO_YIELD) -#define cco_yield_v(ret) \ - do { \ - *_state = __LINE__; return ret; goto _resume; \ - case __LINE__:; \ - } while (0) - -#define cco_await(promise) cco_await_v_2(promise, CCO_AWAIT) -#define cco_await_v(...) c_MACRO_OVERLOAD(cco_await_v, __VA_ARGS__) -#define cco_await_v_1(promise) cco_await_v_2(promise, ) -#define cco_await_v_2(promise, ret) \ - do { \ - *_state = __LINE__; \ - case __LINE__: if (!(promise)) {return ret; goto _resume;} \ - } while (0) - -/* cco_await_on(): assumes coroutine returns a cco_result value (int) */ -#define cco_await_on(corocall) \ - do { \ - *_state = __LINE__; \ - case __LINE__: { int _r = corocall; if (_r != CCO_DONE) {return _r; goto _resume;} } \ - } while (0) - -/* cco_block_on(): assumes coroutine returns a cco_result value (int) */ -#define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__) -#define cco_block_on_1(corocall) while ((corocall) != CCO_DONE) -#define cco_block_on_2(corocall, result) while ((*(result) = (corocall)) != CCO_DONE) - -#define cco_cleanup \ - *_state = CCO_STATE_CLEANUP; case CCO_STATE_CLEANUP - -#define cco_return \ - do { \ - *_state = *_state >= 0 ? CCO_STATE_CLEANUP : CCO_STATE_DONE; \ - goto _resume; \ - } while (0) - -#define cco_yield_final() cco_yield_final_v(CCO_YIELD) -#define cco_yield_final_v(value) \ - do { \ - *_state = *_state >= 0 ? CCO_STATE_CLEANUP : CCO_STATE_DONE; \ - return value; \ - } while (0) - -#define cco_stop(co) \ - do { \ - int* _s = &(co)->cco_state; \ - if (*_s > 0) *_s = CCO_STATE_CLEANUP; \ - else if (*_s == 0) *_s = CCO_STATE_DONE; \ - } while (0) - -#define cco_reset(co) \ - (void)((co)->cco_state = 0) - -/* - * Tasks (optional) - */ - -struct cco_runtime; - -#define cco_task_struct(Name, ...) \ - struct Name { \ - int (*cco_fn)(struct Name*, struct cco_runtime*); \ - int cco_state, cco_expect; \ - __VA_ARGS__ \ - } - -typedef cco_task_struct(cco_task, /**/) cco_task; - -typedef struct cco_runtime { - int result, top; - cco_task* stack[]; -} cco_runtime; - -#define cco_cast_task(task) \ - ((cco_task *)(task) + 0*sizeof((task)->cco_fn(task, (cco_runtime*)0) + ((int*)0 == &(task)->cco_state))) - -#define cco_resume(task, rt) \ - (task)->cco_fn(task, rt) - -#define cco_block_task(...) c_MACRO_OVERLOAD(cco_block_task, __VA_ARGS__) -#define cco_block_task_1(task) cco_block_task_3(task, rt, 16) -#define cco_block_task_3(task, rt, STACKDEPTH) \ - for (struct { int result, top; cco_task* stack[STACKDEPTH]; } rt = {.stack={cco_cast_task(task)}}; \ - (((rt.result = cco_resume(rt.stack[rt.top], (cco_runtime*)&rt)) & rt.stack[rt.top]->cco_expect) || --rt.top >= 0); ) - -#define cco_await_task(...) c_MACRO_OVERLOAD(cco_await_task, __VA_ARGS__) -#define cco_await_task_2(task, rt) cco_await_task_3(task, rt, CCO_DONE) -#define cco_await_task_3(task, rt, resultbits) \ - do { \ - cco_runtime* _rt = rt; \ - (_rt->stack[++_rt->top] = cco_cast_task(task))->cco_expect = ~(resultbits); \ - cco_yield_v(CCO_AWAIT); \ - } while (0) - -/* - * Semaphore - */ - -typedef struct { intptr_t count; } cco_sem; - -#define cco_sem_await(sem) cco_sem_await_v_2(sem, CCO_AWAIT) -#define cco_sem_await_v(...) c_MACRO_OVERLOAD(cco_sem_await_v, __VA_ARGS__) -#define cco_sem_await_v_1(sem) cco_sem_await_v_2(sem, ) -#define cco_sem_await_v_2(sem, ret) \ - do { \ - cco_await_v_2((sem)->count > 0, ret); \ - --(sem)->count; \ - } while (0) - -#define cco_sem_release(sem) ++(sem)->count -#define cco_sem_from(value) ((cco_sem){value}) -#define cco_sem_set(sem, value) ((sem)->count = value) - -/* - * Timer - */ - -#ifdef _WIN32 - #ifdef __cplusplus - #define _c_LINKC extern "C" __declspec(dllimport) - #else - #define _c_LINKC __declspec(dllimport) - #endif - #if 1 // _WIN32_WINNT < _WIN32_WINNT_WIN8 || defined __TINYC__ - #define _c_getsystime GetSystemTimeAsFileTime - #else - #define _c_getsystime GetSystemTimePreciseAsFileTime - #endif - struct _FILETIME; - _c_LINKC void _c_getsystime(struct _FILETIME*); - _c_LINKC void Sleep(unsigned long); - - static inline double cco_time(void) { /* seconds since epoch */ - unsigned long long quad; /* 64-bit value representing 1/10th usecs since Jan 1 1601, 00:00 UTC */ - _c_getsystime((struct _FILETIME*)&quad); - return (double)(quad - 116444736000000000ULL)*1e-7; /* time diff Jan 1 1601-Jan 1 1970 in 1/10th usecs */ - } - - static inline void cco_sleep(double sec) { - Sleep((unsigned long)(sec*1000.0)); - } -#else - #include - static inline double cco_time(void) { /* seconds since epoch */ - struct timeval tv; - gettimeofday(&tv, NULL); - return (double)tv.tv_sec + (double)tv.tv_usec*1e-6; - } - - static inline void cco_sleep(double sec) { - struct timeval tv; - tv.tv_sec = (time_t)sec; - tv.tv_usec = (suseconds_t)((sec - (double)(long)sec)*1e6); - select(0, NULL, NULL, NULL, &tv); - } -#endif - -typedef struct { double interval, start; } cco_timer; - -#define cco_timer_await(tm, sec) cco_timer_await_v_3(tm, sec, CCO_AWAIT) -#define cco_timer_await_v(...) c_MACRO_OVERLOAD(cco_timer_await_v, __VA_ARGS__) -#define cco_timer_await_v_2(tm, sec) cco_timer_await_v_3(tm, sec, ) -#define cco_timer_await_v_3(tm, sec, ret) \ - do { \ - cco_timer_start(tm, sec); \ - cco_await_v_2(cco_timer_expired(tm), ret); \ - } while (0) - -static inline void cco_timer_start(cco_timer* tm, double sec) { - tm->interval = sec; - tm->start = cco_time(); -} - -static inline cco_timer cco_timer_from(double sec) { - cco_timer tm = {.interval=sec, .start=cco_time()}; - return tm; -} - -static inline void cco_timer_restart(cco_timer* tm) { - tm->start = cco_time(); -} - -static inline bool cco_timer_expired(cco_timer* tm) { - return cco_time() - tm->start >= tm->interval; -} - -static inline double cco_timer_elapsed(cco_timer* tm) { - return cco_time() - tm->start; -} - -static inline double cco_timer_remaining(cco_timer* tm) { - return tm->start + tm->interval - cco_time(); -} - -#endif diff --git a/include/stc/algorithm.h b/include/stc/algorithm.h new file mode 100644 index 00000000..cf3ab328 --- /dev/null +++ b/include/stc/algorithm.h @@ -0,0 +1,8 @@ +#ifndef STC_CALGO_INCLUDED +#define STC_CALGO_INCLUDED + +#include "algo/raii.h" +#include "algo/crange.h" +#include "algo/filter.h" + +#endif diff --git a/include/stc/calgo.h b/include/stc/calgo.h deleted file mode 100644 index 63ef97b9..00000000 --- a/include/stc/calgo.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef STC_CALGO_INCLUDED -#define STC_CALGO_INCLUDED - -#include "algo/raii.h" -#include "algo/crange.h" -#include "algo/filter.h" -#include "algo/coroutine.h" - -#endif diff --git a/include/stc/coroutine.h b/include/stc/coroutine.h new file mode 100644 index 00000000..f89d20af --- /dev/null +++ b/include/stc/coroutine.h @@ -0,0 +1,273 @@ +/* MIT License + * + * Copyright (c) 2023 Tyge Løvset + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef STC_COROUTINE_INCLUDED +#define STC_COROUTINE_INCLUDED +/* +#include +#include + +struct iterpair { + int max_x, max_y; + int x, y; + int cco_state; // required member +}; + +int iterpair(struct iterpair* I) { + cco_routine(I) { + for (I->x = 0; I->x < I->max_x; I->x++) + for (I->y = 0; I->y < I->max_y; I->y++) + cco_yield(); + + cco_cleanup: // required if there is cleanup code + puts("final"); + } + return 0; // CCO_DONE +} + +int main(void) { + struct iterpair it = {.max_x=3, .max_y=3}; + int n = 0; + while (iterpair(&it)) + { + printf("%d %d\n", it.x, it.y); + // example of early stop: + if (++n == 7) cco_stop(&it); // signal to stop/finalize in next + } + return 0; +} +*/ +#include "ccommon.h" + +enum { + CCO_STATE_CLEANUP = -1, + CCO_STATE_DONE = -2, +}; +typedef enum { + CCO_DONE = 0, + CCO_AWAIT = 1<<0, + CCO_YIELD = 1<<1, +} cco_result; + +#define cco_initial(co) ((co)->cco_state == 0) +#define cco_suspended(co) ((co)->cco_state > 0) +#define cco_done(co) ((co)->cco_state == CCO_STATE_DONE) + +#define cco_routine(co) \ + for (int* _state = &(co)->cco_state; *_state != CCO_STATE_DONE; *_state = CCO_STATE_DONE) \ + _resume: switch (*_state) case 0: // thanks, @liigo! + +#define cco_yield() cco_yield_v(CCO_YIELD) +#define cco_yield_v(ret) \ + do { \ + *_state = __LINE__; return ret; goto _resume; \ + case __LINE__:; \ + } while (0) + +#define cco_await(promise) cco_await_v_2(promise, CCO_AWAIT) +#define cco_await_v(...) c_MACRO_OVERLOAD(cco_await_v, __VA_ARGS__) +#define cco_await_v_1(promise) cco_await_v_2(promise, ) +#define cco_await_v_2(promise, ret) \ + do { \ + *_state = __LINE__; \ + case __LINE__: if (!(promise)) {return ret; goto _resume;} \ + } while (0) + +/* cco_await_on(): assumes coroutine returns a cco_result value (int) */ +#define cco_await_on(corocall) \ + do { \ + *_state = __LINE__; \ + case __LINE__: { int _r = corocall; if (_r != CCO_DONE) {return _r; goto _resume;} } \ + } while (0) + +/* cco_block_on(): assumes coroutine returns a cco_result value (int) */ +#define cco_block_on(...) c_MACRO_OVERLOAD(cco_block_on, __VA_ARGS__) +#define cco_block_on_1(corocall) while ((corocall) != CCO_DONE) +#define cco_block_on_2(corocall, result) while ((*(result) = (corocall)) != CCO_DONE) + +#define cco_cleanup \ + *_state = CCO_STATE_CLEANUP; case CCO_STATE_CLEANUP + +#define cco_return \ + do { \ + *_state = *_state >= 0 ? CCO_STATE_CLEANUP : CCO_STATE_DONE; \ + goto _resume; \ + } while (0) + +#define cco_yield_final() cco_yield_final_v(CCO_YIELD) +#define cco_yield_final_v(value) \ + do { \ + *_state = *_state >= 0 ? CCO_STATE_CLEANUP : CCO_STATE_DONE; \ + return value; \ + } while (0) + +#define cco_stop(co) \ + do { \ + int* _s = &(co)->cco_state; \ + if (*_s > 0) *_s = CCO_STATE_CLEANUP; \ + else if (*_s == 0) *_s = CCO_STATE_DONE; \ + } while (0) + +#define cco_reset(co) \ + (void)((co)->cco_state = 0) + +/* + * Tasks (optional) + */ + +struct cco_runtime; + +#define cco_task_struct(Name, ...) \ + struct Name { \ + int (*cco_func)(struct Name*, struct cco_runtime*); \ + int cco_state, cco_expect; \ + __VA_ARGS__ \ + } + +typedef cco_task_struct(cco_task, /**/) cco_task; + +typedef struct cco_runtime { + int result, top; cco_task* stack[]; +} cco_runtime; + +#define cco_cast_task(task) \ + ((cco_task *)(task) + 0*sizeof((task)->cco_func(task, (cco_runtime*)0) + ((int*)0 == &(task)->cco_state))) + +#define cco_resume(task, rt) \ + (task)->cco_func(task, rt) + +#define cco_task_await(...) c_MACRO_OVERLOAD(cco_task_await, __VA_ARGS__) +#define cco_task_await_2(task, rt) cco_task_await_3(task, rt, CCO_DONE) +#define cco_task_await_3(task, rt, resultbits) \ + do { \ + cco_runtime* _rt = rt; \ + (_rt->stack[++_rt->top] = cco_cast_task(task))->cco_expect = (resultbits); \ + cco_yield_v(CCO_AWAIT); \ + } while (0) + +#define cco_task_block_on(...) c_MACRO_OVERLOAD(cco_task_block_on, __VA_ARGS__) +#define cco_task_block_on_1(task) cco_task_block_on_3(task, _rt, 16) +#define cco_task_block_on_3(task, rt, STACKDEPTH) \ + for (struct { int result, top; cco_task* stack[STACKDEPTH]; } rt = {.stack={cco_cast_task(task)}}; \ + (((rt.result = cco_resume(rt.stack[rt.top], (cco_runtime*)&rt)) & ~rt.stack[rt.top]->cco_expect) || --rt.top >= 0); ) + +/* + * Semaphore + */ + +typedef struct { intptr_t count; } cco_sem; + +#define cco_sem_await(sem) cco_sem_await_v_2(sem, CCO_AWAIT) +#define cco_sem_await_v(...) c_MACRO_OVERLOAD(cco_sem_await_v, __VA_ARGS__) +#define cco_sem_await_v_1(sem) cco_sem_await_v_2(sem, ) +#define cco_sem_await_v_2(sem, ret) \ + do { \ + cco_await_v_2((sem)->count > 0, ret); \ + --(sem)->count; \ + } while (0) + +#define cco_sem_release(sem) ++(sem)->count +#define cco_sem_from(value) ((cco_sem){value}) +#define cco_sem_set(sem, value) ((sem)->count = value) + +/* + * Timer + */ + +#ifdef _WIN32 + #ifdef __cplusplus + #define _c_LINKC extern "C" __declspec(dllimport) + #else + #define _c_LINKC __declspec(dllimport) + #endif + #if 1 // _WIN32_WINNT < _WIN32_WINNT_WIN8 || defined __TINYC__ + #define _c_getsystime GetSystemTimeAsFileTime + #else + #define _c_getsystime GetSystemTimePreciseAsFileTime + #endif + struct _FILETIME; + _c_LINKC void _c_getsystime(struct _FILETIME*); + _c_LINKC void Sleep(unsigned long); + + static inline double cco_time(void) { /* seconds since epoch */ + unsigned long long quad; /* 64-bit value representing 1/10th usecs since Jan 1 1601, 00:00 UTC */ + _c_getsystime((struct _FILETIME*)&quad); + return (double)(quad - 116444736000000000ULL)*1e-7; /* time diff Jan 1 1601-Jan 1 1970 in 1/10th usecs */ + } + + static inline void cco_sleep(double sec) { + Sleep((unsigned long)(sec*1000.0)); + } +#else + #include + static inline double cco_time(void) { /* seconds since epoch */ + struct timeval tv; + gettimeofday(&tv, NULL); + return (double)tv.tv_sec + (double)tv.tv_usec*1e-6; + } + + static inline void cco_sleep(double sec) { + struct timeval tv; + tv.tv_sec = (time_t)sec; + tv.tv_usec = (suseconds_t)((sec - (double)(long)sec)*1e6); + select(0, NULL, NULL, NULL, &tv); + } +#endif + +typedef struct { double interval, start; } cco_timer; + +#define cco_timer_await(tm, sec) cco_timer_await_v_3(tm, sec, CCO_AWAIT) +#define cco_timer_await_v(...) c_MACRO_OVERLOAD(cco_timer_await_v, __VA_ARGS__) +#define cco_timer_await_v_2(tm, sec) cco_timer_await_v_3(tm, sec, ) +#define cco_timer_await_v_3(tm, sec, ret) \ + do { \ + cco_timer_start(tm, sec); \ + cco_await_v_2(cco_timer_expired(tm), ret); \ + } while (0) + +static inline void cco_timer_start(cco_timer* tm, double sec) { + tm->interval = sec; + tm->start = cco_time(); +} + +static inline cco_timer cco_timer_from(double sec) { + cco_timer tm = {.interval=sec, .start=cco_time()}; + return tm; +} + +static inline void cco_timer_restart(cco_timer* tm) { + tm->start = cco_time(); +} + +static inline bool cco_timer_expired(cco_timer* tm) { + return cco_time() - tm->start >= tm->interval; +} + +static inline double cco_timer_elapsed(cco_timer* tm) { + return cco_time() - tm->start; +} + +static inline double cco_timer_remaining(cco_timer* tm) { + return tm->start + tm->interval - cco_time(); +} + +#endif diff --git a/include/stc/crand.h b/include/stc/crand.h index 0a6aa9e0..32722762 100644 --- a/include/stc/crand.h +++ b/include/stc/crand.h @@ -32,20 +32,20 @@ int main(void) { uint64_t seed = 123456789; crand_t rng = crand_init(seed); - crand_unif_t dist1 = crand_unif_init(1, 6); - crand_norm_t dist3 = crand_norm_init(1.0, 10.0); + crand_uniform_t dist1 = crand_uniform_init(1, 6); + crand_normal_t dist3 = crand_normal_init(1.0, 10.0); uint64_t i = crand_u64(&rng); - int64_t iu = crand_unif(&rng, &dist1); - double xn = crand_norm(&rng, &dist3); + int64_t iu = crand_uniform(&rng, &dist1); + double xn = crand_normal(&rng, &dist3); } */ #include #include typedef struct crand { uint64_t state[5]; } crand_t; -typedef struct crand_unif { int64_t lower; uint64_t range, threshold; } crand_unif_t; -typedef struct crand_norm { double mean, stddev, next; int has_next; } crand_norm_t; +typedef struct crand_uniform { int64_t lower; uint64_t range, threshold; } crand_uniform_t; +typedef struct crand_normal { double mean, stddev, next; int has_next; } crand_normal_t; /* PRNG crand_t. * Very fast PRNG suited for parallel usage with Weyl-sequence parameter. @@ -67,14 +67,14 @@ STC_API double crandf(void); STC_API crand_t crand_init(uint64_t seed); /* Unbiased bounded uniform distribution. range [low, high] */ -STC_API crand_unif_t crand_unif_init(int64_t low, int64_t high); -STC_API int64_t crand_unif(crand_t* rng, crand_unif_t* dist); +STC_API crand_uniform_t crand_uniform_init(int64_t low, int64_t high); +STC_API int64_t crand_uniform(crand_t* rng, crand_uniform_t* dist); /* Normal/gaussian distribution. */ -STC_INLINE crand_norm_t crand_norm_init(double mean, double stddev) - { crand_norm_t r = {mean, stddev, 0.0, 0}; return r; } +STC_INLINE crand_normal_t crand_normal_init(double mean, double stddev) + { crand_normal_t r = {mean, stddev, 0.0, 0}; return r; } -STC_API double crand_norm(crand_t* rng, crand_norm_t* dist); +STC_API double crand_normal(crand_t* rng, crand_normal_t* dist); /* Main crand_t prng */ STC_INLINE uint64_t crand_u64(crand_t* rng) { @@ -95,11 +95,10 @@ STC_INLINE double crand_f64(crand_t* rng) { /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) || defined(i_static) -/* Global random() */ -static crand_t crand_global = {{ - 0x26aa069ea2fb1a4d, 0x70c72c95cd592d04, - 0x504f333d3aa0b359, 0x9e3779b97f4a7c15, - 0x6a09e667a754166b +/* Global random seed */ +static crand_t crand_global = {{ // csrand(0) + 0x9e3779b97f4a7c15, 0x6f68261b57e7a770, + 0xe220a838bf5c9dde, 0x7c17d1800457b1ba, 0x1, }}; STC_DEF void csrand(uint64_t seed) @@ -116,20 +115,20 @@ STC_DEF crand_t crand_init(uint64_t seed) { s[0] = seed + 0x9e3779b97f4a7c15; s[1] = (s[0] ^ (s[0] >> 30))*0xbf58476d1ce4e5b9; s[2] = (s[1] ^ (s[1] >> 27))*0x94d049bb133111eb; - s[3] = (s[2] ^ (s[2] >> 31)); - s[4] = ((seed + 0x6aa069ea2fb1a4d) << 1) | 1; + s[3] = s[0] ^ s[2] ^ (s[2] >> 31); + s[4] = (seed << 1) | 1; return rng; } /* Init unbiased uniform uint RNG with bounds [low, high] */ -STC_DEF crand_unif_t crand_unif_init(int64_t low, int64_t high) { - crand_unif_t dist = {low, (uint64_t) (high - low + 1)}; +STC_DEF crand_uniform_t crand_uniform_init(int64_t low, int64_t high) { + crand_uniform_t dist = {low, (uint64_t) (high - low + 1)}; dist.threshold = (uint64_t)(0 - dist.range) % dist.range; return dist; } /* Int64 uniform distributed RNG, range [low, high]. */ -STC_DEF int64_t crand_unif(crand_t* rng, crand_unif_t* d) { +STC_DEF int64_t crand_uniform(crand_t* rng, crand_uniform_t* d) { uint64_t lo, hi; #ifdef c_umul128 do { c_umul128(crand_u64(rng), d->range, &lo, &hi); } while (lo < d->threshold); @@ -140,7 +139,7 @@ STC_DEF int64_t crand_unif(crand_t* rng, crand_unif_t* d) { } /* Normal distribution PRNG. Marsaglia polar method */ -STC_DEF double crand_norm(crand_t* rng, crand_norm_t* dist) { +STC_DEF double crand_normal(crand_t* rng, crand_normal_t* dist) { double u1, u2, s, m; if (dist->has_next++ & 1) return dist->next*dist->stddev + dist->mean; diff --git a/misc/examples/algorithms/forfilter.c b/misc/examples/algorithms/forfilter.c index f3c008b3..644b8459 100644 --- a/misc/examples/algorithms/forfilter.c +++ b/misc/examples/algorithms/forfilter.c @@ -3,8 +3,7 @@ #include #define i_implement #include -#include -#include +#include #define i_type IVec #define i_key int diff --git a/misc/examples/algorithms/forloops.c b/misc/examples/algorithms/forloops.c index 72d745f8..300eee18 100644 --- a/misc/examples/algorithms/forloops.c +++ b/misc/examples/algorithms/forloops.c @@ -1,5 +1,5 @@ #include -#include +#include #define i_type IVec #define i_key int diff --git a/misc/examples/algorithms/random.c b/misc/examples/algorithms/random.c index b7c0f277..ccd0711d 100644 --- a/misc/examples/algorithms/random.c +++ b/misc/examples/algorithms/random.c @@ -4,11 +4,11 @@ int main(void) { - const int N = 1000000000; + long long N = 1000000000; const uint64_t seed = (uint64_t)time(NULL), range = 1000000; crand_t rng = crand_init(seed); - int64_t sum; + long long sum; clock_t diff, before; printf("Compare speed of full and unbiased ranged random numbers...\n"); @@ -18,19 +18,19 @@ int main(void) sum += (uint32_t)crand_u64(&rng); } diff = clock() - before; - printf("full range\t\t: %f secs, %d, avg: %f\n", - (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + printf("full range\t\t: %f secs, %lld, avg: %f\n", + (double)diff/CLOCKS_PER_SEC, N, (double)(sum/N)); - crand_unif_t dist1 = crand_unif_init(0, range); + crand_uniform_t dist1 = crand_uniform_init(0, range); rng = crand_init(seed); sum = 0; before = clock(); c_forrange (N) { - sum += crand_unif(&rng, &dist1); // unbiased + sum += crand_uniform(&rng, &dist1); // unbiased } diff = clock() - before; - printf("unbiased 0-%" PRIu64 "\t: %f secs, %d, avg: %f\n", - range, (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + printf("unbiased 0-%" PRIu64 "\t: %f secs, %lld, avg: %f\n", + range, (double)diff/CLOCKS_PER_SEC, N, (double)(sum/N)); sum = 0; rng = crand_init(seed); @@ -39,7 +39,7 @@ int main(void) sum += (int64_t)(crand_u64(&rng) % (range + 1)); // biased } diff = clock() - before; - printf("biased 0-%" PRIu64 " \t: %f secs, %d, avg: %f\n", - range, (double)diff/CLOCKS_PER_SEC, N, (double)sum/N); + printf("biased 0-%" PRIu64 " \t: %f secs, %lld, avg: %f\n", + range, (double)diff/CLOCKS_PER_SEC, N, (double)(sum/N)); } diff --git a/misc/examples/bitsets/prime.c b/misc/examples/bitsets/prime.c index cb3b095a..e5764d83 100644 --- a/misc/examples/bitsets/prime.c +++ b/misc/examples/bitsets/prime.c @@ -2,23 +2,23 @@ #include #include #include -#include -#include +#include +typedef long long llong; -cbits sieveOfEratosthenes(int64_t n) +cbits sieveOfEratosthenes(llong n) { cbits bits = cbits_with_size(n/2 + 1, true); - int64_t q = (int64_t)sqrt((double) n) + 1; - for (int64_t i = 3; i < q; i += 2) { - int64_t j = i; + llong q = (llong)sqrt((double) n) + 1; + for (llong i = 3; i < q; i += 2) { + llong j = i; for (; j < n; j += 2) { if (cbits_test(&bits, j>>1)) { i = j; break; } } - for (int64_t j = i*i; j < n; j += i*2) + for (llong j = i*i; j < n; j += i*2) cbits_reset(&bits, j>>1); } return bits; @@ -26,12 +26,12 @@ cbits sieveOfEratosthenes(int64_t n) int main(void) { - int n = 1000000000; - printf("Computing prime numbers up to %d\n", n); + llong n = 100000000; + printf("Computing prime numbers up to %lld\n", n); clock_t t = clock(); cbits primes = sieveOfEratosthenes(n + 1); - int np = (int)cbits_count(&primes); + llong np = cbits_count(&primes); t = clock() - t; puts("Show all the primes in the range [2, 1000):"); @@ -50,7 +50,7 @@ int main(void) printf("%lld ", *i.ref); if (c_flt_getcount(i) % 10 == 0) puts(""); } - printf("Number of primes: %d, time: %.2f\n\n", np, (double)t/CLOCKS_PER_SEC); + printf("Number of primes: %lld, time: %.2f\n\n", np, (double)t/CLOCKS_PER_SEC); cbits_drop(&primes); } diff --git a/misc/examples/coroutines/cointerleave.c b/misc/examples/coroutines/cointerleave.c index 599ceaab..ea0d4dac 100644 --- a/misc/examples/coroutines/cointerleave.c +++ b/misc/examples/coroutines/cointerleave.c @@ -1,6 +1,6 @@ // https://www.youtube.com/watch?v=8sEe-4tig_A #include -#include +#include #define i_type IVec #define i_key int #include diff --git a/misc/examples/coroutines/coread.c b/misc/examples/coroutines/coread.c index a13f6be5..56248108 100644 --- a/misc/examples/coroutines/coread.c +++ b/misc/examples/coroutines/coread.c @@ -1,6 +1,6 @@ #define i_implement #include -#include +#include #include // Read file line by line using coroutines: diff --git a/misc/examples/coroutines/coroutines.c b/misc/examples/coroutines/coroutines.c index b8dfaa13..de0fcda5 100644 --- a/misc/examples/coroutines/coroutines.c +++ b/misc/examples/coroutines/coroutines.c @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/misc/examples/coroutines/cotasks1.c b/misc/examples/coroutines/cotasks1.c index c87582f1..27999ccf 100644 --- a/misc/examples/coroutines/cotasks1.c +++ b/misc/examples/coroutines/cotasks1.c @@ -4,7 +4,7 @@ #include #define i_static #include -#include +#include struct next_value { int val; diff --git a/misc/examples/coroutines/cotasks2.c b/misc/examples/coroutines/cotasks2.c index 293583bc..9ca69bda 100644 --- a/misc/examples/coroutines/cotasks2.c +++ b/misc/examples/coroutines/cotasks2.c @@ -4,7 +4,7 @@ #include #define i_static #include -#include +#include cco_task_struct (next_value, int val; @@ -45,7 +45,7 @@ int produce_items(struct produce_items* p, cco_runtime* rt) while (true) { // await for next CCO_YIELD in next_value() - cco_await_task(&p->next, rt, CCO_YIELD); + cco_task_await(&p->next, rt, CCO_YIELD); cstr_printf(&p->str, "item %d", p->next.val); print_time(); printf("produced %s\n", cstr_str(&p->str)); @@ -71,7 +71,7 @@ int consume_items(struct consume_items* c, cco_runtime* rt) for (c->i = 1; c->i <= c->n; ++c->i) { printf("consume #%d\n", c->i); - cco_await_task(&c->produce, rt, CCO_YIELD); + cco_task_await(&c->produce, rt, CCO_YIELD); print_time(); printf("consumed %s\n", cstr_str(&c->produce.str)); } @@ -87,12 +87,12 @@ int main(void) { struct consume_items consume = { .n=5, - .cco_fn=consume_items, - .produce={.cco_fn=produce_items, .next={.cco_fn=next_value}}, + .cco_func=consume_items, + .produce={.cco_func=produce_items, .next={.cco_func=next_value}}, }; int count = 0; - cco_block_task(&consume) + cco_task_block_on(&consume) { ++count; //cco_sleep(0.001); diff --git a/misc/examples/coroutines/dining_philosophers.c b/misc/examples/coroutines/dining_philosophers.c index a5063a42..abe09204 100644 --- a/misc/examples/coroutines/dining_philosophers.c +++ b/misc/examples/coroutines/dining_philosophers.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include // Define the number of philosophers and forks enum { diff --git a/misc/examples/coroutines/generator.c b/misc/examples/coroutines/generator.c index a15f9ba5..f9e59fea 100644 --- a/misc/examples/coroutines/generator.c +++ b/misc/examples/coroutines/generator.c @@ -1,7 +1,7 @@ // https://quuxplusone.github.io/blog/2019/03/06/pythagorean-triples/ -#include #include +#include typedef struct { int size; diff --git a/misc/examples/coroutines/scheduler.c b/misc/examples/coroutines/scheduler.c index 38defd0f..78461277 100644 --- a/misc/examples/coroutines/scheduler.c +++ b/misc/examples/coroutines/scheduler.c @@ -1,6 +1,6 @@ // https://www.youtube.com/watch?v=8sEe-4tig_A #include -#include +#include struct Task { int (*fn)(struct Task*); diff --git a/misc/examples/coroutines/triples.c b/misc/examples/coroutines/triples.c index 9f2fcc1e..fe1ca7c3 100644 --- a/misc/examples/coroutines/triples.c +++ b/misc/examples/coroutines/triples.c @@ -1,7 +1,7 @@ // https://quuxplusone.github.io/blog/2019/03/06/pythagorean-triples/ -#include #include +#include int gcd(int a, int b) { while (b) { diff --git a/misc/examples/linkedlists/list.c b/misc/examples/linkedlists/list.c index ad8bebb8..09591314 100644 --- a/misc/examples/linkedlists/list.c +++ b/misc/examples/linkedlists/list.c @@ -1,6 +1,6 @@ #include #include -#include +#include #include #define i_type DList diff --git a/misc/examples/make.sh b/misc/examples/make.sh index 7135ffdf..b362f275 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -38,8 +38,8 @@ fi if [ $run = 0 ] ; then for i in */*.c ; do - #out=$(basename $i .c).exe - out=$(dirname $i)/$(basename $i .c).exe + out=$(basename $i .c).exe + #out=$(dirname $i)/$(basename $i .c).exe echo $comp -I../../include $i $clibs $oflag$out $comp -I../../include $i $clibs $oflag$out done @@ -47,8 +47,8 @@ else for i in */*.c ; do echo $comp -I../../include $i $clibs $comp -I../../include $i $clibs - #out=$(basename $i .c).exe - out=$(dirname $i)/$(basename $i .c).exe + out=$(basename $i .c).exe + #out=$(dirname $i)/$(basename $i .c).exe if [ -f $out ]; then ./$out; fi done fi diff --git a/misc/examples/priorityqueues/priority.c b/misc/examples/priorityqueues/priority.c index bf2e188a..18684e73 100644 --- a/misc/examples/priorityqueues/priority.c +++ b/misc/examples/priorityqueues/priority.c @@ -11,21 +11,21 @@ int main(void) { intptr_t N = 10000000; crand_t rng = crand_init((uint64_t)time(NULL)); - crand_unif_t dist = crand_unif_init(0, N * 10); + crand_uniform_t dist = crand_uniform_init(0, N * 10); cpque_i heap = {0}; // Push ten million random numbers to priority queue printf("Push %" c_ZI " numbers\n", N); c_forrange (N) - cpque_i_push(&heap, crand_unif(&rng, &dist)); + cpque_i_push(&heap, crand_uniform(&rng, &dist)); // push some negative numbers too. c_forlist (i, int, {-231, -32, -873, -4, -343}) cpque_i_push(&heap, *i.ref); c_forrange (N) - cpque_i_push(&heap, crand_unif(&rng, &dist)); + cpque_i_push(&heap, crand_uniform(&rng, &dist)); puts("Extract the hundred smallest."); c_forrange (100) { diff --git a/misc/examples/queues/new_queue.c b/misc/examples/queues/new_queue.c index f3592df6..3904c50c 100644 --- a/misc/examples/queues/new_queue.c +++ b/misc/examples/queues/new_queue.c @@ -23,19 +23,19 @@ int point_cmp(const Point* a, const Point* b) { int main(void) { int n = 50000000; crand_t rng = crand_init((uint64_t)time(NULL)); - crand_unif_t dist = crand_unif_init(0, n); + crand_uniform_t dist = crand_uniform_init(0, n); IQ Q = {0}; // Push 50'000'000 random numbers onto the queue. c_forrange (n) - IQ_push(&Q, (int)crand_unif(&rng, &dist)); + IQ_push(&Q, (int)crand_uniform(&rng, &dist)); // Push or pop on the queue 50 million times printf("befor: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q)); c_forrange (n) { - int r = (int)crand_unif(&rng, &dist); + int r = (int)crand_uniform(&rng, &dist); if (r & 3) IQ_push(&Q, r); else diff --git a/misc/examples/queues/queue.c b/misc/examples/queues/queue.c index 56b5beb9..913524cc 100644 --- a/misc/examples/queues/queue.c +++ b/misc/examples/queues/queue.c @@ -7,20 +7,20 @@ int main(void) { int n = 100000000; - crand_unif_t dist; + crand_uniform_t dist; crand_t rng = crand_init(1234); - dist = crand_unif_init(0, n); + dist = crand_uniform_init(0, n); cqueue_i queue = {0}; // Push ten million random numbers onto the queue. c_forrange (n) - cqueue_i_push(&queue, (int)crand_unif(&rng, &dist)); + cqueue_i_push(&queue, (int)crand_uniform(&rng, &dist)); // Push or pop on the queue ten million times printf("%d\n", n); c_forrange (n) { // forrange uses initial n only. - int r = (int)crand_unif(&rng, &dist); + int r = (int)crand_uniform(&rng, &dist); if (r & 1) ++n, cqueue_i_push(&queue, r); else diff --git a/misc/examples/sortedmaps/gauss2.c b/misc/examples/sortedmaps/gauss2.c index 1ab8ade5..02ce4bc5 100644 --- a/misc/examples/sortedmaps/gauss2.c +++ b/misc/examples/sortedmaps/gauss2.c @@ -21,14 +21,14 @@ int main(void) printf("Mean %f, StdDev %f\n", Mean, StdDev); // Setup random engine with normal distribution. - crand_norm_t dist = crand_norm_init(Mean, StdDev); + crand_normal_t dist = crand_normal_init(Mean, StdDev); // Create and init histogram map with defered destruct csmap_int hist = {0}; cstr bar = {0}; c_forrange (N) { - int index = (int)round(crand_norm(&rng, &dist)); + int index = (int)round(crand_normal(&rng, &dist)); csmap_int_insert(&hist, index, 0).ref->second += 1; } diff --git a/misc/examples/spans/mdspan.c b/misc/examples/spans/mdspan.c new file mode 100644 index 00000000..4427299c --- /dev/null +++ b/misc/examples/spans/mdspan.c @@ -0,0 +1,51 @@ +#include +#include +#include + +using_cspan3(DSpan, double); + +int main(void) { + const int nx=5, ny=4, nz=3; + double* data = c_new_n(double, nx*ny*nz); + + printf("\nMultidim span ms[5, 4, 3], fortran ordered"); + DSpan3 ms = cspan_md_order('F', data, nx, ny, nz); // Fortran, not 'C' + + int idx = 0; + c_forrange (i, ms.shape[0]) + c_forrange (j, ms.shape[1]) + c_forrange (k, ms.shape[2]) + *cspan_at(&ms, i, j, k) = ++idx; + + cspan_transpose(&ms); + + printf(", transposed:\n\n"); + c_forrange (i, ms.shape[0]) { + c_forrange (j, ms.shape[1]) { + c_forrange (k, ms.shape[2]) + printf(" %3g", *cspan_at(&ms, i, j, k)); + puts(""); + } + puts(""); + } + + DSpan2 sub; + + puts("Slicing:"); + printf("\nms[0, :, :] "); + sub = cspan_slice(DSpan2, &ms, {0}, {c_ALL}, {c_ALL}); + c_foreach (i, DSpan2, sub) printf(" %g", *i.ref); + puts(""); + + printf("\nms[:, 0, :] "); + sub = cspan_slice(DSpan2, &ms, {c_ALL}, {0}, {c_ALL}); + c_foreach (i, DSpan2, sub) printf(" %g", *i.ref); + puts(""); + + sub = cspan_slice(DSpan2, &ms, {c_ALL}, {c_ALL}, {0}); + printf("\nms[:, :, 0] "); + c_foreach (i, DSpan2, sub) printf(" %g", *i.ref); + puts(""); + + free(data); +} -- cgit v1.2.3