|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
|
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
43 | #include <sys/time.h> |
12 | #include <time.h> |
44 | #include <time.h> |
13 | |
45 | |
14 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
15 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
… | |
… | |
29 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
30 | #endif |
62 | #endif |
31 | |
63 | |
32 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
33 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
34 | |
67 | |
35 | #include "ev.h" |
68 | #include "ev.h" |
36 | |
|
|
37 | struct ev_watcher { |
|
|
38 | EV_WATCHER (ev_watcher); |
|
|
39 | }; |
|
|
40 | |
|
|
41 | struct ev_watcher_list { |
|
|
42 | EV_WATCHER_LIST (ev_watcher_list); |
|
|
43 | }; |
|
|
44 | |
69 | |
45 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
46 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
|
|
72 | typedef struct ev_watcher_time *WT; |
47 | |
73 | |
48 | static ev_tstamp now, diff; /* monotonic clock */ |
74 | static ev_tstamp now, diff; /* monotonic clock */ |
49 | ev_tstamp ev_now; |
75 | ev_tstamp ev_now; |
50 | int ev_method; |
76 | int ev_method; |
51 | |
77 | |
… | |
… | |
87 | } |
113 | } |
88 | |
114 | |
89 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
90 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
91 | { \ |
117 | { \ |
92 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
93 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
94 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
95 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
96 | cur = newcnt; \ |
127 | cur = newcnt; \ |
97 | } |
128 | } |
98 | |
129 | |
… | |
… | |
131 | static int pendingmax, pendingcnt; |
162 | static int pendingmax, pendingcnt; |
132 | |
163 | |
133 | static void |
164 | static void |
134 | event (W w, int events) |
165 | event (W w, int events) |
135 | { |
166 | { |
|
|
167 | if (w->active) |
|
|
168 | { |
136 | w->pending = ++pendingcnt; |
169 | w->pending = ++pendingcnt; |
137 | array_needsize (pendings, pendingmax, pendingcnt, ); |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
138 | pendings [pendingcnt - 1].w = w; |
171 | pendings [pendingcnt - 1].w = w; |
139 | pendings [pendingcnt - 1].events = events; |
172 | pendings [pendingcnt - 1].events = events; |
|
|
173 | } |
140 | } |
174 | } |
141 | |
175 | |
142 | static void |
176 | static void |
143 | fd_event (int fd, int events) |
177 | fd_event (int fd, int events) |
144 | { |
178 | { |
… | |
… | |
161 | |
195 | |
162 | for (i = 0; i < eventcnt; ++i) |
196 | for (i = 0; i < eventcnt; ++i) |
163 | event (events [i], type); |
197 | event (events [i], type); |
164 | } |
198 | } |
165 | |
199 | |
166 | /*****************************************************************************/ |
200 | /* called on EBADF to verify fds */ |
|
|
201 | static void |
|
|
202 | fd_recheck (void) |
|
|
203 | { |
|
|
204 | int fd; |
167 | |
205 | |
|
|
206 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
207 | if (anfds [fd].wev) |
|
|
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
209 | while (anfds [fd].head) |
|
|
210 | { |
|
|
211 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
212 | evio_stop (anfds [fd].head); |
|
|
213 | } |
|
|
214 | } |
|
|
215 | |
|
|
216 | /*****************************************************************************/ |
|
|
217 | |
168 | static struct ev_timer **atimers; |
218 | static struct ev_timer **timers; |
169 | static int atimermax, atimercnt; |
219 | static int timermax, timercnt; |
170 | |
220 | |
171 | static struct ev_timer **rtimers; |
221 | static struct ev_periodic **periodics; |
172 | static int rtimermax, rtimercnt; |
222 | static int periodicmax, periodiccnt; |
173 | |
223 | |
174 | static void |
224 | static void |
175 | upheap (struct ev_timer **timers, int k) |
225 | upheap (WT *timers, int k) |
176 | { |
226 | { |
177 | struct ev_timer *w = timers [k]; |
227 | WT w = timers [k]; |
178 | |
228 | |
179 | while (k && timers [k >> 1]->at > w->at) |
229 | while (k && timers [k >> 1]->at > w->at) |
180 | { |
230 | { |
181 | timers [k] = timers [k >> 1]; |
231 | timers [k] = timers [k >> 1]; |
182 | timers [k]->active = k + 1; |
232 | timers [k]->active = k + 1; |
… | |
… | |
187 | timers [k]->active = k + 1; |
237 | timers [k]->active = k + 1; |
188 | |
238 | |
189 | } |
239 | } |
190 | |
240 | |
191 | static void |
241 | static void |
192 | downheap (struct ev_timer **timers, int N, int k) |
242 | downheap (WT *timers, int N, int k) |
193 | { |
243 | { |
194 | struct ev_timer *w = timers [k]; |
244 | WT w = timers [k]; |
195 | |
245 | |
196 | while (k < (N >> 1)) |
246 | while (k < (N >> 1)) |
197 | { |
247 | { |
198 | int j = k << 1; |
248 | int j = k << 1; |
199 | |
249 | |
… | |
… | |
286 | /*****************************************************************************/ |
336 | /*****************************************************************************/ |
287 | |
337 | |
288 | static struct ev_idle **idles; |
338 | static struct ev_idle **idles; |
289 | static int idlemax, idlecnt; |
339 | static int idlemax, idlecnt; |
290 | |
340 | |
|
|
341 | static struct ev_prepare **prepares; |
|
|
342 | static int preparemax, preparecnt; |
|
|
343 | |
291 | static struct ev_check **checks; |
344 | static struct ev_check **checks; |
292 | static int checkmax, checkcnt; |
345 | static int checkmax, checkcnt; |
293 | |
346 | |
294 | /*****************************************************************************/ |
347 | /*****************************************************************************/ |
295 | |
348 | |
|
|
349 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
350 | static struct ev_signal childev; |
|
|
351 | |
|
|
352 | #ifndef WCONTINUED |
|
|
353 | # define WCONTINUED 0 |
|
|
354 | #endif |
|
|
355 | |
|
|
356 | static void |
|
|
357 | childcb (struct ev_signal *sw, int revents) |
|
|
358 | { |
|
|
359 | struct ev_child *w; |
|
|
360 | int pid, status; |
|
|
361 | |
|
|
362 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
363 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
364 | if (w->pid == pid || w->pid == -1) |
|
|
365 | { |
|
|
366 | w->status = status; |
|
|
367 | event ((W)w, EV_CHILD); |
|
|
368 | } |
|
|
369 | } |
|
|
370 | |
|
|
371 | /*****************************************************************************/ |
|
|
372 | |
296 | #if HAVE_EPOLL |
373 | #if HAVE_EPOLL |
297 | # include "ev_epoll.c" |
374 | # include "ev_epoll.c" |
298 | #endif |
375 | #endif |
299 | #if HAVE_SELECT |
376 | #if HAVE_SELECT |
300 | # include "ev_select.c" |
377 | # include "ev_select.c" |
301 | #endif |
378 | #endif |
302 | |
379 | |
|
|
380 | int |
|
|
381 | ev_version_major (void) |
|
|
382 | { |
|
|
383 | return EV_VERSION_MAJOR; |
|
|
384 | } |
|
|
385 | |
|
|
386 | int |
|
|
387 | ev_version_minor (void) |
|
|
388 | { |
|
|
389 | return EV_VERSION_MINOR; |
|
|
390 | } |
|
|
391 | |
303 | int ev_init (int flags) |
392 | int ev_init (int flags) |
304 | { |
393 | { |
|
|
394 | if (!ev_method) |
|
|
395 | { |
305 | #if HAVE_MONOTONIC |
396 | #if HAVE_MONOTONIC |
306 | { |
397 | { |
307 | struct timespec ts; |
398 | struct timespec ts; |
308 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
309 | have_monotonic = 1; |
400 | have_monotonic = 1; |
310 | } |
401 | } |
311 | #endif |
402 | #endif |
312 | |
403 | |
313 | ev_now = ev_time (); |
404 | ev_now = ev_time (); |
314 | now = get_clock (); |
405 | now = get_clock (); |
315 | diff = ev_now - now; |
406 | diff = ev_now - now; |
316 | |
407 | |
317 | if (pipe (sigpipe)) |
408 | if (pipe (sigpipe)) |
318 | return 0; |
409 | return 0; |
319 | |
410 | |
320 | ev_method = EVMETHOD_NONE; |
411 | ev_method = EVMETHOD_NONE; |
321 | #if HAVE_EPOLL |
412 | #if HAVE_EPOLL |
322 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
323 | #endif |
414 | #endif |
324 | #if HAVE_SELECT |
415 | #if HAVE_SELECT |
325 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
326 | #endif |
417 | #endif |
327 | |
418 | |
328 | if (ev_method) |
419 | if (ev_method) |
329 | { |
420 | { |
330 | evw_init (&sigev, sigcb, 0); |
421 | evw_init (&sigev, sigcb); |
331 | siginit (); |
422 | siginit (); |
|
|
423 | |
|
|
424 | evsignal_init (&childev, childcb, SIGCHLD); |
|
|
425 | evsignal_start (&childev); |
|
|
426 | } |
332 | } |
427 | } |
333 | |
428 | |
334 | return ev_method; |
429 | return ev_method; |
335 | } |
430 | } |
336 | |
431 | |
337 | /*****************************************************************************/ |
432 | /*****************************************************************************/ |
338 | |
433 | |
|
|
434 | void |
339 | void ev_prefork (void) |
435 | ev_prefork (void) |
340 | { |
436 | { |
341 | /* nop */ |
437 | /* nop */ |
342 | } |
438 | } |
343 | |
439 | |
|
|
440 | void |
344 | void ev_postfork_parent (void) |
441 | ev_postfork_parent (void) |
345 | { |
442 | { |
346 | /* nop */ |
443 | /* nop */ |
347 | } |
444 | } |
348 | |
445 | |
|
|
446 | void |
349 | void ev_postfork_child (void) |
447 | ev_postfork_child (void) |
350 | { |
448 | { |
351 | #if HAVE_EPOLL |
449 | #if HAVE_EPOLL |
352 | if (ev_method == EVMETHOD_EPOLL) |
450 | if (ev_method == EVMETHOD_EPOLL) |
353 | epoll_postfork_child (); |
451 | epoll_postfork_child (); |
354 | #endif |
452 | #endif |
… | |
… | |
387 | |
485 | |
388 | fdchangecnt = 0; |
486 | fdchangecnt = 0; |
389 | } |
487 | } |
390 | |
488 | |
391 | static void |
489 | static void |
392 | call_pending () |
490 | call_pending (void) |
393 | { |
491 | { |
394 | int i; |
492 | while (pendingcnt) |
395 | |
|
|
396 | for (i = 0; i < pendingcnt; ++i) |
|
|
397 | { |
493 | { |
398 | ANPENDING *p = pendings + i; |
494 | ANPENDING *p = pendings + --pendingcnt; |
399 | |
495 | |
400 | if (p->w) |
496 | if (p->w) |
401 | { |
497 | { |
402 | p->w->pending = 0; |
498 | p->w->pending = 0; |
403 | p->w->cb (p->w, p->events); |
499 | p->w->cb (p->w, p->events); |
404 | } |
500 | } |
405 | } |
501 | } |
406 | |
|
|
407 | pendingcnt = 0; |
|
|
408 | } |
502 | } |
409 | |
503 | |
410 | static void |
504 | static void |
411 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
505 | timers_reify (void) |
412 | { |
506 | { |
413 | while (timercnt && timers [0]->at <= now) |
507 | while (timercnt && timers [0]->at <= now) |
414 | { |
508 | { |
415 | struct ev_timer *w = timers [0]; |
509 | struct ev_timer *w = timers [0]; |
|
|
510 | |
|
|
511 | event ((W)w, EV_TIMEOUT); |
416 | |
512 | |
417 | /* first reschedule or stop timer */ |
513 | /* first reschedule or stop timer */ |
418 | if (w->repeat) |
514 | if (w->repeat) |
419 | { |
515 | { |
420 | if (w->is_abs) |
|
|
421 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
422 | else |
|
|
423 | w->at = now + w->repeat; |
516 | w->at = now + w->repeat; |
424 | |
517 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
425 | assert (w->at > now); |
|
|
426 | |
|
|
427 | downheap (timers, timercnt, 0); |
518 | downheap ((WT *)timers, timercnt, 0); |
428 | } |
519 | } |
429 | else |
520 | else |
430 | { |
|
|
431 | evtimer_stop (w); /* nonrepeating: stop timer */ |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
432 | --timercnt; /* maybe pass by reference instead? */ |
522 | } |
|
|
523 | } |
|
|
524 | |
|
|
525 | static void |
|
|
526 | periodics_reify (void) |
|
|
527 | { |
|
|
528 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
529 | { |
|
|
530 | struct ev_periodic *w = periodics [0]; |
|
|
531 | |
|
|
532 | /* first reschedule or stop timer */ |
|
|
533 | if (w->interval) |
433 | } |
534 | { |
|
|
535 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
536 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
537 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
538 | } |
|
|
539 | else |
|
|
540 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
434 | |
541 | |
435 | event ((W)w, EV_TIMEOUT); |
542 | event ((W)w, EV_TIMEOUT); |
436 | } |
543 | } |
437 | } |
544 | } |
438 | |
545 | |
439 | static void |
546 | static void |
440 | time_update () |
547 | periodics_reschedule (ev_tstamp diff) |
441 | { |
548 | { |
442 | int i; |
549 | int i; |
|
|
550 | |
|
|
551 | /* adjust periodics after time jump */ |
|
|
552 | for (i = 0; i < periodiccnt; ++i) |
|
|
553 | { |
|
|
554 | struct ev_periodic *w = periodics [i]; |
|
|
555 | |
|
|
556 | if (w->interval) |
|
|
557 | { |
|
|
558 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
559 | |
|
|
560 | if (fabs (diff) >= 1e-4) |
|
|
561 | { |
|
|
562 | evperiodic_stop (w); |
|
|
563 | evperiodic_start (w); |
|
|
564 | |
|
|
565 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
566 | } |
|
|
567 | } |
|
|
568 | } |
|
|
569 | } |
|
|
570 | |
|
|
571 | static void |
|
|
572 | time_update (void) |
|
|
573 | { |
|
|
574 | int i; |
|
|
575 | |
443 | ev_now = ev_time (); |
576 | ev_now = ev_time (); |
444 | |
577 | |
445 | if (have_monotonic) |
578 | if (have_monotonic) |
446 | { |
579 | { |
447 | ev_tstamp odiff = diff; |
580 | ev_tstamp odiff = diff; |
448 | |
581 | |
449 | /* detecting time jumps is much more difficult */ |
|
|
450 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
582 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
451 | { |
583 | { |
452 | now = get_clock (); |
584 | now = get_clock (); |
453 | diff = ev_now - now; |
585 | diff = ev_now - now; |
454 | |
586 | |
455 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
587 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
456 | return; /* all is well */ |
588 | return; /* all is well */ |
457 | |
589 | |
458 | ev_now = ev_time (); |
590 | ev_now = ev_time (); |
459 | } |
591 | } |
460 | |
592 | |
461 | /* time jump detected, reschedule atimers */ |
593 | periodics_reschedule (diff - odiff); |
462 | for (i = 0; i < atimercnt; ++i) |
594 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
463 | { |
|
|
464 | struct ev_timer *w = atimers [i]; |
|
|
465 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
466 | } |
|
|
467 | } |
595 | } |
468 | else |
596 | else |
469 | { |
597 | { |
470 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
598 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
471 | /* time jump detected, adjust rtimers */ |
599 | { |
|
|
600 | periodics_reschedule (ev_now - now); |
|
|
601 | |
|
|
602 | /* adjust timers. this is easy, as the offset is the same for all */ |
472 | for (i = 0; i < rtimercnt; ++i) |
603 | for (i = 0; i < timercnt; ++i) |
473 | rtimers [i]->at += ev_now - now; |
604 | timers [i]->at += diff; |
|
|
605 | } |
474 | |
606 | |
475 | now = ev_now; |
607 | now = ev_now; |
476 | } |
608 | } |
477 | } |
609 | } |
478 | |
610 | |
479 | int ev_loop_done; |
611 | int ev_loop_done; |
480 | |
612 | |
481 | void ev_loop (int flags) |
613 | void ev_loop (int flags) |
482 | { |
614 | { |
483 | double block; |
615 | double block; |
484 | ev_loop_done = flags & EVLOOP_ONESHOT; |
616 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
485 | |
|
|
486 | if (checkcnt) |
|
|
487 | { |
|
|
488 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
489 | call_pending (); |
|
|
490 | } |
|
|
491 | |
617 | |
492 | do |
618 | do |
493 | { |
619 | { |
|
|
620 | /* queue check watchers (and execute them) */ |
|
|
621 | if (preparecnt) |
|
|
622 | { |
|
|
623 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
624 | call_pending (); |
|
|
625 | } |
|
|
626 | |
494 | /* update fd-related kernel structures */ |
627 | /* update fd-related kernel structures */ |
495 | fd_reify (); |
628 | fd_reify (); |
496 | |
629 | |
497 | /* calculate blocking time */ |
630 | /* calculate blocking time */ |
|
|
631 | |
|
|
632 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
633 | always have timers, we just calculate it always */ |
|
|
634 | ev_now = ev_time (); |
|
|
635 | |
498 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
636 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
499 | block = 0.; |
637 | block = 0.; |
500 | else |
638 | else |
501 | { |
639 | { |
502 | block = MAX_BLOCKTIME; |
640 | block = MAX_BLOCKTIME; |
503 | |
641 | |
504 | if (rtimercnt) |
642 | if (timercnt) |
505 | { |
643 | { |
506 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
644 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
507 | if (block > to) block = to; |
645 | if (block > to) block = to; |
508 | } |
646 | } |
509 | |
647 | |
510 | if (atimercnt) |
648 | if (periodiccnt) |
511 | { |
649 | { |
512 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
650 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
513 | if (block > to) block = to; |
651 | if (block > to) block = to; |
514 | } |
652 | } |
515 | |
653 | |
516 | if (block < 0.) block = 0.; |
654 | if (block < 0.) block = 0.; |
517 | } |
655 | } |
… | |
… | |
520 | |
658 | |
521 | /* update ev_now, do magic */ |
659 | /* update ev_now, do magic */ |
522 | time_update (); |
660 | time_update (); |
523 | |
661 | |
524 | /* queue pending timers and reschedule them */ |
662 | /* queue pending timers and reschedule them */ |
525 | /* absolute timers first */ |
663 | timers_reify (); /* relative timers called last */ |
526 | timers_reify (atimers, atimercnt, ev_now); |
664 | periodics_reify (); /* absolute timers called first */ |
527 | /* relative timers second */ |
|
|
528 | timers_reify (rtimers, rtimercnt, now); |
|
|
529 | |
665 | |
530 | /* queue idle watchers unless io or timers are pending */ |
666 | /* queue idle watchers unless io or timers are pending */ |
531 | if (!pendingcnt) |
667 | if (!pendingcnt) |
532 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
668 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
533 | |
669 | |
534 | /* queue check and possibly idle watchers */ |
670 | /* queue check watchers, to be executed first */ |
|
|
671 | if (checkcnt) |
535 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
672 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
536 | |
673 | |
537 | call_pending (); |
674 | call_pending (); |
538 | } |
675 | } |
539 | while (!ev_loop_done); |
676 | while (!ev_loop_done); |
|
|
677 | |
|
|
678 | if (ev_loop_done != 2) |
|
|
679 | ev_loop_done = 0; |
540 | } |
680 | } |
541 | |
681 | |
542 | /*****************************************************************************/ |
682 | /*****************************************************************************/ |
543 | |
683 | |
544 | static void |
684 | static void |
… | |
… | |
562 | head = &(*head)->next; |
702 | head = &(*head)->next; |
563 | } |
703 | } |
564 | } |
704 | } |
565 | |
705 | |
566 | static void |
706 | static void |
|
|
707 | ev_clear (W w) |
|
|
708 | { |
|
|
709 | if (w->pending) |
|
|
710 | { |
|
|
711 | pendings [w->pending - 1].w = 0; |
|
|
712 | w->pending = 0; |
|
|
713 | } |
|
|
714 | } |
|
|
715 | |
|
|
716 | static void |
567 | ev_start (W w, int active) |
717 | ev_start (W w, int active) |
568 | { |
718 | { |
569 | w->pending = 0; |
|
|
570 | w->active = active; |
719 | w->active = active; |
571 | } |
720 | } |
572 | |
721 | |
573 | static void |
722 | static void |
574 | ev_stop (W w) |
723 | ev_stop (W w) |
575 | { |
724 | { |
576 | if (w->pending) |
|
|
577 | pendings [w->pending - 1].w = 0; |
|
|
578 | |
|
|
579 | w->active = 0; |
725 | w->active = 0; |
580 | } |
726 | } |
581 | |
727 | |
582 | /*****************************************************************************/ |
728 | /*****************************************************************************/ |
583 | |
729 | |
… | |
… | |
599 | } |
745 | } |
600 | |
746 | |
601 | void |
747 | void |
602 | evio_stop (struct ev_io *w) |
748 | evio_stop (struct ev_io *w) |
603 | { |
749 | { |
|
|
750 | ev_clear ((W)w); |
604 | if (!ev_is_active (w)) |
751 | if (!ev_is_active (w)) |
605 | return; |
752 | return; |
606 | |
753 | |
607 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
754 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
608 | ev_stop ((W)w); |
755 | ev_stop ((W)w); |
… | |
… | |
616 | evtimer_start (struct ev_timer *w) |
763 | evtimer_start (struct ev_timer *w) |
617 | { |
764 | { |
618 | if (ev_is_active (w)) |
765 | if (ev_is_active (w)) |
619 | return; |
766 | return; |
620 | |
767 | |
621 | if (w->is_abs) |
768 | w->at += now; |
|
|
769 | |
|
|
770 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
771 | |
|
|
772 | ev_start ((W)w, ++timercnt); |
|
|
773 | array_needsize (timers, timermax, timercnt, ); |
|
|
774 | timers [timercnt - 1] = w; |
|
|
775 | upheap ((WT *)timers, timercnt - 1); |
|
|
776 | } |
|
|
777 | |
|
|
778 | void |
|
|
779 | evtimer_stop (struct ev_timer *w) |
|
|
780 | { |
|
|
781 | ev_clear ((W)w); |
|
|
782 | if (!ev_is_active (w)) |
|
|
783 | return; |
|
|
784 | |
|
|
785 | if (w->active < timercnt--) |
|
|
786 | { |
|
|
787 | timers [w->active - 1] = timers [timercnt]; |
|
|
788 | downheap ((WT *)timers, timercnt, w->active - 1); |
622 | { |
789 | } |
623 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
790 | |
|
|
791 | w->at = w->repeat; |
|
|
792 | |
|
|
793 | ev_stop ((W)w); |
|
|
794 | } |
|
|
795 | |
|
|
796 | void |
|
|
797 | evtimer_again (struct ev_timer *w) |
|
|
798 | { |
|
|
799 | if (ev_is_active (w)) |
|
|
800 | { |
624 | if (w->repeat) |
801 | if (w->repeat) |
|
|
802 | { |
|
|
803 | w->at = now + w->repeat; |
|
|
804 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
805 | } |
|
|
806 | else |
|
|
807 | evtimer_stop (w); |
|
|
808 | } |
|
|
809 | else if (w->repeat) |
|
|
810 | evtimer_start (w); |
|
|
811 | } |
|
|
812 | |
|
|
813 | void |
|
|
814 | evperiodic_start (struct ev_periodic *w) |
|
|
815 | { |
|
|
816 | if (ev_is_active (w)) |
|
|
817 | return; |
|
|
818 | |
|
|
819 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
820 | |
|
|
821 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
822 | if (w->interval) |
625 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
823 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
626 | |
824 | |
627 | ev_start ((W)w, ++atimercnt); |
825 | ev_start ((W)w, ++periodiccnt); |
628 | array_needsize (atimers, atimermax, atimercnt, ); |
826 | array_needsize (periodics, periodicmax, periodiccnt, ); |
629 | atimers [atimercnt - 1] = w; |
827 | periodics [periodiccnt - 1] = w; |
630 | upheap (atimers, atimercnt - 1); |
828 | upheap ((WT *)periodics, periodiccnt - 1); |
631 | } |
|
|
632 | else |
|
|
633 | { |
|
|
634 | w->at += now; |
|
|
635 | |
|
|
636 | ev_start ((W)w, ++rtimercnt); |
|
|
637 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
|
|
638 | rtimers [rtimercnt - 1] = w; |
|
|
639 | upheap (rtimers, rtimercnt - 1); |
|
|
640 | } |
|
|
641 | |
|
|
642 | } |
829 | } |
643 | |
830 | |
644 | void |
831 | void |
645 | evtimer_stop (struct ev_timer *w) |
832 | evperiodic_stop (struct ev_periodic *w) |
646 | { |
833 | { |
|
|
834 | ev_clear ((W)w); |
647 | if (!ev_is_active (w)) |
835 | if (!ev_is_active (w)) |
648 | return; |
836 | return; |
649 | |
837 | |
650 | if (w->is_abs) |
|
|
651 | { |
|
|
652 | if (w->active < atimercnt--) |
838 | if (w->active < periodiccnt--) |
653 | { |
|
|
654 | atimers [w->active - 1] = atimers [atimercnt]; |
|
|
655 | downheap (atimers, atimercnt, w->active - 1); |
|
|
656 | } |
|
|
657 | } |
839 | { |
658 | else |
840 | periodics [w->active - 1] = periodics [periodiccnt]; |
659 | { |
841 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
660 | if (w->active < rtimercnt--) |
|
|
661 | { |
|
|
662 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
|
|
663 | downheap (rtimers, rtimercnt, w->active - 1); |
|
|
664 | } |
|
|
665 | } |
842 | } |
666 | |
843 | |
667 | ev_stop ((W)w); |
844 | ev_stop ((W)w); |
668 | } |
845 | } |
669 | |
846 | |
… | |
… | |
688 | } |
865 | } |
689 | |
866 | |
690 | void |
867 | void |
691 | evsignal_stop (struct ev_signal *w) |
868 | evsignal_stop (struct ev_signal *w) |
692 | { |
869 | { |
|
|
870 | ev_clear ((W)w); |
693 | if (!ev_is_active (w)) |
871 | if (!ev_is_active (w)) |
694 | return; |
872 | return; |
695 | |
873 | |
696 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
874 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
697 | ev_stop ((W)w); |
875 | ev_stop ((W)w); |
… | |
… | |
710 | idles [idlecnt - 1] = w; |
888 | idles [idlecnt - 1] = w; |
711 | } |
889 | } |
712 | |
890 | |
713 | void evidle_stop (struct ev_idle *w) |
891 | void evidle_stop (struct ev_idle *w) |
714 | { |
892 | { |
|
|
893 | ev_clear ((W)w); |
|
|
894 | if (ev_is_active (w)) |
|
|
895 | return; |
|
|
896 | |
715 | idles [w->active - 1] = idles [--idlecnt]; |
897 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
898 | ev_stop ((W)w); |
|
|
899 | } |
|
|
900 | |
|
|
901 | void evprepare_start (struct ev_prepare *w) |
|
|
902 | { |
|
|
903 | if (ev_is_active (w)) |
|
|
904 | return; |
|
|
905 | |
|
|
906 | ev_start ((W)w, ++preparecnt); |
|
|
907 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
908 | prepares [preparecnt - 1] = w; |
|
|
909 | } |
|
|
910 | |
|
|
911 | void evprepare_stop (struct ev_prepare *w) |
|
|
912 | { |
|
|
913 | ev_clear ((W)w); |
|
|
914 | if (ev_is_active (w)) |
|
|
915 | return; |
|
|
916 | |
|
|
917 | prepares [w->active - 1] = prepares [--preparecnt]; |
716 | ev_stop ((W)w); |
918 | ev_stop ((W)w); |
717 | } |
919 | } |
718 | |
920 | |
719 | void evcheck_start (struct ev_check *w) |
921 | void evcheck_start (struct ev_check *w) |
720 | { |
922 | { |
… | |
… | |
726 | checks [checkcnt - 1] = w; |
928 | checks [checkcnt - 1] = w; |
727 | } |
929 | } |
728 | |
930 | |
729 | void evcheck_stop (struct ev_check *w) |
931 | void evcheck_stop (struct ev_check *w) |
730 | { |
932 | { |
|
|
933 | ev_clear ((W)w); |
|
|
934 | if (ev_is_active (w)) |
|
|
935 | return; |
|
|
936 | |
731 | checks [w->active - 1] = checks [--checkcnt]; |
937 | checks [w->active - 1] = checks [--checkcnt]; |
732 | ev_stop ((W)w); |
938 | ev_stop ((W)w); |
733 | } |
939 | } |
734 | |
940 | |
|
|
941 | void evchild_start (struct ev_child *w) |
|
|
942 | { |
|
|
943 | if (ev_is_active (w)) |
|
|
944 | return; |
|
|
945 | |
|
|
946 | ev_start ((W)w, 1); |
|
|
947 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
948 | } |
|
|
949 | |
|
|
950 | void evchild_stop (struct ev_child *w) |
|
|
951 | { |
|
|
952 | ev_clear ((W)w); |
|
|
953 | if (ev_is_active (w)) |
|
|
954 | return; |
|
|
955 | |
|
|
956 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
957 | ev_stop ((W)w); |
|
|
958 | } |
|
|
959 | |
|
|
960 | /*****************************************************************************/ |
|
|
961 | |
|
|
962 | struct ev_once |
|
|
963 | { |
|
|
964 | struct ev_io io; |
|
|
965 | struct ev_timer to; |
|
|
966 | void (*cb)(int revents, void *arg); |
|
|
967 | void *arg; |
|
|
968 | }; |
|
|
969 | |
|
|
970 | static void |
|
|
971 | once_cb (struct ev_once *once, int revents) |
|
|
972 | { |
|
|
973 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
974 | void *arg = once->arg; |
|
|
975 | |
|
|
976 | evio_stop (&once->io); |
|
|
977 | evtimer_stop (&once->to); |
|
|
978 | free (once); |
|
|
979 | |
|
|
980 | cb (revents, arg); |
|
|
981 | } |
|
|
982 | |
|
|
983 | static void |
|
|
984 | once_cb_io (struct ev_io *w, int revents) |
|
|
985 | { |
|
|
986 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
987 | } |
|
|
988 | |
|
|
989 | static void |
|
|
990 | once_cb_to (struct ev_timer *w, int revents) |
|
|
991 | { |
|
|
992 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
993 | } |
|
|
994 | |
|
|
995 | void |
|
|
996 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
997 | { |
|
|
998 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
999 | |
|
|
1000 | if (!once) |
|
|
1001 | cb (EV_ERROR, arg); |
|
|
1002 | else |
|
|
1003 | { |
|
|
1004 | once->cb = cb; |
|
|
1005 | once->arg = arg; |
|
|
1006 | |
|
|
1007 | evw_init (&once->io, once_cb_io); |
|
|
1008 | |
|
|
1009 | if (fd >= 0) |
|
|
1010 | { |
|
|
1011 | evio_set (&once->io, fd, events); |
|
|
1012 | evio_start (&once->io); |
|
|
1013 | } |
|
|
1014 | |
|
|
1015 | evw_init (&once->to, once_cb_to); |
|
|
1016 | |
|
|
1017 | if (timeout >= 0.) |
|
|
1018 | { |
|
|
1019 | evtimer_set (&once->to, timeout, 0.); |
|
|
1020 | evtimer_start (&once->to); |
|
|
1021 | } |
|
|
1022 | } |
|
|
1023 | } |
|
|
1024 | |
735 | /*****************************************************************************/ |
1025 | /*****************************************************************************/ |
736 | |
1026 | |
737 | #if 0 |
1027 | #if 0 |
|
|
1028 | |
|
|
1029 | struct ev_io wio; |
738 | |
1030 | |
739 | static void |
1031 | static void |
740 | sin_cb (struct ev_io *w, int revents) |
1032 | sin_cb (struct ev_io *w, int revents) |
741 | { |
1033 | { |
742 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
1034 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
… | |
… | |
752 | |
1044 | |
753 | static void |
1045 | static void |
754 | scb (struct ev_signal *w, int revents) |
1046 | scb (struct ev_signal *w, int revents) |
755 | { |
1047 | { |
756 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1048 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
1049 | evio_stop (&wio); |
|
|
1050 | evio_start (&wio); |
757 | } |
1051 | } |
758 | |
1052 | |
759 | static void |
1053 | static void |
760 | gcb (struct ev_signal *w, int revents) |
1054 | gcb (struct ev_signal *w, int revents) |
761 | { |
1055 | { |
762 | fprintf (stderr, "generic %x\n", revents); |
1056 | fprintf (stderr, "generic %x\n", revents); |
|
|
1057 | |
763 | } |
1058 | } |
764 | |
1059 | |
765 | int main (void) |
1060 | int main (void) |
766 | { |
1061 | { |
767 | struct ev_io sin; |
|
|
768 | |
|
|
769 | ev_init (0); |
1062 | ev_init (0); |
770 | |
1063 | |
771 | evw_init (&sin, sin_cb, 55); |
|
|
772 | evio_set (&sin, 0, EV_READ); |
1064 | evio_init (&wio, sin_cb, 0, EV_READ); |
773 | evio_start (&sin); |
1065 | evio_start (&wio); |
774 | |
1066 | |
775 | struct ev_timer t[10000]; |
1067 | struct ev_timer t[10000]; |
776 | |
1068 | |
777 | #if 0 |
1069 | #if 0 |
778 | int i; |
1070 | int i; |
779 | for (i = 0; i < 10000; ++i) |
1071 | for (i = 0; i < 10000; ++i) |
780 | { |
1072 | { |
781 | struct ev_timer *w = t + i; |
1073 | struct ev_timer *w = t + i; |
782 | evw_init (w, ocb, i); |
1074 | evw_init (w, ocb, i); |
783 | evtimer_set_abs (w, drand48 (), 0.99775533); |
1075 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
784 | evtimer_start (w); |
1076 | evtimer_start (w); |
785 | if (drand48 () < 0.5) |
1077 | if (drand48 () < 0.5) |
786 | evtimer_stop (w); |
1078 | evtimer_stop (w); |
787 | } |
1079 | } |
788 | #endif |
1080 | #endif |
789 | |
1081 | |
790 | struct ev_timer t1; |
1082 | struct ev_timer t1; |
791 | evw_init (&t1, ocb, 0); |
1083 | evtimer_init (&t1, ocb, 5, 10); |
792 | evtimer_set_abs (&t1, 5, 10); |
|
|
793 | evtimer_start (&t1); |
1084 | evtimer_start (&t1); |
794 | |
1085 | |
795 | struct ev_signal sig; |
1086 | struct ev_signal sig; |
796 | evw_init (&sig, scb, 65535); |
|
|
797 | evsignal_set (&sig, SIGQUIT); |
1087 | evsignal_init (&sig, scb, SIGQUIT); |
798 | evsignal_start (&sig); |
1088 | evsignal_start (&sig); |
799 | |
1089 | |
800 | struct ev_check cw; |
1090 | struct ev_check cw; |
801 | evw_init (&cw, gcb, 0); |
1091 | evcheck_init (&cw, gcb); |
802 | evcheck_start (&cw); |
1092 | evcheck_start (&cw); |
803 | |
1093 | |
804 | struct ev_idle iw; |
1094 | struct ev_idle iw; |
805 | evw_init (&iw, gcb, 0); |
1095 | evidle_init (&iw, gcb); |
806 | evidle_start (&iw); |
1096 | evidle_start (&iw); |
807 | |
1097 | |
808 | ev_loop (0); |
1098 | ev_loop (0); |
809 | |
1099 | |
810 | return 0; |
1100 | return 0; |