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