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

Comparing libev/ev.c (file contents):
Revision 1.17 by root, Wed Oct 31 14:44:15 2007 UTC vs.
Revision 1.28 by root, Thu Nov 1 06:48:49 2007 UTC

36 36
37#include <stdio.h> 37#include <stdio.h>
38 38
39#include <assert.h> 39#include <assert.h>
40#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
41#include <sys/time.h> 43#include <sys/time.h>
42#include <time.h> 44#include <time.h>
43
44#define HAVE_EPOLL 1
45 45
46#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
49# endif 49# endif
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 62#endif
63 63
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
66 67
67#include "ev.h" 68#include "ev.h"
68 69
69typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
70typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
112} 113}
113 114
114#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
115 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
116 { \ 117 { \
117 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
118 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
119 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
120 cur = newcnt; \ 127 cur = newcnt; \
121 } 128 }
122 129
123/*****************************************************************************/ 130/*****************************************************************************/
124 131
125typedef struct 132typedef struct
126{ 133{
127 struct ev_io *head; 134 struct ev_io *head;
128 unsigned char wev, rev; /* want, received event set */ 135 int events;
129} ANFD; 136} ANFD;
130 137
131static ANFD *anfds; 138static ANFD *anfds;
132static int anfdmax; 139static int anfdmax;
133 140
134static int *fdchanges;
135static int fdchangemax, fdchangecnt;
136
137static void 141static void
138anfds_init (ANFD *base, int count) 142anfds_init (ANFD *base, int count)
139{ 143{
140 while (count--) 144 while (count--)
141 { 145 {
142 base->head = 0; 146 base->head = 0;
143 base->wev = base->rev = EV_NONE; 147 base->events = EV_NONE;
144 ++base; 148 ++base;
145 } 149 }
146} 150}
147 151
148typedef struct 152typedef struct
165 pendings [pendingcnt - 1].events = events; 169 pendings [pendingcnt - 1].events = events;
166 } 170 }
167} 171}
168 172
169static void 173static void
174queue_events (W *events, int eventcnt, int type)
175{
176 int i;
177
178 for (i = 0; i < eventcnt; ++i)
179 event (events [i], type);
180}
181
182static void
170fd_event (int fd, int events) 183fd_event (int fd, int events)
171{ 184{
172 ANFD *anfd = anfds + fd; 185 ANFD *anfd = anfds + fd;
173 struct ev_io *w; 186 struct ev_io *w;
174 187
179 if (ev) 192 if (ev)
180 event ((W)w, ev); 193 event ((W)w, ev);
181 } 194 }
182} 195}
183 196
197/*****************************************************************************/
198
199static int *fdchanges;
200static int fdchangemax, fdchangecnt;
201
184static void 202static void
185queue_events (W *events, int eventcnt, int type) 203fd_reify (void)
186{ 204{
187 int i; 205 int i;
188 206
189 for (i = 0; i < eventcnt; ++i) 207 for (i = 0; i < fdchangecnt; ++i)
190 event (events [i], type); 208 {
209 int fd = fdchanges [i];
210 ANFD *anfd = anfds + fd;
211 struct ev_io *w;
212
213 int events = 0;
214
215 for (w = anfd->head; w; w = w->next)
216 events |= w->events;
217
218 anfd->events &= ~EV_REIFY;
219
220 if (anfd->events != events)
221 {
222 method_modify (fd, anfd->events, events);
223 anfd->events = events;
224 }
225 }
226
227 fdchangecnt = 0;
228}
229
230static void
231fd_change (int fd)
232{
233 if (anfds [fd].events & EV_REIFY)
234 return;
235
236 anfds [fd].events |= EV_REIFY;
237
238 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd;
241}
242
243/* called on EBADF to verify fds */
244static void
245fd_recheck (void)
246{
247 int fd;
248
249 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head)
253 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 ev_io_stop (anfds [fd].head);
256 }
191} 257}
192 258
193/*****************************************************************************/ 259/*****************************************************************************/
194 260
195static struct ev_timer **timers; 261static struct ev_timer **timers;
304 370
305 /* rather than sort out wether we really need nb, set it */ 371 /* rather than sort out wether we really need nb, set it */
306 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
307 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
308 374
309 evio_set (&sigev, sigpipe [0], EV_READ); 375 ev_io_set (&sigev, sigpipe [0], EV_READ);
310 evio_start (&sigev); 376 ev_io_start (&sigev);
311} 377}
312 378
313/*****************************************************************************/ 379/*****************************************************************************/
314 380
315static struct ev_idle **idles; 381static struct ev_idle **idles;
316static int idlemax, idlecnt; 382static int idlemax, idlecnt;
317 383
384static struct ev_prepare **prepares;
385static int preparemax, preparecnt;
386
318static struct ev_check **checks; 387static struct ev_check **checks;
319static int checkmax, checkcnt; 388static int checkmax, checkcnt;
320 389
321/*****************************************************************************/ 390/*****************************************************************************/
322 391
392static struct ev_child *childs [PID_HASHSIZE];
393static struct ev_signal childev;
394
395#ifndef WCONTINUED
396# define WCONTINUED 0
397#endif
398
399static void
400childcb (struct ev_signal *sw, int revents)
401{
402 struct ev_child *w;
403 int pid, status;
404
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1)
408 {
409 w->status = status;
410 event ((W)w, EV_CHILD);
411 }
412}
413
414/*****************************************************************************/
415
323#if HAVE_EPOLL 416#if HAVE_EPOLL
324# include "ev_epoll.c" 417# include "ev_epoll.c"
325#endif 418#endif
326#if HAVE_SELECT 419#if HAVE_SELECT
327# include "ev_select.c" 420# include "ev_select.c"
328#endif 421#endif
329 422
423int
424ev_version_major (void)
425{
426 return EV_VERSION_MAJOR;
427}
428
429int
430ev_version_minor (void)
431{
432 return EV_VERSION_MINOR;
433}
434
330int ev_init (int flags) 435int ev_init (int flags)
331{ 436{
437 if (!ev_method)
438 {
332#if HAVE_MONOTONIC 439#if HAVE_MONOTONIC
333 { 440 {
334 struct timespec ts; 441 struct timespec ts;
335 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 442 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
336 have_monotonic = 1; 443 have_monotonic = 1;
337 } 444 }
338#endif 445#endif
339 446
340 ev_now = ev_time (); 447 ev_now = ev_time ();
341 now = get_clock (); 448 now = get_clock ();
342 diff = ev_now - now; 449 diff = ev_now - now;
343 450
344 if (pipe (sigpipe)) 451 if (pipe (sigpipe))
345 return 0; 452 return 0;
346 453
347 ev_method = EVMETHOD_NONE; 454 ev_method = EVMETHOD_NONE;
348#if HAVE_EPOLL 455#if HAVE_EPOLL
349 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 456 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
350#endif 457#endif
351#if HAVE_SELECT 458#if HAVE_SELECT
352 if (ev_method == EVMETHOD_NONE) select_init (flags); 459 if (ev_method == EVMETHOD_NONE) select_init (flags);
353#endif 460#endif
354 461
355 if (ev_method) 462 if (ev_method)
356 { 463 {
357 evw_init (&sigev, sigcb); 464 ev_watcher_init (&sigev, sigcb);
358 siginit (); 465 siginit ();
466
467 ev_signal_init (&childev, childcb, SIGCHLD);
468 ev_signal_start (&childev);
469 }
359 } 470 }
360 471
361 return ev_method; 472 return ev_method;
362} 473}
363 474
364/*****************************************************************************/ 475/*****************************************************************************/
365 476
477void
366void ev_prefork (void) 478ev_prefork (void)
367{ 479{
368 /* nop */ 480 /* nop */
369} 481}
370 482
483void
371void ev_postfork_parent (void) 484ev_postfork_parent (void)
372{ 485{
373 /* nop */ 486 /* nop */
374} 487}
375 488
489void
376void ev_postfork_child (void) 490ev_postfork_child (void)
377{ 491{
378#if HAVE_EPOLL 492#if HAVE_EPOLL
379 if (ev_method == EVMETHOD_EPOLL) 493 if (ev_method == EVMETHOD_EPOLL)
380 epoll_postfork_child (); 494 epoll_postfork_child ();
381#endif 495#endif
382 496
383 evio_stop (&sigev); 497 ev_io_stop (&sigev);
384 close (sigpipe [0]); 498 close (sigpipe [0]);
385 close (sigpipe [1]); 499 close (sigpipe [1]);
386 pipe (sigpipe); 500 pipe (sigpipe);
387 siginit (); 501 siginit ();
388} 502}
389 503
390/*****************************************************************************/ 504/*****************************************************************************/
391 505
392static void 506static void
393fd_reify (void)
394{
395 int i;
396
397 for (i = 0; i < fdchangecnt; ++i)
398 {
399 int fd = fdchanges [i];
400 ANFD *anfd = anfds + fd;
401 struct ev_io *w;
402
403 int wev = 0;
404
405 for (w = anfd->head; w; w = w->next)
406 wev |= w->events;
407
408 if (anfd->wev != wev)
409 {
410 method_modify (fd, anfd->wev, wev);
411 anfd->wev = wev;
412 }
413 }
414
415 fdchangecnt = 0;
416}
417
418static void
419call_pending () 507call_pending (void)
420{ 508{
421 int i; 509 while (pendingcnt)
422
423 for (i = 0; i < pendingcnt; ++i)
424 { 510 {
425 ANPENDING *p = pendings + i; 511 ANPENDING *p = pendings + --pendingcnt;
426 512
427 if (p->w) 513 if (p->w)
428 { 514 {
429 p->w->pending = 0; 515 p->w->pending = 0;
430 p->w->cb (p->w, p->events); 516 p->w->cb (p->w, p->events);
431 } 517 }
432 } 518 }
433
434 pendingcnt = 0;
435} 519}
436 520
437static void 521static void
438timers_reify () 522timers_reify (void)
439{ 523{
440 while (timercnt && timers [0]->at <= now) 524 while (timercnt && timers [0]->at <= now)
441 { 525 {
442 struct ev_timer *w = timers [0]; 526 struct ev_timer *w = timers [0];
443 527
449 w->at = now + w->repeat; 533 w->at = now + w->repeat;
450 assert (("timer timeout in the past, negative repeat?", w->at > now)); 534 assert (("timer timeout in the past, negative repeat?", w->at > now));
451 downheap ((WT *)timers, timercnt, 0); 535 downheap ((WT *)timers, timercnt, 0);
452 } 536 }
453 else 537 else
454 evtimer_stop (w); /* nonrepeating: stop timer */ 538 ev_timer_stop (w); /* nonrepeating: stop timer */
455 } 539 }
456} 540}
457 541
458static void 542static void
459periodics_reify () 543periodics_reify (void)
460{ 544{
461 while (periodiccnt && periodics [0]->at <= ev_now) 545 while (periodiccnt && periodics [0]->at <= ev_now)
462 { 546 {
463 struct ev_periodic *w = periodics [0]; 547 struct ev_periodic *w = periodics [0];
464 548
468 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
469 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 553 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
470 downheap ((WT *)periodics, periodiccnt, 0); 554 downheap ((WT *)periodics, periodiccnt, 0);
471 } 555 }
472 else 556 else
473 evperiodic_stop (w); /* nonrepeating: stop timer */ 557 ev_periodic_stop (w); /* nonrepeating: stop timer */
474 558
475 event ((W)w, EV_TIMEOUT); 559 event ((W)w, EV_TIMEOUT);
476 } 560 }
477} 561}
478 562
490 { 574 {
491 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 575 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
492 576
493 if (fabs (diff) >= 1e-4) 577 if (fabs (diff) >= 1e-4)
494 { 578 {
495 evperiodic_stop (w); 579 ev_periodic_stop (w);
496 evperiodic_start (w); 580 ev_periodic_start (w);
497 581
498 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 582 i = 0; /* restart loop, inefficient, but time jumps should be rare */
499 } 583 }
500 } 584 }
501 } 585 }
502} 586}
503 587
504static void 588static void
505time_update () 589time_update (void)
506{ 590{
507 int i; 591 int i;
508 592
509 ev_now = ev_time (); 593 ev_now = ev_time ();
510 594
544int ev_loop_done; 628int ev_loop_done;
545 629
546void ev_loop (int flags) 630void ev_loop (int flags)
547{ 631{
548 double block; 632 double block;
549 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
550
551 if (checkcnt)
552 {
553 queue_events ((W *)checks, checkcnt, EV_CHECK);
554 call_pending ();
555 }
556 634
557 do 635 do
558 { 636 {
637 /* queue check watchers (and execute them) */
638 if (preparecnt)
639 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending ();
642 }
643
559 /* update fd-related kernel structures */ 644 /* update fd-related kernel structures */
560 fd_reify (); 645 fd_reify ();
561 646
562 /* calculate blocking time */ 647 /* calculate blocking time */
563 648
564 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 649 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */
565 ev_now = ev_time (); 651 ev_now = ev_time ();
566 652
567 if (flags & EVLOOP_NONBLOCK || idlecnt) 653 if (flags & EVLOOP_NONBLOCK || idlecnt)
568 block = 0.; 654 block = 0.;
569 else 655 else
589 675
590 /* update ev_now, do magic */ 676 /* update ev_now, do magic */
591 time_update (); 677 time_update ();
592 678
593 /* queue pending timers and reschedule them */ 679 /* queue pending timers and reschedule them */
680 timers_reify (); /* relative timers called last */
594 periodics_reify (); /* absolute timers first */ 681 periodics_reify (); /* absolute timers called first */
595 timers_reify (); /* relative timers second */
596 682
597 /* queue idle watchers unless io or timers are pending */ 683 /* queue idle watchers unless io or timers are pending */
598 if (!pendingcnt) 684 if (!pendingcnt)
599 queue_events ((W *)idles, idlecnt, EV_IDLE); 685 queue_events ((W *)idles, idlecnt, EV_IDLE);
600 686
601 /* queue check and possibly idle watchers */ 687 /* queue check watchers, to be executed first */
688 if (checkcnt)
602 queue_events ((W *)checks, checkcnt, EV_CHECK); 689 queue_events ((W *)checks, checkcnt, EV_CHECK);
603 690
604 call_pending (); 691 call_pending ();
605 } 692 }
606 while (!ev_loop_done); 693 while (!ev_loop_done);
607 694
656} 743}
657 744
658/*****************************************************************************/ 745/*****************************************************************************/
659 746
660void 747void
661evio_start (struct ev_io *w) 748ev_io_start (struct ev_io *w)
662{ 749{
663 if (ev_is_active (w)) 750 if (ev_is_active (w))
664 return; 751 return;
665 752
666 int fd = w->fd; 753 int fd = w->fd;
667 754
668 ev_start ((W)w, 1); 755 ev_start ((W)w, 1);
669 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
670 wlist_add ((WL *)&anfds[fd].head, (WL)w); 757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
671 758
672 ++fdchangecnt; 759 fd_change (fd);
673 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
674 fdchanges [fdchangecnt - 1] = fd;
675} 760}
676 761
677void 762void
678evio_stop (struct ev_io *w) 763ev_io_stop (struct ev_io *w)
679{ 764{
680 ev_clear ((W)w); 765 ev_clear ((W)w);
681 if (!ev_is_active (w)) 766 if (!ev_is_active (w))
682 return; 767 return;
683 768
684 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
685 ev_stop ((W)w); 770 ev_stop ((W)w);
686 771
687 ++fdchangecnt; 772 fd_change (w->fd);
688 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
689 fdchanges [fdchangecnt - 1] = w->fd;
690} 773}
691 774
692void 775void
693evtimer_start (struct ev_timer *w) 776ev_timer_start (struct ev_timer *w)
694{ 777{
695 if (ev_is_active (w)) 778 if (ev_is_active (w))
696 return; 779 return;
697 780
698 w->at += now; 781 w->at += now;
704 timers [timercnt - 1] = w; 787 timers [timercnt - 1] = w;
705 upheap ((WT *)timers, timercnt - 1); 788 upheap ((WT *)timers, timercnt - 1);
706} 789}
707 790
708void 791void
709evtimer_stop (struct ev_timer *w) 792ev_timer_stop (struct ev_timer *w)
710{ 793{
711 ev_clear ((W)w); 794 ev_clear ((W)w);
712 if (!ev_is_active (w)) 795 if (!ev_is_active (w))
713 return; 796 return;
714 797
722 805
723 ev_stop ((W)w); 806 ev_stop ((W)w);
724} 807}
725 808
726void 809void
727evtimer_again (struct ev_timer *w) 810ev_timer_again (struct ev_timer *w)
728{ 811{
729 if (ev_is_active (w)) 812 if (ev_is_active (w))
730 { 813 {
731 if (w->repeat) 814 if (w->repeat)
732 { 815 {
733 w->at = now + w->repeat; 816 w->at = now + w->repeat;
734 downheap ((WT *)timers, timercnt, w->active - 1); 817 downheap ((WT *)timers, timercnt, w->active - 1);
735 } 818 }
736 else 819 else
737 evtimer_stop (w); 820 ev_timer_stop (w);
738 } 821 }
739 else if (w->repeat) 822 else if (w->repeat)
740 evtimer_start (w); 823 ev_timer_start (w);
741} 824}
742 825
743void 826void
744evperiodic_start (struct ev_periodic *w) 827ev_periodic_start (struct ev_periodic *w)
745{ 828{
746 if (ev_is_active (w)) 829 if (ev_is_active (w))
747 return; 830 return;
748 831
749 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
757 periodics [periodiccnt - 1] = w; 840 periodics [periodiccnt - 1] = w;
758 upheap ((WT *)periodics, periodiccnt - 1); 841 upheap ((WT *)periodics, periodiccnt - 1);
759} 842}
760 843
761void 844void
762evperiodic_stop (struct ev_periodic *w) 845ev_periodic_stop (struct ev_periodic *w)
763{ 846{
764 ev_clear ((W)w); 847 ev_clear ((W)w);
765 if (!ev_is_active (w)) 848 if (!ev_is_active (w))
766 return; 849 return;
767 850
773 856
774 ev_stop ((W)w); 857 ev_stop ((W)w);
775} 858}
776 859
777void 860void
778evsignal_start (struct ev_signal *w) 861ev_signal_start (struct ev_signal *w)
779{ 862{
780 if (ev_is_active (w)) 863 if (ev_is_active (w))
781 return; 864 return;
782 865
783 ev_start ((W)w, 1); 866 ev_start ((W)w, 1);
793 sigaction (w->signum, &sa, 0); 876 sigaction (w->signum, &sa, 0);
794 } 877 }
795} 878}
796 879
797void 880void
798evsignal_stop (struct ev_signal *w) 881ev_signal_stop (struct ev_signal *w)
799{ 882{
800 ev_clear ((W)w); 883 ev_clear ((W)w);
801 if (!ev_is_active (w)) 884 if (!ev_is_active (w))
802 return; 885 return;
803 886
806 889
807 if (!signals [w->signum - 1].head) 890 if (!signals [w->signum - 1].head)
808 signal (w->signum, SIG_DFL); 891 signal (w->signum, SIG_DFL);
809} 892}
810 893
894void
811void evidle_start (struct ev_idle *w) 895ev_idle_start (struct ev_idle *w)
812{ 896{
813 if (ev_is_active (w)) 897 if (ev_is_active (w))
814 return; 898 return;
815 899
816 ev_start ((W)w, ++idlecnt); 900 ev_start ((W)w, ++idlecnt);
817 array_needsize (idles, idlemax, idlecnt, ); 901 array_needsize (idles, idlemax, idlecnt, );
818 idles [idlecnt - 1] = w; 902 idles [idlecnt - 1] = w;
819} 903}
820 904
905void
821void evidle_stop (struct ev_idle *w) 906ev_idle_stop (struct ev_idle *w)
822{ 907{
823 ev_clear ((W)w); 908 ev_clear ((W)w);
824 if (ev_is_active (w)) 909 if (ev_is_active (w))
825 return; 910 return;
826 911
827 idles [w->active - 1] = idles [--idlecnt]; 912 idles [w->active - 1] = idles [--idlecnt];
828 ev_stop ((W)w); 913 ev_stop ((W)w);
829} 914}
830 915
916void
917ev_prepare_start (struct ev_prepare *w)
918{
919 if (ev_is_active (w))
920 return;
921
922 ev_start ((W)w, ++preparecnt);
923 array_needsize (prepares, preparemax, preparecnt, );
924 prepares [preparecnt - 1] = w;
925}
926
927void
928ev_prepare_stop (struct ev_prepare *w)
929{
930 ev_clear ((W)w);
931 if (ev_is_active (w))
932 return;
933
934 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w);
936}
937
938void
831void evcheck_start (struct ev_check *w) 939ev_check_start (struct ev_check *w)
832{ 940{
833 if (ev_is_active (w)) 941 if (ev_is_active (w))
834 return; 942 return;
835 943
836 ev_start ((W)w, ++checkcnt); 944 ev_start ((W)w, ++checkcnt);
837 array_needsize (checks, checkmax, checkcnt, ); 945 array_needsize (checks, checkmax, checkcnt, );
838 checks [checkcnt - 1] = w; 946 checks [checkcnt - 1] = w;
839} 947}
840 948
949void
841void evcheck_stop (struct ev_check *w) 950ev_check_stop (struct ev_check *w)
842{ 951{
843 ev_clear ((W)w); 952 ev_clear ((W)w);
844 if (ev_is_active (w)) 953 if (ev_is_active (w))
845 return; 954 return;
846 955
847 checks [w->active - 1] = checks [--checkcnt]; 956 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w);
958}
959
960void
961ev_child_start (struct ev_child *w)
962{
963 if (ev_is_active (w))
964 return;
965
966 ev_start ((W)w, 1);
967 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
968}
969
970void
971ev_child_stop (struct ev_child *w)
972{
973 ev_clear ((W)w);
974 if (ev_is_active (w))
975 return;
976
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
848 ev_stop ((W)w); 978 ev_stop ((W)w);
849} 979}
850 980
851/*****************************************************************************/ 981/*****************************************************************************/
852 982
862once_cb (struct ev_once *once, int revents) 992once_cb (struct ev_once *once, int revents)
863{ 993{
864 void (*cb)(int revents, void *arg) = once->cb; 994 void (*cb)(int revents, void *arg) = once->cb;
865 void *arg = once->arg; 995 void *arg = once->arg;
866 996
867 evio_stop (&once->io); 997 ev_io_stop (&once->io);
868 evtimer_stop (&once->to); 998 ev_timer_stop (&once->to);
869 free (once); 999 free (once);
870 1000
871 cb (revents, arg); 1001 cb (revents, arg);
872} 1002}
873 1003
893 else 1023 else
894 { 1024 {
895 once->cb = cb; 1025 once->cb = cb;
896 once->arg = arg; 1026 once->arg = arg;
897 1027
898 evw_init (&once->io, once_cb_io); 1028 ev_watcher_init (&once->io, once_cb_io);
899 1029
900 if (fd >= 0) 1030 if (fd >= 0)
901 { 1031 {
902 evio_set (&once->io, fd, events); 1032 ev_io_set (&once->io, fd, events);
903 evio_start (&once->io); 1033 ev_io_start (&once->io);
904 } 1034 }
905 1035
906 evw_init (&once->to, once_cb_to); 1036 ev_watcher_init (&once->to, once_cb_to);
907 1037
908 if (timeout >= 0.) 1038 if (timeout >= 0.)
909 { 1039 {
910 evtimer_set (&once->to, timeout, 0.); 1040 ev_timer_set (&once->to, timeout, 0.);
911 evtimer_start (&once->to); 1041 ev_timer_start (&once->to);
912 } 1042 }
913 } 1043 }
914} 1044}
915 1045
916/*****************************************************************************/ 1046/*****************************************************************************/
927 1057
928static void 1058static void
929ocb (struct ev_timer *w, int revents) 1059ocb (struct ev_timer *w, int revents)
930{ 1060{
931 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1061 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
932 evtimer_stop (w); 1062 ev_timer_stop (w);
933 evtimer_start (w); 1063 ev_timer_start (w);
934} 1064}
935 1065
936static void 1066static void
937scb (struct ev_signal *w, int revents) 1067scb (struct ev_signal *w, int revents)
938{ 1068{
939 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1069 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
940 evio_stop (&wio); 1070 ev_io_stop (&wio);
941 evio_start (&wio); 1071 ev_io_start (&wio);
942} 1072}
943 1073
944static void 1074static void
945gcb (struct ev_signal *w, int revents) 1075gcb (struct ev_signal *w, int revents)
946{ 1076{
950 1080
951int main (void) 1081int main (void)
952{ 1082{
953 ev_init (0); 1083 ev_init (0);
954 1084
955 evio_init (&wio, sin_cb, 0, EV_READ); 1085 ev_io_init (&wio, sin_cb, 0, EV_READ);
956 evio_start (&wio); 1086 ev_io_start (&wio);
957 1087
958 struct ev_timer t[10000]; 1088 struct ev_timer t[10000];
959 1089
960#if 0 1090#if 0
961 int i; 1091 int i;
962 for (i = 0; i < 10000; ++i) 1092 for (i = 0; i < 10000; ++i)
963 { 1093 {
964 struct ev_timer *w = t + i; 1094 struct ev_timer *w = t + i;
965 evw_init (w, ocb, i); 1095 ev_watcher_init (w, ocb, i);
966 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1096 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
967 evtimer_start (w); 1097 ev_timer_start (w);
968 if (drand48 () < 0.5) 1098 if (drand48 () < 0.5)
969 evtimer_stop (w); 1099 ev_timer_stop (w);
970 } 1100 }
971#endif 1101#endif
972 1102
973 struct ev_timer t1; 1103 struct ev_timer t1;
974 evtimer_init (&t1, ocb, 5, 10); 1104 ev_timer_init (&t1, ocb, 5, 10);
975 evtimer_start (&t1); 1105 ev_timer_start (&t1);
976 1106
977 struct ev_signal sig; 1107 struct ev_signal sig;
978 evsignal_init (&sig, scb, SIGQUIT); 1108 ev_signal_init (&sig, scb, SIGQUIT);
979 evsignal_start (&sig); 1109 ev_signal_start (&sig);
980 1110
981 struct ev_check cw; 1111 struct ev_check cw;
982 evcheck_init (&cw, gcb); 1112 ev_check_init (&cw, gcb);
983 evcheck_start (&cw); 1113 ev_check_start (&cw);
984 1114
985 struct ev_idle iw; 1115 struct ev_idle iw;
986 evidle_init (&iw, gcb); 1116 ev_idle_init (&iw, gcb);
987 evidle_start (&iw); 1117 ev_idle_start (&iw);
988 1118
989 ev_loop (0); 1119 ev_loop (0);
990 1120
991 return 0; 1121 return 0;
992} 1122}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines