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) |
|
|
104 | |
|
|
105 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
106 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
76 | |
107 | |
77 | typedef struct ev_watcher *W; |
108 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
109 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
110 | typedef struct ev_watcher_time *WT; |
80 | |
111 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
112 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
82 | ev_tstamp ev_now; |
113 | ev_tstamp ev_now; |
83 | int ev_method; |
114 | int ev_method; |
84 | |
115 | |
85 | static int have_monotonic; /* runtime */ |
116 | static int have_monotonic; /* runtime */ |
86 | |
117 | |
… | |
… | |
106 | |
137 | |
107 | static ev_tstamp |
138 | static ev_tstamp |
108 | get_clock (void) |
139 | get_clock (void) |
109 | { |
140 | { |
110 | #if EV_USE_MONOTONIC |
141 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
142 | if (expect_true (have_monotonic)) |
112 | { |
143 | { |
113 | struct timespec ts; |
144 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
145 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
146 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
147 | } |
… | |
… | |
120 | } |
151 | } |
121 | |
152 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
153 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
154 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
155 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
156 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
157 | { \ |
127 | int newcnt = cur; \ |
158 | int newcnt = cur; \ |
128 | do \ |
159 | do \ |
129 | { \ |
160 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
161 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
165 | { |
196 | { |
166 | W w; |
197 | W w; |
167 | int events; |
198 | int events; |
168 | } ANPENDING; |
199 | } ANPENDING; |
169 | |
200 | |
170 | static ANPENDING *pendings; |
201 | static ANPENDING *pendings [NUMPRI]; |
171 | static int pendingmax, pendingcnt; |
202 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
172 | |
203 | |
173 | static void |
204 | static void |
174 | event (W w, int events) |
205 | event (W w, int events) |
175 | { |
206 | { |
176 | if (w->pending) |
207 | if (w->pending) |
177 | { |
208 | { |
178 | pendings [w->pending - 1].events |= events; |
209 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
179 | return; |
210 | return; |
180 | } |
211 | } |
181 | |
212 | |
182 | w->pending = ++pendingcnt; |
213 | w->pending = ++pendingcnt [ABSPRI (w)]; |
183 | array_needsize (pendings, pendingmax, pendingcnt, ); |
214 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
184 | pendings [pendingcnt - 1].w = w; |
215 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
185 | pendings [pendingcnt - 1].events = events; |
216 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
186 | } |
217 | } |
187 | |
218 | |
188 | static void |
219 | static void |
189 | queue_events (W *events, int eventcnt, int type) |
220 | queue_events (W *events, int eventcnt, int type) |
190 | { |
221 | { |
… | |
… | |
253 | ++fdchangecnt; |
284 | ++fdchangecnt; |
254 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
285 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
255 | fdchanges [fdchangecnt - 1] = fd; |
286 | fdchanges [fdchangecnt - 1] = fd; |
256 | } |
287 | } |
257 | |
288 | |
|
|
289 | static void |
|
|
290 | fd_kill (int fd) |
|
|
291 | { |
|
|
292 | struct ev_io *w; |
|
|
293 | |
|
|
294 | printf ("killing fd %d\n", fd);//D |
|
|
295 | while ((w = anfds [fd].head)) |
|
|
296 | { |
|
|
297 | ev_io_stop (w); |
|
|
298 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
299 | } |
|
|
300 | } |
|
|
301 | |
258 | /* called on EBADF to verify fds */ |
302 | /* called on EBADF to verify fds */ |
259 | static void |
303 | static void |
260 | fd_recheck (void) |
304 | fd_ebadf (void) |
261 | { |
305 | { |
262 | int fd; |
306 | int fd; |
263 | |
307 | |
264 | for (fd = 0; fd < anfdmax; ++fd) |
308 | for (fd = 0; fd < anfdmax; ++fd) |
265 | if (anfds [fd].events) |
309 | if (anfds [fd].events) |
266 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
310 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
267 | while (anfds [fd].head) |
311 | fd_kill (fd); |
|
|
312 | } |
|
|
313 | |
|
|
314 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
315 | static void |
|
|
316 | fd_enomem (void) |
|
|
317 | { |
|
|
318 | int fd = anfdmax; |
|
|
319 | |
|
|
320 | while (fd--) |
|
|
321 | if (anfds [fd].events) |
268 | { |
322 | { |
269 | ev_io_stop (anfds [fd].head); |
323 | close (fd); |
270 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
324 | fd_kill (fd); |
|
|
325 | return; |
271 | } |
326 | } |
272 | } |
327 | } |
273 | |
328 | |
274 | /*****************************************************************************/ |
329 | /*****************************************************************************/ |
275 | |
330 | |
276 | static struct ev_timer **timers; |
331 | static struct ev_timer **timers; |
… | |
… | |
323 | /*****************************************************************************/ |
378 | /*****************************************************************************/ |
324 | |
379 | |
325 | typedef struct |
380 | typedef struct |
326 | { |
381 | { |
327 | struct ev_signal *head; |
382 | struct ev_signal *head; |
328 | sig_atomic_t gotsig; |
383 | sig_atomic_t volatile gotsig; |
329 | } ANSIG; |
384 | } ANSIG; |
330 | |
385 | |
331 | static ANSIG *signals; |
386 | static ANSIG *signals; |
332 | static int signalmax; |
387 | static int signalmax; |
333 | |
388 | |
334 | static int sigpipe [2]; |
389 | static int sigpipe [2]; |
335 | static sig_atomic_t gotsig; |
390 | static sig_atomic_t volatile gotsig; |
336 | static struct ev_io sigev; |
391 | static struct ev_io sigev; |
337 | |
392 | |
338 | static void |
393 | static void |
339 | signals_init (ANSIG *base, int count) |
394 | signals_init (ANSIG *base, int count) |
340 | { |
395 | { |
… | |
… | |
353 | signals [signum - 1].gotsig = 1; |
408 | signals [signum - 1].gotsig = 1; |
354 | |
409 | |
355 | if (!gotsig) |
410 | if (!gotsig) |
356 | { |
411 | { |
357 | gotsig = 1; |
412 | gotsig = 1; |
358 | write (sigpipe [1], &gotsig, 1); |
413 | write (sigpipe [1], &signum, 1); |
359 | } |
414 | } |
360 | } |
415 | } |
361 | |
416 | |
362 | static void |
417 | static void |
363 | sigcb (struct ev_io *iow, int revents) |
418 | sigcb (struct ev_io *iow, int revents) |
364 | { |
419 | { |
365 | struct ev_signal *w; |
420 | struct ev_signal *w; |
366 | int sig; |
421 | int signum; |
367 | |
422 | |
|
|
423 | read (sigpipe [0], &revents, 1); |
368 | gotsig = 0; |
424 | gotsig = 0; |
369 | read (sigpipe [0], &revents, 1); |
|
|
370 | |
425 | |
371 | for (sig = signalmax; sig--; ) |
426 | for (signum = signalmax; signum--; ) |
372 | if (signals [sig].gotsig) |
427 | if (signals [signum].gotsig) |
373 | { |
428 | { |
374 | signals [sig].gotsig = 0; |
429 | signals [signum].gotsig = 0; |
375 | |
430 | |
376 | for (w = signals [sig].head; w; w = w->next) |
431 | for (w = signals [signum].head; w; w = w->next) |
377 | event ((W)w, EV_SIGNAL); |
432 | event ((W)w, EV_SIGNAL); |
378 | } |
433 | } |
379 | } |
434 | } |
380 | |
435 | |
381 | static void |
436 | static void |
… | |
… | |
418 | struct ev_child *w; |
473 | struct ev_child *w; |
419 | int pid, status; |
474 | int pid, status; |
420 | |
475 | |
421 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
476 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
422 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
477 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
423 | if (w->pid == pid || w->pid == -1) |
478 | if (w->pid == pid || !w->pid) |
424 | { |
479 | { |
425 | w->status = status; |
480 | w->status = status; |
426 | event ((W)w, EV_CHILD); |
481 | event ((W)w, EV_CHILD); |
427 | } |
482 | } |
428 | } |
483 | } |
… | |
… | |
430 | /*****************************************************************************/ |
485 | /*****************************************************************************/ |
431 | |
486 | |
432 | #if EV_USE_EPOLL |
487 | #if EV_USE_EPOLL |
433 | # include "ev_epoll.c" |
488 | # include "ev_epoll.c" |
434 | #endif |
489 | #endif |
|
|
490 | #if EV_USE_POLL |
|
|
491 | # include "ev_poll.c" |
|
|
492 | #endif |
435 | #if EV_USE_SELECT |
493 | #if EV_USE_SELECT |
436 | # include "ev_select.c" |
494 | # include "ev_select.c" |
437 | #endif |
495 | #endif |
438 | |
496 | |
439 | int |
497 | int |
… | |
… | |
446 | ev_version_minor (void) |
504 | ev_version_minor (void) |
447 | { |
505 | { |
448 | return EV_VERSION_MINOR; |
506 | return EV_VERSION_MINOR; |
449 | } |
507 | } |
450 | |
508 | |
|
|
509 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
510 | static int |
|
|
511 | enable_secure () |
|
|
512 | { |
|
|
513 | return getuid () != geteuid () |
|
|
514 | || getgid () != getegid (); |
|
|
515 | } |
|
|
516 | |
451 | int ev_init (int flags) |
517 | int ev_init (int methods) |
452 | { |
518 | { |
453 | if (!ev_method) |
519 | if (!ev_method) |
454 | { |
520 | { |
455 | #if EV_USE_MONOTONIC |
521 | #if EV_USE_MONOTONIC |
456 | { |
522 | { |
… | |
… | |
458 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
524 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
459 | have_monotonic = 1; |
525 | have_monotonic = 1; |
460 | } |
526 | } |
461 | #endif |
527 | #endif |
462 | |
528 | |
463 | ev_now = ev_time (); |
529 | ev_now = ev_time (); |
464 | now = get_clock (); |
530 | now = get_clock (); |
|
|
531 | now_floor = now; |
465 | diff = ev_now - now; |
532 | diff = ev_now - now; |
466 | |
533 | |
467 | if (pipe (sigpipe)) |
534 | if (pipe (sigpipe)) |
468 | return 0; |
535 | return 0; |
469 | |
536 | |
|
|
537 | if (methods == EVMETHOD_AUTO) |
|
|
538 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
539 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
540 | else |
470 | ev_method = EVMETHOD_NONE; |
541 | methods = EVMETHOD_ANY; |
|
|
542 | |
|
|
543 | ev_method = 0; |
471 | #if EV_USE_EPOLL |
544 | #if EV_USE_EPOLL |
472 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
545 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
546 | #endif |
|
|
547 | #if EV_USE_POLL |
|
|
548 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
473 | #endif |
549 | #endif |
474 | #if EV_USE_SELECT |
550 | #if EV_USE_SELECT |
475 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
551 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
476 | #endif |
552 | #endif |
477 | |
553 | |
478 | if (ev_method) |
554 | if (ev_method) |
479 | { |
555 | { |
480 | ev_watcher_init (&sigev, sigcb); |
556 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
489 | } |
565 | } |
490 | |
566 | |
491 | /*****************************************************************************/ |
567 | /*****************************************************************************/ |
492 | |
568 | |
493 | void |
569 | void |
494 | ev_prefork (void) |
570 | ev_fork_prepare (void) |
495 | { |
571 | { |
496 | /* nop */ |
572 | /* nop */ |
497 | } |
573 | } |
498 | |
574 | |
499 | void |
575 | void |
500 | ev_postfork_parent (void) |
576 | ev_fork_parent (void) |
501 | { |
577 | { |
502 | /* nop */ |
578 | /* nop */ |
503 | } |
579 | } |
504 | |
580 | |
505 | void |
581 | void |
506 | ev_postfork_child (void) |
582 | ev_fork_child (void) |
507 | { |
583 | { |
508 | #if EV_USE_EPOLL |
584 | #if EV_USE_EPOLL |
509 | if (ev_method == EVMETHOD_EPOLL) |
585 | if (ev_method == EVMETHOD_EPOLL) |
510 | epoll_postfork_child (); |
586 | epoll_postfork_child (); |
511 | #endif |
587 | #endif |
… | |
… | |
520 | /*****************************************************************************/ |
596 | /*****************************************************************************/ |
521 | |
597 | |
522 | static void |
598 | static void |
523 | call_pending (void) |
599 | call_pending (void) |
524 | { |
600 | { |
|
|
601 | int pri; |
|
|
602 | |
|
|
603 | for (pri = NUMPRI; pri--; ) |
525 | while (pendingcnt) |
604 | while (pendingcnt [pri]) |
526 | { |
605 | { |
527 | ANPENDING *p = pendings + --pendingcnt; |
606 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
528 | |
607 | |
529 | if (p->w) |
608 | if (p->w) |
530 | { |
609 | { |
531 | p->w->pending = 0; |
610 | p->w->pending = 0; |
532 | p->w->cb (p->w, p->events); |
611 | p->w->cb (p->w, p->events); |
533 | } |
612 | } |
534 | } |
613 | } |
535 | } |
614 | } |
536 | |
615 | |
537 | static void |
616 | static void |
538 | timers_reify (void) |
617 | timers_reify (void) |
539 | { |
618 | { |
… | |
… | |
599 | } |
678 | } |
600 | } |
679 | } |
601 | } |
680 | } |
602 | } |
681 | } |
603 | |
682 | |
|
|
683 | static int |
|
|
684 | time_update_monotonic (void) |
|
|
685 | { |
|
|
686 | now = get_clock (); |
|
|
687 | |
|
|
688 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
689 | { |
|
|
690 | ev_now = now + diff; |
|
|
691 | return 0; |
|
|
692 | } |
|
|
693 | else |
|
|
694 | { |
|
|
695 | now_floor = now; |
|
|
696 | ev_now = ev_time (); |
|
|
697 | return 1; |
|
|
698 | } |
|
|
699 | } |
|
|
700 | |
604 | static void |
701 | static void |
605 | time_update (void) |
702 | time_update (void) |
606 | { |
703 | { |
607 | int i; |
704 | int i; |
608 | |
705 | |
609 | ev_now = ev_time (); |
706 | #if EV_USE_MONOTONIC |
610 | |
|
|
611 | if (have_monotonic) |
707 | if (expect_true (have_monotonic)) |
612 | { |
708 | { |
|
|
709 | if (time_update_monotonic ()) |
|
|
710 | { |
613 | ev_tstamp odiff = diff; |
711 | ev_tstamp odiff = diff; |
614 | |
712 | |
615 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
713 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
616 | { |
714 | { |
617 | now = get_clock (); |
|
|
618 | diff = ev_now - now; |
715 | diff = ev_now - now; |
619 | |
716 | |
620 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
717 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
621 | return; /* all is well */ |
718 | return; /* all is well */ |
622 | |
719 | |
623 | ev_now = ev_time (); |
720 | ev_now = ev_time (); |
|
|
721 | now = get_clock (); |
|
|
722 | now_floor = now; |
624 | } |
723 | } |
625 | |
724 | |
626 | periodics_reschedule (diff - odiff); |
725 | periodics_reschedule (diff - odiff); |
627 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
726 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
727 | } |
628 | } |
728 | } |
629 | else |
729 | else |
|
|
730 | #endif |
630 | { |
731 | { |
|
|
732 | ev_now = ev_time (); |
|
|
733 | |
631 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
734 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
632 | { |
735 | { |
633 | periodics_reschedule (ev_now - now); |
736 | periodics_reschedule (ev_now - now); |
634 | |
737 | |
635 | /* adjust timers. this is easy, as the offset is the same for all */ |
738 | /* adjust timers. this is easy, as the offset is the same for all */ |
636 | for (i = 0; i < timercnt; ++i) |
739 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
649 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
752 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
650 | |
753 | |
651 | do |
754 | do |
652 | { |
755 | { |
653 | /* queue check watchers (and execute them) */ |
756 | /* queue check watchers (and execute them) */ |
654 | if (preparecnt) |
757 | if (expect_false (preparecnt)) |
655 | { |
758 | { |
656 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
759 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
657 | call_pending (); |
760 | call_pending (); |
658 | } |
761 | } |
659 | |
762 | |
… | |
… | |
662 | |
765 | |
663 | /* calculate blocking time */ |
766 | /* calculate blocking time */ |
664 | |
767 | |
665 | /* we only need this for !monotonic clockor timers, but as we basically |
768 | /* we only need this for !monotonic clockor timers, but as we basically |
666 | always have timers, we just calculate it always */ |
769 | always have timers, we just calculate it always */ |
|
|
770 | #if EV_USE_MONOTONIC |
|
|
771 | if (expect_true (have_monotonic)) |
|
|
772 | time_update_monotonic (); |
|
|
773 | else |
|
|
774 | #endif |
|
|
775 | { |
667 | ev_now = ev_time (); |
776 | ev_now = ev_time (); |
|
|
777 | now = ev_now; |
|
|
778 | } |
668 | |
779 | |
669 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
780 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
670 | block = 0.; |
781 | block = 0.; |
671 | else |
782 | else |
672 | { |
783 | { |
673 | block = MAX_BLOCKTIME; |
784 | block = MAX_BLOCKTIME; |
674 | |
785 | |
675 | if (timercnt) |
786 | if (timercnt) |
676 | { |
787 | { |
677 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
788 | ev_tstamp to = timers [0]->at - now + method_fudge; |
678 | if (block > to) block = to; |
789 | if (block > to) block = to; |
679 | } |
790 | } |
680 | |
791 | |
681 | if (periodiccnt) |
792 | if (periodiccnt) |
682 | { |
793 | { |
… | |
… | |
739 | static void |
850 | static void |
740 | ev_clear_pending (W w) |
851 | ev_clear_pending (W w) |
741 | { |
852 | { |
742 | if (w->pending) |
853 | if (w->pending) |
743 | { |
854 | { |
744 | pendings [w->pending - 1].w = 0; |
855 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
745 | w->pending = 0; |
856 | w->pending = 0; |
746 | } |
857 | } |
747 | } |
858 | } |
748 | |
859 | |
749 | static void |
860 | static void |
… | |
… | |
761 | /*****************************************************************************/ |
872 | /*****************************************************************************/ |
762 | |
873 | |
763 | void |
874 | void |
764 | ev_io_start (struct ev_io *w) |
875 | ev_io_start (struct ev_io *w) |
765 | { |
876 | { |
|
|
877 | int fd = w->fd; |
|
|
878 | |
766 | if (ev_is_active (w)) |
879 | if (ev_is_active (w)) |
767 | return; |
880 | return; |
768 | |
|
|
769 | int fd = w->fd; |
|
|
770 | |
881 | |
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
882 | assert (("ev_io_start called with negative fd", fd >= 0)); |
772 | |
883 | |
773 | ev_start ((W)w, 1); |
884 | ev_start ((W)w, 1); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
885 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |