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 |
… | |
… | |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
45 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
46 | #include <sys/time.h> |
48 | #include <sys/time.h> |
47 | #include <time.h> |
49 | #include <time.h> |
48 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
49 | #ifndef EV_USE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
54 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
55 | #endif |
54 | |
56 | |
55 | #ifndef EV_USE_SELECT |
57 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
58 | # define EV_USE_SELECT 1 |
57 | #endif |
59 | #endif |
58 | |
60 | |
|
|
61 | #ifndef EV_USE_POLL |
|
|
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
63 | #endif |
|
|
64 | |
59 | #ifndef EV_USE_EPOLL |
65 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
66 | # define EV_USE_EPOLL 0 |
61 | #endif |
67 | #endif |
62 | |
68 | |
|
|
69 | #ifndef EV_USE_REALTIME |
|
|
70 | # define EV_USE_REALTIME 1 |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | /**/ |
|
|
74 | |
|
|
75 | #ifndef CLOCK_MONOTONIC |
|
|
76 | # undef EV_USE_MONOTONIC |
|
|
77 | # define EV_USE_MONOTONIC 0 |
|
|
78 | #endif |
|
|
79 | |
63 | #ifndef CLOCK_REALTIME |
80 | #ifndef CLOCK_REALTIME |
|
|
81 | # undef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 0 |
82 | # define EV_USE_REALTIME 0 |
65 | #endif |
83 | #endif |
66 | #ifndef EV_USE_REALTIME |
84 | |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
85 | /**/ |
68 | #endif |
|
|
69 | |
86 | |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
87 | #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) */ |
88 | #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 */ |
89 | #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 */ |
90 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
74 | |
91 | |
75 | #include "ev.h" |
92 | #include "ev.h" |
|
|
93 | |
|
|
94 | #if __GNUC__ >= 3 |
|
|
95 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
96 | # define inline inline |
|
|
97 | #else |
|
|
98 | # define expect(expr,value) (expr) |
|
|
99 | # define inline static |
|
|
100 | #endif |
|
|
101 | |
|
|
102 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
103 | #define expect_true(expr) expect ((expr) != 0, 1) |
76 | |
104 | |
77 | typedef struct ev_watcher *W; |
105 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
106 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
107 | typedef struct ev_watcher_time *WT; |
80 | |
108 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
109 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
82 | ev_tstamp ev_now; |
110 | ev_tstamp ev_now; |
83 | int ev_method; |
111 | int ev_method; |
84 | |
112 | |
85 | static int have_monotonic; /* runtime */ |
113 | static int have_monotonic; /* runtime */ |
86 | |
114 | |
… | |
… | |
106 | |
134 | |
107 | static ev_tstamp |
135 | static ev_tstamp |
108 | get_clock (void) |
136 | get_clock (void) |
109 | { |
137 | { |
110 | #if EV_USE_MONOTONIC |
138 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
139 | if (expect_true (have_monotonic)) |
112 | { |
140 | { |
113 | struct timespec ts; |
141 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
142 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
143 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
144 | } |
… | |
… | |
120 | } |
148 | } |
121 | |
149 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
150 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
151 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
152 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
153 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
154 | { \ |
127 | int newcnt = cur; \ |
155 | int newcnt = cur; \ |
128 | do \ |
156 | do \ |
129 | { \ |
157 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
158 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
139 | /*****************************************************************************/ |
167 | /*****************************************************************************/ |
140 | |
168 | |
141 | typedef struct |
169 | typedef struct |
142 | { |
170 | { |
143 | struct ev_io *head; |
171 | struct ev_io *head; |
144 | int events; |
172 | unsigned char events; |
|
|
173 | unsigned char reify; |
145 | } ANFD; |
174 | } ANFD; |
146 | |
175 | |
147 | static ANFD *anfds; |
176 | static ANFD *anfds; |
148 | static int anfdmax; |
177 | static int anfdmax; |
149 | |
178 | |
… | |
… | |
152 | { |
181 | { |
153 | while (count--) |
182 | while (count--) |
154 | { |
183 | { |
155 | base->head = 0; |
184 | base->head = 0; |
156 | base->events = EV_NONE; |
185 | base->events = EV_NONE; |
|
|
186 | base->reify = 0; |
|
|
187 | |
157 | ++base; |
188 | ++base; |
158 | } |
189 | } |
159 | } |
190 | } |
160 | |
191 | |
161 | typedef struct |
192 | typedef struct |
… | |
… | |
225 | int events = 0; |
256 | int events = 0; |
226 | |
257 | |
227 | for (w = anfd->head; w; w = w->next) |
258 | for (w = anfd->head; w; w = w->next) |
228 | events |= w->events; |
259 | events |= w->events; |
229 | |
260 | |
230 | anfd->events &= ~EV_REIFY; |
261 | anfd->reify = 0; |
231 | |
262 | |
232 | if (anfd->events != events) |
263 | if (anfd->events != events) |
233 | { |
264 | { |
234 | method_modify (fd, anfd->events, events); |
265 | method_modify (fd, anfd->events, events); |
235 | anfd->events = events; |
266 | anfd->events = events; |
… | |
… | |
240 | } |
271 | } |
241 | |
272 | |
242 | static void |
273 | static void |
243 | fd_change (int fd) |
274 | fd_change (int fd) |
244 | { |
275 | { |
245 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
276 | if (anfds [fd].reify || fdchangecnt < 0) |
246 | return; |
277 | return; |
247 | |
278 | |
248 | anfds [fd].events |= EV_REIFY; |
279 | anfds [fd].reify = 1; |
249 | |
280 | |
250 | ++fdchangecnt; |
281 | ++fdchangecnt; |
251 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
282 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
252 | fdchanges [fdchangecnt - 1] = fd; |
283 | fdchanges [fdchangecnt - 1] = fd; |
253 | } |
284 | } |
254 | |
285 | |
|
|
286 | static void |
|
|
287 | fd_kill (int fd) |
|
|
288 | { |
|
|
289 | struct ev_io *w; |
|
|
290 | |
|
|
291 | printf ("killing fd %d\n", fd);//D |
|
|
292 | while ((w = anfds [fd].head)) |
|
|
293 | { |
|
|
294 | ev_io_stop (w); |
|
|
295 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
296 | } |
|
|
297 | } |
|
|
298 | |
255 | /* called on EBADF to verify fds */ |
299 | /* called on EBADF to verify fds */ |
256 | static void |
300 | static void |
257 | fd_recheck (void) |
301 | fd_ebadf (void) |
258 | { |
302 | { |
259 | int fd; |
303 | int fd; |
260 | |
304 | |
261 | for (fd = 0; fd < anfdmax; ++fd) |
305 | for (fd = 0; fd < anfdmax; ++fd) |
262 | if (anfds [fd].events) |
306 | if (anfds [fd].events) |
263 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
307 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
264 | while (anfds [fd].head) |
308 | fd_kill (fd); |
|
|
309 | } |
|
|
310 | |
|
|
311 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
312 | static void |
|
|
313 | fd_enomem (void) |
|
|
314 | { |
|
|
315 | int fd = anfdmax; |
|
|
316 | |
|
|
317 | while (fd--) |
|
|
318 | if (anfds [fd].events) |
265 | { |
319 | { |
266 | ev_io_stop (anfds [fd].head); |
320 | close (fd); |
267 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
321 | fd_kill (fd); |
|
|
322 | return; |
268 | } |
323 | } |
269 | } |
324 | } |
270 | |
325 | |
271 | /*****************************************************************************/ |
326 | /*****************************************************************************/ |
272 | |
327 | |
273 | static struct ev_timer **timers; |
328 | static struct ev_timer **timers; |
… | |
… | |
320 | /*****************************************************************************/ |
375 | /*****************************************************************************/ |
321 | |
376 | |
322 | typedef struct |
377 | typedef struct |
323 | { |
378 | { |
324 | struct ev_signal *head; |
379 | struct ev_signal *head; |
325 | sig_atomic_t gotsig; |
380 | sig_atomic_t volatile gotsig; |
326 | } ANSIG; |
381 | } ANSIG; |
327 | |
382 | |
328 | static ANSIG *signals; |
383 | static ANSIG *signals; |
329 | static int signalmax; |
384 | static int signalmax; |
330 | |
385 | |
331 | static int sigpipe [2]; |
386 | static int sigpipe [2]; |
332 | static sig_atomic_t gotsig; |
387 | static sig_atomic_t volatile gotsig; |
333 | static struct ev_io sigev; |
388 | static struct ev_io sigev; |
334 | |
389 | |
335 | static void |
390 | static void |
336 | signals_init (ANSIG *base, int count) |
391 | signals_init (ANSIG *base, int count) |
337 | { |
392 | { |
338 | while (count--) |
393 | while (count--) |
339 | { |
394 | { |
340 | base->head = 0; |
395 | base->head = 0; |
341 | base->gotsig = 0; |
396 | base->gotsig = 0; |
|
|
397 | |
342 | ++base; |
398 | ++base; |
343 | } |
399 | } |
344 | } |
400 | } |
345 | |
401 | |
346 | static void |
402 | static void |
… | |
… | |
349 | signals [signum - 1].gotsig = 1; |
405 | signals [signum - 1].gotsig = 1; |
350 | |
406 | |
351 | if (!gotsig) |
407 | if (!gotsig) |
352 | { |
408 | { |
353 | gotsig = 1; |
409 | gotsig = 1; |
354 | write (sigpipe [1], &gotsig, 1); |
410 | write (sigpipe [1], &signum, 1); |
355 | } |
411 | } |
356 | } |
412 | } |
357 | |
413 | |
358 | static void |
414 | static void |
359 | sigcb (struct ev_io *iow, int revents) |
415 | sigcb (struct ev_io *iow, int revents) |
360 | { |
416 | { |
361 | struct ev_signal *w; |
417 | struct ev_signal *w; |
362 | int sig; |
418 | int signum; |
363 | |
419 | |
|
|
420 | read (sigpipe [0], &revents, 1); |
364 | gotsig = 0; |
421 | gotsig = 0; |
365 | read (sigpipe [0], &revents, 1); |
|
|
366 | |
422 | |
367 | for (sig = signalmax; sig--; ) |
423 | for (signum = signalmax; signum--; ) |
368 | if (signals [sig].gotsig) |
424 | if (signals [signum].gotsig) |
369 | { |
425 | { |
370 | signals [sig].gotsig = 0; |
426 | signals [signum].gotsig = 0; |
371 | |
427 | |
372 | for (w = signals [sig].head; w; w = w->next) |
428 | for (w = signals [signum].head; w; w = w->next) |
373 | event ((W)w, EV_SIGNAL); |
429 | event ((W)w, EV_SIGNAL); |
374 | } |
430 | } |
375 | } |
431 | } |
376 | |
432 | |
377 | static void |
433 | static void |
… | |
… | |
414 | struct ev_child *w; |
470 | struct ev_child *w; |
415 | int pid, status; |
471 | int pid, status; |
416 | |
472 | |
417 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
473 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
418 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
474 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
419 | if (w->pid == pid || w->pid == -1) |
475 | if (w->pid == pid || !w->pid) |
420 | { |
476 | { |
421 | w->status = status; |
477 | w->status = status; |
422 | event ((W)w, EV_CHILD); |
478 | event ((W)w, EV_CHILD); |
423 | } |
479 | } |
424 | } |
480 | } |
… | |
… | |
426 | /*****************************************************************************/ |
482 | /*****************************************************************************/ |
427 | |
483 | |
428 | #if EV_USE_EPOLL |
484 | #if EV_USE_EPOLL |
429 | # include "ev_epoll.c" |
485 | # include "ev_epoll.c" |
430 | #endif |
486 | #endif |
|
|
487 | #if EV_USE_POLL |
|
|
488 | # include "ev_poll.c" |
|
|
489 | #endif |
431 | #if EV_USE_SELECT |
490 | #if EV_USE_SELECT |
432 | # include "ev_select.c" |
491 | # include "ev_select.c" |
433 | #endif |
492 | #endif |
434 | |
493 | |
435 | int |
494 | int |
… | |
… | |
442 | ev_version_minor (void) |
501 | ev_version_minor (void) |
443 | { |
502 | { |
444 | return EV_VERSION_MINOR; |
503 | return EV_VERSION_MINOR; |
445 | } |
504 | } |
446 | |
505 | |
|
|
506 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
507 | static int |
|
|
508 | enable_secure () |
|
|
509 | { |
|
|
510 | return getuid () != geteuid () |
|
|
511 | || getgid () != getegid (); |
|
|
512 | } |
|
|
513 | |
447 | int ev_init (int flags) |
514 | int ev_init (int methods) |
448 | { |
515 | { |
449 | if (!ev_method) |
516 | if (!ev_method) |
450 | { |
517 | { |
451 | #if EV_USE_MONOTONIC |
518 | #if EV_USE_MONOTONIC |
452 | { |
519 | { |
… | |
… | |
454 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
521 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
455 | have_monotonic = 1; |
522 | have_monotonic = 1; |
456 | } |
523 | } |
457 | #endif |
524 | #endif |
458 | |
525 | |
459 | ev_now = ev_time (); |
526 | ev_now = ev_time (); |
460 | now = get_clock (); |
527 | now = get_clock (); |
|
|
528 | now_floor = now; |
461 | diff = ev_now - now; |
529 | diff = ev_now - now; |
462 | |
530 | |
463 | if (pipe (sigpipe)) |
531 | if (pipe (sigpipe)) |
464 | return 0; |
532 | return 0; |
465 | |
533 | |
|
|
534 | if (methods == EVMETHOD_AUTO) |
|
|
535 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
536 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
537 | else |
466 | ev_method = EVMETHOD_NONE; |
538 | methods = EVMETHOD_ANY; |
|
|
539 | |
|
|
540 | ev_method = 0; |
467 | #if EV_USE_EPOLL |
541 | #if EV_USE_EPOLL |
468 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
542 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
543 | #endif |
|
|
544 | #if EV_USE_POLL |
|
|
545 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
469 | #endif |
546 | #endif |
470 | #if EV_USE_SELECT |
547 | #if EV_USE_SELECT |
471 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
548 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
472 | #endif |
549 | #endif |
473 | |
550 | |
474 | if (ev_method) |
551 | if (ev_method) |
475 | { |
552 | { |
476 | ev_watcher_init (&sigev, sigcb); |
553 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
485 | } |
562 | } |
486 | |
563 | |
487 | /*****************************************************************************/ |
564 | /*****************************************************************************/ |
488 | |
565 | |
489 | void |
566 | void |
490 | ev_prefork (void) |
567 | ev_fork_prepare (void) |
491 | { |
568 | { |
492 | /* nop */ |
569 | /* nop */ |
493 | } |
570 | } |
494 | |
571 | |
495 | void |
572 | void |
496 | ev_postfork_parent (void) |
573 | ev_fork_parent (void) |
497 | { |
574 | { |
498 | /* nop */ |
575 | /* nop */ |
499 | } |
576 | } |
500 | |
577 | |
501 | void |
578 | void |
502 | ev_postfork_child (void) |
579 | ev_fork_child (void) |
503 | { |
580 | { |
504 | #if EV_USE_EPOLL |
581 | #if EV_USE_EPOLL |
505 | if (ev_method == EVMETHOD_EPOLL) |
582 | if (ev_method == EVMETHOD_EPOLL) |
506 | epoll_postfork_child (); |
583 | epoll_postfork_child (); |
507 | #endif |
584 | #endif |
… | |
… | |
538 | struct ev_timer *w = timers [0]; |
615 | struct ev_timer *w = timers [0]; |
539 | |
616 | |
540 | /* first reschedule or stop timer */ |
617 | /* first reschedule or stop timer */ |
541 | if (w->repeat) |
618 | if (w->repeat) |
542 | { |
619 | { |
|
|
620 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
543 | w->at = now + w->repeat; |
621 | w->at = now + w->repeat; |
544 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
545 | downheap ((WT *)timers, timercnt, 0); |
622 | downheap ((WT *)timers, timercnt, 0); |
546 | } |
623 | } |
547 | else |
624 | else |
548 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
625 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
549 | |
626 | |
… | |
… | |
560 | |
637 | |
561 | /* first reschedule or stop timer */ |
638 | /* first reschedule or stop timer */ |
562 | if (w->interval) |
639 | if (w->interval) |
563 | { |
640 | { |
564 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
641 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
565 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
642 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
566 | downheap ((WT *)periodics, periodiccnt, 0); |
643 | downheap ((WT *)periodics, periodiccnt, 0); |
567 | } |
644 | } |
568 | else |
645 | else |
569 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
646 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
570 | |
647 | |
571 | event ((W)w, EV_TIMEOUT); |
648 | event ((W)w, EV_PERIODIC); |
572 | } |
649 | } |
573 | } |
650 | } |
574 | |
651 | |
575 | static void |
652 | static void |
576 | periodics_reschedule (ev_tstamp diff) |
653 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
595 | } |
672 | } |
596 | } |
673 | } |
597 | } |
674 | } |
598 | } |
675 | } |
599 | |
676 | |
|
|
677 | static int |
|
|
678 | time_update_monotonic (void) |
|
|
679 | { |
|
|
680 | now = get_clock (); |
|
|
681 | |
|
|
682 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
683 | { |
|
|
684 | ev_now = now + diff; |
|
|
685 | return 0; |
|
|
686 | } |
|
|
687 | else |
|
|
688 | { |
|
|
689 | now_floor = now; |
|
|
690 | ev_now = ev_time (); |
|
|
691 | return 1; |
|
|
692 | } |
|
|
693 | } |
|
|
694 | |
600 | static void |
695 | static void |
601 | time_update (void) |
696 | time_update (void) |
602 | { |
697 | { |
603 | int i; |
698 | int i; |
604 | |
699 | |
605 | ev_now = ev_time (); |
700 | #if EV_USE_MONOTONIC |
606 | |
|
|
607 | if (have_monotonic) |
701 | if (expect_true (have_monotonic)) |
608 | { |
702 | { |
|
|
703 | if (time_update_monotonic ()) |
|
|
704 | { |
609 | ev_tstamp odiff = diff; |
705 | ev_tstamp odiff = diff; |
610 | |
706 | |
611 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
707 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
612 | { |
708 | { |
613 | now = get_clock (); |
|
|
614 | diff = ev_now - now; |
709 | diff = ev_now - now; |
615 | |
710 | |
616 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
711 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
617 | return; /* all is well */ |
712 | return; /* all is well */ |
618 | |
713 | |
619 | ev_now = ev_time (); |
714 | ev_now = ev_time (); |
|
|
715 | now = get_clock (); |
|
|
716 | now_floor = now; |
620 | } |
717 | } |
621 | |
718 | |
622 | periodics_reschedule (diff - odiff); |
719 | periodics_reschedule (diff - odiff); |
623 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
720 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
721 | } |
624 | } |
722 | } |
625 | else |
723 | else |
|
|
724 | #endif |
626 | { |
725 | { |
|
|
726 | ev_now = ev_time (); |
|
|
727 | |
627 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
728 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
628 | { |
729 | { |
629 | periodics_reschedule (ev_now - now); |
730 | periodics_reschedule (ev_now - now); |
630 | |
731 | |
631 | /* adjust timers. this is easy, as the offset is the same for all */ |
732 | /* adjust timers. this is easy, as the offset is the same for all */ |
632 | for (i = 0; i < timercnt; ++i) |
733 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
645 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
746 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
646 | |
747 | |
647 | do |
748 | do |
648 | { |
749 | { |
649 | /* queue check watchers (and execute them) */ |
750 | /* queue check watchers (and execute them) */ |
650 | if (preparecnt) |
751 | if (expect_false (preparecnt)) |
651 | { |
752 | { |
652 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
753 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
653 | call_pending (); |
754 | call_pending (); |
654 | } |
755 | } |
655 | |
756 | |
… | |
… | |
658 | |
759 | |
659 | /* calculate blocking time */ |
760 | /* calculate blocking time */ |
660 | |
761 | |
661 | /* we only need this for !monotonic clockor timers, but as we basically |
762 | /* we only need this for !monotonic clockor timers, but as we basically |
662 | always have timers, we just calculate it always */ |
763 | always have timers, we just calculate it always */ |
|
|
764 | #if EV_USE_MONOTONIC |
|
|
765 | if (expect_true (have_monotonic)) |
|
|
766 | time_update_monotonic (); |
|
|
767 | else |
|
|
768 | #endif |
|
|
769 | { |
663 | ev_now = ev_time (); |
770 | ev_now = ev_time (); |
|
|
771 | now = ev_now; |
|
|
772 | } |
664 | |
773 | |
665 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
774 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
666 | block = 0.; |
775 | block = 0.; |
667 | else |
776 | else |
668 | { |
777 | { |
669 | block = MAX_BLOCKTIME; |
778 | block = MAX_BLOCKTIME; |
670 | |
779 | |
671 | if (timercnt) |
780 | if (timercnt) |
672 | { |
781 | { |
673 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
782 | ev_tstamp to = timers [0]->at - now + method_fudge; |
674 | if (block > to) block = to; |
783 | if (block > to) block = to; |
675 | } |
784 | } |
676 | |
785 | |
677 | if (periodiccnt) |
786 | if (periodiccnt) |
678 | { |
787 | { |
… | |
… | |
731 | head = &(*head)->next; |
840 | head = &(*head)->next; |
732 | } |
841 | } |
733 | } |
842 | } |
734 | |
843 | |
735 | static void |
844 | static void |
736 | ev_clear (W w) |
845 | ev_clear_pending (W w) |
737 | { |
846 | { |
738 | if (w->pending) |
847 | if (w->pending) |
739 | { |
848 | { |
740 | pendings [w->pending - 1].w = 0; |
849 | pendings [w->pending - 1].w = 0; |
741 | w->pending = 0; |
850 | w->pending = 0; |
… | |
… | |
757 | /*****************************************************************************/ |
866 | /*****************************************************************************/ |
758 | |
867 | |
759 | void |
868 | void |
760 | ev_io_start (struct ev_io *w) |
869 | ev_io_start (struct ev_io *w) |
761 | { |
870 | { |
|
|
871 | int fd = w->fd; |
|
|
872 | |
762 | if (ev_is_active (w)) |
873 | if (ev_is_active (w)) |
763 | return; |
874 | return; |
764 | |
875 | |
765 | int fd = w->fd; |
876 | assert (("ev_io_start called with negative fd", fd >= 0)); |
766 | |
877 | |
767 | ev_start ((W)w, 1); |
878 | ev_start ((W)w, 1); |
768 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
879 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
769 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
880 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
770 | |
881 | |
… | |
… | |
772 | } |
883 | } |
773 | |
884 | |
774 | void |
885 | void |
775 | ev_io_stop (struct ev_io *w) |
886 | ev_io_stop (struct ev_io *w) |
776 | { |
887 | { |
777 | ev_clear ((W)w); |
888 | ev_clear_pending ((W)w); |
778 | if (!ev_is_active (w)) |
889 | if (!ev_is_active (w)) |
779 | return; |
890 | return; |
780 | |
891 | |
781 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
892 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
782 | ev_stop ((W)w); |
893 | ev_stop ((W)w); |
… | |
… | |
790 | if (ev_is_active (w)) |
901 | if (ev_is_active (w)) |
791 | return; |
902 | return; |
792 | |
903 | |
793 | w->at += now; |
904 | w->at += now; |
794 | |
905 | |
795 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
906 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
796 | |
907 | |
797 | ev_start ((W)w, ++timercnt); |
908 | ev_start ((W)w, ++timercnt); |
798 | array_needsize (timers, timermax, timercnt, ); |
909 | array_needsize (timers, timermax, timercnt, ); |
799 | timers [timercnt - 1] = w; |
910 | timers [timercnt - 1] = w; |
800 | upheap ((WT *)timers, timercnt - 1); |
911 | upheap ((WT *)timers, timercnt - 1); |
801 | } |
912 | } |
802 | |
913 | |
803 | void |
914 | void |
804 | ev_timer_stop (struct ev_timer *w) |
915 | ev_timer_stop (struct ev_timer *w) |
805 | { |
916 | { |
806 | ev_clear ((W)w); |
917 | ev_clear_pending ((W)w); |
807 | if (!ev_is_active (w)) |
918 | if (!ev_is_active (w)) |
808 | return; |
919 | return; |
809 | |
920 | |
810 | if (w->active < timercnt--) |
921 | if (w->active < timercnt--) |
811 | { |
922 | { |
… | |
… | |
839 | ev_periodic_start (struct ev_periodic *w) |
950 | ev_periodic_start (struct ev_periodic *w) |
840 | { |
951 | { |
841 | if (ev_is_active (w)) |
952 | if (ev_is_active (w)) |
842 | return; |
953 | return; |
843 | |
954 | |
844 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
955 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
845 | |
956 | |
846 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
957 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
847 | if (w->interval) |
958 | if (w->interval) |
848 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
959 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
849 | |
960 | |
… | |
… | |
854 | } |
965 | } |
855 | |
966 | |
856 | void |
967 | void |
857 | ev_periodic_stop (struct ev_periodic *w) |
968 | ev_periodic_stop (struct ev_periodic *w) |
858 | { |
969 | { |
859 | ev_clear ((W)w); |
970 | ev_clear_pending ((W)w); |
860 | if (!ev_is_active (w)) |
971 | if (!ev_is_active (w)) |
861 | return; |
972 | return; |
862 | |
973 | |
863 | if (w->active < periodiccnt--) |
974 | if (w->active < periodiccnt--) |
864 | { |
975 | { |
… | |
… | |
872 | void |
983 | void |
873 | ev_signal_start (struct ev_signal *w) |
984 | ev_signal_start (struct ev_signal *w) |
874 | { |
985 | { |
875 | if (ev_is_active (w)) |
986 | if (ev_is_active (w)) |
876 | return; |
987 | return; |
|
|
988 | |
|
|
989 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
877 | |
990 | |
878 | ev_start ((W)w, 1); |
991 | ev_start ((W)w, 1); |
879 | array_needsize (signals, signalmax, w->signum, signals_init); |
992 | array_needsize (signals, signalmax, w->signum, signals_init); |
880 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
993 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
881 | |
994 | |
… | |
… | |
890 | } |
1003 | } |
891 | |
1004 | |
892 | void |
1005 | void |
893 | ev_signal_stop (struct ev_signal *w) |
1006 | ev_signal_stop (struct ev_signal *w) |
894 | { |
1007 | { |
895 | ev_clear ((W)w); |
1008 | ev_clear_pending ((W)w); |
896 | if (!ev_is_active (w)) |
1009 | if (!ev_is_active (w)) |
897 | return; |
1010 | return; |
898 | |
1011 | |
899 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1012 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
900 | ev_stop ((W)w); |
1013 | ev_stop ((W)w); |
… | |
… | |
915 | } |
1028 | } |
916 | |
1029 | |
917 | void |
1030 | void |
918 | ev_idle_stop (struct ev_idle *w) |
1031 | ev_idle_stop (struct ev_idle *w) |
919 | { |
1032 | { |
920 | ev_clear ((W)w); |
1033 | ev_clear_pending ((W)w); |
921 | if (ev_is_active (w)) |
1034 | if (ev_is_active (w)) |
922 | return; |
1035 | return; |
923 | |
1036 | |
924 | idles [w->active - 1] = idles [--idlecnt]; |
1037 | idles [w->active - 1] = idles [--idlecnt]; |
925 | ev_stop ((W)w); |
1038 | ev_stop ((W)w); |
… | |
… | |
937 | } |
1050 | } |
938 | |
1051 | |
939 | void |
1052 | void |
940 | ev_prepare_stop (struct ev_prepare *w) |
1053 | ev_prepare_stop (struct ev_prepare *w) |
941 | { |
1054 | { |
942 | ev_clear ((W)w); |
1055 | ev_clear_pending ((W)w); |
943 | if (ev_is_active (w)) |
1056 | if (ev_is_active (w)) |
944 | return; |
1057 | return; |
945 | |
1058 | |
946 | prepares [w->active - 1] = prepares [--preparecnt]; |
1059 | prepares [w->active - 1] = prepares [--preparecnt]; |
947 | ev_stop ((W)w); |
1060 | ev_stop ((W)w); |
… | |
… | |
959 | } |
1072 | } |
960 | |
1073 | |
961 | void |
1074 | void |
962 | ev_check_stop (struct ev_check *w) |
1075 | ev_check_stop (struct ev_check *w) |
963 | { |
1076 | { |
964 | ev_clear ((W)w); |
1077 | ev_clear_pending ((W)w); |
965 | if (ev_is_active (w)) |
1078 | if (ev_is_active (w)) |
966 | return; |
1079 | return; |
967 | |
1080 | |
968 | checks [w->active - 1] = checks [--checkcnt]; |
1081 | checks [w->active - 1] = checks [--checkcnt]; |
969 | ev_stop ((W)w); |
1082 | ev_stop ((W)w); |
… | |
… | |
980 | } |
1093 | } |
981 | |
1094 | |
982 | void |
1095 | void |
983 | ev_child_stop (struct ev_child *w) |
1096 | ev_child_stop (struct ev_child *w) |
984 | { |
1097 | { |
985 | ev_clear ((W)w); |
1098 | ev_clear_pending ((W)w); |
986 | if (ev_is_active (w)) |
1099 | if (ev_is_active (w)) |
987 | return; |
1100 | return; |
988 | |
1101 | |
989 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1102 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
990 | ev_stop ((W)w); |
1103 | ev_stop ((W)w); |