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

Comparing libev/ev.c (file contents):
Revision 1.28 by root, Thu Nov 1 06:48:49 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
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
41#include <sys/types.h> 46#include <sys/types.h>
42#include <sys/wait.h> 47#include <sys/wait.h>
43#include <sys/time.h> 48#include <sys/time.h>
44#include <time.h> 49#include <time.h>
45 50
51/**/
52
46#ifndef HAVE_MONOTONIC 53#ifndef EV_USE_MONOTONIC
54# define EV_USE_MONOTONIC 1
55#endif
56
57#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1
59#endif
60
61#ifndef EV_USE_EPOLL
62# define EV_USE_EPOLL 0
63#endif
64
65#ifndef EV_USE_REALTIME
66# define EV_USE_REALTIME 1
67#endif
68
69/**/
70
47# ifdef CLOCK_MONOTONIC 71#ifndef CLOCK_MONOTONIC
72# undef EV_USE_MONOTONIC
48# define HAVE_MONOTONIC 1 73# define EV_USE_MONOTONIC 0
49# endif 74#endif
50#endif
51 75
52#ifndef HAVE_SELECT
53# define HAVE_SELECT 1
54#endif
55
56#ifndef HAVE_EPOLL
57# define HAVE_EPOLL 0
58#endif
59
60#ifndef HAVE_REALTIME 76#ifndef CLOCK_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 77# undef EV_USE_REALTIME
78# define EV_USE_REALTIME 0
62#endif 79#endif
80
81/**/
63 82
64#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) */
65#define MAX_BLOCKTIME 60. 84#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
66#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 */
67 87
68#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)
69 100
70typedef struct ev_watcher *W; 101typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 102typedef struct ev_watcher_list *WL;
72typedef struct ev_watcher_time *WT; 103typedef struct ev_watcher_time *WT;
73 104
74static ev_tstamp now, diff; /* monotonic clock */ 105static ev_tstamp now_floor, now, diff; /* monotonic clock */
75ev_tstamp ev_now; 106ev_tstamp ev_now;
76int ev_method; 107int ev_method;
77 108
78static int have_monotonic; /* runtime */ 109static int have_monotonic; /* runtime */
79 110
84/*****************************************************************************/ 115/*****************************************************************************/
85 116
86ev_tstamp 117ev_tstamp
87ev_time (void) 118ev_time (void)
88{ 119{
89#if HAVE_REALTIME 120#if EV_USE_REALTIME
90 struct timespec ts; 121 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 122 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 123 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 124#else
94 struct timeval tv; 125 struct timeval tv;
98} 129}
99 130
100static ev_tstamp 131static ev_tstamp
101get_clock (void) 132get_clock (void)
102{ 133{
103#if HAVE_MONOTONIC 134#if EV_USE_MONOTONIC
104 if (have_monotonic) 135 if (expect_true (have_monotonic))
105 { 136 {
106 struct timespec ts; 137 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 138 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 139 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 140 }
110#endif 141#endif
111 142
112 return ev_time (); 143 return ev_time ();
113} 144}
114 145
146#define array_roundsize(base,n) ((n) | 4 & ~3)
147
115#define array_needsize(base,cur,cnt,init) \ 148#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 149 if (expect_false ((cnt) > cur)) \
117 { \ 150 { \
118 int newcnt = cur; \ 151 int newcnt = cur; \
119 do \ 152 do \
120 { \ 153 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 154 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 155 } \
123 while ((cnt) > newcnt); \ 156 while ((cnt) > newcnt); \
124 \ 157 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 158 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 159 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 163/*****************************************************************************/
131 164
132typedef struct 165typedef struct
133{ 166{
134 struct ev_io *head; 167 struct ev_io *head;
135 int events; 168 unsigned char events;
169 unsigned char reify;
136} ANFD; 170} ANFD;
137 171
138static ANFD *anfds; 172static ANFD *anfds;
139static int anfdmax; 173static int anfdmax;
140 174
143{ 177{
144 while (count--) 178 while (count--)
145 { 179 {
146 base->head = 0; 180 base->head = 0;
147 base->events = EV_NONE; 181 base->events = EV_NONE;
182 base->reify = 0;
183
148 ++base; 184 ++base;
149 } 185 }
150} 186}
151 187
152typedef struct 188typedef struct
159static int pendingmax, pendingcnt; 195static int pendingmax, pendingcnt;
160 196
161static void 197static void
162event (W w, int events) 198event (W w, int events)
163{ 199{
164 if (w->active) 200 if (w->pending)
201 {
202 pendings [w->pending - 1].events |= events;
203 return;
165 { 204 }
205
166 w->pending = ++pendingcnt; 206 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, ); 207 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w; 208 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 209 pendings [pendingcnt - 1].events = events;
170 }
171} 210}
172 211
173static void 212static void
174queue_events (W *events, int eventcnt, int type) 213queue_events (W *events, int eventcnt, int type)
175{ 214{
213 int events = 0; 252 int events = 0;
214 253
215 for (w = anfd->head; w; w = w->next) 254 for (w = anfd->head; w; w = w->next)
216 events |= w->events; 255 events |= w->events;
217 256
218 anfd->events &= ~EV_REIFY; 257 anfd->reify = 0;
219 258
220 if (anfd->events != events) 259 if (anfd->events != events)
221 { 260 {
222 method_modify (fd, anfd->events, events); 261 method_modify (fd, anfd->events, events);
223 anfd->events = events; 262 anfd->events = events;
228} 267}
229 268
230static void 269static void
231fd_change (int fd) 270fd_change (int fd)
232{ 271{
233 if (anfds [fd].events & EV_REIFY) 272 if (anfds [fd].reify || fdchangecnt < 0)
234 return; 273 return;
235 274
236 anfds [fd].events |= EV_REIFY; 275 anfds [fd].reify = 1;
237 276
238 ++fdchangecnt; 277 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 278 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd; 279 fdchanges [fdchangecnt - 1] = fd;
241} 280}
249 for (fd = 0; fd < anfdmax; ++fd) 288 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 289 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 290 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 291 while (anfds [fd].head)
253 { 292 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 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);
256 } 295 }
257} 296}
258 297
259/*****************************************************************************/ 298/*****************************************************************************/
260 299
308/*****************************************************************************/ 347/*****************************************************************************/
309 348
310typedef struct 349typedef struct
311{ 350{
312 struct ev_signal *head; 351 struct ev_signal *head;
313 sig_atomic_t gotsig; 352 sig_atomic_t volatile gotsig;
314} ANSIG; 353} ANSIG;
315 354
316static ANSIG *signals; 355static ANSIG *signals;
317static int signalmax; 356static int signalmax;
318 357
319static int sigpipe [2]; 358static int sigpipe [2];
320static sig_atomic_t gotsig; 359static sig_atomic_t volatile gotsig;
321static struct ev_io sigev; 360static struct ev_io sigev;
322 361
323static void 362static void
324signals_init (ANSIG *base, int count) 363signals_init (ANSIG *base, int count)
325{ 364{
326 while (count--) 365 while (count--)
327 { 366 {
328 base->head = 0; 367 base->head = 0;
329 base->gotsig = 0; 368 base->gotsig = 0;
369
330 ++base; 370 ++base;
331 } 371 }
332} 372}
333 373
334static void 374static void
337 signals [signum - 1].gotsig = 1; 377 signals [signum - 1].gotsig = 1;
338 378
339 if (!gotsig) 379 if (!gotsig)
340 { 380 {
341 gotsig = 1; 381 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1); 382 write (sigpipe [1], &signum, 1);
343 } 383 }
344} 384}
345 385
346static void 386static void
347sigcb (struct ev_io *iow, int revents) 387sigcb (struct ev_io *iow, int revents)
348{ 388{
349 struct ev_signal *w; 389 struct ev_signal *w;
350 int sig; 390 int signum;
351 391
392 read (sigpipe [0], &revents, 1);
352 gotsig = 0; 393 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354 394
355 for (sig = signalmax; sig--; ) 395 for (signum = signalmax; signum--; )
356 if (signals [sig].gotsig) 396 if (signals [signum].gotsig)
357 { 397 {
358 signals [sig].gotsig = 0; 398 signals [signum].gotsig = 0;
359 399
360 for (w = signals [sig].head; w; w = w->next) 400 for (w = signals [signum].head; w; w = w->next)
361 event ((W)w, EV_SIGNAL); 401 event ((W)w, EV_SIGNAL);
362 } 402 }
363} 403}
364 404
365static void 405static void
402 struct ev_child *w; 442 struct ev_child *w;
403 int pid, status; 443 int pid, status;
404 444
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 445 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 446 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1) 447 if (w->pid == pid || !w->pid)
408 { 448 {
409 w->status = status; 449 w->status = status;
410 event ((W)w, EV_CHILD); 450 event ((W)w, EV_CHILD);
411 } 451 }
412} 452}
413 453
414/*****************************************************************************/ 454/*****************************************************************************/
415 455
416#if HAVE_EPOLL 456#if EV_USE_EPOLL
417# include "ev_epoll.c" 457# include "ev_epoll.c"
418#endif 458#endif
419#if HAVE_SELECT 459#if EV_USE_SELECT
420# include "ev_select.c" 460# include "ev_select.c"
421#endif 461#endif
422 462
423int 463int
424ev_version_major (void) 464ev_version_major (void)
434 474
435int ev_init (int flags) 475int ev_init (int flags)
436{ 476{
437 if (!ev_method) 477 if (!ev_method)
438 { 478 {
439#if HAVE_MONOTONIC 479#if EV_USE_MONOTONIC
440 { 480 {
441 struct timespec ts; 481 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 482 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 483 have_monotonic = 1;
444 } 484 }
445#endif 485#endif
446 486
447 ev_now = ev_time (); 487 ev_now = ev_time ();
448 now = get_clock (); 488 now = get_clock ();
489 now_floor = now;
449 diff = ev_now - now; 490 diff = ev_now - now;
450 491
451 if (pipe (sigpipe)) 492 if (pipe (sigpipe))
452 return 0; 493 return 0;
453 494
454 ev_method = EVMETHOD_NONE; 495 ev_method = EVMETHOD_NONE;
455#if HAVE_EPOLL 496#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 497 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457#endif 498#endif
458#if HAVE_SELECT 499#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 500 if (ev_method == EVMETHOD_NONE) select_init (flags);
460#endif 501#endif
461 502
462 if (ev_method) 503 if (ev_method)
463 { 504 {
473} 514}
474 515
475/*****************************************************************************/ 516/*****************************************************************************/
476 517
477void 518void
478ev_prefork (void) 519ev_fork_prepare (void)
479{ 520{
480 /* nop */ 521 /* nop */
481} 522}
482 523
483void 524void
484ev_postfork_parent (void) 525ev_fork_parent (void)
485{ 526{
486 /* nop */ 527 /* nop */
487} 528}
488 529
489void 530void
490ev_postfork_child (void) 531ev_fork_child (void)
491{ 532{
492#if HAVE_EPOLL 533#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 534 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 535 epoll_postfork_child ();
495#endif 536#endif
496 537
497 ev_io_stop (&sigev); 538 ev_io_stop (&sigev);
523{ 564{
524 while (timercnt && timers [0]->at <= now) 565 while (timercnt && timers [0]->at <= now)
525 { 566 {
526 struct ev_timer *w = timers [0]; 567 struct ev_timer *w = timers [0];
527 568
528 event ((W)w, EV_TIMEOUT);
529
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 */
578
579 event ((W)w, EV_TIMEOUT);
539 } 580 }
540} 581}
541 582
542static void 583static void
543periodics_reify (void) 584periodics_reify (void)
548 589
549 /* first reschedule or stop timer */ 590 /* first reschedule or stop timer */
550 if (w->interval) 591 if (w->interval)
551 { 592 {
552 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;
553 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));
554 downheap ((WT *)periodics, periodiccnt, 0); 595 downheap ((WT *)periodics, periodiccnt, 0);
555 } 596 }
556 else 597 else
557 ev_periodic_stop (w); /* nonrepeating: stop timer */ 598 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 599
559 event ((W)w, EV_TIMEOUT); 600 event ((W)w, EV_PERIODIC);
560 } 601 }
561} 602}
562 603
563static void 604static void
564periodics_reschedule (ev_tstamp diff) 605periodics_reschedule (ev_tstamp diff)
583 } 624 }
584 } 625 }
585 } 626 }
586} 627}
587 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
588static void 647static void
589time_update (void) 648time_update (void)
590{ 649{
591 int i; 650 int i;
592 651
593 ev_now = ev_time (); 652#if EV_USE_MONOTONIC
594
595 if (have_monotonic) 653 if (expect_true (have_monotonic))
596 { 654 {
655 if (time_update_monotonic ())
656 {
597 ev_tstamp odiff = diff; 657 ev_tstamp odiff = diff;
598 658
599 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 */
600 { 660 {
601 now = get_clock ();
602 diff = ev_now - now; 661 diff = ev_now - now;
603 662
604 if (fabs (odiff - diff) < MIN_TIMEJUMP) 663 if (fabs (odiff - diff) < MIN_TIMEJUMP)
605 return; /* all is well */ 664 return; /* all is well */
606 665
607 ev_now = ev_time (); 666 ev_now = ev_time ();
667 now = get_clock ();
668 now_floor = now;
608 } 669 }
609 670
610 periodics_reschedule (diff - odiff); 671 periodics_reschedule (diff - odiff);
611 /* no timer adjustment, as the monotonic clock doesn't jump */ 672 /* no timer adjustment, as the monotonic clock doesn't jump */
673 }
612 } 674 }
613 else 675 else
676#endif
614 { 677 {
678 ev_now = ev_time ();
679
615 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))
616 { 681 {
617 periodics_reschedule (ev_now - now); 682 periodics_reschedule (ev_now - now);
618 683
619 /* 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 */
620 for (i = 0; i < timercnt; ++i) 685 for (i = 0; i < timercnt; ++i)
633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 698 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
634 699
635 do 700 do
636 { 701 {
637 /* queue check watchers (and execute them) */ 702 /* queue check watchers (and execute them) */
638 if (preparecnt) 703 if (expect_false (preparecnt))
639 { 704 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 705 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending (); 706 call_pending ();
642 } 707 }
643 708
646 711
647 /* calculate blocking time */ 712 /* calculate blocking time */
648 713
649 /* 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
650 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 {
651 ev_now = ev_time (); 722 ev_now = ev_time ();
723 now = ev_now;
724 }
652 725
653 if (flags & EVLOOP_NONBLOCK || idlecnt) 726 if (flags & EVLOOP_NONBLOCK || idlecnt)
654 block = 0.; 727 block = 0.;
655 else 728 else
656 { 729 {
657 block = MAX_BLOCKTIME; 730 block = MAX_BLOCKTIME;
658 731
659 if (timercnt) 732 if (timercnt)
660 { 733 {
661 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; 734 ev_tstamp to = timers [0]->at - now + method_fudge;
662 if (block > to) block = to; 735 if (block > to) block = to;
663 } 736 }
664 737
665 if (periodiccnt) 738 if (periodiccnt)
666 { 739 {
719 head = &(*head)->next; 792 head = &(*head)->next;
720 } 793 }
721} 794}
722 795
723static void 796static void
724ev_clear (W w) 797ev_clear_pending (W w)
725{ 798{
726 if (w->pending) 799 if (w->pending)
727 { 800 {
728 pendings [w->pending - 1].w = 0; 801 pendings [w->pending - 1].w = 0;
729 w->pending = 0; 802 w->pending = 0;
745/*****************************************************************************/ 818/*****************************************************************************/
746 819
747void 820void
748ev_io_start (struct ev_io *w) 821ev_io_start (struct ev_io *w)
749{ 822{
823 int fd = w->fd;
824
750 if (ev_is_active (w)) 825 if (ev_is_active (w))
751 return; 826 return;
752 827
753 int fd = w->fd; 828 assert (("ev_io_start called with negative fd", fd >= 0));
754 829
755 ev_start ((W)w, 1); 830 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 831 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w); 832 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 833
760} 835}
761 836
762void 837void
763ev_io_stop (struct ev_io *w) 838ev_io_stop (struct ev_io *w)
764{ 839{
765 ev_clear ((W)w); 840 ev_clear_pending ((W)w);
766 if (!ev_is_active (w)) 841 if (!ev_is_active (w))
767 return; 842 return;
768 843
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 844 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w); 845 ev_stop ((W)w);
778 if (ev_is_active (w)) 853 if (ev_is_active (w))
779 return; 854 return;
780 855
781 w->at += now; 856 w->at += now;
782 857
783 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.));
784 859
785 ev_start ((W)w, ++timercnt); 860 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, ); 861 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w; 862 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 863 upheap ((WT *)timers, timercnt - 1);
789} 864}
790 865
791void 866void
792ev_timer_stop (struct ev_timer *w) 867ev_timer_stop (struct ev_timer *w)
793{ 868{
794 ev_clear ((W)w); 869 ev_clear_pending ((W)w);
795 if (!ev_is_active (w)) 870 if (!ev_is_active (w))
796 return; 871 return;
797 872
798 if (w->active < timercnt--) 873 if (w->active < timercnt--)
799 { 874 {
827ev_periodic_start (struct ev_periodic *w) 902ev_periodic_start (struct ev_periodic *w)
828{ 903{
829 if (ev_is_active (w)) 904 if (ev_is_active (w))
830 return; 905 return;
831 906
832 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.));
833 908
834 /* 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 */
835 if (w->interval) 910 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 911 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837 912
842} 917}
843 918
844void 919void
845ev_periodic_stop (struct ev_periodic *w) 920ev_periodic_stop (struct ev_periodic *w)
846{ 921{
847 ev_clear ((W)w); 922 ev_clear_pending ((W)w);
848 if (!ev_is_active (w)) 923 if (!ev_is_active (w))
849 return; 924 return;
850 925
851 if (w->active < periodiccnt--) 926 if (w->active < periodiccnt--)
852 { 927 {
860void 935void
861ev_signal_start (struct ev_signal *w) 936ev_signal_start (struct ev_signal *w)
862{ 937{
863 if (ev_is_active (w)) 938 if (ev_is_active (w))
864 return; 939 return;
940
941 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
865 942
866 ev_start ((W)w, 1); 943 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init); 944 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 945 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 946
878} 955}
879 956
880void 957void
881ev_signal_stop (struct ev_signal *w) 958ev_signal_stop (struct ev_signal *w)
882{ 959{
883 ev_clear ((W)w); 960 ev_clear_pending ((W)w);
884 if (!ev_is_active (w)) 961 if (!ev_is_active (w))
885 return; 962 return;
886 963
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 964 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w); 965 ev_stop ((W)w);
903} 980}
904 981
905void 982void
906ev_idle_stop (struct ev_idle *w) 983ev_idle_stop (struct ev_idle *w)
907{ 984{
908 ev_clear ((W)w); 985 ev_clear_pending ((W)w);
909 if (ev_is_active (w)) 986 if (ev_is_active (w))
910 return; 987 return;
911 988
912 idles [w->active - 1] = idles [--idlecnt]; 989 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w); 990 ev_stop ((W)w);
925} 1002}
926 1003
927void 1004void
928ev_prepare_stop (struct ev_prepare *w) 1005ev_prepare_stop (struct ev_prepare *w)
929{ 1006{
930 ev_clear ((W)w); 1007 ev_clear_pending ((W)w);
931 if (ev_is_active (w)) 1008 if (ev_is_active (w))
932 return; 1009 return;
933 1010
934 prepares [w->active - 1] = prepares [--preparecnt]; 1011 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w); 1012 ev_stop ((W)w);
947} 1024}
948 1025
949void 1026void
950ev_check_stop (struct ev_check *w) 1027ev_check_stop (struct ev_check *w)
951{ 1028{
952 ev_clear ((W)w); 1029 ev_clear_pending ((W)w);
953 if (ev_is_active (w)) 1030 if (ev_is_active (w))
954 return; 1031 return;
955 1032
956 checks [w->active - 1] = checks [--checkcnt]; 1033 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w); 1034 ev_stop ((W)w);
968} 1045}
969 1046
970void 1047void
971ev_child_stop (struct ev_child *w) 1048ev_child_stop (struct ev_child *w)
972{ 1049{
973 ev_clear ((W)w); 1050 ev_clear_pending ((W)w);
974 if (ev_is_active (w)) 1051 if (ev_is_active (w))
975 return; 1052 return;
976 1053
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1054 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w); 1055 ev_stop ((W)w);
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1094ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1095{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1096 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1097
1021 if (!once) 1098 if (!once)
1022 cb (EV_ERROR, arg); 1099 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1100 else
1024 { 1101 {
1025 once->cb = cb; 1102 once->cb = cb;
1026 once->arg = arg; 1103 once->arg = arg;
1027 1104
1028 ev_watcher_init (&once->io, once_cb_io); 1105 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1106 if (fd >= 0)
1031 { 1107 {
1032 ev_io_set (&once->io, fd, events); 1108 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1109 ev_io_start (&once->io);
1034 } 1110 }
1035 1111
1036 ev_watcher_init (&once->to, once_cb_to); 1112 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1113 if (timeout >= 0.)
1039 { 1114 {
1040 ev_timer_set (&once->to, timeout, 0.); 1115 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1116 ev_timer_start (&once->to);
1042 } 1117 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines