forked from qbazd/utask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutest.c
139 lines (112 loc) · 1.85 KB
/
utest.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include "utask.h"
#include <signal.h>
pthread_t timer_thread;
volatile int run = 1;
#define TIPS 10
#define D1S TIPS
#define D3S (3*D1S)
#define D500MS (D1S/2)
#define D100MS (D1S/10)
void* timer_thread_fun(void* arg)
{
while (run)
{
utask_sleep_process();
usleep(1e6 / TIPS);
}
return;
}
int semaphore = 0;
void task_print(utask_t* t)
{
switch (t->istate)
{
case 0:
t->istate = 1;
utask_wait_eq(t,&semaphore,0,D3S);
break;
case 1:
if (t->sleep == 0)
{
t->istate = 0;
return;
}
semaphore = 1;
printf("%d\n",t->arg);
utask_sleep(t,D1S);
t->istate = 2;
break;
case 2:
semaphore = 0;
t->istate = 0;
utask_sleep(t,D100MS);
break;
}
}
void task_timedout(utask_t* t)
{
switch (t->istate)
{
case 0:
utask_wait_eq(t,&semaphore,0,D500MS);
t->istate = 1;
break;
case 1:
if (t->sleep == 0)
{
printf("T\n");
t->istate = 0;
}
else
{
t->istate = 2;
semaphore = 1;
utask_sleep(t,D500MS);
}
break;
case 2:
printf("OK\n");
semaphore = 0;
t->istate = 0;
utask_sleep(t,D1S);
break;
}
}
void signal_handler(int sig)
{
fprintf(stderr,"Got signal!\n");
run = 0;
}
void mcu_sleep(utask_timer_t s)
{
if (s == 0)
return;
printf("%dT\n",s);
usleep((1e6 * s / TIPS) / 2 + 1000);
}
int main()
{
utask_init();
signal(SIGINT,signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGTERM, signal_handler);
pthread_create(&timer_thread,NULL,timer_thread_fun,NULL);
utask_t* t;
t = utask_add(task_print);
t->arg = 1;
t = utask_add(task_print);
t->arg = 2;
t = utask_add(task_print);
t->arg = 3;
utask_add(task_timedout);
utask_put_mcu_to_sleep = mcu_sleep;
while (run)
{
utask_schedule();
}
pthread_join(timer_thread,NULL);
return 0;
}