-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
93 lines (74 loc) · 1.95 KB
/
main.c
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <eztasks.h>
#define TASK_COUNT 2
#define BUFFER_SIZE 2048
#define STATE_READING 0
#define STATE_WRITE_OUT 1
taskstatus_t task_read_file(task_t* task, tasktime_t execTime) {
uint16_t state = ezt_task__get_state(task);
taskstatus_t status = TS_FAILED;
switch (state) {
case STATE_READING:
FILE* fp = NULL;
char buffer[BUFFER_SIZE+1] = { 0 };
ezt_task__read_in(task, &fp);
size_t bytesRead = fread(buffer, sizeof(char), BUFFER_SIZE, fp);
if(bytesRead > 0) {
} else {
ezt_task__put_state(task, STATE_WRITE_OUT);
}
status = TS_INPROGRESS;
break;
case STATE_WRITE_OUT:
break;
default:
perror("INVALID STATE: task_read_file");
break;
}
return status;
}
task_t* async_read_file(FILE* fp) {
task_t* task = ezt_task__new(&fp, sizeof(FILE**), 0, &task_read_file, BUFFER_SIZE);
return task;
}
taskstatus_t task_wait(task_t* task, tasktime_t execTime) {
tasktime_t ms;
ezt_task__read_in(task, &ms);
if(execTime >= ms) return TS_COMPLETED;
return TS_INPROGRESS;
}
task_t* async_wait(double ms) {
task_t* task = ezt_task__new((tasktime_t[]) { ms }, sizeof(tasktime_t), 0, &task_wait, 0);
return task;
}
int main(int argc, char const *argv[])
{
taskgroup_t tg;
FILE* fp = fopen("test.txt", "r");
if(fp == NULL) {
perror("fopen");
return -1;
}
ezt_taskgroup__init(&tg, TASK_COUNT);
if(!EZT_TG_INITIATED(&tg)) {
fclose(fp);
perror("ezt_taskgroup__init");
return -1;
}
task_t* t_sleep = async_wait(4500);
task_t* t_read = async_read_file(fp);
task_t* tasks[TASK_COUNT] = { t_read, t_sleep };
ezt_taskgroup__gather(&tg, TASK_COUNT, tasks);
if(!EZT_TG_INITIATED(&tg)) {
perror("ezt_taskgroup__gather");
fclose(fp);
ezt_task__free(t_read);
ezt_task__free(t_sleep);
return -1;
}
ezt_taskgroup__await(&tg, 0); // wait for all
fclose(fp);
ezt_taskgroup__clean(&tg);
return 0;
}