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

Comparing libev/ev.c (file contents):
Revision 1.27 by root, Wed Oct 31 22:16:36 2007 UTC vs.
Revision 1.39 by root, Thu Nov 1 17:17:32 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
46#ifndef HAVE_MONOTONIC 51#ifndef EV_USE_MONOTONIC
52# define EV_USE_MONOTONIC 1
53#endif
54
47# ifdef CLOCK_MONOTONIC 55#ifndef CLOCK_MONOTONIC
56# undef EV_USE_MONOTONIC
48# define HAVE_MONOTONIC 1 57# define EV_USE_MONOTONIC 0
49# endif 58#endif
50#endif
51 59
52#ifndef HAVE_SELECT 60#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 61# define EV_USE_SELECT 1
54#endif 62#endif
55 63
56#ifndef HAVE_EPOLL 64#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 65# define EV_USE_EPOLL 0
58#endif 66#endif
59 67
68#ifndef CLOCK_REALTIME
69# define EV_USE_REALTIME 0
70#endif
60#ifndef HAVE_REALTIME 71#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 72# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 73#endif
63 74
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 75#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 76#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 77#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
78#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
67 79
68#include "ev.h" 80#include "ev.h"
69 81
70typedef struct ev_watcher *W; 82typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 83typedef struct ev_watcher_list *WL;
84/*****************************************************************************/ 96/*****************************************************************************/
85 97
86ev_tstamp 98ev_tstamp
87ev_time (void) 99ev_time (void)
88{ 100{
89#if HAVE_REALTIME 101#if EV_USE_REALTIME
90 struct timespec ts; 102 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 103 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 104 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 105#else
94 struct timeval tv; 106 struct timeval tv;
98} 110}
99 111
100static ev_tstamp 112static ev_tstamp
101get_clock (void) 113get_clock (void)
102{ 114{
103#if HAVE_MONOTONIC 115#if EV_USE_MONOTONIC
104 if (have_monotonic) 116 if (have_monotonic)
105 { 117 {
106 struct timespec ts; 118 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 119 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 120 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 121 }
110#endif 122#endif
111 123
112 return ev_time (); 124 return ev_time ();
113} 125}
126
127#define array_roundsize(base,n) ((n) | 4 & ~3)
114 128
115#define array_needsize(base,cur,cnt,init) \ 129#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 130 if ((cnt) > cur) \
117 { \ 131 { \
118 int newcnt = cur; \ 132 int newcnt = cur; \
119 do \ 133 do \
120 { \ 134 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 135 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 136 } \
123 while ((cnt) > newcnt); \ 137 while ((cnt) > newcnt); \
124 \ 138 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 139 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 140 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 144/*****************************************************************************/
131 145
132typedef struct 146typedef struct
133{ 147{
134 struct ev_io *head; 148 struct ev_io *head;
135 int events; 149 unsigned char events;
150 unsigned char reify;
136} ANFD; 151} ANFD;
137 152
138static ANFD *anfds; 153static ANFD *anfds;
139static int anfdmax; 154static int anfdmax;
140 155
143{ 158{
144 while (count--) 159 while (count--)
145 { 160 {
146 base->head = 0; 161 base->head = 0;
147 base->events = EV_NONE; 162 base->events = EV_NONE;
163 base->reify = 0;
164
148 ++base; 165 ++base;
149 } 166 }
150} 167}
151 168
152typedef struct 169typedef struct
159static int pendingmax, pendingcnt; 176static int pendingmax, pendingcnt;
160 177
161static void 178static void
162event (W w, int events) 179event (W w, int events)
163{ 180{
164 if (w->active) 181 if (w->pending)
182 {
183 pendings [w->pending - 1].events |= events;
184 return;
165 { 185 }
186
166 w->pending = ++pendingcnt; 187 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, ); 188 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w; 189 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 190 pendings [pendingcnt - 1].events = events;
170 }
171} 191}
172 192
173static void 193static void
174queue_events (W *events, int eventcnt, int type) 194queue_events (W *events, int eventcnt, int type)
175{ 195{
213 int events = 0; 233 int events = 0;
214 234
215 for (w = anfd->head; w; w = w->next) 235 for (w = anfd->head; w; w = w->next)
216 events |= w->events; 236 events |= w->events;
217 237
218 anfd->events &= ~EV_REIFY; 238 anfd->reify = 0;
219 239
220 if (anfd->events != events) 240 if (anfd->events != events)
221 { 241 {
222 method_modify (fd, anfd->events, events); 242 method_modify (fd, anfd->events, events);
223 anfd->events = events; 243 anfd->events = events;
228} 248}
229 249
230static void 250static void
231fd_change (int fd) 251fd_change (int fd)
232{ 252{
233 if (anfds [fd].events & EV_REIFY) 253 if (anfds [fd].reify || fdchangecnt < 0)
234 return; 254 return;
235 255
236 anfds [fd].events |= EV_REIFY; 256 anfds [fd].reify = 1;
237 257
238 ++fdchangecnt; 258 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 259 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd; 260 fdchanges [fdchangecnt - 1] = fd;
241} 261}
249 for (fd = 0; fd < anfdmax; ++fd) 269 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 270 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 271 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 272 while (anfds [fd].head)
253 { 273 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 evio_stop (anfds [fd].head); 274 ev_io_stop (anfds [fd].head);
275 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
256 } 276 }
257} 277}
258 278
259/*****************************************************************************/ 279/*****************************************************************************/
260 280
308/*****************************************************************************/ 328/*****************************************************************************/
309 329
310typedef struct 330typedef struct
311{ 331{
312 struct ev_signal *head; 332 struct ev_signal *head;
313 sig_atomic_t gotsig; 333 sig_atomic_t volatile gotsig;
314} ANSIG; 334} ANSIG;
315 335
316static ANSIG *signals; 336static ANSIG *signals;
317static int signalmax; 337static int signalmax;
318 338
319static int sigpipe [2]; 339static int sigpipe [2];
320static sig_atomic_t gotsig; 340static sig_atomic_t volatile gotsig;
321static struct ev_io sigev; 341static struct ev_io sigev;
322 342
323static void 343static void
324signals_init (ANSIG *base, int count) 344signals_init (ANSIG *base, int count)
325{ 345{
326 while (count--) 346 while (count--)
327 { 347 {
328 base->head = 0; 348 base->head = 0;
329 base->gotsig = 0; 349 base->gotsig = 0;
350
330 ++base; 351 ++base;
331 } 352 }
332} 353}
333 354
334static void 355static void
337 signals [signum - 1].gotsig = 1; 358 signals [signum - 1].gotsig = 1;
338 359
339 if (!gotsig) 360 if (!gotsig)
340 { 361 {
341 gotsig = 1; 362 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1); 363 write (sigpipe [1], &signum, 1);
343 } 364 }
344} 365}
345 366
346static void 367static void
347sigcb (struct ev_io *iow, int revents) 368sigcb (struct ev_io *iow, int revents)
348{ 369{
349 struct ev_signal *w; 370 struct ev_signal *w;
350 int sig; 371 int signum;
351 372
373 read (sigpipe [0], &revents, 1);
352 gotsig = 0; 374 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354 375
355 for (sig = signalmax; sig--; ) 376 for (signum = signalmax; signum--; )
356 if (signals [sig].gotsig) 377 if (signals [signum].gotsig)
357 { 378 {
358 signals [sig].gotsig = 0; 379 signals [signum].gotsig = 0;
359 380
360 for (w = signals [sig].head; w; w = w->next) 381 for (w = signals [signum].head; w; w = w->next)
361 event ((W)w, EV_SIGNAL); 382 event ((W)w, EV_SIGNAL);
362 } 383 }
363} 384}
364 385
365static void 386static void
370 391
371 /* rather than sort out wether we really need nb, set it */ 392 /* rather than sort out wether we really need nb, set it */
372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 393 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 394 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
374 395
375 evio_set (&sigev, sigpipe [0], EV_READ); 396 ev_io_set (&sigev, sigpipe [0], EV_READ);
376 evio_start (&sigev); 397 ev_io_start (&sigev);
377} 398}
378 399
379/*****************************************************************************/ 400/*****************************************************************************/
380 401
381static struct ev_idle **idles; 402static struct ev_idle **idles;
402 struct ev_child *w; 423 struct ev_child *w;
403 int pid, status; 424 int pid, status;
404 425
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 426 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 427 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1) 428 if (w->pid == pid || !w->pid)
408 { 429 {
409 w->status = status; 430 w->status = status;
410 event ((W)w, EV_CHILD); 431 event ((W)w, EV_CHILD);
411 } 432 }
412} 433}
413 434
414/*****************************************************************************/ 435/*****************************************************************************/
415 436
416#if HAVE_EPOLL 437#if EV_USE_EPOLL
417# include "ev_epoll.c" 438# include "ev_epoll.c"
418#endif 439#endif
419#if HAVE_SELECT 440#if EV_USE_SELECT
420# include "ev_select.c" 441# include "ev_select.c"
421#endif 442#endif
422 443
423int 444int
424ev_version_major (void) 445ev_version_major (void)
434 455
435int ev_init (int flags) 456int ev_init (int flags)
436{ 457{
437 if (!ev_method) 458 if (!ev_method)
438 { 459 {
439#if HAVE_MONOTONIC 460#if EV_USE_MONOTONIC
440 { 461 {
441 struct timespec ts; 462 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 463 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 464 have_monotonic = 1;
444 } 465 }
450 471
451 if (pipe (sigpipe)) 472 if (pipe (sigpipe))
452 return 0; 473 return 0;
453 474
454 ev_method = EVMETHOD_NONE; 475 ev_method = EVMETHOD_NONE;
455#if HAVE_EPOLL 476#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 477 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457#endif 478#endif
458#if HAVE_SELECT 479#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 480 if (ev_method == EVMETHOD_NONE) select_init (flags);
460#endif 481#endif
461 482
462 if (ev_method) 483 if (ev_method)
463 { 484 {
464 evw_init (&sigev, sigcb); 485 ev_watcher_init (&sigev, sigcb);
465 siginit (); 486 siginit ();
466 487
467 evsignal_init (&childev, childcb, SIGCHLD); 488 ev_signal_init (&childev, childcb, SIGCHLD);
468 evsignal_start (&childev); 489 ev_signal_start (&childev);
469 } 490 }
470 } 491 }
471 492
472 return ev_method; 493 return ev_method;
473} 494}
474 495
475/*****************************************************************************/ 496/*****************************************************************************/
476 497
477void 498void
478ev_prefork (void) 499ev_fork_prepare (void)
479{ 500{
480 /* nop */ 501 /* nop */
481} 502}
482 503
483void 504void
484ev_postfork_parent (void) 505ev_fork_parent (void)
485{ 506{
486 /* nop */ 507 /* nop */
487} 508}
488 509
489void 510void
490ev_postfork_child (void) 511ev_fork_child (void)
491{ 512{
492#if HAVE_EPOLL 513#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 514 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 515 epoll_postfork_child ();
495#endif 516#endif
496 517
497 evio_stop (&sigev); 518 ev_io_stop (&sigev);
498 close (sigpipe [0]); 519 close (sigpipe [0]);
499 close (sigpipe [1]); 520 close (sigpipe [1]);
500 pipe (sigpipe); 521 pipe (sigpipe);
501 siginit (); 522 siginit ();
502} 523}
523{ 544{
524 while (timercnt && timers [0]->at <= now) 545 while (timercnt && timers [0]->at <= now)
525 { 546 {
526 struct ev_timer *w = timers [0]; 547 struct ev_timer *w = timers [0];
527 548
528 event ((W)w, EV_TIMEOUT);
529
530 /* first reschedule or stop timer */ 549 /* first reschedule or stop timer */
531 if (w->repeat) 550 if (w->repeat)
532 { 551 {
552 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 553 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 554 downheap ((WT *)timers, timercnt, 0);
536 } 555 }
537 else 556 else
538 evtimer_stop (w); /* nonrepeating: stop timer */ 557 ev_timer_stop (w); /* nonrepeating: stop timer */
558
559 event ((W)w, EV_TIMEOUT);
539 } 560 }
540} 561}
541 562
542static void 563static void
543periodics_reify (void) 564periodics_reify (void)
548 569
549 /* first reschedule or stop timer */ 570 /* first reschedule or stop timer */
550 if (w->interval) 571 if (w->interval)
551 { 572 {
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 573 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)); 574 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0); 575 downheap ((WT *)periodics, periodiccnt, 0);
555 } 576 }
556 else 577 else
557 evperiodic_stop (w); /* nonrepeating: stop timer */ 578 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 579
559 event ((W)w, EV_TIMEOUT); 580 event ((W)w, EV_PERIODIC);
560 } 581 }
561} 582}
562 583
563static void 584static void
564periodics_reschedule (ev_tstamp diff) 585periodics_reschedule (ev_tstamp diff)
574 { 595 {
575 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 596 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
576 597
577 if (fabs (diff) >= 1e-4) 598 if (fabs (diff) >= 1e-4)
578 { 599 {
579 evperiodic_stop (w); 600 ev_periodic_stop (w);
580 evperiodic_start (w); 601 ev_periodic_start (w);
581 602
582 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 603 i = 0; /* restart loop, inefficient, but time jumps should be rare */
583 } 604 }
584 } 605 }
585 } 606 }
719 head = &(*head)->next; 740 head = &(*head)->next;
720 } 741 }
721} 742}
722 743
723static void 744static void
724ev_clear (W w) 745ev_clear_pending (W w)
725{ 746{
726 if (w->pending) 747 if (w->pending)
727 { 748 {
728 pendings [w->pending - 1].w = 0; 749 pendings [w->pending - 1].w = 0;
729 w->pending = 0; 750 w->pending = 0;
743} 764}
744 765
745/*****************************************************************************/ 766/*****************************************************************************/
746 767
747void 768void
748evio_start (struct ev_io *w) 769ev_io_start (struct ev_io *w)
749{ 770{
750 if (ev_is_active (w))
751 return;
752
753 int fd = w->fd; 771 int fd = w->fd;
772
773 if (ev_is_active (w))
774 return;
775
776 assert (("ev_io_start called with negative fd", fd >= 0));
754 777
755 ev_start ((W)w, 1); 778 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 779 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w); 780 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 781
759 fd_change (fd); 782 fd_change (fd);
760} 783}
761 784
762void 785void
763evio_stop (struct ev_io *w) 786ev_io_stop (struct ev_io *w)
764{ 787{
765 ev_clear ((W)w); 788 ev_clear_pending ((W)w);
766 if (!ev_is_active (w)) 789 if (!ev_is_active (w))
767 return; 790 return;
768 791
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 792 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w); 793 ev_stop ((W)w);
771 794
772 fd_change (w->fd); 795 fd_change (w->fd);
773} 796}
774 797
775void 798void
776evtimer_start (struct ev_timer *w) 799ev_timer_start (struct ev_timer *w)
777{ 800{
778 if (ev_is_active (w)) 801 if (ev_is_active (w))
779 return; 802 return;
780 803
781 w->at += now; 804 w->at += now;
782 805
783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 806 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
784 807
785 ev_start ((W)w, ++timercnt); 808 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, ); 809 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w; 810 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 811 upheap ((WT *)timers, timercnt - 1);
789} 812}
790 813
791void 814void
792evtimer_stop (struct ev_timer *w) 815ev_timer_stop (struct ev_timer *w)
793{ 816{
794 ev_clear ((W)w); 817 ev_clear_pending ((W)w);
795 if (!ev_is_active (w)) 818 if (!ev_is_active (w))
796 return; 819 return;
797 820
798 if (w->active < timercnt--) 821 if (w->active < timercnt--)
799 { 822 {
805 828
806 ev_stop ((W)w); 829 ev_stop ((W)w);
807} 830}
808 831
809void 832void
810evtimer_again (struct ev_timer *w) 833ev_timer_again (struct ev_timer *w)
811{ 834{
812 if (ev_is_active (w)) 835 if (ev_is_active (w))
813 { 836 {
814 if (w->repeat) 837 if (w->repeat)
815 { 838 {
816 w->at = now + w->repeat; 839 w->at = now + w->repeat;
817 downheap ((WT *)timers, timercnt, w->active - 1); 840 downheap ((WT *)timers, timercnt, w->active - 1);
818 } 841 }
819 else 842 else
820 evtimer_stop (w); 843 ev_timer_stop (w);
821 } 844 }
822 else if (w->repeat) 845 else if (w->repeat)
823 evtimer_start (w); 846 ev_timer_start (w);
824} 847}
825 848
826void 849void
827evperiodic_start (struct ev_periodic *w) 850ev_periodic_start (struct ev_periodic *w)
828{ 851{
829 if (ev_is_active (w)) 852 if (ev_is_active (w))
830 return; 853 return;
831 854
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 855 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
833 856
834 /* this formula differs from the one in periodic_reify because we do not always round up */ 857 /* this formula differs from the one in periodic_reify because we do not always round up */
835 if (w->interval) 858 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 859 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837 860
840 periodics [periodiccnt - 1] = w; 863 periodics [periodiccnt - 1] = w;
841 upheap ((WT *)periodics, periodiccnt - 1); 864 upheap ((WT *)periodics, periodiccnt - 1);
842} 865}
843 866
844void 867void
845evperiodic_stop (struct ev_periodic *w) 868ev_periodic_stop (struct ev_periodic *w)
846{ 869{
847 ev_clear ((W)w); 870 ev_clear_pending ((W)w);
848 if (!ev_is_active (w)) 871 if (!ev_is_active (w))
849 return; 872 return;
850 873
851 if (w->active < periodiccnt--) 874 if (w->active < periodiccnt--)
852 { 875 {
856 879
857 ev_stop ((W)w); 880 ev_stop ((W)w);
858} 881}
859 882
860void 883void
861evsignal_start (struct ev_signal *w) 884ev_signal_start (struct ev_signal *w)
862{ 885{
863 if (ev_is_active (w)) 886 if (ev_is_active (w))
864 return; 887 return;
888
889 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
865 890
866 ev_start ((W)w, 1); 891 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init); 892 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 893 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 894
876 sigaction (w->signum, &sa, 0); 901 sigaction (w->signum, &sa, 0);
877 } 902 }
878} 903}
879 904
880void 905void
881evsignal_stop (struct ev_signal *w) 906ev_signal_stop (struct ev_signal *w)
882{ 907{
883 ev_clear ((W)w); 908 ev_clear_pending ((W)w);
884 if (!ev_is_active (w)) 909 if (!ev_is_active (w))
885 return; 910 return;
886 911
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 912 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w); 913 ev_stop ((W)w);
889 914
890 if (!signals [w->signum - 1].head) 915 if (!signals [w->signum - 1].head)
891 signal (w->signum, SIG_DFL); 916 signal (w->signum, SIG_DFL);
892} 917}
893 918
919void
894void evidle_start (struct ev_idle *w) 920ev_idle_start (struct ev_idle *w)
895{ 921{
896 if (ev_is_active (w)) 922 if (ev_is_active (w))
897 return; 923 return;
898 924
899 ev_start ((W)w, ++idlecnt); 925 ev_start ((W)w, ++idlecnt);
900 array_needsize (idles, idlemax, idlecnt, ); 926 array_needsize (idles, idlemax, idlecnt, );
901 idles [idlecnt - 1] = w; 927 idles [idlecnt - 1] = w;
902} 928}
903 929
930void
904void evidle_stop (struct ev_idle *w) 931ev_idle_stop (struct ev_idle *w)
905{ 932{
906 ev_clear ((W)w); 933 ev_clear_pending ((W)w);
907 if (ev_is_active (w)) 934 if (ev_is_active (w))
908 return; 935 return;
909 936
910 idles [w->active - 1] = idles [--idlecnt]; 937 idles [w->active - 1] = idles [--idlecnt];
911 ev_stop ((W)w); 938 ev_stop ((W)w);
912} 939}
913 940
941void
914void evprepare_start (struct ev_prepare *w) 942ev_prepare_start (struct ev_prepare *w)
915{ 943{
916 if (ev_is_active (w)) 944 if (ev_is_active (w))
917 return; 945 return;
918 946
919 ev_start ((W)w, ++preparecnt); 947 ev_start ((W)w, ++preparecnt);
920 array_needsize (prepares, preparemax, preparecnt, ); 948 array_needsize (prepares, preparemax, preparecnt, );
921 prepares [preparecnt - 1] = w; 949 prepares [preparecnt - 1] = w;
922} 950}
923 951
952void
924void evprepare_stop (struct ev_prepare *w) 953ev_prepare_stop (struct ev_prepare *w)
925{ 954{
926 ev_clear ((W)w); 955 ev_clear_pending ((W)w);
927 if (ev_is_active (w)) 956 if (ev_is_active (w))
928 return; 957 return;
929 958
930 prepares [w->active - 1] = prepares [--preparecnt]; 959 prepares [w->active - 1] = prepares [--preparecnt];
931 ev_stop ((W)w); 960 ev_stop ((W)w);
932} 961}
933 962
963void
934void evcheck_start (struct ev_check *w) 964ev_check_start (struct ev_check *w)
935{ 965{
936 if (ev_is_active (w)) 966 if (ev_is_active (w))
937 return; 967 return;
938 968
939 ev_start ((W)w, ++checkcnt); 969 ev_start ((W)w, ++checkcnt);
940 array_needsize (checks, checkmax, checkcnt, ); 970 array_needsize (checks, checkmax, checkcnt, );
941 checks [checkcnt - 1] = w; 971 checks [checkcnt - 1] = w;
942} 972}
943 973
974void
944void evcheck_stop (struct ev_check *w) 975ev_check_stop (struct ev_check *w)
945{ 976{
946 ev_clear ((W)w); 977 ev_clear_pending ((W)w);
947 if (ev_is_active (w)) 978 if (ev_is_active (w))
948 return; 979 return;
949 980
950 checks [w->active - 1] = checks [--checkcnt]; 981 checks [w->active - 1] = checks [--checkcnt];
951 ev_stop ((W)w); 982 ev_stop ((W)w);
952} 983}
953 984
985void
954void evchild_start (struct ev_child *w) 986ev_child_start (struct ev_child *w)
955{ 987{
956 if (ev_is_active (w)) 988 if (ev_is_active (w))
957 return; 989 return;
958 990
959 ev_start ((W)w, 1); 991 ev_start ((W)w, 1);
960 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 992 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
961} 993}
962 994
995void
963void evchild_stop (struct ev_child *w) 996ev_child_stop (struct ev_child *w)
964{ 997{
965 ev_clear ((W)w); 998 ev_clear_pending ((W)w);
966 if (ev_is_active (w)) 999 if (ev_is_active (w))
967 return; 1000 return;
968 1001
969 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1002 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
970 ev_stop ((W)w); 1003 ev_stop ((W)w);
984once_cb (struct ev_once *once, int revents) 1017once_cb (struct ev_once *once, int revents)
985{ 1018{
986 void (*cb)(int revents, void *arg) = once->cb; 1019 void (*cb)(int revents, void *arg) = once->cb;
987 void *arg = once->arg; 1020 void *arg = once->arg;
988 1021
989 evio_stop (&once->io); 1022 ev_io_stop (&once->io);
990 evtimer_stop (&once->to); 1023 ev_timer_stop (&once->to);
991 free (once); 1024 free (once);
992 1025
993 cb (revents, arg); 1026 cb (revents, arg);
994} 1027}
995 1028
1009ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1042ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1010{ 1043{
1011 struct ev_once *once = malloc (sizeof (struct ev_once)); 1044 struct ev_once *once = malloc (sizeof (struct ev_once));
1012 1045
1013 if (!once) 1046 if (!once)
1014 cb (EV_ERROR, arg); 1047 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1015 else 1048 else
1016 { 1049 {
1017 once->cb = cb; 1050 once->cb = cb;
1018 once->arg = arg; 1051 once->arg = arg;
1019 1052
1020 evw_init (&once->io, once_cb_io); 1053 ev_watcher_init (&once->io, once_cb_io);
1021
1022 if (fd >= 0) 1054 if (fd >= 0)
1023 { 1055 {
1024 evio_set (&once->io, fd, events); 1056 ev_io_set (&once->io, fd, events);
1025 evio_start (&once->io); 1057 ev_io_start (&once->io);
1026 } 1058 }
1027 1059
1028 evw_init (&once->to, once_cb_to); 1060 ev_watcher_init (&once->to, once_cb_to);
1029
1030 if (timeout >= 0.) 1061 if (timeout >= 0.)
1031 { 1062 {
1032 evtimer_set (&once->to, timeout, 0.); 1063 ev_timer_set (&once->to, timeout, 0.);
1033 evtimer_start (&once->to); 1064 ev_timer_start (&once->to);
1034 } 1065 }
1035 } 1066 }
1036} 1067}
1037 1068
1038/*****************************************************************************/ 1069/*****************************************************************************/
1049 1080
1050static void 1081static void
1051ocb (struct ev_timer *w, int revents) 1082ocb (struct ev_timer *w, int revents)
1052{ 1083{
1053 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1084 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1054 evtimer_stop (w); 1085 ev_timer_stop (w);
1055 evtimer_start (w); 1086 ev_timer_start (w);
1056} 1087}
1057 1088
1058static void 1089static void
1059scb (struct ev_signal *w, int revents) 1090scb (struct ev_signal *w, int revents)
1060{ 1091{
1061 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1092 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1062 evio_stop (&wio); 1093 ev_io_stop (&wio);
1063 evio_start (&wio); 1094 ev_io_start (&wio);
1064} 1095}
1065 1096
1066static void 1097static void
1067gcb (struct ev_signal *w, int revents) 1098gcb (struct ev_signal *w, int revents)
1068{ 1099{
1072 1103
1073int main (void) 1104int main (void)
1074{ 1105{
1075 ev_init (0); 1106 ev_init (0);
1076 1107
1077 evio_init (&wio, sin_cb, 0, EV_READ); 1108 ev_io_init (&wio, sin_cb, 0, EV_READ);
1078 evio_start (&wio); 1109 ev_io_start (&wio);
1079 1110
1080 struct ev_timer t[10000]; 1111 struct ev_timer t[10000];
1081 1112
1082#if 0 1113#if 0
1083 int i; 1114 int i;
1084 for (i = 0; i < 10000; ++i) 1115 for (i = 0; i < 10000; ++i)
1085 { 1116 {
1086 struct ev_timer *w = t + i; 1117 struct ev_timer *w = t + i;
1087 evw_init (w, ocb, i); 1118 ev_watcher_init (w, ocb, i);
1088 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1119 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1089 evtimer_start (w); 1120 ev_timer_start (w);
1090 if (drand48 () < 0.5) 1121 if (drand48 () < 0.5)
1091 evtimer_stop (w); 1122 ev_timer_stop (w);
1092 } 1123 }
1093#endif 1124#endif
1094 1125
1095 struct ev_timer t1; 1126 struct ev_timer t1;
1096 evtimer_init (&t1, ocb, 5, 10); 1127 ev_timer_init (&t1, ocb, 5, 10);
1097 evtimer_start (&t1); 1128 ev_timer_start (&t1);
1098 1129
1099 struct ev_signal sig; 1130 struct ev_signal sig;
1100 evsignal_init (&sig, scb, SIGQUIT); 1131 ev_signal_init (&sig, scb, SIGQUIT);
1101 evsignal_start (&sig); 1132 ev_signal_start (&sig);
1102 1133
1103 struct ev_check cw; 1134 struct ev_check cw;
1104 evcheck_init (&cw, gcb); 1135 ev_check_init (&cw, gcb);
1105 evcheck_start (&cw); 1136 ev_check_start (&cw);
1106 1137
1107 struct ev_idle iw; 1138 struct ev_idle iw;
1108 evidle_init (&iw, gcb); 1139 ev_idle_init (&iw, gcb);
1109 evidle_start (&iw); 1140 ev_idle_start (&iw);
1110 1141
1111 ev_loop (0); 1142 ev_loop (0);
1112 1143
1113 return 0; 1144 return 0;
1114} 1145}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines