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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include "triton_p.h"
static __thread struct list_head timers;
static __thread struct list_head timers_ss;
static __thread int in_timer;
asm(".hidden timer_prepare");
asm(".hidden timer_check");
asm(".hidden timer_init");
static void tv_add(struct timeval *tv,int msec);
void timer_init(void)
{
INIT_LIST_HEAD(&timers);
INIT_LIST_HEAD(&timers_ss);
in_timer=0;
}
void triton_timer_add(struct triton_timer_t*tt)
{
struct timer_t *t=(struct timer_t *)malloc(sizeof(struct timer_t));
t->del=0;
t->timer=tt;
tt->active=1;
list_add_tail(&t->entry,&timers);
}
void triton_timer_del(struct triton_timer_t*tt)
{
struct timer_t *t;
list_for_each_entry(t,&timers,entry)
{
if (t->timer==tt)
{
tt->active=0;
if (in_timer)
{
t->del=1;
}else
{
list_del(&t->entry);
free(t);
}
return;
}
}
}
void triton_timer_single_shot1(int twait,triton_ss_func func,int arg_cnt,...)
{
struct timeval tv;
struct timer_single_shot_t *t=(struct timer_single_shot_t *)malloc(sizeof(struct timer_single_shot_t));
memset(t,0,sizeof(*t));
gettimeofday(&tv,NULL);
tv_add(&tv,twait);
t->ss_func=func;
t->expire_tv=tv;//(struct timeval){tv.tv_sec+twait/1000,tv.tv_usec+(twait%1000)*1000000};
if (arg_cnt)
{
va_list p;
va_start(p,arg_cnt);
t->arg_cnt=arg_cnt;
t->args=malloc(arg_cnt*sizeof(long));
#ifdef BROKEN_GCC
for(i=0; i<arg_cnt; i++)
*((long*)t->args+i)=va_arg(p,long);
#else
memcpy(t->args,p,arg_cnt*sizeof(long));
#endif
va_end(p);
}
list_add_tail(&t->entry,&timers_ss);
}
void triton_timer_single_shot2(struct timeval *tv,triton_ss_func func,int arg_cnt,...)
{
struct timer_single_shot_t *t=(struct timer_single_shot_t *)malloc(sizeof(struct timer_single_shot_t));
memset(t,0,sizeof(*t));
t->ss_func=func;
t->expire_tv=*tv;//(struct timeval){tv.tv_sec+twait/1000,tv.tv_usec+(twait%1000)*1000000};
if (arg_cnt)
{
va_list p;
va_start(p,arg_cnt);
t->arg_cnt=arg_cnt;
t->args=malloc(arg_cnt*sizeof(long));
#ifdef BROKEN_GCC
for(i=0; i<arg_cnt; i++)
*((long*)t->args+i)=va_arg(p,long);
#else
memcpy(t->args,p,arg_cnt*sizeof(long));
#endif
va_end(p);
}
list_add_tail(&t->entry,&timers_ss);
}
void triton_timer_single_shot3(int tv_sec,int tv_usec,triton_ss_func func,int arg_cnt,...)
{
struct timer_single_shot_t *t=(struct timer_single_shot_t *)malloc(sizeof(struct timer_single_shot_t));
memset(t,0,sizeof(*t));
t->ss_func=func;
t->expire_tv.tv_sec=tv_sec;
t->expire_tv.tv_usec=tv_usec;
if (arg_cnt)
{
va_list p;
va_start(p,arg_cnt);
t->arg_cnt=arg_cnt;
t->args=malloc(arg_cnt*sizeof(long));
#ifdef BROKEN_GCC
for(i=0; i<arg_cnt; i++)
*((int*)t->args+i)=va_arg(p,long);
#else
memcpy(t->args,p,arg_cnt*sizeof(long));
#endif
va_end(p);
}
list_add_tail(&t->entry,&timers_ss);
}
int timer_prepare(struct timeval *tv)
{
struct timer_t *t;
struct timer_single_shot_t *ss_t;
int twait=-1,twait0;
list_for_each_entry(t,&timers,entry)
{
twait0=(t->timer->expire_tv.tv_sec-tv->tv_sec)*1000+
(t->timer->expire_tv.tv_usec-tv->tv_usec)/1000;
if (twait0<0) twait0=0;
if (twait0>=0 && (twait==-1 || twait0<twait))
twait=twait0;
}
if (twait)
{
list_for_each_entry(ss_t,&timers_ss,entry)
{
twait0=(ss_t->expire_tv.tv_sec-tv->tv_sec)*1000+
(ss_t->expire_tv.tv_usec-tv->tv_usec)/1000;
if (twait0<0) twait0=0;
if (twait0>=0 && (twait==-1 || twait0<twait))
twait=twait0;
}
}
return twait;
}
void timer_check(struct timeval *tv)
{
struct timer_t *t;
struct timer_single_shot_t *ss_t;
struct list_head *p1,*p2;
int twait0;
in_timer=1;
list_for_each_safe(p1,p2,&timers)
{
t=list_entry(p1,struct timer_t,entry);
if (t->del) continue;
twait0=(t->timer->expire_tv.tv_sec-tv->tv_sec)*1000+
(t->timer->expire_tv.tv_usec-tv->tv_usec)/1000;
if (twait0<=0)
{
if (!t->timer->expire(t->timer))
{
t->timer->active=0;
list_del(&t->entry);
free(t);
continue;
}
if (t->timer->period)
{
tv_add(&t->timer->expire_tv,t->timer->period);
}
}
}
list_for_each_safe(p1,p2,&timers_ss)
{
ss_t=list_entry(p1,struct timer_single_shot_t,entry);
twait0=(ss_t->expire_tv.tv_sec-tv->tv_sec)*1000+
(ss_t->expire_tv.tv_usec-tv->tv_usec)/1000;
if (twait0<=0)
{
list_del(&ss_t->entry);
if (ss_t->arg_cnt)
{
//args_p=&ss_t->args;
//memcpy(pp+ARG_OFFSET,ss_t->args,ss_t->arg_cnt*sizeof(int));
//__builtin_apply(ss_t->ss_func,pp,ARG_OFFSET+ss_t->arg_cnt*sizeof(int));
dyn_call(ss_t->ss_func,ss_t->arg_cnt,ss_t->args);
free(ss_t->args);
}else ss_t->ss_func();
free(ss_t);
}
}
list_for_each_safe(p1,p2,&timers)
{
t=list_entry(p1,struct timer_t,entry);
if (t->del)
{
list_del(&t->entry);
t->timer->active=0;
free(t);
}
}
in_timer=0;
}
static void tv_add(struct timeval *tv,int msec)
{
tv->tv_sec+=msec/1000;
tv->tv_usec+=(msec%1000)*1000;
if (tv->tv_usec>=1000000)
{
tv->tv_sec++;
tv->tv_usec-=1000000;
}
}
|