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