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

Comparing libev/ev.c (file contents):
Revision 1.25 by root, Wed Oct 31 21:34:45 2007 UTC vs.
Revision 1.31 by root, Thu Nov 1 09:05:33 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;
109 } 116 }
110#endif 117#endif
111 118
112 return ev_time (); 119 return ev_time ();
113} 120}
121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
114 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; \ 127 int newcnt = cur; \
119 do \ 128 do \
120 { \ 129 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 130 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 131 } \
123 while ((cnt) > newcnt); \ 132 while ((cnt) > newcnt); \
124 \ 133 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 139/*****************************************************************************/
131 140
132typedef struct 141typedef struct
133{ 142{
134 struct ev_io *head; 143 struct ev_io *head;
135 unsigned char wev, rev; /* want, received event set */ 144 int events;
136} ANFD; 145} ANFD;
137 146
138static ANFD *anfds; 147static ANFD *anfds;
139static int anfdmax; 148static int anfdmax;
140 149
141static int *fdchanges;
142static int fdchangemax, fdchangecnt;
143
144static void 150static void
145anfds_init (ANFD *base, int count) 151anfds_init (ANFD *base, int count)
146{ 152{
147 while (count--) 153 while (count--)
148 { 154 {
149 base->head = 0; 155 base->head = 0;
150 base->wev = base->rev = EV_NONE; 156 base->events = EV_NONE;
151 ++base; 157 ++base;
152 } 158 }
153} 159}
154 160
155typedef struct 161typedef struct
162static int pendingmax, pendingcnt; 168static int pendingmax, pendingcnt;
163 169
164static void 170static void
165event (W w, int events) 171event (W w, int events)
166{ 172{
167 if (w->active)
168 {
169 w->pending = ++pendingcnt; 173 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, ); 174 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w; 175 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 176 pendings [pendingcnt - 1].events = events;
173 } 177}
178
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);
174} 186}
175 187
176static void 188static void
177fd_event (int fd, int events) 189fd_event (int fd, int events)
178{ 190{
186 if (ev) 198 if (ev)
187 event ((W)w, ev); 199 event ((W)w, ev);
188 } 200 }
189} 201}
190 202
203/*****************************************************************************/
204
205static int *fdchanges;
206static int fdchangemax, fdchangecnt;
207
191static void 208static void
192queue_events (W *events, int eventcnt, int type) 209fd_reify (void)
193{ 210{
194 int i; 211 int i;
195 212
196 for (i = 0; i < eventcnt; ++i) 213 for (i = 0; i < fdchangecnt; ++i)
197 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;
198} 247}
199 248
200/* called on EBADF to verify fds */ 249/* called on EBADF to verify fds */
201static void 250static void
202fd_recheck (void) 251fd_recheck (void)
203{ 252{
204 int fd; 253 int fd;
205 254
206 for (fd = 0; fd < anfdmax; ++fd) 255 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev) 256 if (anfds [fd].events)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head) 258 while (anfds [fd].head)
210 { 259 {
211 event ((W)anfds [fd].head, EV_ERROR); 260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
212 evio_stop (anfds [fd].head); 261 ev_io_stop (anfds [fd].head);
213 } 262 }
214} 263}
215 264
216/*****************************************************************************/ 265/*****************************************************************************/
217 266
327 376
328 /* rather than sort out wether we really need nb, set it */ 377 /* rather than sort out wether we really need nb, set it */
329 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 378 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
330 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 379 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
331 380
332 evio_set (&sigev, sigpipe [0], EV_READ); 381 ev_io_set (&sigev, sigpipe [0], EV_READ);
333 evio_start (&sigev); 382 ev_io_start (&sigev);
334} 383}
335 384
336/*****************************************************************************/ 385/*****************************************************************************/
337 386
338static struct ev_idle **idles; 387static struct ev_idle **idles;
368 } 417 }
369} 418}
370 419
371/*****************************************************************************/ 420/*****************************************************************************/
372 421
373#if HAVE_EPOLL 422#if EV_USE_EPOLL
374# include "ev_epoll.c" 423# include "ev_epoll.c"
375#endif 424#endif
376#if HAVE_SELECT 425#if EV_USE_SELECT
377# include "ev_select.c" 426# include "ev_select.c"
378#endif 427#endif
379 428
380int 429int
381ev_version_major (void) 430ev_version_major (void)
391 440
392int ev_init (int flags) 441int ev_init (int flags)
393{ 442{
394 if (!ev_method) 443 if (!ev_method)
395 { 444 {
396#if HAVE_MONOTONIC 445#if EV_USE_MONOTONIC
397 { 446 {
398 struct timespec ts; 447 struct timespec ts;
399 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 448 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
400 have_monotonic = 1; 449 have_monotonic = 1;
401 } 450 }
407 456
408 if (pipe (sigpipe)) 457 if (pipe (sigpipe))
409 return 0; 458 return 0;
410 459
411 ev_method = EVMETHOD_NONE; 460 ev_method = EVMETHOD_NONE;
412#if HAVE_EPOLL 461#if EV_USE_EPOLL
413 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 462 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
414#endif 463#endif
415#if HAVE_SELECT 464#if EV_USE_SELECT
416 if (ev_method == EVMETHOD_NONE) select_init (flags); 465 if (ev_method == EVMETHOD_NONE) select_init (flags);
417#endif 466#endif
418 467
419 if (ev_method) 468 if (ev_method)
420 { 469 {
421 evw_init (&sigev, sigcb); 470 ev_watcher_init (&sigev, sigcb);
422 siginit (); 471 siginit ();
423 472
424 evsignal_init (&childev, childcb, SIGCHLD); 473 ev_signal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev); 474 ev_signal_start (&childev);
426 } 475 }
427 } 476 }
428 477
429 return ev_method; 478 return ev_method;
430} 479}
444} 493}
445 494
446void 495void
447ev_postfork_child (void) 496ev_postfork_child (void)
448{ 497{
449#if HAVE_EPOLL 498#if EV_USE_EPOLL
450 if (ev_method == EVMETHOD_EPOLL) 499 if (ev_method == EVMETHOD_EPOLL)
451 epoll_postfork_child (); 500 epoll_postfork_child ();
452#endif 501#endif
453 502
454 evio_stop (&sigev); 503 ev_io_stop (&sigev);
455 close (sigpipe [0]); 504 close (sigpipe [0]);
456 close (sigpipe [1]); 505 close (sigpipe [1]);
457 pipe (sigpipe); 506 pipe (sigpipe);
458 siginit (); 507 siginit ();
459} 508}
460 509
461/*****************************************************************************/ 510/*****************************************************************************/
462 511
463static void 512static void
464fd_reify (void)
465{
466 int i;
467
468 for (i = 0; i < fdchangecnt; ++i)
469 {
470 int fd = fdchanges [i];
471 ANFD *anfd = anfds + fd;
472 struct ev_io *w;
473
474 int wev = 0;
475
476 for (w = anfd->head; w; w = w->next)
477 wev |= w->events;
478
479 if (anfd->wev != wev)
480 {
481 method_modify (fd, anfd->wev, wev);
482 anfd->wev = wev;
483 }
484 }
485
486 fdchangecnt = 0;
487}
488
489static void
490call_pending (void) 513call_pending (void)
491{ 514{
492 while (pendingcnt) 515 while (pendingcnt)
493 { 516 {
494 ANPENDING *p = pendings + --pendingcnt; 517 ANPENDING *p = pendings + --pendingcnt;
505timers_reify (void) 528timers_reify (void)
506{ 529{
507 while (timercnt && timers [0]->at <= now) 530 while (timercnt && timers [0]->at <= now)
508 { 531 {
509 struct ev_timer *w = timers [0]; 532 struct ev_timer *w = timers [0];
510
511 event ((W)w, EV_TIMEOUT);
512 533
513 /* first reschedule or stop timer */ 534 /* first reschedule or stop timer */
514 if (w->repeat) 535 if (w->repeat)
515 { 536 {
516 w->at = now + w->repeat; 537 w->at = now + w->repeat;
517 assert (("timer timeout in the past, negative repeat?", w->at > now)); 538 assert (("timer timeout in the past, negative repeat?", w->at > now));
518 downheap ((WT *)timers, timercnt, 0); 539 downheap ((WT *)timers, timercnt, 0);
519 } 540 }
520 else 541 else
521 evtimer_stop (w); /* nonrepeating: stop timer */ 542 ev_timer_stop (w); /* nonrepeating: stop timer */
543
544 event ((W)w, EV_TIMEOUT);
522 } 545 }
523} 546}
524 547
525static void 548static void
526periodics_reify (void) 549periodics_reify (void)
535 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;
536 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));
537 downheap ((WT *)periodics, periodiccnt, 0); 560 downheap ((WT *)periodics, periodiccnt, 0);
538 } 561 }
539 else 562 else
540 evperiodic_stop (w); /* nonrepeating: stop timer */ 563 ev_periodic_stop (w); /* nonrepeating: stop timer */
541 564
542 event ((W)w, EV_TIMEOUT); 565 event ((W)w, EV_TIMEOUT);
543 } 566 }
544} 567}
545 568
557 { 580 {
558 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;
559 582
560 if (fabs (diff) >= 1e-4) 583 if (fabs (diff) >= 1e-4)
561 { 584 {
562 evperiodic_stop (w); 585 ev_periodic_stop (w);
563 evperiodic_start (w); 586 ev_periodic_start (w);
564 587
565 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 588 i = 0; /* restart loop, inefficient, but time jumps should be rare */
566 } 589 }
567 } 590 }
568 } 591 }
611int ev_loop_done; 634int ev_loop_done;
612 635
613void ev_loop (int flags) 636void ev_loop (int flags)
614{ 637{
615 double block; 638 double block;
616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 639 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
617 640
618 do 641 do
619 { 642 {
620 /* queue check watchers (and execute them) */ 643 /* queue check watchers (and execute them) */
621 if (preparecnt) 644 if (preparecnt)
726} 749}
727 750
728/*****************************************************************************/ 751/*****************************************************************************/
729 752
730void 753void
731evio_start (struct ev_io *w) 754ev_io_start (struct ev_io *w)
732{ 755{
733 if (ev_is_active (w)) 756 if (ev_is_active (w))
734 return; 757 return;
735 758
736 int fd = w->fd; 759 int fd = w->fd;
737 760
738 ev_start ((W)w, 1); 761 ev_start ((W)w, 1);
739 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 762 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
740 wlist_add ((WL *)&anfds[fd].head, (WL)w); 763 wlist_add ((WL *)&anfds[fd].head, (WL)w);
741 764
742 ++fdchangecnt; 765 fd_change (fd);
743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
744 fdchanges [fdchangecnt - 1] = fd;
745
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
748} 766}
749 767
750void 768void
751evio_stop (struct ev_io *w) 769ev_io_stop (struct ev_io *w)
752{ 770{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
755 ev_clear ((W)w); 771 ev_clear ((W)w);
756 if (!ev_is_active (w)) 772 if (!ev_is_active (w))
757 return; 773 return;
758 774
759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
760 ev_stop ((W)w); 776 ev_stop ((W)w);
761 777
762 ++fdchangecnt; 778 fd_change (w->fd);
763 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
764 fdchanges [fdchangecnt - 1] = w->fd;
765} 779}
766 780
767void 781void
768evtimer_start (struct ev_timer *w) 782ev_timer_start (struct ev_timer *w)
769{ 783{
770 if (ev_is_active (w)) 784 if (ev_is_active (w))
771 return; 785 return;
772 786
773 w->at += now; 787 w->at += now;
779 timers [timercnt - 1] = w; 793 timers [timercnt - 1] = w;
780 upheap ((WT *)timers, timercnt - 1); 794 upheap ((WT *)timers, timercnt - 1);
781} 795}
782 796
783void 797void
784evtimer_stop (struct ev_timer *w) 798ev_timer_stop (struct ev_timer *w)
785{ 799{
786 ev_clear ((W)w); 800 ev_clear ((W)w);
787 if (!ev_is_active (w)) 801 if (!ev_is_active (w))
788 return; 802 return;
789 803
797 811
798 ev_stop ((W)w); 812 ev_stop ((W)w);
799} 813}
800 814
801void 815void
802evtimer_again (struct ev_timer *w) 816ev_timer_again (struct ev_timer *w)
803{ 817{
804 if (ev_is_active (w)) 818 if (ev_is_active (w))
805 { 819 {
806 if (w->repeat) 820 if (w->repeat)
807 { 821 {
808 w->at = now + w->repeat; 822 w->at = now + w->repeat;
809 downheap ((WT *)timers, timercnt, w->active - 1); 823 downheap ((WT *)timers, timercnt, w->active - 1);
810 } 824 }
811 else 825 else
812 evtimer_stop (w); 826 ev_timer_stop (w);
813 } 827 }
814 else if (w->repeat) 828 else if (w->repeat)
815 evtimer_start (w); 829 ev_timer_start (w);
816} 830}
817 831
818void 832void
819evperiodic_start (struct ev_periodic *w) 833ev_periodic_start (struct ev_periodic *w)
820{ 834{
821 if (ev_is_active (w)) 835 if (ev_is_active (w))
822 return; 836 return;
823 837
824 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.));
832 periodics [periodiccnt - 1] = w; 846 periodics [periodiccnt - 1] = w;
833 upheap ((WT *)periodics, periodiccnt - 1); 847 upheap ((WT *)periodics, periodiccnt - 1);
834} 848}
835 849
836void 850void
837evperiodic_stop (struct ev_periodic *w) 851ev_periodic_stop (struct ev_periodic *w)
838{ 852{
839 ev_clear ((W)w); 853 ev_clear ((W)w);
840 if (!ev_is_active (w)) 854 if (!ev_is_active (w))
841 return; 855 return;
842 856
848 862
849 ev_stop ((W)w); 863 ev_stop ((W)w);
850} 864}
851 865
852void 866void
853evsignal_start (struct ev_signal *w) 867ev_signal_start (struct ev_signal *w)
854{ 868{
855 if (ev_is_active (w)) 869 if (ev_is_active (w))
856 return; 870 return;
857 871
858 ev_start ((W)w, 1); 872 ev_start ((W)w, 1);
868 sigaction (w->signum, &sa, 0); 882 sigaction (w->signum, &sa, 0);
869 } 883 }
870} 884}
871 885
872void 886void
873evsignal_stop (struct ev_signal *w) 887ev_signal_stop (struct ev_signal *w)
874{ 888{
875 ev_clear ((W)w); 889 ev_clear ((W)w);
876 if (!ev_is_active (w)) 890 if (!ev_is_active (w))
877 return; 891 return;
878 892
881 895
882 if (!signals [w->signum - 1].head) 896 if (!signals [w->signum - 1].head)
883 signal (w->signum, SIG_DFL); 897 signal (w->signum, SIG_DFL);
884} 898}
885 899
900void
886void evidle_start (struct ev_idle *w) 901ev_idle_start (struct ev_idle *w)
887{ 902{
888 if (ev_is_active (w)) 903 if (ev_is_active (w))
889 return; 904 return;
890 905
891 ev_start ((W)w, ++idlecnt); 906 ev_start ((W)w, ++idlecnt);
892 array_needsize (idles, idlemax, idlecnt, ); 907 array_needsize (idles, idlemax, idlecnt, );
893 idles [idlecnt - 1] = w; 908 idles [idlecnt - 1] = w;
894} 909}
895 910
911void
896void evidle_stop (struct ev_idle *w) 912ev_idle_stop (struct ev_idle *w)
897{ 913{
898 ev_clear ((W)w); 914 ev_clear ((W)w);
899 if (ev_is_active (w)) 915 if (ev_is_active (w))
900 return; 916 return;
901 917
902 idles [w->active - 1] = idles [--idlecnt]; 918 idles [w->active - 1] = idles [--idlecnt];
903 ev_stop ((W)w); 919 ev_stop ((W)w);
904} 920}
905 921
922void
906void evprepare_start (struct ev_prepare *w) 923ev_prepare_start (struct ev_prepare *w)
907{ 924{
908 if (ev_is_active (w)) 925 if (ev_is_active (w))
909 return; 926 return;
910 927
911 ev_start ((W)w, ++preparecnt); 928 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, ); 929 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w; 930 prepares [preparecnt - 1] = w;
914} 931}
915 932
933void
916void evprepare_stop (struct ev_prepare *w) 934ev_prepare_stop (struct ev_prepare *w)
917{ 935{
918 ev_clear ((W)w); 936 ev_clear ((W)w);
919 if (ev_is_active (w)) 937 if (ev_is_active (w))
920 return; 938 return;
921 939
922 prepares [w->active - 1] = prepares [--preparecnt]; 940 prepares [w->active - 1] = prepares [--preparecnt];
923 ev_stop ((W)w); 941 ev_stop ((W)w);
924} 942}
925 943
944void
926void evcheck_start (struct ev_check *w) 945ev_check_start (struct ev_check *w)
927{ 946{
928 if (ev_is_active (w)) 947 if (ev_is_active (w))
929 return; 948 return;
930 949
931 ev_start ((W)w, ++checkcnt); 950 ev_start ((W)w, ++checkcnt);
932 array_needsize (checks, checkmax, checkcnt, ); 951 array_needsize (checks, checkmax, checkcnt, );
933 checks [checkcnt - 1] = w; 952 checks [checkcnt - 1] = w;
934} 953}
935 954
955void
936void evcheck_stop (struct ev_check *w) 956ev_check_stop (struct ev_check *w)
937{ 957{
938 ev_clear ((W)w); 958 ev_clear ((W)w);
939 if (ev_is_active (w)) 959 if (ev_is_active (w))
940 return; 960 return;
941 961
942 checks [w->active - 1] = checks [--checkcnt]; 962 checks [w->active - 1] = checks [--checkcnt];
943 ev_stop ((W)w); 963 ev_stop ((W)w);
944} 964}
945 965
966void
946void evchild_start (struct ev_child *w) 967ev_child_start (struct ev_child *w)
947{ 968{
948 if (ev_is_active (w)) 969 if (ev_is_active (w))
949 return; 970 return;
950 971
951 ev_start ((W)w, 1); 972 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 973 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953} 974}
954 975
976void
955void evchild_stop (struct ev_child *w) 977ev_child_stop (struct ev_child *w)
956{ 978{
957 ev_clear ((W)w); 979 ev_clear ((W)w);
958 if (ev_is_active (w)) 980 if (ev_is_active (w))
959 return; 981 return;
960 982
976once_cb (struct ev_once *once, int revents) 998once_cb (struct ev_once *once, int revents)
977{ 999{
978 void (*cb)(int revents, void *arg) = once->cb; 1000 void (*cb)(int revents, void *arg) = once->cb;
979 void *arg = once->arg; 1001 void *arg = once->arg;
980 1002
981 evio_stop (&once->io); 1003 ev_io_stop (&once->io);
982 evtimer_stop (&once->to); 1004 ev_timer_stop (&once->to);
983 free (once); 1005 free (once);
984 1006
985 cb (revents, arg); 1007 cb (revents, arg);
986} 1008}
987 1009
1001ev_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)
1002{ 1024{
1003 struct ev_once *once = malloc (sizeof (struct ev_once)); 1025 struct ev_once *once = malloc (sizeof (struct ev_once));
1004 1026
1005 if (!once) 1027 if (!once)
1006 cb (EV_ERROR, arg); 1028 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1007 else 1029 else
1008 { 1030 {
1009 once->cb = cb; 1031 once->cb = cb;
1010 once->arg = arg; 1032 once->arg = arg;
1011 1033
1012 evw_init (&once->io, once_cb_io); 1034 ev_watcher_init (&once->io, once_cb_io);
1013
1014 if (fd >= 0) 1035 if (fd >= 0)
1015 { 1036 {
1016 evio_set (&once->io, fd, events); 1037 ev_io_set (&once->io, fd, events);
1017 evio_start (&once->io); 1038 ev_io_start (&once->io);
1018 } 1039 }
1019 1040
1020 evw_init (&once->to, once_cb_to); 1041 ev_watcher_init (&once->to, once_cb_to);
1021
1022 if (timeout >= 0.) 1042 if (timeout >= 0.)
1023 { 1043 {
1024 evtimer_set (&once->to, timeout, 0.); 1044 ev_timer_set (&once->to, timeout, 0.);
1025 evtimer_start (&once->to); 1045 ev_timer_start (&once->to);
1026 } 1046 }
1027 } 1047 }
1028} 1048}
1029 1049
1030/*****************************************************************************/ 1050/*****************************************************************************/
1041 1061
1042static void 1062static void
1043ocb (struct ev_timer *w, int revents) 1063ocb (struct ev_timer *w, int revents)
1044{ 1064{
1045 //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);
1046 evtimer_stop (w); 1066 ev_timer_stop (w);
1047 evtimer_start (w); 1067 ev_timer_start (w);
1048} 1068}
1049 1069
1050static void 1070static void
1051scb (struct ev_signal *w, int revents) 1071scb (struct ev_signal *w, int revents)
1052{ 1072{
1053 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1073 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1054 evio_stop (&wio); 1074 ev_io_stop (&wio);
1055 evio_start (&wio); 1075 ev_io_start (&wio);
1056} 1076}
1057 1077
1058static void 1078static void
1059gcb (struct ev_signal *w, int revents) 1079gcb (struct ev_signal *w, int revents)
1060{ 1080{
1064 1084
1065int main (void) 1085int main (void)
1066{ 1086{
1067 ev_init (0); 1087 ev_init (0);
1068 1088
1069 evio_init (&wio, sin_cb, 0, EV_READ); 1089 ev_io_init (&wio, sin_cb, 0, EV_READ);
1070 evio_start (&wio); 1090 ev_io_start (&wio);
1071 1091
1072 struct ev_timer t[10000]; 1092 struct ev_timer t[10000];
1073 1093
1074#if 0 1094#if 0
1075 int i; 1095 int i;
1076 for (i = 0; i < 10000; ++i) 1096 for (i = 0; i < 10000; ++i)
1077 { 1097 {
1078 struct ev_timer *w = t + i; 1098 struct ev_timer *w = t + i;
1079 evw_init (w, ocb, i); 1099 ev_watcher_init (w, ocb, i);
1080 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1100 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1081 evtimer_start (w); 1101 ev_timer_start (w);
1082 if (drand48 () < 0.5) 1102 if (drand48 () < 0.5)
1083 evtimer_stop (w); 1103 ev_timer_stop (w);
1084 } 1104 }
1085#endif 1105#endif
1086 1106
1087 struct ev_timer t1; 1107 struct ev_timer t1;
1088 evtimer_init (&t1, ocb, 5, 10); 1108 ev_timer_init (&t1, ocb, 5, 10);
1089 evtimer_start (&t1); 1109 ev_timer_start (&t1);
1090 1110
1091 struct ev_signal sig; 1111 struct ev_signal sig;
1092 evsignal_init (&sig, scb, SIGQUIT); 1112 ev_signal_init (&sig, scb, SIGQUIT);
1093 evsignal_start (&sig); 1113 ev_signal_start (&sig);
1094 1114
1095 struct ev_check cw; 1115 struct ev_check cw;
1096 evcheck_init (&cw, gcb); 1116 ev_check_init (&cw, gcb);
1097 evcheck_start (&cw); 1117 ev_check_start (&cw);
1098 1118
1099 struct ev_idle iw; 1119 struct ev_idle iw;
1100 evidle_init (&iw, gcb); 1120 ev_idle_init (&iw, gcb);
1101 evidle_start (&iw); 1121 ev_idle_start (&iw);
1102 1122
1103 ev_loop (0); 1123 ev_loop (0);
1104 1124
1105 return 0; 1125 return 0;
1106} 1126}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines