… | |
… | |
42 | #include <stdio.h> |
42 | #include <stdio.h> |
43 | |
43 | |
44 | #include <assert.h> |
44 | #include <assert.h> |
45 | #include <errno.h> |
45 | #include <errno.h> |
46 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
47 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
48 | #include <sys/time.h> |
50 | #include <sys/time.h> |
49 | #include <time.h> |
51 | #include <time.h> |
50 | |
52 | |
|
|
53 | /**/ |
|
|
54 | |
51 | #ifndef EV_USE_MONOTONIC |
55 | #ifndef EV_USE_MONOTONIC |
52 | # define EV_USE_MONOTONIC 1 |
56 | # define EV_USE_MONOTONIC 1 |
53 | #endif |
57 | #endif |
|
|
58 | |
|
|
59 | #ifndef EV_USE_SELECT |
|
|
60 | # define EV_USE_SELECT 1 |
|
|
61 | #endif |
|
|
62 | |
|
|
63 | #ifndef EV_USE_POLL |
|
|
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
65 | #endif |
|
|
66 | |
|
|
67 | #ifndef EV_USE_EPOLL |
|
|
68 | # define EV_USE_EPOLL 0 |
|
|
69 | #endif |
|
|
70 | |
|
|
71 | #ifndef EV_USE_KQUEUE |
|
|
72 | # define EV_USE_KQUEUE 0 |
|
|
73 | #endif |
|
|
74 | |
|
|
75 | #ifndef EV_USE_REALTIME |
|
|
76 | # define EV_USE_REALTIME 1 |
|
|
77 | #endif |
|
|
78 | |
|
|
79 | /**/ |
54 | |
80 | |
55 | #ifndef CLOCK_MONOTONIC |
81 | #ifndef CLOCK_MONOTONIC |
56 | # undef EV_USE_MONOTONIC |
82 | # undef EV_USE_MONOTONIC |
57 | # define EV_USE_MONOTONIC 0 |
83 | # define EV_USE_MONOTONIC 0 |
58 | #endif |
84 | #endif |
59 | |
85 | |
60 | #ifndef EV_USE_SELECT |
|
|
61 | # define EV_USE_SELECT 1 |
|
|
62 | #endif |
|
|
63 | |
|
|
64 | #ifndef EV_USE_EPOLL |
|
|
65 | # define EV_USE_EPOLL 0 |
|
|
66 | #endif |
|
|
67 | |
|
|
68 | #ifndef CLOCK_REALTIME |
86 | #ifndef CLOCK_REALTIME |
|
|
87 | # undef EV_USE_REALTIME |
69 | # define EV_USE_REALTIME 0 |
88 | # define EV_USE_REALTIME 0 |
70 | #endif |
89 | #endif |
71 | #ifndef EV_USE_REALTIME |
90 | |
72 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
91 | /**/ |
73 | #endif |
|
|
74 | |
92 | |
75 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
76 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
77 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
78 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
79 | |
97 | |
80 | #include "ev.h" |
98 | #include "ev.h" |
|
|
99 | |
|
|
100 | #if __GNUC__ >= 3 |
|
|
101 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
102 | # define inline inline |
|
|
103 | #else |
|
|
104 | # define expect(expr,value) (expr) |
|
|
105 | # define inline static |
|
|
106 | #endif |
|
|
107 | |
|
|
108 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
109 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
110 | |
|
|
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
81 | |
113 | |
82 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
83 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
84 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
85 | |
117 | |
86 | static ev_tstamp now, diff; /* monotonic clock */ |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
87 | ev_tstamp ev_now; |
119 | ev_tstamp ev_now; |
88 | int ev_method; |
120 | int ev_method; |
89 | |
121 | |
90 | static int have_monotonic; /* runtime */ |
122 | static int have_monotonic; /* runtime */ |
91 | |
123 | |
… | |
… | |
111 | |
143 | |
112 | static ev_tstamp |
144 | static ev_tstamp |
113 | get_clock (void) |
145 | get_clock (void) |
114 | { |
146 | { |
115 | #if EV_USE_MONOTONIC |
147 | #if EV_USE_MONOTONIC |
116 | if (have_monotonic) |
148 | if (expect_true (have_monotonic)) |
117 | { |
149 | { |
118 | struct timespec ts; |
150 | struct timespec ts; |
119 | clock_gettime (CLOCK_MONOTONIC, &ts); |
151 | clock_gettime (CLOCK_MONOTONIC, &ts); |
120 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
121 | } |
153 | } |
… | |
… | |
125 | } |
157 | } |
126 | |
158 | |
127 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
128 | |
160 | |
129 | #define array_needsize(base,cur,cnt,init) \ |
161 | #define array_needsize(base,cur,cnt,init) \ |
130 | if ((cnt) > cur) \ |
162 | if (expect_false ((cnt) > cur)) \ |
131 | { \ |
163 | { \ |
132 | int newcnt = cur; \ |
164 | int newcnt = cur; \ |
133 | do \ |
165 | do \ |
134 | { \ |
166 | { \ |
135 | newcnt = array_roundsize (base, newcnt << 1); \ |
167 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
170 | { |
202 | { |
171 | W w; |
203 | W w; |
172 | int events; |
204 | int events; |
173 | } ANPENDING; |
205 | } ANPENDING; |
174 | |
206 | |
175 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
176 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
177 | |
209 | |
178 | static void |
210 | static void |
179 | event (W w, int events) |
211 | event (W w, int events) |
180 | { |
212 | { |
181 | if (w->pending) |
213 | if (w->pending) |
182 | { |
214 | { |
183 | pendings [w->pending - 1].events |= events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
184 | return; |
216 | return; |
185 | } |
217 | } |
186 | |
218 | |
187 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
188 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
189 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
190 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
191 | } |
223 | } |
192 | |
224 | |
193 | static void |
225 | static void |
194 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
195 | { |
227 | { |
… | |
… | |
258 | ++fdchangecnt; |
290 | ++fdchangecnt; |
259 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
260 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
261 | } |
293 | } |
262 | |
294 | |
|
|
295 | static void |
|
|
296 | fd_kill (int fd) |
|
|
297 | { |
|
|
298 | struct ev_io *w; |
|
|
299 | |
|
|
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
|
|
302 | { |
|
|
303 | ev_io_stop (w); |
|
|
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
263 | /* called on EBADF to verify fds */ |
308 | /* called on EBADF to verify fds */ |
264 | static void |
309 | static void |
265 | fd_recheck (void) |
310 | fd_ebadf (void) |
266 | { |
311 | { |
267 | int fd; |
312 | int fd; |
268 | |
313 | |
269 | for (fd = 0; fd < anfdmax; ++fd) |
314 | for (fd = 0; fd < anfdmax; ++fd) |
270 | if (anfds [fd].events) |
315 | if (anfds [fd].events) |
271 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
272 | while (anfds [fd].head) |
317 | fd_kill (fd); |
|
|
318 | } |
|
|
319 | |
|
|
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
321 | static void |
|
|
322 | fd_enomem (void) |
|
|
323 | { |
|
|
324 | int fd = anfdmax; |
|
|
325 | |
|
|
326 | while (fd--) |
|
|
327 | if (anfds [fd].events) |
273 | { |
328 | { |
274 | ev_io_stop (anfds [fd].head); |
329 | close (fd); |
275 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
330 | fd_kill (fd); |
|
|
331 | return; |
276 | } |
332 | } |
277 | } |
333 | } |
278 | |
334 | |
279 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
280 | |
336 | |
281 | static struct ev_timer **timers; |
337 | static struct ev_timer **timers; |
… | |
… | |
357 | { |
413 | { |
358 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
359 | |
415 | |
360 | if (!gotsig) |
416 | if (!gotsig) |
361 | { |
417 | { |
|
|
418 | int old_errno = errno; |
362 | gotsig = 1; |
419 | gotsig = 1; |
363 | write (sigpipe [1], &signum, 1); |
420 | write (sigpipe [1], &signum, 1); |
|
|
421 | errno = old_errno; |
364 | } |
422 | } |
365 | } |
423 | } |
366 | |
424 | |
367 | static void |
425 | static void |
368 | sigcb (struct ev_io *iow, int revents) |
426 | sigcb (struct ev_io *iow, int revents) |
… | |
… | |
384 | } |
442 | } |
385 | |
443 | |
386 | static void |
444 | static void |
387 | siginit (void) |
445 | siginit (void) |
388 | { |
446 | { |
|
|
447 | #ifndef WIN32 |
389 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
390 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
391 | |
450 | |
392 | /* rather than sort out wether we really need nb, set it */ |
451 | /* rather than sort out wether we really need nb, set it */ |
393 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
394 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
454 | #endif |
395 | |
455 | |
396 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
456 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
397 | ev_io_start (&sigev); |
457 | ev_io_start (&sigev); |
398 | } |
458 | } |
399 | |
459 | |
… | |
… | |
411 | /*****************************************************************************/ |
471 | /*****************************************************************************/ |
412 | |
472 | |
413 | static struct ev_child *childs [PID_HASHSIZE]; |
473 | static struct ev_child *childs [PID_HASHSIZE]; |
414 | static struct ev_signal childev; |
474 | static struct ev_signal childev; |
415 | |
475 | |
|
|
476 | #ifndef WIN32 |
|
|
477 | |
416 | #ifndef WCONTINUED |
478 | #ifndef WCONTINUED |
417 | # define WCONTINUED 0 |
479 | # define WCONTINUED 0 |
418 | #endif |
480 | #endif |
419 | |
481 | |
420 | static void |
482 | static void |
|
|
483 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
484 | { |
|
|
485 | struct ev_child *w; |
|
|
486 | |
|
|
487 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
488 | if (w->pid == pid || !w->pid) |
|
|
489 | { |
|
|
490 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
491 | w->rpid = pid; |
|
|
492 | w->rstatus = status; |
|
|
493 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
494 | event ((W)w, EV_CHILD); |
|
|
495 | } |
|
|
496 | } |
|
|
497 | |
|
|
498 | static void |
421 | childcb (struct ev_signal *sw, int revents) |
499 | childcb (struct ev_signal *sw, int revents) |
422 | { |
500 | { |
423 | struct ev_child *w; |
|
|
424 | int pid, status; |
501 | int pid, status; |
425 | |
502 | |
|
|
503 | printf ("chld %x\n", revents);//D |
426 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
504 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
427 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
505 | { |
428 | if (w->pid == pid || w->pid == -1) |
506 | /* make sure we are called again until all childs have been reaped */ |
429 | { |
|
|
430 | w->status = status; |
|
|
431 | event ((W)w, EV_CHILD); |
507 | event ((W)sw, EV_SIGNAL); |
432 | } |
508 | |
|
|
509 | child_reap (sw, pid, pid, status); |
|
|
510 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
511 | } |
433 | } |
512 | } |
|
|
513 | |
|
|
514 | #endif |
434 | |
515 | |
435 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
436 | |
517 | |
|
|
518 | #if EV_USE_KQUEUE |
|
|
519 | # include "ev_kqueue.c" |
|
|
520 | #endif |
437 | #if EV_USE_EPOLL |
521 | #if EV_USE_EPOLL |
438 | # include "ev_epoll.c" |
522 | # include "ev_epoll.c" |
439 | #endif |
523 | #endif |
|
|
524 | #if EV_USE_POLL |
|
|
525 | # include "ev_poll.c" |
|
|
526 | #endif |
440 | #if EV_USE_SELECT |
527 | #if EV_USE_SELECT |
441 | # include "ev_select.c" |
528 | # include "ev_select.c" |
442 | #endif |
529 | #endif |
443 | |
530 | |
444 | int |
531 | int |
… | |
… | |
451 | ev_version_minor (void) |
538 | ev_version_minor (void) |
452 | { |
539 | { |
453 | return EV_VERSION_MINOR; |
540 | return EV_VERSION_MINOR; |
454 | } |
541 | } |
455 | |
542 | |
|
|
543 | /* return true if we are running with elevated privileges and should ignore env variables */ |
|
|
544 | static int |
|
|
545 | enable_secure () |
|
|
546 | { |
|
|
547 | #ifdef WIN32 |
|
|
548 | return 0; |
|
|
549 | #else |
|
|
550 | return getuid () != geteuid () |
|
|
551 | || getgid () != getegid (); |
|
|
552 | #endif |
|
|
553 | } |
|
|
554 | |
456 | int ev_init (int flags) |
555 | int ev_init (int methods) |
457 | { |
556 | { |
458 | if (!ev_method) |
557 | if (!ev_method) |
459 | { |
558 | { |
460 | #if EV_USE_MONOTONIC |
559 | #if EV_USE_MONOTONIC |
461 | { |
560 | { |
… | |
… | |
463 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
562 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
464 | have_monotonic = 1; |
563 | have_monotonic = 1; |
465 | } |
564 | } |
466 | #endif |
565 | #endif |
467 | |
566 | |
468 | ev_now = ev_time (); |
567 | ev_now = ev_time (); |
469 | now = get_clock (); |
568 | now = get_clock (); |
|
|
569 | now_floor = now; |
470 | diff = ev_now - now; |
570 | diff = ev_now - now; |
471 | |
571 | |
472 | if (pipe (sigpipe)) |
572 | if (pipe (sigpipe)) |
473 | return 0; |
573 | return 0; |
474 | |
574 | |
|
|
575 | if (methods == EVMETHOD_AUTO) |
|
|
576 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
577 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
578 | else |
475 | ev_method = EVMETHOD_NONE; |
579 | methods = EVMETHOD_ANY; |
|
|
580 | |
|
|
581 | ev_method = 0; |
|
|
582 | #if EV_USE_KQUEUE |
|
|
583 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
584 | #endif |
476 | #if EV_USE_EPOLL |
585 | #if EV_USE_EPOLL |
477 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
586 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
587 | #endif |
|
|
588 | #if EV_USE_POLL |
|
|
589 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
478 | #endif |
590 | #endif |
479 | #if EV_USE_SELECT |
591 | #if EV_USE_SELECT |
480 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
592 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
481 | #endif |
593 | #endif |
482 | |
594 | |
483 | if (ev_method) |
595 | if (ev_method) |
484 | { |
596 | { |
485 | ev_watcher_init (&sigev, sigcb); |
597 | ev_watcher_init (&sigev, sigcb); |
|
|
598 | ev_set_priority (&sigev, EV_MAXPRI); |
486 | siginit (); |
599 | siginit (); |
487 | |
600 | |
|
|
601 | #ifndef WIN32 |
488 | ev_signal_init (&childev, childcb, SIGCHLD); |
602 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
603 | ev_set_priority (&childev, EV_MAXPRI); |
489 | ev_signal_start (&childev); |
604 | ev_signal_start (&childev); |
|
|
605 | #endif |
490 | } |
606 | } |
491 | } |
607 | } |
492 | |
608 | |
493 | return ev_method; |
609 | return ev_method; |
494 | } |
610 | } |
… | |
… | |
525 | /*****************************************************************************/ |
641 | /*****************************************************************************/ |
526 | |
642 | |
527 | static void |
643 | static void |
528 | call_pending (void) |
644 | call_pending (void) |
529 | { |
645 | { |
|
|
646 | int pri; |
|
|
647 | |
|
|
648 | for (pri = NUMPRI; pri--; ) |
530 | while (pendingcnt) |
649 | while (pendingcnt [pri]) |
531 | { |
650 | { |
532 | ANPENDING *p = pendings + --pendingcnt; |
651 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
533 | |
652 | |
534 | if (p->w) |
653 | if (p->w) |
535 | { |
654 | { |
536 | p->w->pending = 0; |
655 | p->w->pending = 0; |
537 | p->w->cb (p->w, p->events); |
656 | p->w->cb (p->w, p->events); |
538 | } |
657 | } |
539 | } |
658 | } |
540 | } |
659 | } |
541 | |
660 | |
542 | static void |
661 | static void |
543 | timers_reify (void) |
662 | timers_reify (void) |
544 | { |
663 | { |
… | |
… | |
604 | } |
723 | } |
605 | } |
724 | } |
606 | } |
725 | } |
607 | } |
726 | } |
608 | |
727 | |
|
|
728 | static int |
|
|
729 | time_update_monotonic (void) |
|
|
730 | { |
|
|
731 | now = get_clock (); |
|
|
732 | |
|
|
733 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
734 | { |
|
|
735 | ev_now = now + diff; |
|
|
736 | return 0; |
|
|
737 | } |
|
|
738 | else |
|
|
739 | { |
|
|
740 | now_floor = now; |
|
|
741 | ev_now = ev_time (); |
|
|
742 | return 1; |
|
|
743 | } |
|
|
744 | } |
|
|
745 | |
609 | static void |
746 | static void |
610 | time_update (void) |
747 | time_update (void) |
611 | { |
748 | { |
612 | int i; |
749 | int i; |
613 | |
750 | |
614 | ev_now = ev_time (); |
751 | #if EV_USE_MONOTONIC |
615 | |
|
|
616 | if (have_monotonic) |
752 | if (expect_true (have_monotonic)) |
617 | { |
753 | { |
618 | ev_tstamp odiff = diff; |
754 | if (time_update_monotonic ()) |
619 | |
|
|
620 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
621 | { |
755 | { |
622 | now = get_clock (); |
756 | ev_tstamp odiff = diff; |
|
|
757 | |
|
|
758 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
759 | { |
623 | diff = ev_now - now; |
760 | diff = ev_now - now; |
624 | |
761 | |
625 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
762 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
626 | return; /* all is well */ |
763 | return; /* all is well */ |
627 | |
764 | |
628 | ev_now = ev_time (); |
765 | ev_now = ev_time (); |
|
|
766 | now = get_clock (); |
|
|
767 | now_floor = now; |
|
|
768 | } |
|
|
769 | |
|
|
770 | periodics_reschedule (diff - odiff); |
|
|
771 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
629 | } |
772 | } |
630 | |
|
|
631 | periodics_reschedule (diff - odiff); |
|
|
632 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
633 | } |
773 | } |
634 | else |
774 | else |
|
|
775 | #endif |
635 | { |
776 | { |
|
|
777 | ev_now = ev_time (); |
|
|
778 | |
636 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
779 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
637 | { |
780 | { |
638 | periodics_reschedule (ev_now - now); |
781 | periodics_reschedule (ev_now - now); |
639 | |
782 | |
640 | /* adjust timers. this is easy, as the offset is the same for all */ |
783 | /* adjust timers. this is easy, as the offset is the same for all */ |
641 | for (i = 0; i < timercnt; ++i) |
784 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
654 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
797 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
655 | |
798 | |
656 | do |
799 | do |
657 | { |
800 | { |
658 | /* queue check watchers (and execute them) */ |
801 | /* queue check watchers (and execute them) */ |
659 | if (preparecnt) |
802 | if (expect_false (preparecnt)) |
660 | { |
803 | { |
661 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
804 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
662 | call_pending (); |
805 | call_pending (); |
663 | } |
806 | } |
664 | |
807 | |
… | |
… | |
667 | |
810 | |
668 | /* calculate blocking time */ |
811 | /* calculate blocking time */ |
669 | |
812 | |
670 | /* we only need this for !monotonic clockor timers, but as we basically |
813 | /* we only need this for !monotonic clockor timers, but as we basically |
671 | always have timers, we just calculate it always */ |
814 | always have timers, we just calculate it always */ |
|
|
815 | #if EV_USE_MONOTONIC |
|
|
816 | if (expect_true (have_monotonic)) |
|
|
817 | time_update_monotonic (); |
|
|
818 | else |
|
|
819 | #endif |
|
|
820 | { |
672 | ev_now = ev_time (); |
821 | ev_now = ev_time (); |
|
|
822 | now = ev_now; |
|
|
823 | } |
673 | |
824 | |
674 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
825 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
675 | block = 0.; |
826 | block = 0.; |
676 | else |
827 | else |
677 | { |
828 | { |
678 | block = MAX_BLOCKTIME; |
829 | block = MAX_BLOCKTIME; |
679 | |
830 | |
680 | if (timercnt) |
831 | if (timercnt) |
681 | { |
832 | { |
682 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
833 | ev_tstamp to = timers [0]->at - now + method_fudge; |
683 | if (block > to) block = to; |
834 | if (block > to) block = to; |
684 | } |
835 | } |
685 | |
836 | |
686 | if (periodiccnt) |
837 | if (periodiccnt) |
687 | { |
838 | { |
… | |
… | |
744 | static void |
895 | static void |
745 | ev_clear_pending (W w) |
896 | ev_clear_pending (W w) |
746 | { |
897 | { |
747 | if (w->pending) |
898 | if (w->pending) |
748 | { |
899 | { |
749 | pendings [w->pending - 1].w = 0; |
900 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
750 | w->pending = 0; |
901 | w->pending = 0; |
751 | } |
902 | } |
752 | } |
903 | } |
753 | |
904 | |
754 | static void |
905 | static void |
755 | ev_start (W w, int active) |
906 | ev_start (W w, int active) |
756 | { |
907 | { |
|
|
908 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
909 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
910 | |
757 | w->active = active; |
911 | w->active = active; |
758 | } |
912 | } |
759 | |
913 | |
760 | static void |
914 | static void |
761 | ev_stop (W w) |
915 | ev_stop (W w) |
… | |
… | |
878 | } |
1032 | } |
879 | |
1033 | |
880 | ev_stop ((W)w); |
1034 | ev_stop ((W)w); |
881 | } |
1035 | } |
882 | |
1036 | |
|
|
1037 | #ifndef SA_RESTART |
|
|
1038 | # define SA_RESTART 0 |
|
|
1039 | #endif |
|
|
1040 | |
883 | void |
1041 | void |
884 | ev_signal_start (struct ev_signal *w) |
1042 | ev_signal_start (struct ev_signal *w) |
885 | { |
1043 | { |
886 | if (ev_is_active (w)) |
1044 | if (ev_is_active (w)) |
887 | return; |
1045 | return; |
… | |
… | |
895 | if (!w->next) |
1053 | if (!w->next) |
896 | { |
1054 | { |
897 | struct sigaction sa; |
1055 | struct sigaction sa; |
898 | sa.sa_handler = sighandler; |
1056 | sa.sa_handler = sighandler; |
899 | sigfillset (&sa.sa_mask); |
1057 | sigfillset (&sa.sa_mask); |
900 | sa.sa_flags = 0; |
1058 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
901 | sigaction (w->signum, &sa, 0); |
1059 | sigaction (w->signum, &sa, 0); |
902 | } |
1060 | } |
903 | } |
1061 | } |
904 | |
1062 | |
905 | void |
1063 | void |