1 contributor
76 lines1.6 KB
Newer
Older
-
+
commited
{line.log.rev}
on
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
13
typedef struct asynctestpartparam {
14
char *name;
15
float delay;
16
int count;
17
} asynctestpartparam;
18
19
bool asynctestpart(void *param, void **res){
20
(void)res;
21
asynctestpartparam *spec = (asynctestpartparam *)param;
22
printf("%s started\n", spec->name);
23
24
for (int i=0; i < spec->count; ++i){
25
ASleep(spec->delay, NULL);
26
printf("%s %d\n", spec->name, i);
27
}
28
return false;
29
}
30
31
bool asynctest(void *param, void **res){
32
(void)param;
33
(void)res;
34
printf("async test started\n");
35
36
asynctestpartparam task1param = {
37
"First",
38
0.5f,
39
5
40
};
41
Task task1;
42
Task_ctor(&task1, asynctestpart, &task1param);
43
printf("task1 going\n");
44
45
asynctestpartparam task2param = {
46
"Second",
47
0.75f,
48
8
49
};
50
Task *task2 = Task_New(asynctestpart, &task2param);
51
printf("task2 going\n");
52
53
bool canceled1 = Task_Await(&task1, NULL);
54
bool canceled2 = Task_Await(task2, NULL);
55
56
Task_Delete(task2);
57
Task_dtor(&task1);
58
59
printf("Tasks complete %d %d\n", canceled1, canceled2);
60
return canceled1 | canceled2;
61
}
62
63
int main(int argc, char *argv[]) {
64
(void)argc;
65
(void)argv;
66
67
ASleep_StartSystem();
68
Coroutine_StartSystem();
69
void *res = NULL;
70
bool canceled = Task_Run(asynctest, NULL, &res);
71
Coroutine_StopSystem();
72
ASleep_StopSystem();
73
74
printf("Async result %d:%p\n", canceled, res);
75
}
76