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

Comparing libev/ev.c (file contents):
Revision 1.22 by root, Wed Oct 31 19:07:43 2007 UTC vs.
Revision 1.32 by root, Thu Nov 1 09:21:51 2007 UTC

24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * (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. 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
29 32
30#include <math.h> 33#include <math.h>
31#include <stdlib.h> 34#include <stdlib.h>
32#include <unistd.h> 35#include <unistd.h>
33#include <fcntl.h> 36#include <fcntl.h>
41#include <sys/types.h> 44#include <sys/types.h>
42#include <sys/wait.h> 45#include <sys/wait.h>
43#include <sys/time.h> 46#include <sys/time.h>
44#include <time.h> 47#include <time.h>
45 48
46#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
49# endif 52# endif
50#endif 53#endif
51 54
52#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
54#endif 57#endif
55 58
56#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
58#endif 61#endif
59 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
60#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 68#endif
63 69
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 71#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 */ 72#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 */
67 74
68#include "ev.h" 75#include "ev.h"
69 76
70typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
84/*****************************************************************************/ 91/*****************************************************************************/
85 92
86ev_tstamp 93ev_tstamp
87ev_time (void) 94ev_time (void)
88{ 95{
89#if HAVE_REALTIME 96#if EV_USE_REALTIME
90 struct timespec ts; 97 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 100#else
94 struct timeval tv; 101 struct timeval tv;
98} 105}
99 106
100static ev_tstamp 107static ev_tstamp
101get_clock (void) 108get_clock (void)
102{ 109{
103#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
104 if (have_monotonic) 111 if (have_monotonic)
105 { 112 {
106 struct timespec ts; 113 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
110#endif 117#endif
111 118
112 return ev_time (); 119 return ev_time ();
113} 120}
114 121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
123
115#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
117 { \ 126 { \
118 int newcnt = cur ? cur << 1 : 16; \ 127 int newcnt = cur; \
128 do \
129 { \
130 newcnt = array_roundsize (base, newcnt << 1); \
131 } \
132 while ((cnt) > newcnt); \
133 \
119 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
120 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
121 cur = newcnt; \ 136 cur = newcnt; \
122 } 137 }
123 138
124/*****************************************************************************/ 139/*****************************************************************************/
125 140
126typedef struct 141typedef struct
127{ 142{
128 struct ev_io *head; 143 struct ev_io *head;
129 unsigned char wev, rev; /* want, received event set */ 144 int events;
130} ANFD; 145} ANFD;
131 146
132static ANFD *anfds; 147static ANFD *anfds;
133static int anfdmax; 148static int anfdmax;
134 149
135static int *fdchanges;
136static int fdchangemax, fdchangecnt;
137
138static void 150static void
139anfds_init (ANFD *base, int count) 151anfds_init (ANFD *base, int count)
140{ 152{
141 while (count--) 153 while (count--)
142 { 154 {
143 base->head = 0; 155 base->head = 0;
144 base->wev = base->rev = EV_NONE; 156 base->events = EV_NONE;
145 ++base; 157 ++base;
146 } 158 }
147} 159}
148 160
149typedef struct 161typedef struct
156static int pendingmax, pendingcnt; 168static int pendingmax, pendingcnt;
157 169
158static void 170static void
159event (W w, int events) 171event (W w, int events)
160{ 172{
161 if (w->active) 173 if (w->pending)
174 {
175 pendings [w->pending - 1].events |= events;
176 return;
162 { 177 }
178
163 w->pending = ++pendingcnt; 179 w->pending = ++pendingcnt;
164 array_needsize (pendings, pendingmax, pendingcnt, ); 180 array_needsize (pendings, pendingmax, pendingcnt, );
165 pendings [pendingcnt - 1].w = w; 181 pendings [pendingcnt - 1].w = w;
166 pendings [pendingcnt - 1].events = events; 182 pendings [pendingcnt - 1].events = events;
167 } 183}
184
185static void
186queue_events (W *events, int eventcnt, int type)
187{
188 int i;
189
190 for (i = 0; i < eventcnt; ++i)
191 event (events [i], type);
168} 192}
169 193
170static void 194static void
171fd_event (int fd, int events) 195fd_event (int fd, int events)
172{ 196{
180 if (ev) 204 if (ev)
181 event ((W)w, ev); 205 event ((W)w, ev);
182 } 206 }
183} 207}
184 208
209/*****************************************************************************/
210
211static int *fdchanges;
212static int fdchangemax, fdchangecnt;
213
185static void 214static void
186queue_events (W *events, int eventcnt, int type) 215fd_reify (void)
187{ 216{
188 int i; 217 int i;
189 218
190 for (i = 0; i < eventcnt; ++i) 219 for (i = 0; i < fdchangecnt; ++i)
191 event (events [i], type); 220 {
221 int fd = fdchanges [i];
222 ANFD *anfd = anfds + fd;
223 struct ev_io *w;
224
225 int events = 0;
226
227 for (w = anfd->head; w; w = w->next)
228 events |= w->events;
229
230 anfd->events &= ~EV_REIFY;
231
232 if (anfd->events != events)
233 {
234 method_modify (fd, anfd->events, events);
235 anfd->events = events;
236 }
237 }
238
239 fdchangecnt = 0;
240}
241
242static void
243fd_change (int fd)
244{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0)
246 return;
247
248 anfds [fd].events |= EV_REIFY;
249
250 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd;
192} 253}
193 254
194/* called on EBADF to verify fds */ 255/* called on EBADF to verify fds */
195static void 256static void
196fd_recheck () 257fd_recheck (void)
197{ 258{
198 int fd; 259 int fd;
199 260
200 for (fd = 0; fd < anfdmax; ++fd) 261 for (fd = 0; fd < anfdmax; ++fd)
201 if (anfds [fd].wev) 262 if (anfds [fd].events)
202 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
203 while (anfds [fd].head) 264 while (anfds [fd].head)
265 {
204 evio_stop (anfds [fd].head); 266 ev_io_stop (anfds [fd].head);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
268 }
205} 269}
206 270
207/*****************************************************************************/ 271/*****************************************************************************/
208 272
209static struct ev_timer **timers; 273static struct ev_timer **timers;
318 382
319 /* rather than sort out wether we really need nb, set it */ 383 /* rather than sort out wether we really need nb, set it */
320 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
321 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
322 386
323 evio_set (&sigev, sigpipe [0], EV_READ); 387 ev_io_set (&sigev, sigpipe [0], EV_READ);
324 evio_start (&sigev); 388 ev_io_start (&sigev);
325} 389}
326 390
327/*****************************************************************************/ 391/*****************************************************************************/
328 392
329static struct ev_idle **idles; 393static struct ev_idle **idles;
359 } 423 }
360} 424}
361 425
362/*****************************************************************************/ 426/*****************************************************************************/
363 427
364#if HAVE_EPOLL 428#if EV_USE_EPOLL
365# include "ev_epoll.c" 429# include "ev_epoll.c"
366#endif 430#endif
367#if HAVE_SELECT 431#if EV_USE_SELECT
368# include "ev_select.c" 432# include "ev_select.c"
369#endif 433#endif
370 434
435int
436ev_version_major (void)
437{
438 return EV_VERSION_MAJOR;
439}
440
441int
442ev_version_minor (void)
443{
444 return EV_VERSION_MINOR;
445}
446
371int ev_init (int flags) 447int ev_init (int flags)
372{ 448{
373#if HAVE_MONOTONIC
374 {
375 struct timespec ts;
376 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
377 have_monotonic = 1;
378 }
379#endif
380
381 ev_now = ev_time ();
382 now = get_clock ();
383 diff = ev_now - now;
384
385 if (pipe (sigpipe))
386 return 0;
387
388 ev_method = EVMETHOD_NONE;
389#if HAVE_EPOLL
390 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
391#endif
392#if HAVE_SELECT
393 if (ev_method == EVMETHOD_NONE) select_init (flags);
394#endif
395
396 if (ev_method) 449 if (!ev_method)
450 {
451#if EV_USE_MONOTONIC
397 { 452 {
453 struct timespec ts;
454 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
455 have_monotonic = 1;
456 }
457#endif
458
459 ev_now = ev_time ();
460 now = get_clock ();
461 diff = ev_now - now;
462
463 if (pipe (sigpipe))
464 return 0;
465
466 ev_method = EVMETHOD_NONE;
467#if EV_USE_EPOLL
468 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
469#endif
470#if EV_USE_SELECT
471 if (ev_method == EVMETHOD_NONE) select_init (flags);
472#endif
473
474 if (ev_method)
475 {
398 evw_init (&sigev, sigcb); 476 ev_watcher_init (&sigev, sigcb);
399 siginit (); 477 siginit ();
400 478
401 evsignal_init (&childev, childcb, SIGCHLD); 479 ev_signal_init (&childev, childcb, SIGCHLD);
402 evsignal_start (&childev); 480 ev_signal_start (&childev);
481 }
403 } 482 }
404 483
405 return ev_method; 484 return ev_method;
406} 485}
407 486
408/*****************************************************************************/ 487/*****************************************************************************/
409 488
489void
410void ev_prefork (void) 490ev_prefork (void)
411{ 491{
412 /* nop */ 492 /* nop */
413} 493}
414 494
495void
415void ev_postfork_parent (void) 496ev_postfork_parent (void)
416{ 497{
417 /* nop */ 498 /* nop */
418} 499}
419 500
501void
420void ev_postfork_child (void) 502ev_postfork_child (void)
421{ 503{
422#if HAVE_EPOLL 504#if EV_USE_EPOLL
423 if (ev_method == EVMETHOD_EPOLL) 505 if (ev_method == EVMETHOD_EPOLL)
424 epoll_postfork_child (); 506 epoll_postfork_child ();
425#endif 507#endif
426 508
427 evio_stop (&sigev); 509 ev_io_stop (&sigev);
428 close (sigpipe [0]); 510 close (sigpipe [0]);
429 close (sigpipe [1]); 511 close (sigpipe [1]);
430 pipe (sigpipe); 512 pipe (sigpipe);
431 siginit (); 513 siginit ();
432} 514}
433 515
434/*****************************************************************************/ 516/*****************************************************************************/
435 517
436static void 518static void
437fd_reify (void)
438{
439 int i;
440
441 for (i = 0; i < fdchangecnt; ++i)
442 {
443 int fd = fdchanges [i];
444 ANFD *anfd = anfds + fd;
445 struct ev_io *w;
446
447 int wev = 0;
448
449 for (w = anfd->head; w; w = w->next)
450 wev |= w->events;
451
452 if (anfd->wev != wev)
453 {
454 method_modify (fd, anfd->wev, wev);
455 anfd->wev = wev;
456 }
457 }
458
459 fdchangecnt = 0;
460}
461
462static void
463call_pending () 519call_pending (void)
464{ 520{
465 while (pendingcnt) 521 while (pendingcnt)
466 { 522 {
467 ANPENDING *p = pendings + --pendingcnt; 523 ANPENDING *p = pendings + --pendingcnt;
468 524
473 } 529 }
474 } 530 }
475} 531}
476 532
477static void 533static void
478timers_reify () 534timers_reify (void)
479{ 535{
480 while (timercnt && timers [0]->at <= now) 536 while (timercnt && timers [0]->at <= now)
481 { 537 {
482 struct ev_timer *w = timers [0]; 538 struct ev_timer *w = timers [0];
483
484 event ((W)w, EV_TIMEOUT);
485 539
486 /* first reschedule or stop timer */ 540 /* first reschedule or stop timer */
487 if (w->repeat) 541 if (w->repeat)
488 { 542 {
489 w->at = now + w->repeat; 543 w->at = now + w->repeat;
490 assert (("timer timeout in the past, negative repeat?", w->at > now)); 544 assert (("timer timeout in the past, negative repeat?", w->at > now));
491 downheap ((WT *)timers, timercnt, 0); 545 downheap ((WT *)timers, timercnt, 0);
492 } 546 }
493 else 547 else
494 evtimer_stop (w); /* nonrepeating: stop timer */ 548 ev_timer_stop (w); /* nonrepeating: stop timer */
495 }
496}
497 549
550 event ((W)w, EV_TIMEOUT);
551 }
552}
553
498static void 554static void
499periodics_reify () 555periodics_reify (void)
500{ 556{
501 while (periodiccnt && periodics [0]->at <= ev_now) 557 while (periodiccnt && periodics [0]->at <= ev_now)
502 { 558 {
503 struct ev_periodic *w = periodics [0]; 559 struct ev_periodic *w = periodics [0];
504 560
508 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 564 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
509 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 565 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
510 downheap ((WT *)periodics, periodiccnt, 0); 566 downheap ((WT *)periodics, periodiccnt, 0);
511 } 567 }
512 else 568 else
513 evperiodic_stop (w); /* nonrepeating: stop timer */ 569 ev_periodic_stop (w); /* nonrepeating: stop timer */
514 570
515 event ((W)w, EV_TIMEOUT); 571 event ((W)w, EV_TIMEOUT);
516 } 572 }
517} 573}
518 574
530 { 586 {
531 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 587 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
532 588
533 if (fabs (diff) >= 1e-4) 589 if (fabs (diff) >= 1e-4)
534 { 590 {
535 evperiodic_stop (w); 591 ev_periodic_stop (w);
536 evperiodic_start (w); 592 ev_periodic_start (w);
537 593
538 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 594 i = 0; /* restart loop, inefficient, but time jumps should be rare */
539 } 595 }
540 } 596 }
541 } 597 }
542} 598}
543 599
544static void 600static void
545time_update () 601time_update (void)
546{ 602{
547 int i; 603 int i;
548 604
549 ev_now = ev_time (); 605 ev_now = ev_time ();
550 606
584int ev_loop_done; 640int ev_loop_done;
585 641
586void ev_loop (int flags) 642void ev_loop (int flags)
587{ 643{
588 double block; 644 double block;
589 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
590 646
591 do 647 do
592 { 648 {
593 /* queue check watchers (and execute them) */ 649 /* queue check watchers (and execute them) */
594 if (preparecnt) 650 if (preparecnt)
699} 755}
700 756
701/*****************************************************************************/ 757/*****************************************************************************/
702 758
703void 759void
704evio_start (struct ev_io *w) 760ev_io_start (struct ev_io *w)
705{ 761{
706 if (ev_is_active (w)) 762 if (ev_is_active (w))
707 return; 763 return;
708 764
709 int fd = w->fd; 765 int fd = w->fd;
710 766
711 ev_start ((W)w, 1); 767 ev_start ((W)w, 1);
712 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 768 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
713 wlist_add ((WL *)&anfds[fd].head, (WL)w); 769 wlist_add ((WL *)&anfds[fd].head, (WL)w);
714 770
715 ++fdchangecnt; 771 fd_change (fd);
716 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
717 fdchanges [fdchangecnt - 1] = fd;
718} 772}
719 773
720void 774void
721evio_stop (struct ev_io *w) 775ev_io_stop (struct ev_io *w)
722{ 776{
723 ev_clear ((W)w); 777 ev_clear ((W)w);
724 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
725 return; 779 return;
726 780
727 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
728 ev_stop ((W)w); 782 ev_stop ((W)w);
729 783
730 ++fdchangecnt; 784 fd_change (w->fd);
731 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
732 fdchanges [fdchangecnt - 1] = w->fd;
733} 785}
734 786
735void 787void
736evtimer_start (struct ev_timer *w) 788ev_timer_start (struct ev_timer *w)
737{ 789{
738 if (ev_is_active (w)) 790 if (ev_is_active (w))
739 return; 791 return;
740 792
741 w->at += now; 793 w->at += now;
747 timers [timercnt - 1] = w; 799 timers [timercnt - 1] = w;
748 upheap ((WT *)timers, timercnt - 1); 800 upheap ((WT *)timers, timercnt - 1);
749} 801}
750 802
751void 803void
752evtimer_stop (struct ev_timer *w) 804ev_timer_stop (struct ev_timer *w)
753{ 805{
754 ev_clear ((W)w); 806 ev_clear ((W)w);
755 if (!ev_is_active (w)) 807 if (!ev_is_active (w))
756 return; 808 return;
757 809
765 817
766 ev_stop ((W)w); 818 ev_stop ((W)w);
767} 819}
768 820
769void 821void
770evtimer_again (struct ev_timer *w) 822ev_timer_again (struct ev_timer *w)
771{ 823{
772 if (ev_is_active (w)) 824 if (ev_is_active (w))
773 { 825 {
774 if (w->repeat) 826 if (w->repeat)
775 { 827 {
776 w->at = now + w->repeat; 828 w->at = now + w->repeat;
777 downheap ((WT *)timers, timercnt, w->active - 1); 829 downheap ((WT *)timers, timercnt, w->active - 1);
778 } 830 }
779 else 831 else
780 evtimer_stop (w); 832 ev_timer_stop (w);
781 } 833 }
782 else if (w->repeat) 834 else if (w->repeat)
783 evtimer_start (w); 835 ev_timer_start (w);
784} 836}
785 837
786void 838void
787evperiodic_start (struct ev_periodic *w) 839ev_periodic_start (struct ev_periodic *w)
788{ 840{
789 if (ev_is_active (w)) 841 if (ev_is_active (w))
790 return; 842 return;
791 843
792 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 844 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
800 periodics [periodiccnt - 1] = w; 852 periodics [periodiccnt - 1] = w;
801 upheap ((WT *)periodics, periodiccnt - 1); 853 upheap ((WT *)periodics, periodiccnt - 1);
802} 854}
803 855
804void 856void
805evperiodic_stop (struct ev_periodic *w) 857ev_periodic_stop (struct ev_periodic *w)
806{ 858{
807 ev_clear ((W)w); 859 ev_clear ((W)w);
808 if (!ev_is_active (w)) 860 if (!ev_is_active (w))
809 return; 861 return;
810 862
816 868
817 ev_stop ((W)w); 869 ev_stop ((W)w);
818} 870}
819 871
820void 872void
821evsignal_start (struct ev_signal *w) 873ev_signal_start (struct ev_signal *w)
822{ 874{
823 if (ev_is_active (w)) 875 if (ev_is_active (w))
824 return; 876 return;
825 877
826 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
836 sigaction (w->signum, &sa, 0); 888 sigaction (w->signum, &sa, 0);
837 } 889 }
838} 890}
839 891
840void 892void
841evsignal_stop (struct ev_signal *w) 893ev_signal_stop (struct ev_signal *w)
842{ 894{
843 ev_clear ((W)w); 895 ev_clear ((W)w);
844 if (!ev_is_active (w)) 896 if (!ev_is_active (w))
845 return; 897 return;
846 898
849 901
850 if (!signals [w->signum - 1].head) 902 if (!signals [w->signum - 1].head)
851 signal (w->signum, SIG_DFL); 903 signal (w->signum, SIG_DFL);
852} 904}
853 905
906void
854void evidle_start (struct ev_idle *w) 907ev_idle_start (struct ev_idle *w)
855{ 908{
856 if (ev_is_active (w)) 909 if (ev_is_active (w))
857 return; 910 return;
858 911
859 ev_start ((W)w, ++idlecnt); 912 ev_start ((W)w, ++idlecnt);
860 array_needsize (idles, idlemax, idlecnt, ); 913 array_needsize (idles, idlemax, idlecnt, );
861 idles [idlecnt - 1] = w; 914 idles [idlecnt - 1] = w;
862} 915}
863 916
917void
864void evidle_stop (struct ev_idle *w) 918ev_idle_stop (struct ev_idle *w)
865{ 919{
866 ev_clear ((W)w); 920 ev_clear ((W)w);
867 if (ev_is_active (w)) 921 if (ev_is_active (w))
868 return; 922 return;
869 923
870 idles [w->active - 1] = idles [--idlecnt]; 924 idles [w->active - 1] = idles [--idlecnt];
871 ev_stop ((W)w); 925 ev_stop ((W)w);
872} 926}
873 927
928void
874void evprepare_start (struct ev_prepare *w) 929ev_prepare_start (struct ev_prepare *w)
875{ 930{
876 if (ev_is_active (w)) 931 if (ev_is_active (w))
877 return; 932 return;
878 933
879 ev_start ((W)w, ++preparecnt); 934 ev_start ((W)w, ++preparecnt);
880 array_needsize (prepares, preparemax, preparecnt, ); 935 array_needsize (prepares, preparemax, preparecnt, );
881 prepares [preparecnt - 1] = w; 936 prepares [preparecnt - 1] = w;
882} 937}
883 938
939void
884void evprepare_stop (struct ev_prepare *w) 940ev_prepare_stop (struct ev_prepare *w)
885{ 941{
886 ev_clear ((W)w); 942 ev_clear ((W)w);
887 if (ev_is_active (w)) 943 if (ev_is_active (w))
888 return; 944 return;
889 945
890 prepares [w->active - 1] = prepares [--preparecnt]; 946 prepares [w->active - 1] = prepares [--preparecnt];
891 ev_stop ((W)w); 947 ev_stop ((W)w);
892} 948}
893 949
950void
894void evcheck_start (struct ev_check *w) 951ev_check_start (struct ev_check *w)
895{ 952{
896 if (ev_is_active (w)) 953 if (ev_is_active (w))
897 return; 954 return;
898 955
899 ev_start ((W)w, ++checkcnt); 956 ev_start ((W)w, ++checkcnt);
900 array_needsize (checks, checkmax, checkcnt, ); 957 array_needsize (checks, checkmax, checkcnt, );
901 checks [checkcnt - 1] = w; 958 checks [checkcnt - 1] = w;
902} 959}
903 960
961void
904void evcheck_stop (struct ev_check *w) 962ev_check_stop (struct ev_check *w)
905{ 963{
906 ev_clear ((W)w); 964 ev_clear ((W)w);
907 if (ev_is_active (w)) 965 if (ev_is_active (w))
908 return; 966 return;
909 967
910 checks [w->active - 1] = checks [--checkcnt]; 968 checks [w->active - 1] = checks [--checkcnt];
911 ev_stop ((W)w); 969 ev_stop ((W)w);
912} 970}
913 971
972void
914void evchild_start (struct ev_child *w) 973ev_child_start (struct ev_child *w)
915{ 974{
916 if (ev_is_active (w)) 975 if (ev_is_active (w))
917 return; 976 return;
918 977
919 ev_start ((W)w, 1); 978 ev_start ((W)w, 1);
920 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 979 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
921} 980}
922 981
982void
923void evchild_stop (struct ev_child *w) 983ev_child_stop (struct ev_child *w)
924{ 984{
925 ev_clear ((W)w); 985 ev_clear ((W)w);
926 if (ev_is_active (w)) 986 if (ev_is_active (w))
927 return; 987 return;
928 988
944once_cb (struct ev_once *once, int revents) 1004once_cb (struct ev_once *once, int revents)
945{ 1005{
946 void (*cb)(int revents, void *arg) = once->cb; 1006 void (*cb)(int revents, void *arg) = once->cb;
947 void *arg = once->arg; 1007 void *arg = once->arg;
948 1008
949 evio_stop (&once->io); 1009 ev_io_stop (&once->io);
950 evtimer_stop (&once->to); 1010 ev_timer_stop (&once->to);
951 free (once); 1011 free (once);
952 1012
953 cb (revents, arg); 1013 cb (revents, arg);
954} 1014}
955 1015
969ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1029ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
970{ 1030{
971 struct ev_once *once = malloc (sizeof (struct ev_once)); 1031 struct ev_once *once = malloc (sizeof (struct ev_once));
972 1032
973 if (!once) 1033 if (!once)
974 cb (EV_ERROR, arg); 1034 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
975 else 1035 else
976 { 1036 {
977 once->cb = cb; 1037 once->cb = cb;
978 once->arg = arg; 1038 once->arg = arg;
979 1039
980 evw_init (&once->io, once_cb_io); 1040 ev_watcher_init (&once->io, once_cb_io);
981
982 if (fd >= 0) 1041 if (fd >= 0)
983 { 1042 {
984 evio_set (&once->io, fd, events); 1043 ev_io_set (&once->io, fd, events);
985 evio_start (&once->io); 1044 ev_io_start (&once->io);
986 } 1045 }
987 1046
988 evw_init (&once->to, once_cb_to); 1047 ev_watcher_init (&once->to, once_cb_to);
989
990 if (timeout >= 0.) 1048 if (timeout >= 0.)
991 { 1049 {
992 evtimer_set (&once->to, timeout, 0.); 1050 ev_timer_set (&once->to, timeout, 0.);
993 evtimer_start (&once->to); 1051 ev_timer_start (&once->to);
994 } 1052 }
995 } 1053 }
996} 1054}
997 1055
998/*****************************************************************************/ 1056/*****************************************************************************/
1009 1067
1010static void 1068static void
1011ocb (struct ev_timer *w, int revents) 1069ocb (struct ev_timer *w, int revents)
1012{ 1070{
1013 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1071 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1014 evtimer_stop (w); 1072 ev_timer_stop (w);
1015 evtimer_start (w); 1073 ev_timer_start (w);
1016} 1074}
1017 1075
1018static void 1076static void
1019scb (struct ev_signal *w, int revents) 1077scb (struct ev_signal *w, int revents)
1020{ 1078{
1021 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1079 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1022 evio_stop (&wio); 1080 ev_io_stop (&wio);
1023 evio_start (&wio); 1081 ev_io_start (&wio);
1024} 1082}
1025 1083
1026static void 1084static void
1027gcb (struct ev_signal *w, int revents) 1085gcb (struct ev_signal *w, int revents)
1028{ 1086{
1032 1090
1033int main (void) 1091int main (void)
1034{ 1092{
1035 ev_init (0); 1093 ev_init (0);
1036 1094
1037 evio_init (&wio, sin_cb, 0, EV_READ); 1095 ev_io_init (&wio, sin_cb, 0, EV_READ);
1038 evio_start (&wio); 1096 ev_io_start (&wio);
1039 1097
1040 struct ev_timer t[10000]; 1098 struct ev_timer t[10000];
1041 1099
1042#if 0 1100#if 0
1043 int i; 1101 int i;
1044 for (i = 0; i < 10000; ++i) 1102 for (i = 0; i < 10000; ++i)
1045 { 1103 {
1046 struct ev_timer *w = t + i; 1104 struct ev_timer *w = t + i;
1047 evw_init (w, ocb, i); 1105 ev_watcher_init (w, ocb, i);
1048 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1106 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1049 evtimer_start (w); 1107 ev_timer_start (w);
1050 if (drand48 () < 0.5) 1108 if (drand48 () < 0.5)
1051 evtimer_stop (w); 1109 ev_timer_stop (w);
1052 } 1110 }
1053#endif 1111#endif
1054 1112
1055 struct ev_timer t1; 1113 struct ev_timer t1;
1056 evtimer_init (&t1, ocb, 5, 10); 1114 ev_timer_init (&t1, ocb, 5, 10);
1057 evtimer_start (&t1); 1115 ev_timer_start (&t1);
1058 1116
1059 struct ev_signal sig; 1117 struct ev_signal sig;
1060 evsignal_init (&sig, scb, SIGQUIT); 1118 ev_signal_init (&sig, scb, SIGQUIT);
1061 evsignal_start (&sig); 1119 ev_signal_start (&sig);
1062 1120
1063 struct ev_check cw; 1121 struct ev_check cw;
1064 evcheck_init (&cw, gcb); 1122 ev_check_init (&cw, gcb);
1065 evcheck_start (&cw); 1123 ev_check_start (&cw);
1066 1124
1067 struct ev_idle iw; 1125 struct ev_idle iw;
1068 evidle_init (&iw, gcb); 1126 ev_idle_init (&iw, gcb);
1069 evidle_start (&iw); 1127 ev_idle_start (&iw);
1070 1128
1071 ev_loop (0); 1129 ev_loop (0);
1072 1130
1073 return 0; 1131 return 0;
1074} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines