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 |
… | |
… | |
59 | #ifndef EV_USE_EPOLL |
61 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
62 | # define EV_USE_EPOLL 0 |
61 | #endif |
63 | #endif |
62 | |
64 | |
63 | #ifndef EV_USE_REALTIME |
65 | #ifndef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
66 | # define EV_USE_REALTIME 1 |
65 | #endif |
67 | #endif |
|
|
68 | |
|
|
69 | /**/ |
|
|
70 | |
|
|
71 | #ifndef CLOCK_MONOTONIC |
|
|
72 | # undef EV_USE_MONOTONIC |
|
|
73 | # define EV_USE_MONOTONIC 0 |
|
|
74 | #endif |
|
|
75 | |
|
|
76 | #ifndef CLOCK_REALTIME |
|
|
77 | # undef EV_USE_REALTIME |
|
|
78 | # define EV_USE_REALTIME 0 |
|
|
79 | #endif |
|
|
80 | |
|
|
81 | /**/ |
66 | |
82 | |
67 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
83 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
68 | #define MAX_BLOCKTIME 59.731 |
84 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
69 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
85 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
86 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
70 | |
87 | |
71 | #include "ev.h" |
88 | #include "ev.h" |
|
|
89 | |
|
|
90 | #if __GNUC__ >= 3 |
|
|
91 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
92 | # define inline inline |
|
|
93 | #else |
|
|
94 | # define expect(expr,value) (expr) |
|
|
95 | # define inline static |
|
|
96 | #endif |
|
|
97 | |
|
|
98 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
99 | #define expect_true(expr) expect ((expr) != 0, 1) |
72 | |
100 | |
73 | typedef struct ev_watcher *W; |
101 | typedef struct ev_watcher *W; |
74 | typedef struct ev_watcher_list *WL; |
102 | typedef struct ev_watcher_list *WL; |
75 | typedef struct ev_watcher_time *WT; |
103 | typedef struct ev_watcher_time *WT; |
76 | |
104 | |
77 | static ev_tstamp now, diff; /* monotonic clock */ |
105 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
78 | ev_tstamp ev_now; |
106 | ev_tstamp ev_now; |
79 | int ev_method; |
107 | int ev_method; |
80 | |
108 | |
81 | static int have_monotonic; /* runtime */ |
109 | static int have_monotonic; /* runtime */ |
82 | |
110 | |
… | |
… | |
102 | |
130 | |
103 | static ev_tstamp |
131 | static ev_tstamp |
104 | get_clock (void) |
132 | get_clock (void) |
105 | { |
133 | { |
106 | #if EV_USE_MONOTONIC |
134 | #if EV_USE_MONOTONIC |
107 | if (have_monotonic) |
135 | if (expect_true (have_monotonic)) |
108 | { |
136 | { |
109 | struct timespec ts; |
137 | struct timespec ts; |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
138 | clock_gettime (CLOCK_MONOTONIC, &ts); |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
139 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
112 | } |
140 | } |
… | |
… | |
116 | } |
144 | } |
117 | |
145 | |
118 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
146 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
119 | |
147 | |
120 | #define array_needsize(base,cur,cnt,init) \ |
148 | #define array_needsize(base,cur,cnt,init) \ |
121 | if ((cnt) > cur) \ |
149 | if (expect_false ((cnt) > cur)) \ |
122 | { \ |
150 | { \ |
123 | int newcnt = cur; \ |
151 | int newcnt = cur; \ |
124 | do \ |
152 | do \ |
125 | { \ |
153 | { \ |
126 | newcnt = array_roundsize (base, newcnt << 1); \ |
154 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
135 | /*****************************************************************************/ |
163 | /*****************************************************************************/ |
136 | |
164 | |
137 | typedef struct |
165 | typedef struct |
138 | { |
166 | { |
139 | struct ev_io *head; |
167 | struct ev_io *head; |
140 | int events; |
168 | unsigned char events; |
|
|
169 | unsigned char reify; |
141 | } ANFD; |
170 | } ANFD; |
142 | |
171 | |
143 | static ANFD *anfds; |
172 | static ANFD *anfds; |
144 | static int anfdmax; |
173 | static int anfdmax; |
145 | |
174 | |
… | |
… | |
148 | { |
177 | { |
149 | while (count--) |
178 | while (count--) |
150 | { |
179 | { |
151 | base->head = 0; |
180 | base->head = 0; |
152 | base->events = EV_NONE; |
181 | base->events = EV_NONE; |
|
|
182 | base->reify = 0; |
|
|
183 | |
153 | ++base; |
184 | ++base; |
154 | } |
185 | } |
155 | } |
186 | } |
156 | |
187 | |
157 | typedef struct |
188 | typedef struct |
… | |
… | |
164 | static int pendingmax, pendingcnt; |
195 | static int pendingmax, pendingcnt; |
165 | |
196 | |
166 | static void |
197 | static void |
167 | event (W w, int events) |
198 | event (W w, int events) |
168 | { |
199 | { |
|
|
200 | if (w->pending) |
|
|
201 | { |
|
|
202 | pendings [w->pending - 1].events |= events; |
|
|
203 | return; |
|
|
204 | } |
|
|
205 | |
169 | w->pending = ++pendingcnt; |
206 | w->pending = ++pendingcnt; |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
207 | array_needsize (pendings, pendingmax, pendingcnt, ); |
171 | pendings [pendingcnt - 1].w = w; |
208 | pendings [pendingcnt - 1].w = w; |
172 | pendings [pendingcnt - 1].events = events; |
209 | pendings [pendingcnt - 1].events = events; |
173 | } |
210 | } |
… | |
… | |
215 | int events = 0; |
252 | int events = 0; |
216 | |
253 | |
217 | for (w = anfd->head; w; w = w->next) |
254 | for (w = anfd->head; w; w = w->next) |
218 | events |= w->events; |
255 | events |= w->events; |
219 | |
256 | |
220 | anfd->events &= ~EV_REIFY; |
257 | anfd->reify = 0; |
221 | |
258 | |
222 | if (anfd->events != events) |
259 | if (anfd->events != events) |
223 | { |
260 | { |
224 | method_modify (fd, anfd->events, events); |
261 | method_modify (fd, anfd->events, events); |
225 | anfd->events = events; |
262 | anfd->events = events; |
… | |
… | |
230 | } |
267 | } |
231 | |
268 | |
232 | static void |
269 | static void |
233 | fd_change (int fd) |
270 | fd_change (int fd) |
234 | { |
271 | { |
235 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
272 | if (anfds [fd].reify || fdchangecnt < 0) |
236 | return; |
273 | return; |
237 | |
274 | |
238 | anfds [fd].events |= EV_REIFY; |
275 | anfds [fd].reify = 1; |
239 | |
276 | |
240 | ++fdchangecnt; |
277 | ++fdchangecnt; |
241 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
278 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
242 | fdchanges [fdchangecnt - 1] = fd; |
279 | fdchanges [fdchangecnt - 1] = fd; |
243 | } |
280 | } |
… | |
… | |
251 | for (fd = 0; fd < anfdmax; ++fd) |
288 | for (fd = 0; fd < anfdmax; ++fd) |
252 | if (anfds [fd].events) |
289 | if (anfds [fd].events) |
253 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
290 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
254 | while (anfds [fd].head) |
291 | while (anfds [fd].head) |
255 | { |
292 | { |
256 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
|
|
257 | ev_io_stop (anfds [fd].head); |
293 | ev_io_stop (anfds [fd].head); |
|
|
294 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
258 | } |
295 | } |
259 | } |
296 | } |
260 | |
297 | |
261 | /*****************************************************************************/ |
298 | /*****************************************************************************/ |
262 | |
299 | |
… | |
… | |
310 | /*****************************************************************************/ |
347 | /*****************************************************************************/ |
311 | |
348 | |
312 | typedef struct |
349 | typedef struct |
313 | { |
350 | { |
314 | struct ev_signal *head; |
351 | struct ev_signal *head; |
315 | sig_atomic_t gotsig; |
352 | sig_atomic_t volatile gotsig; |
316 | } ANSIG; |
353 | } ANSIG; |
317 | |
354 | |
318 | static ANSIG *signals; |
355 | static ANSIG *signals; |
319 | static int signalmax; |
356 | static int signalmax; |
320 | |
357 | |
321 | static int sigpipe [2]; |
358 | static int sigpipe [2]; |
322 | static sig_atomic_t gotsig; |
359 | static sig_atomic_t volatile gotsig; |
323 | static struct ev_io sigev; |
360 | static struct ev_io sigev; |
324 | |
361 | |
325 | static void |
362 | static void |
326 | signals_init (ANSIG *base, int count) |
363 | signals_init (ANSIG *base, int count) |
327 | { |
364 | { |
328 | while (count--) |
365 | while (count--) |
329 | { |
366 | { |
330 | base->head = 0; |
367 | base->head = 0; |
331 | base->gotsig = 0; |
368 | base->gotsig = 0; |
|
|
369 | |
332 | ++base; |
370 | ++base; |
333 | } |
371 | } |
334 | } |
372 | } |
335 | |
373 | |
336 | static void |
374 | static void |
… | |
… | |
339 | signals [signum - 1].gotsig = 1; |
377 | signals [signum - 1].gotsig = 1; |
340 | |
378 | |
341 | if (!gotsig) |
379 | if (!gotsig) |
342 | { |
380 | { |
343 | gotsig = 1; |
381 | gotsig = 1; |
344 | write (sigpipe [1], &gotsig, 1); |
382 | write (sigpipe [1], &signum, 1); |
345 | } |
383 | } |
346 | } |
384 | } |
347 | |
385 | |
348 | static void |
386 | static void |
349 | sigcb (struct ev_io *iow, int revents) |
387 | sigcb (struct ev_io *iow, int revents) |
350 | { |
388 | { |
351 | struct ev_signal *w; |
389 | struct ev_signal *w; |
352 | int sig; |
390 | int signum; |
353 | |
391 | |
|
|
392 | read (sigpipe [0], &revents, 1); |
354 | gotsig = 0; |
393 | gotsig = 0; |
355 | read (sigpipe [0], &revents, 1); |
|
|
356 | |
394 | |
357 | for (sig = signalmax; sig--; ) |
395 | for (signum = signalmax; signum--; ) |
358 | if (signals [sig].gotsig) |
396 | if (signals [signum].gotsig) |
359 | { |
397 | { |
360 | signals [sig].gotsig = 0; |
398 | signals [signum].gotsig = 0; |
361 | |
399 | |
362 | for (w = signals [sig].head; w; w = w->next) |
400 | for (w = signals [signum].head; w; w = w->next) |
363 | event ((W)w, EV_SIGNAL); |
401 | event ((W)w, EV_SIGNAL); |
364 | } |
402 | } |
365 | } |
403 | } |
366 | |
404 | |
367 | static void |
405 | static void |
… | |
… | |
404 | struct ev_child *w; |
442 | struct ev_child *w; |
405 | int pid, status; |
443 | int pid, status; |
406 | |
444 | |
407 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
445 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
408 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
446 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
409 | if (w->pid == pid || w->pid == -1) |
447 | if (w->pid == pid || !w->pid) |
410 | { |
448 | { |
411 | w->status = status; |
449 | w->status = status; |
412 | event ((W)w, EV_CHILD); |
450 | event ((W)w, EV_CHILD); |
413 | } |
451 | } |
414 | } |
452 | } |
… | |
… | |
444 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
482 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
445 | have_monotonic = 1; |
483 | have_monotonic = 1; |
446 | } |
484 | } |
447 | #endif |
485 | #endif |
448 | |
486 | |
449 | ev_now = ev_time (); |
487 | ev_now = ev_time (); |
450 | now = get_clock (); |
488 | now = get_clock (); |
|
|
489 | now_floor = now; |
451 | diff = ev_now - now; |
490 | diff = ev_now - now; |
452 | |
491 | |
453 | if (pipe (sigpipe)) |
492 | if (pipe (sigpipe)) |
454 | return 0; |
493 | return 0; |
455 | |
494 | |
456 | ev_method = EVMETHOD_NONE; |
495 | ev_method = EVMETHOD_NONE; |
… | |
… | |
475 | } |
514 | } |
476 | |
515 | |
477 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
478 | |
517 | |
479 | void |
518 | void |
480 | ev_prefork (void) |
519 | ev_fork_prepare (void) |
481 | { |
520 | { |
482 | /* nop */ |
521 | /* nop */ |
483 | } |
522 | } |
484 | |
523 | |
485 | void |
524 | void |
486 | ev_postfork_parent (void) |
525 | ev_fork_parent (void) |
487 | { |
526 | { |
488 | /* nop */ |
527 | /* nop */ |
489 | } |
528 | } |
490 | |
529 | |
491 | void |
530 | void |
492 | ev_postfork_child (void) |
531 | ev_fork_child (void) |
493 | { |
532 | { |
494 | #if EV_USE_EPOLL |
533 | #if EV_USE_EPOLL |
495 | if (ev_method == EVMETHOD_EPOLL) |
534 | if (ev_method == EVMETHOD_EPOLL) |
496 | epoll_postfork_child (); |
535 | epoll_postfork_child (); |
497 | #endif |
536 | #endif |
… | |
… | |
528 | struct ev_timer *w = timers [0]; |
567 | struct ev_timer *w = timers [0]; |
529 | |
568 | |
530 | /* first reschedule or stop timer */ |
569 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
570 | if (w->repeat) |
532 | { |
571 | { |
|
|
572 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
573 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
574 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
575 | } |
537 | else |
576 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
577 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
539 | |
578 | |
… | |
… | |
550 | |
589 | |
551 | /* first reschedule or stop timer */ |
590 | /* first reschedule or stop timer */ |
552 | if (w->interval) |
591 | if (w->interval) |
553 | { |
592 | { |
554 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
593 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
555 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
594 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
556 | downheap ((WT *)periodics, periodiccnt, 0); |
595 | downheap ((WT *)periodics, periodiccnt, 0); |
557 | } |
596 | } |
558 | else |
597 | else |
559 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
598 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
560 | |
599 | |
561 | event ((W)w, EV_TIMEOUT); |
600 | event ((W)w, EV_PERIODIC); |
562 | } |
601 | } |
563 | } |
602 | } |
564 | |
603 | |
565 | static void |
604 | static void |
566 | periodics_reschedule (ev_tstamp diff) |
605 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
585 | } |
624 | } |
586 | } |
625 | } |
587 | } |
626 | } |
588 | } |
627 | } |
589 | |
628 | |
|
|
629 | static int |
|
|
630 | time_update_monotonic (void) |
|
|
631 | { |
|
|
632 | now = get_clock (); |
|
|
633 | |
|
|
634 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
635 | { |
|
|
636 | ev_now = now + diff; |
|
|
637 | return 0; |
|
|
638 | } |
|
|
639 | else |
|
|
640 | { |
|
|
641 | now_floor = now; |
|
|
642 | ev_now = ev_time (); |
|
|
643 | return 1; |
|
|
644 | } |
|
|
645 | } |
|
|
646 | |
590 | static void |
647 | static void |
591 | time_update (void) |
648 | time_update (void) |
592 | { |
649 | { |
593 | int i; |
650 | int i; |
594 | |
651 | |
595 | ev_now = ev_time (); |
652 | #if EV_USE_MONOTONIC |
596 | |
|
|
597 | if (have_monotonic) |
653 | if (expect_true (have_monotonic)) |
598 | { |
654 | { |
|
|
655 | if (time_update_monotonic ()) |
|
|
656 | { |
599 | ev_tstamp odiff = diff; |
657 | ev_tstamp odiff = diff; |
600 | |
658 | |
601 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
659 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
602 | { |
660 | { |
603 | now = get_clock (); |
|
|
604 | diff = ev_now - now; |
661 | diff = ev_now - now; |
605 | |
662 | |
606 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
663 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
607 | return; /* all is well */ |
664 | return; /* all is well */ |
608 | |
665 | |
609 | ev_now = ev_time (); |
666 | ev_now = ev_time (); |
|
|
667 | now = get_clock (); |
|
|
668 | now_floor = now; |
610 | } |
669 | } |
611 | |
670 | |
612 | periodics_reschedule (diff - odiff); |
671 | periodics_reschedule (diff - odiff); |
613 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
672 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
673 | } |
614 | } |
674 | } |
615 | else |
675 | else |
|
|
676 | #endif |
616 | { |
677 | { |
|
|
678 | ev_now = ev_time (); |
|
|
679 | |
617 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
680 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
618 | { |
681 | { |
619 | periodics_reschedule (ev_now - now); |
682 | periodics_reschedule (ev_now - now); |
620 | |
683 | |
621 | /* adjust timers. this is easy, as the offset is the same for all */ |
684 | /* adjust timers. this is easy, as the offset is the same for all */ |
622 | for (i = 0; i < timercnt; ++i) |
685 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
635 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
698 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
636 | |
699 | |
637 | do |
700 | do |
638 | { |
701 | { |
639 | /* queue check watchers (and execute them) */ |
702 | /* queue check watchers (and execute them) */ |
640 | if (preparecnt) |
703 | if (expect_false (preparecnt)) |
641 | { |
704 | { |
642 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
705 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
643 | call_pending (); |
706 | call_pending (); |
644 | } |
707 | } |
645 | |
708 | |
… | |
… | |
648 | |
711 | |
649 | /* calculate blocking time */ |
712 | /* calculate blocking time */ |
650 | |
713 | |
651 | /* we only need this for !monotonic clockor timers, but as we basically |
714 | /* we only need this for !monotonic clockor timers, but as we basically |
652 | always have timers, we just calculate it always */ |
715 | always have timers, we just calculate it always */ |
|
|
716 | #if EV_USE_MONOTONIC |
|
|
717 | if (expect_true (have_monotonic)) |
|
|
718 | time_update_monotonic (); |
|
|
719 | else |
|
|
720 | #endif |
|
|
721 | { |
653 | ev_now = ev_time (); |
722 | ev_now = ev_time (); |
|
|
723 | now = ev_now; |
|
|
724 | } |
654 | |
725 | |
655 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
726 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
656 | block = 0.; |
727 | block = 0.; |
657 | else |
728 | else |
658 | { |
729 | { |
659 | block = MAX_BLOCKTIME; |
730 | block = MAX_BLOCKTIME; |
660 | |
731 | |
661 | if (timercnt) |
732 | if (timercnt) |
662 | { |
733 | { |
663 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
734 | ev_tstamp to = timers [0]->at - now + method_fudge; |
664 | if (block > to) block = to; |
735 | if (block > to) block = to; |
665 | } |
736 | } |
666 | |
737 | |
667 | if (periodiccnt) |
738 | if (periodiccnt) |
668 | { |
739 | { |
… | |
… | |
721 | head = &(*head)->next; |
792 | head = &(*head)->next; |
722 | } |
793 | } |
723 | } |
794 | } |
724 | |
795 | |
725 | static void |
796 | static void |
726 | ev_clear (W w) |
797 | ev_clear_pending (W w) |
727 | { |
798 | { |
728 | if (w->pending) |
799 | if (w->pending) |
729 | { |
800 | { |
730 | pendings [w->pending - 1].w = 0; |
801 | pendings [w->pending - 1].w = 0; |
731 | w->pending = 0; |
802 | w->pending = 0; |
… | |
… | |
747 | /*****************************************************************************/ |
818 | /*****************************************************************************/ |
748 | |
819 | |
749 | void |
820 | void |
750 | ev_io_start (struct ev_io *w) |
821 | ev_io_start (struct ev_io *w) |
751 | { |
822 | { |
|
|
823 | int fd = w->fd; |
|
|
824 | |
752 | if (ev_is_active (w)) |
825 | if (ev_is_active (w)) |
753 | return; |
826 | return; |
754 | |
827 | |
755 | int fd = w->fd; |
828 | assert (("ev_io_start called with negative fd", fd >= 0)); |
756 | |
829 | |
757 | ev_start ((W)w, 1); |
830 | ev_start ((W)w, 1); |
758 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
831 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
759 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
832 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
760 | |
833 | |
… | |
… | |
762 | } |
835 | } |
763 | |
836 | |
764 | void |
837 | void |
765 | ev_io_stop (struct ev_io *w) |
838 | ev_io_stop (struct ev_io *w) |
766 | { |
839 | { |
767 | ev_clear ((W)w); |
840 | ev_clear_pending ((W)w); |
768 | if (!ev_is_active (w)) |
841 | if (!ev_is_active (w)) |
769 | return; |
842 | return; |
770 | |
843 | |
771 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
844 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
772 | ev_stop ((W)w); |
845 | ev_stop ((W)w); |
… | |
… | |
780 | if (ev_is_active (w)) |
853 | if (ev_is_active (w)) |
781 | return; |
854 | return; |
782 | |
855 | |
783 | w->at += now; |
856 | w->at += now; |
784 | |
857 | |
785 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
858 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
786 | |
859 | |
787 | ev_start ((W)w, ++timercnt); |
860 | ev_start ((W)w, ++timercnt); |
788 | array_needsize (timers, timermax, timercnt, ); |
861 | array_needsize (timers, timermax, timercnt, ); |
789 | timers [timercnt - 1] = w; |
862 | timers [timercnt - 1] = w; |
790 | upheap ((WT *)timers, timercnt - 1); |
863 | upheap ((WT *)timers, timercnt - 1); |
791 | } |
864 | } |
792 | |
865 | |
793 | void |
866 | void |
794 | ev_timer_stop (struct ev_timer *w) |
867 | ev_timer_stop (struct ev_timer *w) |
795 | { |
868 | { |
796 | ev_clear ((W)w); |
869 | ev_clear_pending ((W)w); |
797 | if (!ev_is_active (w)) |
870 | if (!ev_is_active (w)) |
798 | return; |
871 | return; |
799 | |
872 | |
800 | if (w->active < timercnt--) |
873 | if (w->active < timercnt--) |
801 | { |
874 | { |
… | |
… | |
829 | ev_periodic_start (struct ev_periodic *w) |
902 | ev_periodic_start (struct ev_periodic *w) |
830 | { |
903 | { |
831 | if (ev_is_active (w)) |
904 | if (ev_is_active (w)) |
832 | return; |
905 | return; |
833 | |
906 | |
834 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
907 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
835 | |
908 | |
836 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
909 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
837 | if (w->interval) |
910 | if (w->interval) |
838 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
911 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
839 | |
912 | |
… | |
… | |
844 | } |
917 | } |
845 | |
918 | |
846 | void |
919 | void |
847 | ev_periodic_stop (struct ev_periodic *w) |
920 | ev_periodic_stop (struct ev_periodic *w) |
848 | { |
921 | { |
849 | ev_clear ((W)w); |
922 | ev_clear_pending ((W)w); |
850 | if (!ev_is_active (w)) |
923 | if (!ev_is_active (w)) |
851 | return; |
924 | return; |
852 | |
925 | |
853 | if (w->active < periodiccnt--) |
926 | if (w->active < periodiccnt--) |
854 | { |
927 | { |
… | |
… | |
862 | void |
935 | void |
863 | ev_signal_start (struct ev_signal *w) |
936 | ev_signal_start (struct ev_signal *w) |
864 | { |
937 | { |
865 | if (ev_is_active (w)) |
938 | if (ev_is_active (w)) |
866 | return; |
939 | return; |
|
|
940 | |
|
|
941 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
867 | |
942 | |
868 | ev_start ((W)w, 1); |
943 | ev_start ((W)w, 1); |
869 | array_needsize (signals, signalmax, w->signum, signals_init); |
944 | array_needsize (signals, signalmax, w->signum, signals_init); |
870 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
945 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
871 | |
946 | |
… | |
… | |
880 | } |
955 | } |
881 | |
956 | |
882 | void |
957 | void |
883 | ev_signal_stop (struct ev_signal *w) |
958 | ev_signal_stop (struct ev_signal *w) |
884 | { |
959 | { |
885 | ev_clear ((W)w); |
960 | ev_clear_pending ((W)w); |
886 | if (!ev_is_active (w)) |
961 | if (!ev_is_active (w)) |
887 | return; |
962 | return; |
888 | |
963 | |
889 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
964 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
890 | ev_stop ((W)w); |
965 | ev_stop ((W)w); |
… | |
… | |
905 | } |
980 | } |
906 | |
981 | |
907 | void |
982 | void |
908 | ev_idle_stop (struct ev_idle *w) |
983 | ev_idle_stop (struct ev_idle *w) |
909 | { |
984 | { |
910 | ev_clear ((W)w); |
985 | ev_clear_pending ((W)w); |
911 | if (ev_is_active (w)) |
986 | if (ev_is_active (w)) |
912 | return; |
987 | return; |
913 | |
988 | |
914 | idles [w->active - 1] = idles [--idlecnt]; |
989 | idles [w->active - 1] = idles [--idlecnt]; |
915 | ev_stop ((W)w); |
990 | ev_stop ((W)w); |
… | |
… | |
927 | } |
1002 | } |
928 | |
1003 | |
929 | void |
1004 | void |
930 | ev_prepare_stop (struct ev_prepare *w) |
1005 | ev_prepare_stop (struct ev_prepare *w) |
931 | { |
1006 | { |
932 | ev_clear ((W)w); |
1007 | ev_clear_pending ((W)w); |
933 | if (ev_is_active (w)) |
1008 | if (ev_is_active (w)) |
934 | return; |
1009 | return; |
935 | |
1010 | |
936 | prepares [w->active - 1] = prepares [--preparecnt]; |
1011 | prepares [w->active - 1] = prepares [--preparecnt]; |
937 | ev_stop ((W)w); |
1012 | ev_stop ((W)w); |
… | |
… | |
949 | } |
1024 | } |
950 | |
1025 | |
951 | void |
1026 | void |
952 | ev_check_stop (struct ev_check *w) |
1027 | ev_check_stop (struct ev_check *w) |
953 | { |
1028 | { |
954 | ev_clear ((W)w); |
1029 | ev_clear_pending ((W)w); |
955 | if (ev_is_active (w)) |
1030 | if (ev_is_active (w)) |
956 | return; |
1031 | return; |
957 | |
1032 | |
958 | checks [w->active - 1] = checks [--checkcnt]; |
1033 | checks [w->active - 1] = checks [--checkcnt]; |
959 | ev_stop ((W)w); |
1034 | ev_stop ((W)w); |
… | |
… | |
970 | } |
1045 | } |
971 | |
1046 | |
972 | void |
1047 | void |
973 | ev_child_stop (struct ev_child *w) |
1048 | ev_child_stop (struct ev_child *w) |
974 | { |
1049 | { |
975 | ev_clear ((W)w); |
1050 | ev_clear_pending ((W)w); |
976 | if (ev_is_active (w)) |
1051 | if (ev_is_active (w)) |
977 | return; |
1052 | return; |
978 | |
1053 | |
979 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1054 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
980 | ev_stop ((W)w); |
1055 | ev_stop ((W)w); |