1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
40 | #include <stdio.h> |
42 | #include <stdio.h> |
41 | |
43 | |
42 | #include <assert.h> |
44 | #include <assert.h> |
43 | #include <errno.h> |
45 | #include <errno.h> |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
45 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
46 | #include <sys/time.h> |
50 | #include <sys/time.h> |
47 | #include <time.h> |
51 | #include <time.h> |
48 | |
52 | |
|
|
53 | /**/ |
|
|
54 | |
49 | #ifndef EV_USE_MONOTONIC |
55 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
56 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
57 | #endif |
54 | |
58 | |
55 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
57 | #endif |
61 | #endif |
58 | |
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 | |
59 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
61 | #endif |
69 | #endif |
62 | |
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 | /**/ |
|
|
80 | |
|
|
81 | #ifndef CLOCK_MONOTONIC |
|
|
82 | # undef EV_USE_MONOTONIC |
|
|
83 | # define EV_USE_MONOTONIC 0 |
|
|
84 | #endif |
|
|
85 | |
63 | #ifndef CLOCK_REALTIME |
86 | #ifndef CLOCK_REALTIME |
|
|
87 | # undef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 0 |
88 | # define EV_USE_REALTIME 0 |
65 | #endif |
89 | #endif |
66 | #ifndef EV_USE_REALTIME |
90 | |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
91 | /**/ |
68 | #endif |
|
|
69 | |
92 | |
70 | #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) */ |
71 | #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) */ |
72 | #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 */ |
73 | #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 */ |
74 | |
97 | |
75 | #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) |
76 | |
113 | |
77 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
80 | |
117 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
82 | ev_tstamp ev_now; |
119 | ev_tstamp ev_now; |
83 | int ev_method; |
120 | int ev_method; |
84 | |
121 | |
85 | static int have_monotonic; /* runtime */ |
122 | static int have_monotonic; /* runtime */ |
86 | |
123 | |
… | |
… | |
106 | |
143 | |
107 | static ev_tstamp |
144 | static ev_tstamp |
108 | get_clock (void) |
145 | get_clock (void) |
109 | { |
146 | { |
110 | #if EV_USE_MONOTONIC |
147 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
148 | if (expect_true (have_monotonic)) |
112 | { |
149 | { |
113 | struct timespec ts; |
150 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
151 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
153 | } |
… | |
… | |
120 | } |
157 | } |
121 | |
158 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
160 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
161 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
162 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
163 | { \ |
127 | int newcnt = cur; \ |
164 | int newcnt = cur; \ |
128 | do \ |
165 | do \ |
129 | { \ |
166 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
167 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
165 | { |
202 | { |
166 | W w; |
203 | W w; |
167 | int events; |
204 | int events; |
168 | } ANPENDING; |
205 | } ANPENDING; |
169 | |
206 | |
170 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
171 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
172 | |
209 | |
173 | static void |
210 | static void |
174 | event (W w, int events) |
211 | event (W w, int events) |
175 | { |
212 | { |
176 | if (w->pending) |
213 | if (w->pending) |
177 | { |
214 | { |
178 | pendings [w->pending - 1].events |= events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
179 | return; |
216 | return; |
180 | } |
217 | } |
181 | |
218 | |
182 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
183 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
184 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
185 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
186 | } |
223 | } |
187 | |
224 | |
188 | static void |
225 | static void |
189 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
190 | { |
227 | { |
… | |
… | |
253 | ++fdchangecnt; |
290 | ++fdchangecnt; |
254 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
255 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
256 | } |
293 | } |
257 | |
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 | |
258 | /* called on EBADF to verify fds */ |
308 | /* called on EBADF to verify fds */ |
259 | static void |
309 | static void |
260 | fd_recheck (void) |
310 | fd_ebadf (void) |
261 | { |
311 | { |
262 | int fd; |
312 | int fd; |
263 | |
313 | |
264 | for (fd = 0; fd < anfdmax; ++fd) |
314 | for (fd = 0; fd < anfdmax; ++fd) |
265 | if (anfds [fd].events) |
315 | if (anfds [fd].events) |
266 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
267 | 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) |
268 | { |
328 | { |
269 | ev_io_stop (anfds [fd].head); |
329 | close (fd); |
270 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
330 | fd_kill (fd); |
|
|
331 | return; |
271 | } |
332 | } |
272 | } |
333 | } |
273 | |
334 | |
274 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
275 | |
336 | |
276 | static struct ev_timer **timers; |
337 | static struct ev_timer **timers; |
… | |
… | |
352 | { |
413 | { |
353 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
354 | |
415 | |
355 | if (!gotsig) |
416 | if (!gotsig) |
356 | { |
417 | { |
|
|
418 | int old_errno = errno; |
357 | gotsig = 1; |
419 | gotsig = 1; |
358 | write (sigpipe [1], &signum, 1); |
420 | write (sigpipe [1], &signum, 1); |
|
|
421 | errno = old_errno; |
359 | } |
422 | } |
360 | } |
423 | } |
361 | |
424 | |
362 | static void |
425 | static void |
363 | sigcb (struct ev_io *iow, int revents) |
426 | sigcb (struct ev_io *iow, int revents) |
364 | { |
427 | { |
365 | struct ev_signal *w; |
428 | struct ev_signal *w; |
366 | int sig; |
429 | int signum; |
367 | |
430 | |
368 | read (sigpipe [0], &revents, 1); |
431 | read (sigpipe [0], &revents, 1); |
369 | gotsig = 0; |
432 | gotsig = 0; |
370 | |
433 | |
371 | for (sig = signalmax; sig--; ) |
434 | for (signum = signalmax; signum--; ) |
372 | if (signals [sig].gotsig) |
435 | if (signals [signum].gotsig) |
373 | { |
436 | { |
374 | signals [sig].gotsig = 0; |
437 | signals [signum].gotsig = 0; |
375 | |
438 | |
376 | for (w = signals [sig].head; w; w = w->next) |
439 | for (w = signals [signum].head; w; w = w->next) |
377 | event ((W)w, EV_SIGNAL); |
440 | event ((W)w, EV_SIGNAL); |
378 | } |
441 | } |
379 | } |
442 | } |
380 | |
443 | |
381 | static void |
444 | static void |
382 | siginit (void) |
445 | siginit (void) |
383 | { |
446 | { |
|
|
447 | #ifndef WIN32 |
384 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
385 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
386 | |
450 | |
387 | /* rather than sort out wether we really need nb, set it */ |
451 | /* rather than sort out wether we really need nb, set it */ |
388 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
389 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
454 | #endif |
390 | |
455 | |
391 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
456 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
392 | ev_io_start (&sigev); |
457 | ev_io_start (&sigev); |
393 | } |
458 | } |
394 | |
459 | |
… | |
… | |
406 | /*****************************************************************************/ |
471 | /*****************************************************************************/ |
407 | |
472 | |
408 | static struct ev_child *childs [PID_HASHSIZE]; |
473 | static struct ev_child *childs [PID_HASHSIZE]; |
409 | static struct ev_signal childev; |
474 | static struct ev_signal childev; |
410 | |
475 | |
|
|
476 | #ifndef WIN32 |
|
|
477 | |
411 | #ifndef WCONTINUED |
478 | #ifndef WCONTINUED |
412 | # define WCONTINUED 0 |
479 | # define WCONTINUED 0 |
413 | #endif |
480 | #endif |
414 | |
481 | |
415 | 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 |
416 | childcb (struct ev_signal *sw, int revents) |
499 | childcb (struct ev_signal *sw, int revents) |
417 | { |
500 | { |
418 | struct ev_child *w; |
|
|
419 | int pid, status; |
501 | int pid, status; |
420 | |
502 | |
|
|
503 | printf ("chld %x\n", revents);//D |
421 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
504 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
422 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
505 | { |
423 | if (w->pid == pid || w->pid == -1) |
506 | /* make sure we are called again until all childs have been reaped */ |
424 | { |
|
|
425 | w->status = status; |
|
|
426 | event ((W)w, EV_CHILD); |
507 | event ((W)sw, EV_SIGNAL); |
427 | } |
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 | } |
428 | } |
512 | } |
|
|
513 | |
|
|
514 | #endif |
429 | |
515 | |
430 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
431 | |
517 | |
|
|
518 | #if EV_USE_KQUEUE |
|
|
519 | # include "ev_kqueue.c" |
|
|
520 | #endif |
432 | #if EV_USE_EPOLL |
521 | #if EV_USE_EPOLL |
433 | # include "ev_epoll.c" |
522 | # include "ev_epoll.c" |
434 | #endif |
523 | #endif |
|
|
524 | #if EV_USE_POLL |
|
|
525 | # include "ev_poll.c" |
|
|
526 | #endif |
435 | #if EV_USE_SELECT |
527 | #if EV_USE_SELECT |
436 | # include "ev_select.c" |
528 | # include "ev_select.c" |
437 | #endif |
529 | #endif |
438 | |
530 | |
439 | int |
531 | int |
… | |
… | |
446 | ev_version_minor (void) |
538 | ev_version_minor (void) |
447 | { |
539 | { |
448 | return EV_VERSION_MINOR; |
540 | return EV_VERSION_MINOR; |
449 | } |
541 | } |
450 | |
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 | |
451 | int ev_init (int flags) |
555 | int ev_init (int methods) |
452 | { |
556 | { |
453 | if (!ev_method) |
557 | if (!ev_method) |
454 | { |
558 | { |
455 | #if EV_USE_MONOTONIC |
559 | #if EV_USE_MONOTONIC |
456 | { |
560 | { |
… | |
… | |
458 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
562 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
459 | have_monotonic = 1; |
563 | have_monotonic = 1; |
460 | } |
564 | } |
461 | #endif |
565 | #endif |
462 | |
566 | |
463 | ev_now = ev_time (); |
567 | ev_now = ev_time (); |
464 | now = get_clock (); |
568 | now = get_clock (); |
|
|
569 | now_floor = now; |
465 | diff = ev_now - now; |
570 | diff = ev_now - now; |
466 | |
571 | |
467 | if (pipe (sigpipe)) |
572 | if (pipe (sigpipe)) |
468 | return 0; |
573 | return 0; |
469 | |
574 | |
|
|
575 | if (methods == EVMETHOD_AUTO) |
|
|
576 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
577 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
578 | else |
470 | 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 |
471 | #if EV_USE_EPOLL |
585 | #if EV_USE_EPOLL |
472 | 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); |
473 | #endif |
590 | #endif |
474 | #if EV_USE_SELECT |
591 | #if EV_USE_SELECT |
475 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
592 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
476 | #endif |
593 | #endif |
477 | |
594 | |
478 | if (ev_method) |
595 | if (ev_method) |
479 | { |
596 | { |
480 | ev_watcher_init (&sigev, sigcb); |
597 | ev_watcher_init (&sigev, sigcb); |
|
|
598 | ev_set_priority (&sigev, EV_MAXPRI); |
481 | siginit (); |
599 | siginit (); |
482 | |
600 | |
|
|
601 | #ifndef WIN32 |
483 | ev_signal_init (&childev, childcb, SIGCHLD); |
602 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
603 | ev_set_priority (&childev, EV_MAXPRI); |
484 | ev_signal_start (&childev); |
604 | ev_signal_start (&childev); |
|
|
605 | #endif |
485 | } |
606 | } |
486 | } |
607 | } |
487 | |
608 | |
488 | return ev_method; |
609 | return ev_method; |
489 | } |
610 | } |
490 | |
611 | |
491 | /*****************************************************************************/ |
612 | /*****************************************************************************/ |
492 | |
613 | |
493 | void |
614 | void |
494 | ev_prefork (void) |
615 | ev_fork_prepare (void) |
495 | { |
616 | { |
496 | /* nop */ |
617 | /* nop */ |
497 | } |
618 | } |
498 | |
619 | |
499 | void |
620 | void |
500 | ev_postfork_parent (void) |
621 | ev_fork_parent (void) |
501 | { |
622 | { |
502 | /* nop */ |
623 | /* nop */ |
503 | } |
624 | } |
504 | |
625 | |
505 | void |
626 | void |
506 | ev_postfork_child (void) |
627 | ev_fork_child (void) |
507 | { |
628 | { |
508 | #if EV_USE_EPOLL |
629 | #if EV_USE_EPOLL |
509 | if (ev_method == EVMETHOD_EPOLL) |
630 | if (ev_method == EVMETHOD_EPOLL) |
510 | epoll_postfork_child (); |
631 | epoll_postfork_child (); |
511 | #endif |
632 | #endif |
… | |
… | |
520 | /*****************************************************************************/ |
641 | /*****************************************************************************/ |
521 | |
642 | |
522 | static void |
643 | static void |
523 | call_pending (void) |
644 | call_pending (void) |
524 | { |
645 | { |
|
|
646 | int pri; |
|
|
647 | |
|
|
648 | for (pri = NUMPRI; pri--; ) |
525 | while (pendingcnt) |
649 | while (pendingcnt [pri]) |
526 | { |
650 | { |
527 | ANPENDING *p = pendings + --pendingcnt; |
651 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
528 | |
652 | |
529 | if (p->w) |
653 | if (p->w) |
530 | { |
654 | { |
531 | p->w->pending = 0; |
655 | p->w->pending = 0; |
532 | p->w->cb (p->w, p->events); |
656 | p->w->cb (p->w, p->events); |
533 | } |
657 | } |
534 | } |
658 | } |
535 | } |
659 | } |
536 | |
660 | |
537 | static void |
661 | static void |
538 | timers_reify (void) |
662 | timers_reify (void) |
539 | { |
663 | { |
… | |
… | |
599 | } |
723 | } |
600 | } |
724 | } |
601 | } |
725 | } |
602 | } |
726 | } |
603 | |
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 | |
604 | static void |
746 | static void |
605 | time_update (void) |
747 | time_update (void) |
606 | { |
748 | { |
607 | int i; |
749 | int i; |
608 | |
750 | |
609 | ev_now = ev_time (); |
751 | #if EV_USE_MONOTONIC |
610 | |
|
|
611 | if (have_monotonic) |
752 | if (expect_true (have_monotonic)) |
612 | { |
753 | { |
613 | ev_tstamp odiff = diff; |
754 | if (time_update_monotonic ()) |
614 | |
|
|
615 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
616 | { |
755 | { |
617 | now = get_clock (); |
756 | ev_tstamp odiff = diff; |
|
|
757 | |
|
|
758 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
759 | { |
618 | diff = ev_now - now; |
760 | diff = ev_now - now; |
619 | |
761 | |
620 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
762 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
621 | return; /* all is well */ |
763 | return; /* all is well */ |
622 | |
764 | |
623 | 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 */ |
624 | } |
772 | } |
625 | |
|
|
626 | periodics_reschedule (diff - odiff); |
|
|
627 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
628 | } |
773 | } |
629 | else |
774 | else |
|
|
775 | #endif |
630 | { |
776 | { |
|
|
777 | ev_now = ev_time (); |
|
|
778 | |
631 | 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)) |
632 | { |
780 | { |
633 | periodics_reschedule (ev_now - now); |
781 | periodics_reschedule (ev_now - now); |
634 | |
782 | |
635 | /* 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 */ |
636 | for (i = 0; i < timercnt; ++i) |
784 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
649 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
797 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
650 | |
798 | |
651 | do |
799 | do |
652 | { |
800 | { |
653 | /* queue check watchers (and execute them) */ |
801 | /* queue check watchers (and execute them) */ |
654 | if (preparecnt) |
802 | if (expect_false (preparecnt)) |
655 | { |
803 | { |
656 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
804 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
657 | call_pending (); |
805 | call_pending (); |
658 | } |
806 | } |
659 | |
807 | |
… | |
… | |
662 | |
810 | |
663 | /* calculate blocking time */ |
811 | /* calculate blocking time */ |
664 | |
812 | |
665 | /* 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 |
666 | 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 | { |
667 | ev_now = ev_time (); |
821 | ev_now = ev_time (); |
|
|
822 | now = ev_now; |
|
|
823 | } |
668 | |
824 | |
669 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
825 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
670 | block = 0.; |
826 | block = 0.; |
671 | else |
827 | else |
672 | { |
828 | { |
673 | block = MAX_BLOCKTIME; |
829 | block = MAX_BLOCKTIME; |
674 | |
830 | |
675 | if (timercnt) |
831 | if (timercnt) |
676 | { |
832 | { |
677 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
833 | ev_tstamp to = timers [0]->at - now + method_fudge; |
678 | if (block > to) block = to; |
834 | if (block > to) block = to; |
679 | } |
835 | } |
680 | |
836 | |
681 | if (periodiccnt) |
837 | if (periodiccnt) |
682 | { |
838 | { |
… | |
… | |
739 | static void |
895 | static void |
740 | ev_clear_pending (W w) |
896 | ev_clear_pending (W w) |
741 | { |
897 | { |
742 | if (w->pending) |
898 | if (w->pending) |
743 | { |
899 | { |
744 | pendings [w->pending - 1].w = 0; |
900 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
745 | w->pending = 0; |
901 | w->pending = 0; |
746 | } |
902 | } |
747 | } |
903 | } |
748 | |
904 | |
749 | static void |
905 | static void |
750 | ev_start (W w, int active) |
906 | ev_start (W w, int active) |
751 | { |
907 | { |
|
|
908 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
909 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
910 | |
752 | w->active = active; |
911 | w->active = active; |
753 | } |
912 | } |
754 | |
913 | |
755 | static void |
914 | static void |
756 | ev_stop (W w) |
915 | ev_stop (W w) |
… | |
… | |
761 | /*****************************************************************************/ |
920 | /*****************************************************************************/ |
762 | |
921 | |
763 | void |
922 | void |
764 | ev_io_start (struct ev_io *w) |
923 | ev_io_start (struct ev_io *w) |
765 | { |
924 | { |
|
|
925 | int fd = w->fd; |
|
|
926 | |
766 | if (ev_is_active (w)) |
927 | if (ev_is_active (w)) |
767 | return; |
928 | return; |
768 | |
|
|
769 | int fd = w->fd; |
|
|
770 | |
929 | |
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
930 | assert (("ev_io_start called with negative fd", fd >= 0)); |
772 | |
931 | |
773 | ev_start ((W)w, 1); |
932 | ev_start ((W)w, 1); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
933 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
… | |
… | |
873 | } |
1032 | } |
874 | |
1033 | |
875 | ev_stop ((W)w); |
1034 | ev_stop ((W)w); |
876 | } |
1035 | } |
877 | |
1036 | |
|
|
1037 | #ifndef SA_RESTART |
|
|
1038 | # define SA_RESTART 0 |
|
|
1039 | #endif |
|
|
1040 | |
878 | void |
1041 | void |
879 | ev_signal_start (struct ev_signal *w) |
1042 | ev_signal_start (struct ev_signal *w) |
880 | { |
1043 | { |
881 | if (ev_is_active (w)) |
1044 | if (ev_is_active (w)) |
882 | return; |
1045 | return; |
… | |
… | |
890 | if (!w->next) |
1053 | if (!w->next) |
891 | { |
1054 | { |
892 | struct sigaction sa; |
1055 | struct sigaction sa; |
893 | sa.sa_handler = sighandler; |
1056 | sa.sa_handler = sighandler; |
894 | sigfillset (&sa.sa_mask); |
1057 | sigfillset (&sa.sa_mask); |
895 | sa.sa_flags = 0; |
1058 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
896 | sigaction (w->signum, &sa, 0); |
1059 | sigaction (w->signum, &sa, 0); |
897 | } |
1060 | } |
898 | } |
1061 | } |
899 | |
1062 | |
900 | void |
1063 | void |