1 | #include <math.h> |
1 | #include <math.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
|
|
3 | #include <unistd.h> |
|
|
4 | #include <fcntl.h> |
|
|
5 | #include <signal.h> |
3 | |
6 | |
4 | #include <stdio.h> |
7 | #include <stdio.h> |
5 | |
8 | |
|
|
9 | #include <assert.h> |
6 | #include <errno.h> |
10 | #include <errno.h> |
7 | #include <sys/time.h> |
11 | #include <sys/time.h> |
8 | #include <time.h> |
12 | #include <time.h> |
9 | |
13 | |
10 | #ifdef CLOCK_MONOTONIC |
|
|
11 | # define HAVE_MONOTONIC 1 |
|
|
12 | #endif |
|
|
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
14 | #define HAVE_EPOLL 1 |
15 | #define HAVE_REALTIME 1 |
15 | |
|
|
16 | #ifndef HAVE_MONOTONIC |
|
|
17 | # ifdef CLOCK_MONOTONIC |
|
|
18 | # define HAVE_MONOTONIC 1 |
|
|
19 | # endif |
|
|
20 | #endif |
|
|
21 | |
|
|
22 | #ifndef HAVE_SELECT |
16 | #define HAVE_SELECT 0 |
23 | # define HAVE_SELECT 1 |
|
|
24 | #endif |
17 | |
25 | |
|
|
26 | #ifndef HAVE_EPOLL |
|
|
27 | # define HAVE_EPOLL 0 |
|
|
28 | #endif |
|
|
29 | |
|
|
30 | #ifndef HAVE_REALTIME |
|
|
31 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
32 | #endif |
|
|
33 | |
|
|
34 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
18 | #define MAX_BLOCKTIME 60. |
35 | #define MAX_BLOCKTIME 60. |
19 | |
36 | |
20 | #include "ev.h" |
37 | #include "ev.h" |
21 | |
38 | |
22 | struct ev_watcher { |
39 | struct ev_watcher { |
… | |
… | |
25 | |
42 | |
26 | struct ev_watcher_list { |
43 | struct ev_watcher_list { |
27 | EV_WATCHER_LIST (ev_watcher_list); |
44 | EV_WATCHER_LIST (ev_watcher_list); |
28 | }; |
45 | }; |
29 | |
46 | |
|
|
47 | struct ev_watcher_time { |
|
|
48 | EV_WATCHER_TIME (ev_watcher_time); |
|
|
49 | }; |
|
|
50 | |
|
|
51 | typedef struct ev_watcher *W; |
|
|
52 | typedef struct ev_watcher_list *WL; |
|
|
53 | typedef struct ev_watcher_time *WT; |
|
|
54 | |
|
|
55 | static ev_tstamp now, diff; /* monotonic clock */ |
30 | ev_tstamp ev_now; |
56 | ev_tstamp ev_now; |
31 | int ev_method; |
57 | int ev_method; |
32 | |
58 | |
33 | static int have_monotonic; /* runtime */ |
59 | static int have_monotonic; /* runtime */ |
34 | |
60 | |
35 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
61 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
36 | static void (*method_reify)(void); |
62 | static void (*method_modify)(int fd, int oev, int nev); |
37 | static void (*method_poll)(ev_tstamp timeout); |
63 | static void (*method_poll)(ev_tstamp timeout); |
|
|
64 | |
|
|
65 | /*****************************************************************************/ |
38 | |
66 | |
39 | ev_tstamp |
67 | ev_tstamp |
40 | ev_time (void) |
68 | ev_time (void) |
41 | { |
69 | { |
42 | #if HAVE_REALTIME |
70 | #if HAVE_REALTIME |
… | |
… | |
66 | } |
94 | } |
67 | |
95 | |
68 | #define array_needsize(base,cur,cnt,init) \ |
96 | #define array_needsize(base,cur,cnt,init) \ |
69 | if ((cnt) > cur) \ |
97 | if ((cnt) > cur) \ |
70 | { \ |
98 | { \ |
71 | int newcnt = cur; \ |
99 | int newcnt = cur ? cur << 1 : 16; \ |
72 | do \ |
|
|
73 | { \ |
|
|
74 | newcnt += (newcnt >> 1) + 16; \ |
|
|
75 | } \ |
|
|
76 | while ((cnt) > newcnt); \ |
|
|
77 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
100 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
78 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
101 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
79 | init (base + cur, newcnt - cur); \ |
102 | init (base + cur, newcnt - cur); \ |
80 | cur = newcnt; \ |
103 | cur = newcnt; \ |
81 | } |
104 | } |
82 | |
105 | |
|
|
106 | /*****************************************************************************/ |
|
|
107 | |
83 | typedef struct |
108 | typedef struct |
84 | { |
109 | { |
85 | struct ev_io *head; |
110 | struct ev_io *head; |
86 | unsigned char wev, rev; /* want, received event set */ |
111 | unsigned char wev, rev; /* want, received event set */ |
87 | } ANFD; |
112 | } ANFD; |
… | |
… | |
103 | } |
128 | } |
104 | } |
129 | } |
105 | |
130 | |
106 | typedef struct |
131 | typedef struct |
107 | { |
132 | { |
108 | struct ev_watcher *w; |
133 | W w; |
109 | int events; |
134 | int events; |
110 | } ANPENDING; |
135 | } ANPENDING; |
111 | |
136 | |
112 | static ANPENDING *pendings; |
137 | static ANPENDING *pendings; |
113 | static int pendingmax, pendingcnt; |
138 | static int pendingmax, pendingcnt; |
114 | |
139 | |
115 | static void |
140 | static void |
116 | event (struct ev_watcher *w, int events) |
141 | event (W w, int events) |
117 | { |
142 | { |
118 | w->pending = ++pendingcnt; |
143 | w->pending = ++pendingcnt; |
119 | array_needsize (pendings, pendingmax, pendingcnt, ); |
144 | array_needsize (pendings, pendingmax, pendingcnt, ); |
120 | pendings [pendingcnt - 1].w = w; |
145 | pendings [pendingcnt - 1].w = w; |
121 | pendings [pendingcnt - 1].events = events; |
146 | pendings [pendingcnt - 1].events = events; |
… | |
… | |
130 | for (w = anfd->head; w; w = w->next) |
155 | for (w = anfd->head; w; w = w->next) |
131 | { |
156 | { |
132 | int ev = w->events & events; |
157 | int ev = w->events & events; |
133 | |
158 | |
134 | if (ev) |
159 | if (ev) |
135 | event ((struct ev_watcher *)w, ev); |
160 | event ((W)w, ev); |
136 | } |
161 | } |
137 | } |
162 | } |
|
|
163 | |
|
|
164 | static void |
|
|
165 | queue_events (W *events, int eventcnt, int type) |
|
|
166 | { |
|
|
167 | int i; |
|
|
168 | |
|
|
169 | for (i = 0; i < eventcnt; ++i) |
|
|
170 | event (events [i], type); |
|
|
171 | } |
|
|
172 | |
|
|
173 | /*****************************************************************************/ |
138 | |
174 | |
139 | static struct ev_timer **timers; |
175 | static struct ev_timer **timers; |
140 | static int timermax, timercnt; |
176 | static int timermax, timercnt; |
141 | |
177 | |
|
|
178 | static struct ev_periodic **periodics; |
|
|
179 | static int periodicmax, periodiccnt; |
|
|
180 | |
142 | static void |
181 | static void |
143 | upheap (int k) |
182 | upheap (WT *timers, int k) |
144 | { |
183 | { |
145 | struct ev_timer *w = timers [k]; |
184 | WT w = timers [k]; |
146 | |
185 | |
147 | while (k && timers [k >> 1]->at > w->at) |
186 | while (k && timers [k >> 1]->at > w->at) |
148 | { |
187 | { |
149 | timers [k] = timers [k >> 1]; |
188 | timers [k] = timers [k >> 1]; |
150 | timers [k]->active = k + 1; |
189 | timers [k]->active = k + 1; |
… | |
… | |
155 | timers [k]->active = k + 1; |
194 | timers [k]->active = k + 1; |
156 | |
195 | |
157 | } |
196 | } |
158 | |
197 | |
159 | static void |
198 | static void |
160 | downheap (int k) |
199 | downheap (WT *timers, int N, int k) |
161 | { |
200 | { |
162 | struct ev_timer *w = timers [k]; |
201 | WT w = timers [k]; |
163 | |
202 | |
164 | while (k <= (timercnt >> 1)) |
203 | while (k < (N >> 1)) |
165 | { |
204 | { |
166 | int j = k << 1; |
205 | int j = k << 1; |
167 | |
206 | |
168 | if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at) |
207 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
169 | ++j; |
208 | ++j; |
170 | |
209 | |
171 | if (w->at <= timers [j]->at) |
210 | if (w->at <= timers [j]->at) |
172 | break; |
211 | break; |
173 | |
212 | |
174 | timers [k] = timers [j]; |
213 | timers [k] = timers [j]; |
175 | timers [k]->active = k; |
214 | timers [k]->active = k + 1; |
176 | k = j; |
215 | k = j; |
177 | } |
216 | } |
178 | |
217 | |
179 | timers [k] = w; |
218 | timers [k] = w; |
180 | timers [k]->active = k + 1; |
219 | timers [k]->active = k + 1; |
181 | } |
220 | } |
182 | |
221 | |
183 | static struct ev_signal **signals; |
222 | /*****************************************************************************/ |
|
|
223 | |
|
|
224 | typedef struct |
|
|
225 | { |
|
|
226 | struct ev_signal *head; |
|
|
227 | sig_atomic_t gotsig; |
|
|
228 | } ANSIG; |
|
|
229 | |
|
|
230 | static ANSIG *signals; |
184 | static int signalmax, signalcnt; |
231 | static int signalmax; |
185 | |
232 | |
|
|
233 | static int sigpipe [2]; |
|
|
234 | static sig_atomic_t gotsig; |
|
|
235 | static struct ev_io sigev; |
|
|
236 | |
186 | static void |
237 | static void |
187 | signals_init (struct ev_signal **base, int count) |
238 | signals_init (ANSIG *base, int count) |
188 | { |
239 | { |
189 | while (count--) |
240 | while (count--) |
190 | *base++ = 0; |
241 | { |
|
|
242 | base->head = 0; |
|
|
243 | base->gotsig = 0; |
|
|
244 | ++base; |
|
|
245 | } |
191 | } |
246 | } |
|
|
247 | |
|
|
248 | static void |
|
|
249 | sighandler (int signum) |
|
|
250 | { |
|
|
251 | signals [signum - 1].gotsig = 1; |
|
|
252 | |
|
|
253 | if (!gotsig) |
|
|
254 | { |
|
|
255 | gotsig = 1; |
|
|
256 | write (sigpipe [1], &gotsig, 1); |
|
|
257 | } |
|
|
258 | } |
|
|
259 | |
|
|
260 | static void |
|
|
261 | sigcb (struct ev_io *iow, int revents) |
|
|
262 | { |
|
|
263 | struct ev_signal *w; |
|
|
264 | int sig; |
|
|
265 | |
|
|
266 | gotsig = 0; |
|
|
267 | read (sigpipe [0], &revents, 1); |
|
|
268 | |
|
|
269 | for (sig = signalmax; sig--; ) |
|
|
270 | if (signals [sig].gotsig) |
|
|
271 | { |
|
|
272 | signals [sig].gotsig = 0; |
|
|
273 | |
|
|
274 | for (w = signals [sig].head; w; w = w->next) |
|
|
275 | event ((W)w, EV_SIGNAL); |
|
|
276 | } |
|
|
277 | } |
|
|
278 | |
|
|
279 | static void |
|
|
280 | siginit (void) |
|
|
281 | { |
|
|
282 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
|
|
283 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
284 | |
|
|
285 | /* rather than sort out wether we really need nb, set it */ |
|
|
286 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
287 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
288 | |
|
|
289 | evio_set (&sigev, sigpipe [0], EV_READ); |
|
|
290 | evio_start (&sigev); |
|
|
291 | } |
|
|
292 | |
|
|
293 | /*****************************************************************************/ |
|
|
294 | |
|
|
295 | static struct ev_idle **idles; |
|
|
296 | static int idlemax, idlecnt; |
|
|
297 | |
|
|
298 | static struct ev_check **checks; |
|
|
299 | static int checkmax, checkcnt; |
|
|
300 | |
|
|
301 | /*****************************************************************************/ |
192 | |
302 | |
193 | #if HAVE_EPOLL |
303 | #if HAVE_EPOLL |
194 | # include "ev_epoll.c" |
304 | # include "ev_epoll.c" |
195 | #endif |
305 | #endif |
196 | #if HAVE_SELECT |
306 | #if HAVE_SELECT |
… | |
… | |
206 | have_monotonic = 1; |
316 | have_monotonic = 1; |
207 | } |
317 | } |
208 | #endif |
318 | #endif |
209 | |
319 | |
210 | ev_now = ev_time (); |
320 | ev_now = ev_time (); |
|
|
321 | now = get_clock (); |
|
|
322 | diff = ev_now - now; |
211 | |
323 | |
|
|
324 | if (pipe (sigpipe)) |
|
|
325 | return 0; |
|
|
326 | |
|
|
327 | ev_method = EVMETHOD_NONE; |
212 | #if HAVE_EPOLL |
328 | #if HAVE_EPOLL |
213 | if (epoll_init (flags)) |
329 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
214 | return ev_method; |
|
|
215 | #endif |
330 | #endif |
216 | #if HAVE_SELECT |
331 | #if HAVE_SELECT |
217 | if (select_init (flags)) |
332 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
218 | return ev_method; |
|
|
219 | #endif |
333 | #endif |
220 | |
334 | |
221 | ev_method = EVMETHOD_NONE; |
335 | if (ev_method) |
|
|
336 | { |
|
|
337 | evw_init (&sigev, sigcb); |
|
|
338 | siginit (); |
|
|
339 | } |
|
|
340 | |
222 | return ev_method; |
341 | return ev_method; |
223 | } |
342 | } |
224 | |
343 | |
|
|
344 | /*****************************************************************************/ |
|
|
345 | |
225 | void ev_prefork (void) |
346 | void ev_prefork (void) |
226 | { |
347 | { |
|
|
348 | /* nop */ |
227 | } |
349 | } |
228 | |
350 | |
229 | void ev_postfork_parent (void) |
351 | void ev_postfork_parent (void) |
230 | { |
352 | { |
|
|
353 | /* nop */ |
231 | } |
354 | } |
232 | |
355 | |
233 | void ev_postfork_child (void) |
356 | void ev_postfork_child (void) |
234 | { |
357 | { |
235 | #if HAVE_EPOLL |
358 | #if HAVE_EPOLL |
|
|
359 | if (ev_method == EVMETHOD_EPOLL) |
236 | epoll_postfork_child (); |
360 | epoll_postfork_child (); |
237 | #endif |
361 | #endif |
|
|
362 | |
|
|
363 | evio_stop (&sigev); |
|
|
364 | close (sigpipe [0]); |
|
|
365 | close (sigpipe [1]); |
|
|
366 | pipe (sigpipe); |
|
|
367 | siginit (); |
|
|
368 | } |
|
|
369 | |
|
|
370 | /*****************************************************************************/ |
|
|
371 | |
|
|
372 | static void |
|
|
373 | fd_reify (void) |
|
|
374 | { |
|
|
375 | int i; |
|
|
376 | |
|
|
377 | for (i = 0; i < fdchangecnt; ++i) |
|
|
378 | { |
|
|
379 | int fd = fdchanges [i]; |
|
|
380 | ANFD *anfd = anfds + fd; |
|
|
381 | struct ev_io *w; |
|
|
382 | |
|
|
383 | int wev = 0; |
|
|
384 | |
|
|
385 | for (w = anfd->head; w; w = w->next) |
|
|
386 | wev |= w->events; |
|
|
387 | |
|
|
388 | if (anfd->wev != wev) |
|
|
389 | { |
|
|
390 | method_modify (fd, anfd->wev, wev); |
|
|
391 | anfd->wev = wev; |
|
|
392 | } |
|
|
393 | } |
|
|
394 | |
|
|
395 | fdchangecnt = 0; |
238 | } |
396 | } |
239 | |
397 | |
240 | static void |
398 | static void |
241 | call_pending () |
399 | call_pending () |
242 | { |
400 | { |
… | |
… | |
255 | |
413 | |
256 | pendingcnt = 0; |
414 | pendingcnt = 0; |
257 | } |
415 | } |
258 | |
416 | |
259 | static void |
417 | static void |
260 | timer_reify (void) |
418 | timers_reify () |
261 | { |
419 | { |
262 | while (timercnt && timers [0]->at <= ev_now) |
420 | while (timercnt && timers [0]->at <= now) |
263 | { |
421 | { |
264 | struct ev_timer *w = timers [0]; |
422 | struct ev_timer *w = timers [0]; |
265 | |
423 | |
266 | /* first reschedule timer */ |
424 | /* first reschedule or stop timer */ |
267 | if (w->repeat) |
425 | if (w->repeat) |
268 | { |
426 | { |
269 | fprintf (stderr, "a %f now %f repeat %f, %f\n", w->at, ev_now, w->repeat, w->repeat *1e30);//D |
|
|
270 | if (w->is_abs) |
|
|
271 | w->at += floor ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
272 | else |
|
|
273 | w->at = ev_now + w->repeat; |
427 | w->at = now + w->repeat; |
274 | |
428 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
275 | fprintf (stderr, "b %f\n", w->at);//D |
429 | downheap ((WT *)timers, timercnt, 0); |
276 | |
|
|
277 | downheap (0); |
|
|
278 | } |
430 | } |
279 | else |
431 | else |
280 | evtimer_stop (w); /* nonrepeating: stop timer */ |
432 | evtimer_stop (w); /* nonrepeating: stop timer */ |
281 | |
433 | |
282 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
434 | event ((W)w, EV_TIMEOUT); |
|
|
435 | } |
|
|
436 | } |
|
|
437 | |
|
|
438 | static void |
|
|
439 | periodics_reify () |
|
|
440 | { |
|
|
441 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
442 | { |
|
|
443 | struct ev_periodic *w = periodics [0]; |
|
|
444 | |
|
|
445 | /* first reschedule or stop timer */ |
|
|
446 | if (w->interval) |
|
|
447 | { |
|
|
448 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
449 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
450 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
451 | } |
|
|
452 | else |
|
|
453 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
|
|
454 | |
|
|
455 | event ((W)w, EV_TIMEOUT); |
|
|
456 | } |
|
|
457 | } |
|
|
458 | |
|
|
459 | static void |
|
|
460 | time_jump (ev_tstamp diff) |
|
|
461 | { |
|
|
462 | int i; |
|
|
463 | |
|
|
464 | /* adjust periodics */ |
|
|
465 | for (i = 0; i < periodiccnt; ++i) |
|
|
466 | { |
|
|
467 | struct ev_periodic *w = periodics [i]; |
|
|
468 | |
|
|
469 | if (w->interval) |
|
|
470 | { |
|
|
471 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
472 | |
|
|
473 | if (fabs (diff) >= 1e-4) |
|
|
474 | { |
|
|
475 | evperiodic_stop (w); |
|
|
476 | evperiodic_start (w); |
|
|
477 | |
|
|
478 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
479 | } |
|
|
480 | } |
|
|
481 | } |
|
|
482 | |
|
|
483 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
484 | for (i = 0; i < timercnt; ++i) |
|
|
485 | timers [i]->at += diff; |
|
|
486 | } |
|
|
487 | |
|
|
488 | static void |
|
|
489 | time_update () |
|
|
490 | { |
|
|
491 | int i; |
|
|
492 | |
|
|
493 | ev_now = ev_time (); |
|
|
494 | |
|
|
495 | if (have_monotonic) |
|
|
496 | { |
|
|
497 | ev_tstamp odiff = diff; |
|
|
498 | |
|
|
499 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
500 | { |
|
|
501 | now = get_clock (); |
|
|
502 | diff = ev_now - now; |
|
|
503 | |
|
|
504 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
|
|
505 | return; /* all is well */ |
|
|
506 | |
|
|
507 | ev_now = ev_time (); |
|
|
508 | } |
|
|
509 | |
|
|
510 | time_jump (diff - odiff); |
|
|
511 | } |
|
|
512 | else |
|
|
513 | { |
|
|
514 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
|
|
515 | time_jump (ev_now - now); |
|
|
516 | |
|
|
517 | now = ev_now; |
283 | } |
518 | } |
284 | } |
519 | } |
285 | |
520 | |
286 | int ev_loop_done; |
521 | int ev_loop_done; |
287 | |
522 | |
288 | int ev_loop (int flags) |
523 | void ev_loop (int flags) |
289 | { |
524 | { |
290 | double block; |
525 | double block; |
291 | ev_loop_done = flags & EVLOOP_ONESHOT; |
526 | ev_loop_done = flags & EVLOOP_ONESHOT; |
292 | |
527 | |
|
|
528 | if (checkcnt) |
|
|
529 | { |
|
|
530 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
531 | call_pending (); |
|
|
532 | } |
|
|
533 | |
293 | do |
534 | do |
294 | { |
535 | { |
295 | /* update fd-related kernel structures */ |
536 | /* update fd-related kernel structures */ |
296 | method_reify (); fdchangecnt = 0; |
537 | fd_reify (); |
297 | |
538 | |
298 | /* calculate blocking time */ |
539 | /* calculate blocking time */ |
|
|
540 | |
|
|
541 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
299 | ev_now = ev_time (); |
542 | ev_now = ev_time (); |
300 | |
543 | |
301 | if (flags & EVLOOP_NONBLOCK) |
544 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
302 | block = 0.; |
545 | block = 0.; |
303 | else if (!timercnt) |
|
|
304 | block = MAX_BLOCKTIME; |
|
|
305 | else |
546 | else |
306 | { |
547 | { |
|
|
548 | block = MAX_BLOCKTIME; |
|
|
549 | |
|
|
550 | if (timercnt) |
|
|
551 | { |
|
|
552 | ev_tstamp to = timers [0]->at - get_clock () + method_fudge; |
|
|
553 | if (block > to) block = to; |
|
|
554 | } |
|
|
555 | |
|
|
556 | if (periodiccnt) |
|
|
557 | { |
307 | block = timers [0]->at - ev_now + method_fudge; |
558 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
|
|
559 | if (block > to) block = to; |
|
|
560 | } |
|
|
561 | |
308 | if (block < 0.) block = 0.; |
562 | if (block < 0.) block = 0.; |
309 | else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME; |
|
|
310 | } |
563 | } |
311 | |
564 | |
312 | fprintf (stderr, "block %f\n", block);//D |
|
|
313 | method_poll (block); |
565 | method_poll (block); |
314 | |
566 | |
|
|
567 | /* update ev_now, do magic */ |
|
|
568 | time_update (); |
|
|
569 | |
315 | /* put pending timers into pendign queue and reschedule them */ |
570 | /* queue pending timers and reschedule them */ |
316 | timer_reify (); |
571 | periodics_reify (); /* absolute timers first */ |
|
|
572 | timers_reify (); /* relative timers second */ |
317 | |
573 | |
318 | ev_now = ev_time (); |
574 | /* queue idle watchers unless io or timers are pending */ |
|
|
575 | if (!pendingcnt) |
|
|
576 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
|
|
577 | |
|
|
578 | /* queue check and possibly idle watchers */ |
|
|
579 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
580 | |
319 | call_pending (); |
581 | call_pending (); |
320 | } |
582 | } |
321 | while (!ev_loop_done); |
583 | while (!ev_loop_done); |
322 | } |
584 | } |
323 | |
585 | |
|
|
586 | /*****************************************************************************/ |
|
|
587 | |
324 | static void |
588 | static void |
325 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
589 | wlist_add (WL *head, WL elem) |
326 | { |
590 | { |
327 | elem->next = *head; |
591 | elem->next = *head; |
328 | *head = elem; |
592 | *head = elem; |
329 | } |
593 | } |
330 | |
594 | |
331 | static void |
595 | static void |
332 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
596 | wlist_del (WL *head, WL elem) |
333 | { |
597 | { |
334 | while (*head) |
598 | while (*head) |
335 | { |
599 | { |
336 | if (*head == elem) |
600 | if (*head == elem) |
337 | { |
601 | { |
… | |
… | |
342 | head = &(*head)->next; |
606 | head = &(*head)->next; |
343 | } |
607 | } |
344 | } |
608 | } |
345 | |
609 | |
346 | static void |
610 | static void |
347 | ev_start (struct ev_watcher *w, int active) |
611 | ev_start (W w, int active) |
348 | { |
612 | { |
349 | w->pending = 0; |
613 | w->pending = 0; |
350 | w->active = active; |
614 | w->active = active; |
351 | } |
615 | } |
352 | |
616 | |
353 | static void |
617 | static void |
354 | ev_stop (struct ev_watcher *w) |
618 | ev_stop (W w) |
355 | { |
619 | { |
356 | if (w->pending) |
620 | if (w->pending) |
357 | pendings [w->pending - 1].w = 0; |
621 | pendings [w->pending - 1].w = 0; |
358 | |
622 | |
359 | w->active = 0; |
623 | w->active = 0; |
360 | /* nop */ |
|
|
361 | } |
624 | } |
|
|
625 | |
|
|
626 | /*****************************************************************************/ |
362 | |
627 | |
363 | void |
628 | void |
364 | evio_start (struct ev_io *w) |
629 | evio_start (struct ev_io *w) |
365 | { |
630 | { |
366 | if (ev_is_active (w)) |
631 | if (ev_is_active (w)) |
367 | return; |
632 | return; |
368 | |
633 | |
369 | int fd = w->fd; |
634 | int fd = w->fd; |
370 | |
635 | |
371 | ev_start ((struct ev_watcher *)w, 1); |
636 | ev_start ((W)w, 1); |
372 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
637 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
373 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
638 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
374 | |
639 | |
375 | ++fdchangecnt; |
640 | ++fdchangecnt; |
376 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
641 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
377 | fdchanges [fdchangecnt - 1] = fd; |
642 | fdchanges [fdchangecnt - 1] = fd; |
378 | } |
643 | } |
… | |
… | |
381 | evio_stop (struct ev_io *w) |
646 | evio_stop (struct ev_io *w) |
382 | { |
647 | { |
383 | if (!ev_is_active (w)) |
648 | if (!ev_is_active (w)) |
384 | return; |
649 | return; |
385 | |
650 | |
386 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
651 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
387 | ev_stop ((struct ev_watcher *)w); |
652 | ev_stop ((W)w); |
388 | |
653 | |
389 | ++fdchangecnt; |
654 | ++fdchangecnt; |
390 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
655 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
391 | fdchanges [fdchangecnt - 1] = w->fd; |
656 | fdchanges [fdchangecnt - 1] = w->fd; |
392 | } |
657 | } |
393 | |
658 | |
|
|
659 | |
394 | void |
660 | void |
395 | evtimer_start (struct ev_timer *w) |
661 | evtimer_start (struct ev_timer *w) |
396 | { |
662 | { |
397 | if (ev_is_active (w)) |
663 | if (ev_is_active (w)) |
398 | return; |
664 | return; |
399 | |
665 | |
400 | fprintf (stderr, "t1 %f a %d\n", w->at, w->is_abs);//D |
|
|
401 | if (w->is_abs) |
|
|
402 | { |
|
|
403 | if (w->repeat) |
|
|
404 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
405 | } |
|
|
406 | else |
|
|
407 | w->at += ev_now; |
666 | w->at += now; |
408 | fprintf (stderr, "t2 %f a %d\n", w->at, w->is_abs);//D |
|
|
409 | |
667 | |
410 | ev_start ((struct ev_watcher *)w, ++timercnt); |
668 | ev_start ((W)w, ++timercnt); |
411 | array_needsize (timers, timermax, timercnt, ); |
669 | array_needsize (timers, timermax, timercnt, ); |
412 | timers [timercnt - 1] = w; |
670 | timers [timercnt - 1] = w; |
413 | upheap (timercnt - 1); |
671 | upheap ((WT *)timers, timercnt - 1); |
414 | } |
672 | } |
415 | |
673 | |
416 | void |
674 | void |
417 | evtimer_stop (struct ev_timer *w) |
675 | evtimer_stop (struct ev_timer *w) |
418 | { |
676 | { |
419 | if (!ev_is_active (w)) |
677 | if (!ev_is_active (w)) |
420 | return; |
678 | return; |
421 | |
679 | |
|
|
680 | if (w->active < timercnt--) |
|
|
681 | { |
422 | timers [w->active - 1] = timers [--timercnt]; |
682 | timers [w->active - 1] = timers [timercnt]; |
423 | downheap (w->active - 1); |
683 | downheap ((WT *)timers, timercnt, w->active - 1); |
424 | ev_stop ((struct ev_watcher *)w); |
684 | } |
|
|
685 | |
|
|
686 | ev_stop ((W)w); |
|
|
687 | } |
|
|
688 | |
|
|
689 | void |
|
|
690 | evperiodic_start (struct ev_periodic *w) |
|
|
691 | { |
|
|
692 | if (ev_is_active (w)) |
|
|
693 | return; |
|
|
694 | |
|
|
695 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
696 | if (w->interval) |
|
|
697 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
698 | |
|
|
699 | ev_start ((W)w, ++periodiccnt); |
|
|
700 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
701 | periodics [periodiccnt - 1] = w; |
|
|
702 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
703 | } |
|
|
704 | |
|
|
705 | void |
|
|
706 | evperiodic_stop (struct ev_periodic *w) |
|
|
707 | { |
|
|
708 | if (!ev_is_active (w)) |
|
|
709 | return; |
|
|
710 | |
|
|
711 | if (w->active < periodiccnt--) |
|
|
712 | { |
|
|
713 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
714 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
|
|
715 | } |
|
|
716 | |
|
|
717 | ev_stop ((W)w); |
425 | } |
718 | } |
426 | |
719 | |
427 | void |
720 | void |
428 | evsignal_start (struct ev_signal *w) |
721 | evsignal_start (struct ev_signal *w) |
429 | { |
722 | { |
430 | if (ev_is_active (w)) |
723 | if (ev_is_active (w)) |
431 | return; |
724 | return; |
432 | |
725 | |
433 | ev_start ((struct ev_watcher *)w, 1); |
726 | ev_start ((W)w, 1); |
434 | array_needsize (signals, signalmax, w->signum, signals_init); |
727 | array_needsize (signals, signalmax, w->signum, signals_init); |
435 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
728 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
|
|
729 | |
|
|
730 | if (!w->next) |
|
|
731 | { |
|
|
732 | struct sigaction sa; |
|
|
733 | sa.sa_handler = sighandler; |
|
|
734 | sigfillset (&sa.sa_mask); |
|
|
735 | sa.sa_flags = 0; |
|
|
736 | sigaction (w->signum, &sa, 0); |
|
|
737 | } |
436 | } |
738 | } |
437 | |
739 | |
438 | void |
740 | void |
439 | evsignal_stop (struct ev_signal *w) |
741 | evsignal_stop (struct ev_signal *w) |
440 | { |
742 | { |
441 | if (!ev_is_active (w)) |
743 | if (!ev_is_active (w)) |
442 | return; |
744 | return; |
443 | |
745 | |
444 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
746 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
445 | ev_stop ((struct ev_watcher *)w); |
747 | ev_stop ((W)w); |
446 | } |
|
|
447 | |
748 | |
|
|
749 | if (!signals [w->signum - 1].head) |
|
|
750 | signal (w->signum, SIG_DFL); |
|
|
751 | } |
|
|
752 | |
|
|
753 | void evidle_start (struct ev_idle *w) |
|
|
754 | { |
|
|
755 | if (ev_is_active (w)) |
|
|
756 | return; |
|
|
757 | |
|
|
758 | ev_start ((W)w, ++idlecnt); |
|
|
759 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
760 | idles [idlecnt - 1] = w; |
|
|
761 | } |
|
|
762 | |
|
|
763 | void evidle_stop (struct ev_idle *w) |
|
|
764 | { |
|
|
765 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
766 | ev_stop ((W)w); |
|
|
767 | } |
|
|
768 | |
|
|
769 | void evcheck_start (struct ev_check *w) |
|
|
770 | { |
|
|
771 | if (ev_is_active (w)) |
|
|
772 | return; |
|
|
773 | |
|
|
774 | ev_start ((W)w, ++checkcnt); |
|
|
775 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
776 | checks [checkcnt - 1] = w; |
|
|
777 | } |
|
|
778 | |
|
|
779 | void evcheck_stop (struct ev_check *w) |
|
|
780 | { |
|
|
781 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
782 | ev_stop ((W)w); |
|
|
783 | } |
|
|
784 | |
448 | /*****************************************************************************/ |
785 | /*****************************************************************************/ |
|
|
786 | |
449 | #if 1 |
787 | #if 1 |
450 | |
788 | |
|
|
789 | struct ev_io wio; |
|
|
790 | |
451 | static void |
791 | static void |
452 | sin_cb (struct ev_io *w, int revents) |
792 | sin_cb (struct ev_io *w, int revents) |
453 | { |
793 | { |
454 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
794 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
455 | } |
795 | } |
456 | |
796 | |
457 | static void |
797 | static void |
458 | ocb (struct ev_timer *w, int revents) |
798 | ocb (struct ev_timer *w, int revents) |
459 | { |
799 | { |
460 | fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
800 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
801 | evtimer_stop (w); |
|
|
802 | evtimer_start (w); |
|
|
803 | } |
|
|
804 | |
|
|
805 | static void |
|
|
806 | scb (struct ev_signal *w, int revents) |
|
|
807 | { |
|
|
808 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
809 | evio_stop (&wio); |
|
|
810 | evio_start (&wio); |
|
|
811 | } |
|
|
812 | |
|
|
813 | static void |
|
|
814 | gcb (struct ev_signal *w, int revents) |
|
|
815 | { |
|
|
816 | fprintf (stderr, "generic %x\n", revents); |
|
|
817 | |
461 | } |
818 | } |
462 | |
819 | |
463 | int main (void) |
820 | int main (void) |
464 | { |
821 | { |
465 | struct ev_io sin; |
|
|
466 | |
|
|
467 | ev_init (0); |
822 | ev_init (0); |
468 | |
823 | |
469 | evw_init (&sin, sin_cb, 55); |
|
|
470 | evio_set (&sin, 0, EV_READ); |
824 | evio_init (&wio, sin_cb, 0, EV_READ); |
471 | evio_start (&sin); |
825 | evio_start (&wio); |
|
|
826 | |
|
|
827 | struct ev_timer t[10000]; |
|
|
828 | |
|
|
829 | #if 0 |
|
|
830 | int i; |
|
|
831 | for (i = 0; i < 10000; ++i) |
|
|
832 | { |
|
|
833 | struct ev_timer *w = t + i; |
|
|
834 | evw_init (w, ocb, i); |
|
|
835 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
|
|
836 | evtimer_start (w); |
|
|
837 | if (drand48 () < 0.5) |
|
|
838 | evtimer_stop (w); |
|
|
839 | } |
|
|
840 | #endif |
472 | |
841 | |
473 | struct ev_timer t1; |
842 | struct ev_timer t1; |
474 | evw_init (&t1, ocb, 1); |
843 | evtimer_init (&t1, ocb, 5, 10); |
475 | evtimer_set_rel (&t1, 1, 0); |
|
|
476 | evtimer_start (&t1); |
844 | evtimer_start (&t1); |
477 | |
845 | |
478 | struct ev_timer t2; |
846 | struct ev_signal sig; |
479 | evw_init (&t2, ocb, 2); |
847 | evsignal_init (&sig, scb, SIGQUIT); |
480 | evtimer_set_abs (&t2, ev_time () + 2, 0); |
848 | evsignal_start (&sig); |
|
|
849 | |
|
|
850 | struct ev_check cw; |
|
|
851 | evcheck_init (&cw, gcb); |
|
|
852 | evcheck_start (&cw); |
|
|
853 | |
|
|
854 | struct ev_idle iw; |
|
|
855 | evidle_init (&iw, gcb); |
481 | evtimer_start (&t2); |
856 | evidle_start (&iw); |
482 | |
857 | |
483 | ev_loop (0); |
858 | ev_loop (0); |
484 | |
859 | |
485 | return 0; |
860 | return 0; |
486 | } |
861 | } |