ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.29 by root, Thu Nov 1 08:10:03 2007 UTC vs.
Revision 1.40 by root, Fri Nov 2 11:02:23 2007 UTC

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
73typedef struct ev_watcher *W; 101typedef struct ev_watcher *W;
74typedef struct ev_watcher_list *WL; 102typedef struct ev_watcher_list *WL;
75typedef struct ev_watcher_time *WT; 103typedef struct ev_watcher_time *WT;
76 104
77static ev_tstamp now, diff; /* monotonic clock */ 105static ev_tstamp now_floor, now, diff; /* monotonic clock */
78ev_tstamp ev_now; 106ev_tstamp ev_now;
79int ev_method; 107int ev_method;
80 108
81static int have_monotonic; /* runtime */ 109static int have_monotonic; /* runtime */
82 110
102 130
103static ev_tstamp 131static ev_tstamp
104get_clock (void) 132get_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 }
113#endif 141#endif
114 142
115 return ev_time (); 143 return ev_time ();
116} 144}
117 145
118#define array_nextsize(n) (((n) << 1) | 4 & ~3) 146#define array_roundsize(base,n) ((n) | 4 & ~3)
119#define array_prevsize(n) (((n) >> 1) | 4 & ~3)
120 147
121#define array_needsize(base,cur,cnt,init) \ 148#define array_needsize(base,cur,cnt,init) \
122 if ((cnt) > cur) \ 149 if (expect_false ((cnt) > cur)) \
123 { \ 150 { \
124 int newcnt = cur; \ 151 int newcnt = cur; \
125 do \ 152 do \
126 { \ 153 { \
127 newcnt = array_nextsize (newcnt); \ 154 newcnt = array_roundsize (base, newcnt << 1); \
128 } \ 155 } \
129 while ((cnt) > newcnt); \ 156 while ((cnt) > newcnt); \
130 \ 157 \
131 base = realloc (base, sizeof (*base) * (newcnt)); \ 158 base = realloc (base, sizeof (*base) * (newcnt)); \
132 init (base + cur, newcnt - cur); \ 159 init (base + cur, newcnt - cur); \
136/*****************************************************************************/ 163/*****************************************************************************/
137 164
138typedef struct 165typedef struct
139{ 166{
140 struct ev_io *head; 167 struct ev_io *head;
141 int events; 168 unsigned char events;
169 unsigned char reify;
142} ANFD; 170} ANFD;
143 171
144static ANFD *anfds; 172static ANFD *anfds;
145static int anfdmax; 173static int anfdmax;
146 174
149{ 177{
150 while (count--) 178 while (count--)
151 { 179 {
152 base->head = 0; 180 base->head = 0;
153 base->events = EV_NONE; 181 base->events = EV_NONE;
182 base->reify = 0;
183
154 ++base; 184 ++base;
155 } 185 }
156} 186}
157 187
158typedef struct 188typedef struct
165static int pendingmax, pendingcnt; 195static int pendingmax, pendingcnt;
166 196
167static void 197static void
168event (W w, int events) 198event (W w, int events)
169{ 199{
170 if (w->active) 200 if (w->pending)
201 {
202 pendings [w->pending - 1].events |= events;
203 return;
171 { 204 }
205
172 w->pending = ++pendingcnt; 206 w->pending = ++pendingcnt;
173 array_needsize (pendings, pendingmax, pendingcnt, ); 207 array_needsize (pendings, pendingmax, pendingcnt, );
174 pendings [pendingcnt - 1].w = w; 208 pendings [pendingcnt - 1].w = w;
175 pendings [pendingcnt - 1].events = events; 209 pendings [pendingcnt - 1].events = events;
176 }
177} 210}
178 211
179static void 212static void
180queue_events (W *events, int eventcnt, int type) 213queue_events (W *events, int eventcnt, int type)
181{ 214{
219 int events = 0; 252 int events = 0;
220 253
221 for (w = anfd->head; w; w = w->next) 254 for (w = anfd->head; w; w = w->next)
222 events |= w->events; 255 events |= w->events;
223 256
224 anfd->events &= ~EV_REIFY; 257 anfd->reify = 0;
225 258
226 if (anfd->events != events) 259 if (anfd->events != events)
227 { 260 {
228 method_modify (fd, anfd->events, events); 261 method_modify (fd, anfd->events, events);
229 anfd->events = events; 262 anfd->events = events;
234} 267}
235 268
236static void 269static void
237fd_change (int fd) 270fd_change (int fd)
238{ 271{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 272 if (anfds [fd].reify || fdchangecnt < 0)
240 return; 273 return;
241 274
242 anfds [fd].events |= EV_REIFY; 275 anfds [fd].reify = 1;
243 276
244 ++fdchangecnt; 277 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 278 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd; 279 fdchanges [fdchangecnt - 1] = fd;
247} 280}
255 for (fd = 0; fd < anfdmax; ++fd) 288 for (fd = 0; fd < anfdmax; ++fd)
256 if (anfds [fd].events) 289 if (anfds [fd].events)
257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 290 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
258 while (anfds [fd].head) 291 while (anfds [fd].head)
259 { 292 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
261 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);
262 } 295 }
263} 296}
264 297
265/*****************************************************************************/ 298/*****************************************************************************/
266 299
314/*****************************************************************************/ 347/*****************************************************************************/
315 348
316typedef struct 349typedef struct
317{ 350{
318 struct ev_signal *head; 351 struct ev_signal *head;
319 sig_atomic_t gotsig; 352 sig_atomic_t volatile gotsig;
320} ANSIG; 353} ANSIG;
321 354
322static ANSIG *signals; 355static ANSIG *signals;
323static int signalmax; 356static int signalmax;
324 357
325static int sigpipe [2]; 358static int sigpipe [2];
326static sig_atomic_t gotsig; 359static sig_atomic_t volatile gotsig;
327static struct ev_io sigev; 360static struct ev_io sigev;
328 361
329static void 362static void
330signals_init (ANSIG *base, int count) 363signals_init (ANSIG *base, int count)
331{ 364{
332 while (count--) 365 while (count--)
333 { 366 {
334 base->head = 0; 367 base->head = 0;
335 base->gotsig = 0; 368 base->gotsig = 0;
369
336 ++base; 370 ++base;
337 } 371 }
338} 372}
339 373
340static void 374static void
343 signals [signum - 1].gotsig = 1; 377 signals [signum - 1].gotsig = 1;
344 378
345 if (!gotsig) 379 if (!gotsig)
346 { 380 {
347 gotsig = 1; 381 gotsig = 1;
348 write (sigpipe [1], &gotsig, 1); 382 write (sigpipe [1], &signum, 1);
349 } 383 }
350} 384}
351 385
352static void 386static void
353sigcb (struct ev_io *iow, int revents) 387sigcb (struct ev_io *iow, int revents)
354{ 388{
355 struct ev_signal *w; 389 struct ev_signal *w;
356 int sig; 390 int signum;
357 391
392 read (sigpipe [0], &revents, 1);
358 gotsig = 0; 393 gotsig = 0;
359 read (sigpipe [0], &revents, 1);
360 394
361 for (sig = signalmax; sig--; ) 395 for (signum = signalmax; signum--; )
362 if (signals [sig].gotsig) 396 if (signals [signum].gotsig)
363 { 397 {
364 signals [sig].gotsig = 0; 398 signals [signum].gotsig = 0;
365 399
366 for (w = signals [sig].head; w; w = w->next) 400 for (w = signals [signum].head; w; w = w->next)
367 event ((W)w, EV_SIGNAL); 401 event ((W)w, EV_SIGNAL);
368 } 402 }
369} 403}
370 404
371static void 405static void
408 struct ev_child *w; 442 struct ev_child *w;
409 int pid, status; 443 int pid, status;
410 444
411 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
412 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
413 if (w->pid == pid || w->pid == -1) 447 if (w->pid == pid || !w->pid)
414 { 448 {
415 w->status = status; 449 w->status = status;
416 event ((W)w, EV_CHILD); 450 event ((W)w, EV_CHILD);
417 } 451 }
418} 452}
448 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 482 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
449 have_monotonic = 1; 483 have_monotonic = 1;
450 } 484 }
451#endif 485#endif
452 486
453 ev_now = ev_time (); 487 ev_now = ev_time ();
454 now = get_clock (); 488 now = get_clock ();
489 now_floor = now;
455 diff = ev_now - now; 490 diff = ev_now - now;
456 491
457 if (pipe (sigpipe)) 492 if (pipe (sigpipe))
458 return 0; 493 return 0;
459 494
460 ev_method = EVMETHOD_NONE; 495 ev_method = EVMETHOD_NONE;
479} 514}
480 515
481/*****************************************************************************/ 516/*****************************************************************************/
482 517
483void 518void
484ev_prefork (void) 519ev_fork_prepare (void)
485{ 520{
486 /* nop */ 521 /* nop */
487} 522}
488 523
489void 524void
490ev_postfork_parent (void) 525ev_fork_parent (void)
491{ 526{
492 /* nop */ 527 /* nop */
493} 528}
494 529
495void 530void
496ev_postfork_child (void) 531ev_fork_child (void)
497{ 532{
498#if EV_USE_EPOLL 533#if EV_USE_EPOLL
499 if (ev_method == EVMETHOD_EPOLL) 534 if (ev_method == EVMETHOD_EPOLL)
500 epoll_postfork_child (); 535 epoll_postfork_child ();
501#endif 536#endif
529{ 564{
530 while (timercnt && timers [0]->at <= now) 565 while (timercnt && timers [0]->at <= now)
531 { 566 {
532 struct ev_timer *w = timers [0]; 567 struct ev_timer *w = timers [0];
533 568
534 event ((W)w, EV_TIMEOUT);
535
536 /* first reschedule or stop timer */ 569 /* first reschedule or stop timer */
537 if (w->repeat) 570 if (w->repeat)
538 { 571 {
572 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
539 w->at = now + w->repeat; 573 w->at = now + w->repeat;
540 assert (("timer timeout in the past, negative repeat?", w->at > now));
541 downheap ((WT *)timers, timercnt, 0); 574 downheap ((WT *)timers, timercnt, 0);
542 } 575 }
543 else 576 else
544 ev_timer_stop (w); /* nonrepeating: stop timer */ 577 ev_timer_stop (w); /* nonrepeating: stop timer */
578
579 event ((W)w, EV_TIMEOUT);
545 } 580 }
546} 581}
547 582
548static void 583static void
549periodics_reify (void) 584periodics_reify (void)
554 589
555 /* first reschedule or stop timer */ 590 /* first reschedule or stop timer */
556 if (w->interval) 591 if (w->interval)
557 { 592 {
558 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;
559 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));
560 downheap ((WT *)periodics, periodiccnt, 0); 595 downheap ((WT *)periodics, periodiccnt, 0);
561 } 596 }
562 else 597 else
563 ev_periodic_stop (w); /* nonrepeating: stop timer */ 598 ev_periodic_stop (w); /* nonrepeating: stop timer */
564 599
565 event ((W)w, EV_TIMEOUT); 600 event ((W)w, EV_PERIODIC);
566 } 601 }
567} 602}
568 603
569static void 604static void
570periodics_reschedule (ev_tstamp diff) 605periodics_reschedule (ev_tstamp diff)
589 } 624 }
590 } 625 }
591 } 626 }
592} 627}
593 628
629static int
630time_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
594static void 647static void
595time_update (void) 648time_update (void)
596{ 649{
597 int i; 650 int i;
598 651
599 ev_now = ev_time (); 652#if EV_USE_MONOTONIC
600
601 if (have_monotonic) 653 if (expect_true (have_monotonic))
602 { 654 {
655 if (time_update_monotonic ())
656 {
603 ev_tstamp odiff = diff; 657 ev_tstamp odiff = diff;
604 658
605 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 */
606 { 660 {
607 now = get_clock ();
608 diff = ev_now - now; 661 diff = ev_now - now;
609 662
610 if (fabs (odiff - diff) < MIN_TIMEJUMP) 663 if (fabs (odiff - diff) < MIN_TIMEJUMP)
611 return; /* all is well */ 664 return; /* all is well */
612 665
613 ev_now = ev_time (); 666 ev_now = ev_time ();
667 now = get_clock ();
668 now_floor = now;
614 } 669 }
615 670
616 periodics_reschedule (diff - odiff); 671 periodics_reschedule (diff - odiff);
617 /* no timer adjustment, as the monotonic clock doesn't jump */ 672 /* no timer adjustment, as the monotonic clock doesn't jump */
673 }
618 } 674 }
619 else 675 else
676#endif
620 { 677 {
678 ev_now = ev_time ();
679
621 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))
622 { 681 {
623 periodics_reschedule (ev_now - now); 682 periodics_reschedule (ev_now - now);
624 683
625 /* 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 */
626 for (i = 0; i < timercnt; ++i) 685 for (i = 0; i < timercnt; ++i)
639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 698 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
640 699
641 do 700 do
642 { 701 {
643 /* queue check watchers (and execute them) */ 702 /* queue check watchers (and execute them) */
644 if (preparecnt) 703 if (expect_false (preparecnt))
645 { 704 {
646 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 705 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
647 call_pending (); 706 call_pending ();
648 } 707 }
649 708
652 711
653 /* calculate blocking time */ 712 /* calculate blocking time */
654 713
655 /* 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
656 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 {
657 ev_now = ev_time (); 722 ev_now = ev_time ();
723 now = ev_now;
724 }
658 725
659 if (flags & EVLOOP_NONBLOCK || idlecnt) 726 if (flags & EVLOOP_NONBLOCK || idlecnt)
660 block = 0.; 727 block = 0.;
661 else 728 else
662 { 729 {
663 block = MAX_BLOCKTIME; 730 block = MAX_BLOCKTIME;
664 731
665 if (timercnt) 732 if (timercnt)
666 { 733 {
667 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 734 ev_tstamp to = timers [0]->at - now + method_fudge;
668 if (block > to) block = to; 735 if (block > to) block = to;
669 } 736 }
670 737
671 if (periodiccnt) 738 if (periodiccnt)
672 { 739 {
725 head = &(*head)->next; 792 head = &(*head)->next;
726 } 793 }
727} 794}
728 795
729static void 796static void
730ev_clear (W w) 797ev_clear_pending (W w)
731{ 798{
732 if (w->pending) 799 if (w->pending)
733 { 800 {
734 pendings [w->pending - 1].w = 0; 801 pendings [w->pending - 1].w = 0;
735 w->pending = 0; 802 w->pending = 0;
751/*****************************************************************************/ 818/*****************************************************************************/
752 819
753void 820void
754ev_io_start (struct ev_io *w) 821ev_io_start (struct ev_io *w)
755{ 822{
823 int fd = w->fd;
824
756 if (ev_is_active (w)) 825 if (ev_is_active (w))
757 return; 826 return;
758 827
759 int fd = w->fd; 828 assert (("ev_io_start called with negative fd", fd >= 0));
760 829
761 ev_start ((W)w, 1); 830 ev_start ((W)w, 1);
762 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 831 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
763 wlist_add ((WL *)&anfds[fd].head, (WL)w); 832 wlist_add ((WL *)&anfds[fd].head, (WL)w);
764 833
766} 835}
767 836
768void 837void
769ev_io_stop (struct ev_io *w) 838ev_io_stop (struct ev_io *w)
770{ 839{
771 ev_clear ((W)w); 840 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 841 if (!ev_is_active (w))
773 return; 842 return;
774 843
775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 844 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
776 ev_stop ((W)w); 845 ev_stop ((W)w);
784 if (ev_is_active (w)) 853 if (ev_is_active (w))
785 return; 854 return;
786 855
787 w->at += now; 856 w->at += now;
788 857
789 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.));
790 859
791 ev_start ((W)w, ++timercnt); 860 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, ); 861 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w; 862 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1); 863 upheap ((WT *)timers, timercnt - 1);
795} 864}
796 865
797void 866void
798ev_timer_stop (struct ev_timer *w) 867ev_timer_stop (struct ev_timer *w)
799{ 868{
800 ev_clear ((W)w); 869 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 870 if (!ev_is_active (w))
802 return; 871 return;
803 872
804 if (w->active < timercnt--) 873 if (w->active < timercnt--)
805 { 874 {
833ev_periodic_start (struct ev_periodic *w) 902ev_periodic_start (struct ev_periodic *w)
834{ 903{
835 if (ev_is_active (w)) 904 if (ev_is_active (w))
836 return; 905 return;
837 906
838 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.));
839 908
840 /* 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 */
841 if (w->interval) 910 if (w->interval)
842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 911 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
843 912
848} 917}
849 918
850void 919void
851ev_periodic_stop (struct ev_periodic *w) 920ev_periodic_stop (struct ev_periodic *w)
852{ 921{
853 ev_clear ((W)w); 922 ev_clear_pending ((W)w);
854 if (!ev_is_active (w)) 923 if (!ev_is_active (w))
855 return; 924 return;
856 925
857 if (w->active < periodiccnt--) 926 if (w->active < periodiccnt--)
858 { 927 {
866void 935void
867ev_signal_start (struct ev_signal *w) 936ev_signal_start (struct ev_signal *w)
868{ 937{
869 if (ev_is_active (w)) 938 if (ev_is_active (w))
870 return; 939 return;
940
941 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
871 942
872 ev_start ((W)w, 1); 943 ev_start ((W)w, 1);
873 array_needsize (signals, signalmax, w->signum, signals_init); 944 array_needsize (signals, signalmax, w->signum, signals_init);
874 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 945 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
875 946
884} 955}
885 956
886void 957void
887ev_signal_stop (struct ev_signal *w) 958ev_signal_stop (struct ev_signal *w)
888{ 959{
889 ev_clear ((W)w); 960 ev_clear_pending ((W)w);
890 if (!ev_is_active (w)) 961 if (!ev_is_active (w))
891 return; 962 return;
892 963
893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 964 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
894 ev_stop ((W)w); 965 ev_stop ((W)w);
909} 980}
910 981
911void 982void
912ev_idle_stop (struct ev_idle *w) 983ev_idle_stop (struct ev_idle *w)
913{ 984{
914 ev_clear ((W)w); 985 ev_clear_pending ((W)w);
915 if (ev_is_active (w)) 986 if (ev_is_active (w))
916 return; 987 return;
917 988
918 idles [w->active - 1] = idles [--idlecnt]; 989 idles [w->active - 1] = idles [--idlecnt];
919 ev_stop ((W)w); 990 ev_stop ((W)w);
931} 1002}
932 1003
933void 1004void
934ev_prepare_stop (struct ev_prepare *w) 1005ev_prepare_stop (struct ev_prepare *w)
935{ 1006{
936 ev_clear ((W)w); 1007 ev_clear_pending ((W)w);
937 if (ev_is_active (w)) 1008 if (ev_is_active (w))
938 return; 1009 return;
939 1010
940 prepares [w->active - 1] = prepares [--preparecnt]; 1011 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w); 1012 ev_stop ((W)w);
953} 1024}
954 1025
955void 1026void
956ev_check_stop (struct ev_check *w) 1027ev_check_stop (struct ev_check *w)
957{ 1028{
958 ev_clear ((W)w); 1029 ev_clear_pending ((W)w);
959 if (ev_is_active (w)) 1030 if (ev_is_active (w))
960 return; 1031 return;
961 1032
962 checks [w->active - 1] = checks [--checkcnt]; 1033 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w); 1034 ev_stop ((W)w);
974} 1045}
975 1046
976void 1047void
977ev_child_stop (struct ev_child *w) 1048ev_child_stop (struct ev_child *w)
978{ 1049{
979 ev_clear ((W)w); 1050 ev_clear_pending ((W)w);
980 if (ev_is_active (w)) 1051 if (ev_is_active (w))
981 return; 1052 return;
982 1053
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1054 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w); 1055 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines