| 1 | #include <stdio.h> |
| 2 | #include <stddef.h> |
| 3 | #include "coroutine.h" |
| 4 | #include "generator.h" |
| 5 | #include "asleep.h" |
| 6 | #include "task.h" |
| 7 | |
| 8 | #include <dirent.h> |
| 9 | #include <string.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | #define DEMO_STACK_SIZE (8192*sizeof(void *)) |
| 14 | |
| 15 | typedef struct asynctestpartparam { |
| 16 | char *name; |
| 17 | float delay; |
| 18 | int count; |
| 19 | } asynctestpartparam; |
| 20 | |
| 21 | bool asynctestpart(void *param, void **res){ |
| 22 | (void)res; |
| 23 | asynctestpartparam *spec = (asynctestpartparam *)param; |
| 24 | printf("%s started\n", spec->name); |
| 25 | |
| 26 | for (int i=0; i < spec->count; ++i){ |
| 27 | ASleep(spec->delay, NULL); |
| 28 | printf("%s %d\n", spec->name, i); |
| 29 | } |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | bool asynctest(void *param, void **res){ |
| 34 | (void)param; |
| 35 | (void)res; |
| 36 | printf("async test started\n"); |
| 37 | |
| 38 | asynctestpartparam task1param = { |
| 39 | "First", |
| 40 | 0.5f, |
| 41 | 5 |
| 42 | }; |
| 43 | Task task1; |
| 44 | Task_ctor(&task1, DEMO_STACK_SIZE, asynctestpart, &task1param); |
| 45 | printf("task1 going\n"); |
| 46 | |
| 47 | asynctestpartparam task2param = { |
| 48 | "Second", |
| 49 | 0.75f, |
| 50 | 8 |
| 51 | }; |
| 52 | Task *task2 = Task_New(DEMO_STACK_SIZE, asynctestpart, &task2param); |
| 53 | printf("task2 going\n"); |
| 54 | |
| 55 | bool canceled1 = Task_Await(&task1, NULL); |
| 56 | bool canceled2 = Task_Await(task2, NULL); |
| 57 | |
| 58 | Task_Delete(task2); |
| 59 | Task_dtor(&task1); |
| 60 | |
| 61 | printf("Tasks complete %d %d\n", canceled1, canceled2); |
| 62 | return canceled1 | canceled2; |
| 63 | } |
| 64 | |
| 65 | int main(int argc, char *argv[]) { |
| 66 | (void)argc; |
| 67 | (void)argv; |
| 68 | |
| 69 | ASleep_StartSystem(); |
| 70 | void *res = NULL; |
| 71 | bool canceled = Task_Run(DEMO_STACK_SIZE, asynctest, NULL, &res); |
| 72 | ASleep_StopSystem(); |
| 73 | |
| 74 | printf("Async result %d:%p\n", canceled, res); |
| 75 | } |
| 76 | |