8 months ago |
37 |
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 |
|||||
5 months ago |
13 |
#define DEMO_STACK_SIZE (8192*sizeof(void *)) | |||
14 |
|||||
8 months ago |
37 |
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; | ||||
5 months ago |
44 |
Task_ctor(&task1, DEMO_STACK_SIZE, asynctestpart, &task1param); | |||
8 months ago |
37 |
45 |
printf("task1 going\n"); | ||
46 |
|||||
47 |
asynctestpartparam task2param = { | ||||
48 |
"Second", | ||||
49 |
0.75f, | ||||
50 |
8 | ||||
51 |
}; | ||||
5 months ago |
52 |
Task *task2 = Task_New(DEMO_STACK_SIZE, asynctestpart, &task2param); | |||
8 months ago |
37 |
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 |
Coroutine_StartSystem(); | ||||
71 |
void *res = NULL; | ||||
5 months ago |
72 |
bool canceled = Task_Run(DEMO_STACK_SIZE, asynctest, NULL, &res); | |||
8 months ago |
37 |
73 |
Coroutine_StopSystem(); | ||
74 |
ASleep_StopSystem(); | ||||
75 |
|||||
76 |
printf("Async result %d:%p\n", canceled, res); | ||||
77 |
} | ||||
78 |