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 | |
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 | |
|
|
65 | #ifndef EV_USE_REALTIME |
|
|
66 | # define EV_USE_REALTIME 1 |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | /**/ |
|
|
70 | |
|
|
71 | #ifndef CLOCK_MONOTONIC |
|
|
72 | # undef EV_USE_MONOTONIC |
|
|
73 | # define EV_USE_MONOTONIC 0 |
|
|
74 | #endif |
|
|
75 | |
63 | #ifndef CLOCK_REALTIME |
76 | #ifndef CLOCK_REALTIME |
|
|
77 | # undef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 0 |
78 | # define EV_USE_REALTIME 0 |
65 | #endif |
79 | #endif |
66 | #ifndef EV_USE_REALTIME |
80 | |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
81 | /**/ |
68 | #endif |
|
|
69 | |
82 | |
70 | #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) */ |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
84 | #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 */ |
85 | #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 */ |
86 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
74 | |
87 | |
75 | #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) |
76 | |
100 | |
77 | typedef struct ev_watcher *W; |
101 | typedef struct ev_watcher *W; |
78 | typedef struct ev_watcher_list *WL; |
102 | typedef struct ev_watcher_list *WL; |
79 | typedef struct ev_watcher_time *WT; |
103 | typedef struct ev_watcher_time *WT; |
80 | |
104 | |
81 | static ev_tstamp now, diff; /* monotonic clock */ |
105 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
82 | ev_tstamp ev_now; |
106 | ev_tstamp ev_now; |
83 | int ev_method; |
107 | int ev_method; |
84 | |
108 | |
85 | static int have_monotonic; /* runtime */ |
109 | static int have_monotonic; /* runtime */ |
86 | |
110 | |
… | |
… | |
106 | |
130 | |
107 | static ev_tstamp |
131 | static ev_tstamp |
108 | get_clock (void) |
132 | get_clock (void) |
109 | { |
133 | { |
110 | #if EV_USE_MONOTONIC |
134 | #if EV_USE_MONOTONIC |
111 | if (have_monotonic) |
135 | if (expect_true (have_monotonic)) |
112 | { |
136 | { |
113 | struct timespec ts; |
137 | struct timespec ts; |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
138 | clock_gettime (CLOCK_MONOTONIC, &ts); |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
139 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
116 | } |
140 | } |
… | |
… | |
120 | } |
144 | } |
121 | |
145 | |
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
146 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
123 | |
147 | |
124 | #define array_needsize(base,cur,cnt,init) \ |
148 | #define array_needsize(base,cur,cnt,init) \ |
125 | if ((cnt) > cur) \ |
149 | if (expect_false ((cnt) > cur)) \ |
126 | { \ |
150 | { \ |
127 | int newcnt = cur; \ |
151 | int newcnt = cur; \ |
128 | do \ |
152 | do \ |
129 | { \ |
153 | { \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
154 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
139 | /*****************************************************************************/ |
163 | /*****************************************************************************/ |
140 | |
164 | |
141 | typedef struct |
165 | typedef struct |
142 | { |
166 | { |
143 | struct ev_io *head; |
167 | struct ev_io *head; |
144 | int events; |
168 | unsigned char events; |
|
|
169 | unsigned char reify; |
145 | } ANFD; |
170 | } ANFD; |
146 | |
171 | |
147 | static ANFD *anfds; |
172 | static ANFD *anfds; |
148 | static int anfdmax; |
173 | static int anfdmax; |
149 | |
174 | |
… | |
… | |
152 | { |
177 | { |
153 | while (count--) |
178 | while (count--) |
154 | { |
179 | { |
155 | base->head = 0; |
180 | base->head = 0; |
156 | base->events = EV_NONE; |
181 | base->events = EV_NONE; |
|
|
182 | base->reify = 0; |
|
|
183 | |
157 | ++base; |
184 | ++base; |
158 | } |
185 | } |
159 | } |
186 | } |
160 | |
187 | |
161 | typedef struct |
188 | typedef struct |
… | |
… | |
225 | int events = 0; |
252 | int events = 0; |
226 | |
253 | |
227 | for (w = anfd->head; w; w = w->next) |
254 | for (w = anfd->head; w; w = w->next) |
228 | events |= w->events; |
255 | events |= w->events; |
229 | |
256 | |
230 | anfd->events &= ~EV_REIFY; |
257 | anfd->reify = 0; |
231 | |
258 | |
232 | if (anfd->events != events) |
259 | if (anfd->events != events) |
233 | { |
260 | { |
234 | method_modify (fd, anfd->events, events); |
261 | method_modify (fd, anfd->events, events); |
235 | anfd->events = events; |
262 | anfd->events = events; |
… | |
… | |
240 | } |
267 | } |
241 | |
268 | |
242 | static void |
269 | static void |
243 | fd_change (int fd) |
270 | fd_change (int fd) |
244 | { |
271 | { |
245 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
272 | if (anfds [fd].reify || fdchangecnt < 0) |
246 | return; |
273 | return; |
247 | |
274 | |
248 | anfds [fd].events |= EV_REIFY; |
275 | anfds [fd].reify = 1; |
249 | |
276 | |
250 | ++fdchangecnt; |
277 | ++fdchangecnt; |
251 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
278 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
252 | fdchanges [fdchangecnt - 1] = fd; |
279 | fdchanges [fdchangecnt - 1] = fd; |
253 | } |
280 | } |
… | |
… | |
262 | if (anfds [fd].events) |
289 | if (anfds [fd].events) |
263 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
290 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
264 | while (anfds [fd].head) |
291 | while (anfds [fd].head) |
265 | { |
292 | { |
266 | ev_io_stop (anfds [fd].head); |
293 | ev_io_stop (anfds [fd].head); |
267 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
294 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
268 | } |
295 | } |
269 | } |
296 | } |
270 | |
297 | |
271 | /*****************************************************************************/ |
298 | /*****************************************************************************/ |
272 | |
299 | |
… | |
… | |
320 | /*****************************************************************************/ |
347 | /*****************************************************************************/ |
321 | |
348 | |
322 | typedef struct |
349 | typedef struct |
323 | { |
350 | { |
324 | struct ev_signal *head; |
351 | struct ev_signal *head; |
325 | sig_atomic_t gotsig; |
352 | sig_atomic_t volatile gotsig; |
326 | } ANSIG; |
353 | } ANSIG; |
327 | |
354 | |
328 | static ANSIG *signals; |
355 | static ANSIG *signals; |
329 | static int signalmax; |
356 | static int signalmax; |
330 | |
357 | |
331 | static int sigpipe [2]; |
358 | static int sigpipe [2]; |
332 | static sig_atomic_t gotsig; |
359 | static sig_atomic_t volatile gotsig; |
333 | static struct ev_io sigev; |
360 | static struct ev_io sigev; |
334 | |
361 | |
335 | static void |
362 | static void |
336 | signals_init (ANSIG *base, int count) |
363 | signals_init (ANSIG *base, int count) |
337 | { |
364 | { |
338 | while (count--) |
365 | while (count--) |
339 | { |
366 | { |
340 | base->head = 0; |
367 | base->head = 0; |
341 | base->gotsig = 0; |
368 | base->gotsig = 0; |
|
|
369 | |
342 | ++base; |
370 | ++base; |
343 | } |
371 | } |
344 | } |
372 | } |
345 | |
373 | |
346 | static void |
374 | static void |
… | |
… | |
349 | signals [signum - 1].gotsig = 1; |
377 | signals [signum - 1].gotsig = 1; |
350 | |
378 | |
351 | if (!gotsig) |
379 | if (!gotsig) |
352 | { |
380 | { |
353 | gotsig = 1; |
381 | gotsig = 1; |
354 | write (sigpipe [1], &gotsig, 1); |
382 | write (sigpipe [1], &signum, 1); |
355 | } |
383 | } |
356 | } |
384 | } |
357 | |
385 | |
358 | static void |
386 | static void |
359 | sigcb (struct ev_io *iow, int revents) |
387 | sigcb (struct ev_io *iow, int revents) |
360 | { |
388 | { |
361 | struct ev_signal *w; |
389 | struct ev_signal *w; |
362 | int sig; |
390 | int signum; |
363 | |
391 | |
|
|
392 | read (sigpipe [0], &revents, 1); |
364 | gotsig = 0; |
393 | gotsig = 0; |
365 | read (sigpipe [0], &revents, 1); |
|
|
366 | |
394 | |
367 | for (sig = signalmax; sig--; ) |
395 | for (signum = signalmax; signum--; ) |
368 | if (signals [sig].gotsig) |
396 | if (signals [signum].gotsig) |
369 | { |
397 | { |
370 | signals [sig].gotsig = 0; |
398 | signals [signum].gotsig = 0; |
371 | |
399 | |
372 | for (w = signals [sig].head; w; w = w->next) |
400 | for (w = signals [signum].head; w; w = w->next) |
373 | event ((W)w, EV_SIGNAL); |
401 | event ((W)w, EV_SIGNAL); |
374 | } |
402 | } |
375 | } |
403 | } |
376 | |
404 | |
377 | static void |
405 | static void |
… | |
… | |
414 | struct ev_child *w; |
442 | struct ev_child *w; |
415 | int pid, status; |
443 | int pid, status; |
416 | |
444 | |
417 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
445 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
418 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
446 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
419 | if (w->pid == pid || w->pid == -1) |
447 | if (w->pid == pid || !w->pid) |
420 | { |
448 | { |
421 | w->status = status; |
449 | w->status = status; |
422 | event ((W)w, EV_CHILD); |
450 | event ((W)w, EV_CHILD); |
423 | } |
451 | } |
424 | } |
452 | } |
… | |
… | |
454 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
482 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
455 | have_monotonic = 1; |
483 | have_monotonic = 1; |
456 | } |
484 | } |
457 | #endif |
485 | #endif |
458 | |
486 | |
459 | ev_now = ev_time (); |
487 | ev_now = ev_time (); |
460 | now = get_clock (); |
488 | now = get_clock (); |
|
|
489 | now_floor = now; |
461 | diff = ev_now - now; |
490 | diff = ev_now - now; |
462 | |
491 | |
463 | if (pipe (sigpipe)) |
492 | if (pipe (sigpipe)) |
464 | return 0; |
493 | return 0; |
465 | |
494 | |
466 | ev_method = EVMETHOD_NONE; |
495 | ev_method = EVMETHOD_NONE; |
… | |
… | |
485 | } |
514 | } |
486 | |
515 | |
487 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
488 | |
517 | |
489 | void |
518 | void |
490 | ev_prefork (void) |
519 | ev_fork_prepare (void) |
491 | { |
520 | { |
492 | /* nop */ |
521 | /* nop */ |
493 | } |
522 | } |
494 | |
523 | |
495 | void |
524 | void |
496 | ev_postfork_parent (void) |
525 | ev_fork_parent (void) |
497 | { |
526 | { |
498 | /* nop */ |
527 | /* nop */ |
499 | } |
528 | } |
500 | |
529 | |
501 | void |
530 | void |
502 | ev_postfork_child (void) |
531 | ev_fork_child (void) |
503 | { |
532 | { |
504 | #if EV_USE_EPOLL |
533 | #if EV_USE_EPOLL |
505 | if (ev_method == EVMETHOD_EPOLL) |
534 | if (ev_method == EVMETHOD_EPOLL) |
506 | epoll_postfork_child (); |
535 | epoll_postfork_child (); |
507 | #endif |
536 | #endif |
… | |
… | |
538 | struct ev_timer *w = timers [0]; |
567 | struct ev_timer *w = timers [0]; |
539 | |
568 | |
540 | /* first reschedule or stop timer */ |
569 | /* first reschedule or stop timer */ |
541 | if (w->repeat) |
570 | if (w->repeat) |
542 | { |
571 | { |
|
|
572 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
543 | w->at = now + w->repeat; |
573 | w->at = now + w->repeat; |
544 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
545 | downheap ((WT *)timers, timercnt, 0); |
574 | downheap ((WT *)timers, timercnt, 0); |
546 | } |
575 | } |
547 | else |
576 | else |
548 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
577 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
549 | |
578 | |
… | |
… | |
560 | |
589 | |
561 | /* first reschedule or stop timer */ |
590 | /* first reschedule or stop timer */ |
562 | if (w->interval) |
591 | if (w->interval) |
563 | { |
592 | { |
564 | 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; |
565 | 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)); |
566 | downheap ((WT *)periodics, periodiccnt, 0); |
595 | downheap ((WT *)periodics, periodiccnt, 0); |
567 | } |
596 | } |
568 | else |
597 | else |
569 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
598 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
570 | |
599 | |
571 | event ((W)w, EV_TIMEOUT); |
600 | event ((W)w, EV_PERIODIC); |
572 | } |
601 | } |
573 | } |
602 | } |
574 | |
603 | |
575 | static void |
604 | static void |
576 | periodics_reschedule (ev_tstamp diff) |
605 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
595 | } |
624 | } |
596 | } |
625 | } |
597 | } |
626 | } |
598 | } |
627 | } |
599 | |
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 | |
600 | static void |
647 | static void |
601 | time_update (void) |
648 | time_update (void) |
602 | { |
649 | { |
603 | int i; |
650 | int i; |
604 | |
651 | |
605 | ev_now = ev_time (); |
652 | #if EV_USE_MONOTONIC |
606 | |
|
|
607 | if (have_monotonic) |
653 | if (expect_true (have_monotonic)) |
608 | { |
654 | { |
|
|
655 | if (time_update_monotonic ()) |
|
|
656 | { |
609 | ev_tstamp odiff = diff; |
657 | ev_tstamp odiff = diff; |
610 | |
658 | |
611 | 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 */ |
612 | { |
660 | { |
613 | now = get_clock (); |
|
|
614 | diff = ev_now - now; |
661 | diff = ev_now - now; |
615 | |
662 | |
616 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
663 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
617 | return; /* all is well */ |
664 | return; /* all is well */ |
618 | |
665 | |
619 | ev_now = ev_time (); |
666 | ev_now = ev_time (); |
|
|
667 | now = get_clock (); |
|
|
668 | now_floor = now; |
620 | } |
669 | } |
621 | |
670 | |
622 | periodics_reschedule (diff - odiff); |
671 | periodics_reschedule (diff - odiff); |
623 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
672 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
673 | } |
624 | } |
674 | } |
625 | else |
675 | else |
|
|
676 | #endif |
626 | { |
677 | { |
|
|
678 | ev_now = ev_time (); |
|
|
679 | |
627 | 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)) |
628 | { |
681 | { |
629 | periodics_reschedule (ev_now - now); |
682 | periodics_reschedule (ev_now - now); |
630 | |
683 | |
631 | /* 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 */ |
632 | for (i = 0; i < timercnt; ++i) |
685 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
645 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
698 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
646 | |
699 | |
647 | do |
700 | do |
648 | { |
701 | { |
649 | /* queue check watchers (and execute them) */ |
702 | /* queue check watchers (and execute them) */ |
650 | if (preparecnt) |
703 | if (expect_false (preparecnt)) |
651 | { |
704 | { |
652 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
705 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
653 | call_pending (); |
706 | call_pending (); |
654 | } |
707 | } |
655 | |
708 | |
… | |
… | |
658 | |
711 | |
659 | /* calculate blocking time */ |
712 | /* calculate blocking time */ |
660 | |
713 | |
661 | /* 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 |
662 | 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 | { |
663 | ev_now = ev_time (); |
722 | ev_now = ev_time (); |
|
|
723 | now = ev_now; |
|
|
724 | } |
664 | |
725 | |
665 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
726 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
666 | block = 0.; |
727 | block = 0.; |
667 | else |
728 | else |
668 | { |
729 | { |
669 | block = MAX_BLOCKTIME; |
730 | block = MAX_BLOCKTIME; |
670 | |
731 | |
671 | if (timercnt) |
732 | if (timercnt) |
672 | { |
733 | { |
673 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
734 | ev_tstamp to = timers [0]->at - now + method_fudge; |
674 | if (block > to) block = to; |
735 | if (block > to) block = to; |
675 | } |
736 | } |
676 | |
737 | |
677 | if (periodiccnt) |
738 | if (periodiccnt) |
678 | { |
739 | { |
… | |
… | |
731 | head = &(*head)->next; |
792 | head = &(*head)->next; |
732 | } |
793 | } |
733 | } |
794 | } |
734 | |
795 | |
735 | static void |
796 | static void |
736 | ev_clear (W w) |
797 | ev_clear_pending (W w) |
737 | { |
798 | { |
738 | if (w->pending) |
799 | if (w->pending) |
739 | { |
800 | { |
740 | pendings [w->pending - 1].w = 0; |
801 | pendings [w->pending - 1].w = 0; |
741 | w->pending = 0; |
802 | w->pending = 0; |
… | |
… | |
757 | /*****************************************************************************/ |
818 | /*****************************************************************************/ |
758 | |
819 | |
759 | void |
820 | void |
760 | ev_io_start (struct ev_io *w) |
821 | ev_io_start (struct ev_io *w) |
761 | { |
822 | { |
|
|
823 | int fd = w->fd; |
|
|
824 | |
762 | if (ev_is_active (w)) |
825 | if (ev_is_active (w)) |
763 | return; |
826 | return; |
764 | |
827 | |
765 | int fd = w->fd; |
828 | assert (("ev_io_start called with negative fd", fd >= 0)); |
766 | |
829 | |
767 | ev_start ((W)w, 1); |
830 | ev_start ((W)w, 1); |
768 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
831 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
769 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
832 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
770 | |
833 | |
… | |
… | |
772 | } |
835 | } |
773 | |
836 | |
774 | void |
837 | void |
775 | ev_io_stop (struct ev_io *w) |
838 | ev_io_stop (struct ev_io *w) |
776 | { |
839 | { |
777 | ev_clear ((W)w); |
840 | ev_clear_pending ((W)w); |
778 | if (!ev_is_active (w)) |
841 | if (!ev_is_active (w)) |
779 | return; |
842 | return; |
780 | |
843 | |
781 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
844 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
782 | ev_stop ((W)w); |
845 | ev_stop ((W)w); |
… | |
… | |
790 | if (ev_is_active (w)) |
853 | if (ev_is_active (w)) |
791 | return; |
854 | return; |
792 | |
855 | |
793 | w->at += now; |
856 | w->at += now; |
794 | |
857 | |
795 | 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.)); |
796 | |
859 | |
797 | ev_start ((W)w, ++timercnt); |
860 | ev_start ((W)w, ++timercnt); |
798 | array_needsize (timers, timermax, timercnt, ); |
861 | array_needsize (timers, timermax, timercnt, ); |
799 | timers [timercnt - 1] = w; |
862 | timers [timercnt - 1] = w; |
800 | upheap ((WT *)timers, timercnt - 1); |
863 | upheap ((WT *)timers, timercnt - 1); |
801 | } |
864 | } |
802 | |
865 | |
803 | void |
866 | void |
804 | ev_timer_stop (struct ev_timer *w) |
867 | ev_timer_stop (struct ev_timer *w) |
805 | { |
868 | { |
806 | ev_clear ((W)w); |
869 | ev_clear_pending ((W)w); |
807 | if (!ev_is_active (w)) |
870 | if (!ev_is_active (w)) |
808 | return; |
871 | return; |
809 | |
872 | |
810 | if (w->active < timercnt--) |
873 | if (w->active < timercnt--) |
811 | { |
874 | { |
… | |
… | |
839 | ev_periodic_start (struct ev_periodic *w) |
902 | ev_periodic_start (struct ev_periodic *w) |
840 | { |
903 | { |
841 | if (ev_is_active (w)) |
904 | if (ev_is_active (w)) |
842 | return; |
905 | return; |
843 | |
906 | |
844 | 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.)); |
845 | |
908 | |
846 | /* 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 */ |
847 | if (w->interval) |
910 | if (w->interval) |
848 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
911 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
849 | |
912 | |
… | |
… | |
854 | } |
917 | } |
855 | |
918 | |
856 | void |
919 | void |
857 | ev_periodic_stop (struct ev_periodic *w) |
920 | ev_periodic_stop (struct ev_periodic *w) |
858 | { |
921 | { |
859 | ev_clear ((W)w); |
922 | ev_clear_pending ((W)w); |
860 | if (!ev_is_active (w)) |
923 | if (!ev_is_active (w)) |
861 | return; |
924 | return; |
862 | |
925 | |
863 | if (w->active < periodiccnt--) |
926 | if (w->active < periodiccnt--) |
864 | { |
927 | { |
… | |
… | |
872 | void |
935 | void |
873 | ev_signal_start (struct ev_signal *w) |
936 | ev_signal_start (struct ev_signal *w) |
874 | { |
937 | { |
875 | if (ev_is_active (w)) |
938 | if (ev_is_active (w)) |
876 | return; |
939 | return; |
|
|
940 | |
|
|
941 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
877 | |
942 | |
878 | ev_start ((W)w, 1); |
943 | ev_start ((W)w, 1); |
879 | array_needsize (signals, signalmax, w->signum, signals_init); |
944 | array_needsize (signals, signalmax, w->signum, signals_init); |
880 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
945 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
881 | |
946 | |
… | |
… | |
890 | } |
955 | } |
891 | |
956 | |
892 | void |
957 | void |
893 | ev_signal_stop (struct ev_signal *w) |
958 | ev_signal_stop (struct ev_signal *w) |
894 | { |
959 | { |
895 | ev_clear ((W)w); |
960 | ev_clear_pending ((W)w); |
896 | if (!ev_is_active (w)) |
961 | if (!ev_is_active (w)) |
897 | return; |
962 | return; |
898 | |
963 | |
899 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
964 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
900 | ev_stop ((W)w); |
965 | ev_stop ((W)w); |
… | |
… | |
915 | } |
980 | } |
916 | |
981 | |
917 | void |
982 | void |
918 | ev_idle_stop (struct ev_idle *w) |
983 | ev_idle_stop (struct ev_idle *w) |
919 | { |
984 | { |
920 | ev_clear ((W)w); |
985 | ev_clear_pending ((W)w); |
921 | if (ev_is_active (w)) |
986 | if (ev_is_active (w)) |
922 | return; |
987 | return; |
923 | |
988 | |
924 | idles [w->active - 1] = idles [--idlecnt]; |
989 | idles [w->active - 1] = idles [--idlecnt]; |
925 | ev_stop ((W)w); |
990 | ev_stop ((W)w); |
… | |
… | |
937 | } |
1002 | } |
938 | |
1003 | |
939 | void |
1004 | void |
940 | ev_prepare_stop (struct ev_prepare *w) |
1005 | ev_prepare_stop (struct ev_prepare *w) |
941 | { |
1006 | { |
942 | ev_clear ((W)w); |
1007 | ev_clear_pending ((W)w); |
943 | if (ev_is_active (w)) |
1008 | if (ev_is_active (w)) |
944 | return; |
1009 | return; |
945 | |
1010 | |
946 | prepares [w->active - 1] = prepares [--preparecnt]; |
1011 | prepares [w->active - 1] = prepares [--preparecnt]; |
947 | ev_stop ((W)w); |
1012 | ev_stop ((W)w); |
… | |
… | |
959 | } |
1024 | } |
960 | |
1025 | |
961 | void |
1026 | void |
962 | ev_check_stop (struct ev_check *w) |
1027 | ev_check_stop (struct ev_check *w) |
963 | { |
1028 | { |
964 | ev_clear ((W)w); |
1029 | ev_clear_pending ((W)w); |
965 | if (ev_is_active (w)) |
1030 | if (ev_is_active (w)) |
966 | return; |
1031 | return; |
967 | |
1032 | |
968 | checks [w->active - 1] = checks [--checkcnt]; |
1033 | checks [w->active - 1] = checks [--checkcnt]; |
969 | ev_stop ((W)w); |
1034 | ev_stop ((W)w); |
… | |
… | |
980 | } |
1045 | } |
981 | |
1046 | |
982 | void |
1047 | void |
983 | ev_child_stop (struct ev_child *w) |
1048 | ev_child_stop (struct ev_child *w) |
984 | { |
1049 | { |
985 | ev_clear ((W)w); |
1050 | ev_clear_pending ((W)w); |
986 | if (ev_is_active (w)) |
1051 | if (ev_is_active (w)) |
987 | return; |
1052 | return; |
988 | |
1053 | |
989 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1054 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
990 | ev_stop ((W)w); |
1055 | ev_stop ((W)w); |