diff options
| author | tylov <[email protected]> | 2023-08-13 20:24:55 +0200 |
|---|---|---|
| committer | tylov <[email protected]> | 2023-08-13 20:24:55 +0200 |
| commit | 8bb2f5618e4cefe668a663936354cf53191f2129 (patch) | |
| tree | c569115cfe6901a0a1b23b8cc5e96fb4421be0b7 | |
| parent | 972aa23a674f743c187e82444c2271aaa3e9cd06 (diff) | |
| download | STC-modified-8bb2f5618e4cefe668a663936354cf53191f2129.tar.gz STC-modified-8bb2f5618e4cefe668a663936354cf53191f2129.zip | |
Updated scheduler.c example.
| -rw-r--r-- | include/c11/fmt.h | 6 | ||||
| -rw-r--r-- | misc/examples/coroutines/scheduler.c | 73 |
2 files changed, 36 insertions, 43 deletions
diff --git a/include/c11/fmt.h b/include/c11/fmt.h index d2eab8bc..d7c10cbe 100644 --- a/include/c11/fmt.h +++ b/include/c11/fmt.h @@ -63,8 +63,8 @@ int main(void) { time_t now = time(NULL);
struct tm t1 = *localtime(&now), t2 = t1;
t2.tm_year += 2;
- fmt_print(1, "Dates:\n {}\n {}\n", fmt_tm("%Y-%m-%d %X %Z", &t1),
- fmt_tm("%Y-%m-%d %X %Z", &t2));
+ fmt_print("Dates:\n {}\n {}\n", fmt_tm("%Y-%m-%d %X %Z", &t1),
+ fmt_tm("%Y-%m-%d %X %Z", &t2));
}
*/
#include <stdio.h>
@@ -208,7 +208,7 @@ void fmt_close(fmt_stream* ss) { }
const char* fmt_tm(const char *fmt, const struct tm *tp) {
- static char buf[2][64], i = 0;
+ static char buf[2][64], i = 1;
i = !i;
strftime(buf[i], 64, fmt, tp);
return buf[i];
diff --git a/misc/examples/coroutines/scheduler.c b/misc/examples/coroutines/scheduler.c index 78461277..be1810d2 100644 --- a/misc/examples/coroutines/scheduler.c +++ b/misc/examples/coroutines/scheduler.c @@ -2,70 +2,63 @@ #include <stdio.h> #include <stc/coroutine.h> -struct Task { - int (*fn)(struct Task*); - int cco_state; - struct Scheduler* sched; -}; - -#define i_type Scheduler -#define i_key struct Task +#define i_type cco_tasks +#define i_key cco_task* +#define i_keydrop(x) { puts("free task"); free(*x); } +#define i_no_clone #include <stc/cqueue.h> -static bool schedule(Scheduler* sched) -{ - struct Task task = *Scheduler_front(sched); - Scheduler_pop(sched); - - if (!cco_done(&task)) - task.fn(&task); - - return !Scheduler_empty(sched); -} +typedef struct { + cco_tasks tasks; +} cco_scheduler; -static int push_task(const struct Task* task) -{ - Scheduler_push(task->sched, *task); - return CCO_YIELD; +void cco_scheduler_drop(cco_scheduler* sched) { + cco_tasks_drop(&sched->tasks); } +int cco_scheduler_run(cco_scheduler* sched) { + while (!cco_tasks_empty(&sched->tasks)) { + cco_task* task = cco_tasks_pull(&sched->tasks); + if (cco_resume_task(task, NULL)) + cco_tasks_push(&sched->tasks, task); + else + cco_tasks_value_drop(&task); + } + return 0; +} -static int taskA(struct Task* task) -{ +static int taskA(cco_task* task, cco_runtime* rt) { cco_routine(task) { puts("Hello, from task A"); - cco_yield_v(push_task(task)); + cco_yield(); puts("A is back doing work"); - cco_yield_v(push_task(task)); + cco_yield(); puts("A is back doing more work"); - cco_yield_v(push_task(task)); + cco_yield(); puts("A is back doing even more work"); } return 0; } -static int taskB(struct Task* task) -{ +static int taskB(cco_task* task, cco_runtime* rt) { cco_routine(task) { puts("Hello, from task B"); - cco_yield_v(push_task(task)); + cco_yield(); puts("B is back doing work"); - cco_yield_v(push_task(task)); + cco_yield(); puts("B is back doing more work"); } return 0; } -void Use(void) -{ - Scheduler scheduler = c_init(Scheduler, { - {.fn=taskA, .sched=&scheduler}, - {.fn=taskB, .sched=&scheduler}, - }); - - while (schedule(&scheduler)) {} +void Use(void) { + cco_scheduler sched = {.tasks = c_init(cco_tasks, { + c_new(cco_task, {.cco_func=taskA}), + c_new(cco_task, {.cco_func=taskB}), + })}; - Scheduler_drop(&scheduler); + cco_scheduler_run(&sched); + cco_scheduler_drop(&sched); } int main(void) |
