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