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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines