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

Comparing libev/ev.c (file contents):
Revision 1.23 by root, Wed Oct 31 20:10:17 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;
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) 173 if (w->pending)
174 {
175 pendings [w->pending - 1].events |= events;
176 return;
168 { 177 }
178
169 w->pending = ++pendingcnt; 179 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, ); 180 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w; 181 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 182 pendings [pendingcnt - 1].events = events;
173 } 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);
174} 192}
175 193
176static void 194static void
177fd_event (int fd, int events) 195fd_event (int fd, int events)
178{ 196{
186 if (ev) 204 if (ev)
187 event ((W)w, ev); 205 event ((W)w, ev);
188 } 206 }
189} 207}
190 208
209/*****************************************************************************/
210
211static int *fdchanges;
212static int fdchangemax, fdchangecnt;
213
191static void 214static void
192queue_events (W *events, int eventcnt, int type) 215fd_reify (void)
193{ 216{
194 int i; 217 int i;
195 218
196 for (i = 0; i < eventcnt; ++i) 219 for (i = 0; i < fdchangecnt; ++i)
197 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;
198} 253}
199 254
200/* called on EBADF to verify fds */ 255/* called on EBADF to verify fds */
201static void 256static void
202fd_recheck () 257fd_recheck (void)
203{ 258{
204 int fd; 259 int fd;
205 260
206 for (fd = 0; fd < anfdmax; ++fd) 261 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev) 262 if (anfds [fd].events)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head) 264 while (anfds [fd].head)
265 {
210 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 }
211} 269}
212 270
213/*****************************************************************************/ 271/*****************************************************************************/
214 272
215static struct ev_timer **timers; 273static struct ev_timer **timers;
324 382
325 /* rather than sort out wether we really need nb, set it */ 383 /* rather than sort out wether we really need nb, set it */
326 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
327 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
328 386
329 evio_set (&sigev, sigpipe [0], EV_READ); 387 ev_io_set (&sigev, sigpipe [0], EV_READ);
330 evio_start (&sigev); 388 ev_io_start (&sigev);
331} 389}
332 390
333/*****************************************************************************/ 391/*****************************************************************************/
334 392
335static struct ev_idle **idles; 393static struct ev_idle **idles;
365 } 423 }
366} 424}
367 425
368/*****************************************************************************/ 426/*****************************************************************************/
369 427
370#if HAVE_EPOLL 428#if EV_USE_EPOLL
371# include "ev_epoll.c" 429# include "ev_epoll.c"
372#endif 430#endif
373#if HAVE_SELECT 431#if EV_USE_SELECT
374# include "ev_select.c" 432# include "ev_select.c"
375#endif 433#endif
376 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
377int ev_init (int flags) 447int ev_init (int flags)
378{ 448{
379 if (!ev_method) 449 if (!ev_method)
380 { 450 {
381#if HAVE_MONOTONIC 451#if EV_USE_MONOTONIC
382 { 452 {
383 struct timespec ts; 453 struct timespec ts;
384 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 454 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
385 have_monotonic = 1; 455 have_monotonic = 1;
386 } 456 }
392 462
393 if (pipe (sigpipe)) 463 if (pipe (sigpipe))
394 return 0; 464 return 0;
395 465
396 ev_method = EVMETHOD_NONE; 466 ev_method = EVMETHOD_NONE;
397#if HAVE_EPOLL 467#if EV_USE_EPOLL
398 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 468 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
399#endif 469#endif
400#if HAVE_SELECT 470#if EV_USE_SELECT
401 if (ev_method == EVMETHOD_NONE) select_init (flags); 471 if (ev_method == EVMETHOD_NONE) select_init (flags);
402#endif 472#endif
403 473
404 if (ev_method) 474 if (ev_method)
405 { 475 {
406 evw_init (&sigev, sigcb); 476 ev_watcher_init (&sigev, sigcb);
407 siginit (); 477 siginit ();
408 478
409 evsignal_init (&childev, childcb, SIGCHLD); 479 ev_signal_init (&childev, childcb, SIGCHLD);
410 evsignal_start (&childev); 480 ev_signal_start (&childev);
411 } 481 }
412 } 482 }
413 483
414 return ev_method; 484 return ev_method;
415} 485}
416 486
417/*****************************************************************************/ 487/*****************************************************************************/
418 488
489void
419void ev_prefork (void) 490ev_prefork (void)
420{ 491{
421 /* nop */ 492 /* nop */
422} 493}
423 494
495void
424void ev_postfork_parent (void) 496ev_postfork_parent (void)
425{ 497{
426 /* nop */ 498 /* nop */
427} 499}
428 500
501void
429void ev_postfork_child (void) 502ev_postfork_child (void)
430{ 503{
431#if HAVE_EPOLL 504#if EV_USE_EPOLL
432 if (ev_method == EVMETHOD_EPOLL) 505 if (ev_method == EVMETHOD_EPOLL)
433 epoll_postfork_child (); 506 epoll_postfork_child ();
434#endif 507#endif
435 508
436 evio_stop (&sigev); 509 ev_io_stop (&sigev);
437 close (sigpipe [0]); 510 close (sigpipe [0]);
438 close (sigpipe [1]); 511 close (sigpipe [1]);
439 pipe (sigpipe); 512 pipe (sigpipe);
440 siginit (); 513 siginit ();
441} 514}
442 515
443/*****************************************************************************/ 516/*****************************************************************************/
444 517
445static void 518static void
446fd_reify (void)
447{
448 int i;
449
450 for (i = 0; i < fdchangecnt; ++i)
451 {
452 int fd = fdchanges [i];
453 ANFD *anfd = anfds + fd;
454 struct ev_io *w;
455
456 int wev = 0;
457
458 for (w = anfd->head; w; w = w->next)
459 wev |= w->events;
460
461 if (anfd->wev != wev)
462 {
463 method_modify (fd, anfd->wev, wev);
464 anfd->wev = wev;
465 }
466 }
467
468 fdchangecnt = 0;
469}
470
471static void
472call_pending () 519call_pending (void)
473{ 520{
474 while (pendingcnt) 521 while (pendingcnt)
475 { 522 {
476 ANPENDING *p = pendings + --pendingcnt; 523 ANPENDING *p = pendings + --pendingcnt;
477 524
482 } 529 }
483 } 530 }
484} 531}
485 532
486static void 533static void
487timers_reify () 534timers_reify (void)
488{ 535{
489 while (timercnt && timers [0]->at <= now) 536 while (timercnt && timers [0]->at <= now)
490 { 537 {
491 struct ev_timer *w = timers [0]; 538 struct ev_timer *w = timers [0];
492
493 event ((W)w, EV_TIMEOUT);
494 539
495 /* first reschedule or stop timer */ 540 /* first reschedule or stop timer */
496 if (w->repeat) 541 if (w->repeat)
497 { 542 {
498 w->at = now + w->repeat; 543 w->at = now + w->repeat;
499 assert (("timer timeout in the past, negative repeat?", w->at > now)); 544 assert (("timer timeout in the past, negative repeat?", w->at > now));
500 downheap ((WT *)timers, timercnt, 0); 545 downheap ((WT *)timers, timercnt, 0);
501 } 546 }
502 else 547 else
503 evtimer_stop (w); /* nonrepeating: stop timer */ 548 ev_timer_stop (w); /* nonrepeating: stop timer */
504 }
505}
506 549
550 event ((W)w, EV_TIMEOUT);
551 }
552}
553
507static void 554static void
508periodics_reify () 555periodics_reify (void)
509{ 556{
510 while (periodiccnt && periodics [0]->at <= ev_now) 557 while (periodiccnt && periodics [0]->at <= ev_now)
511 { 558 {
512 struct ev_periodic *w = periodics [0]; 559 struct ev_periodic *w = periodics [0];
513 560
517 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;
518 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));
519 downheap ((WT *)periodics, periodiccnt, 0); 566 downheap ((WT *)periodics, periodiccnt, 0);
520 } 567 }
521 else 568 else
522 evperiodic_stop (w); /* nonrepeating: stop timer */ 569 ev_periodic_stop (w); /* nonrepeating: stop timer */
523 570
524 event ((W)w, EV_TIMEOUT); 571 event ((W)w, EV_TIMEOUT);
525 } 572 }
526} 573}
527 574
539 { 586 {
540 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;
541 588
542 if (fabs (diff) >= 1e-4) 589 if (fabs (diff) >= 1e-4)
543 { 590 {
544 evperiodic_stop (w); 591 ev_periodic_stop (w);
545 evperiodic_start (w); 592 ev_periodic_start (w);
546 593
547 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 594 i = 0; /* restart loop, inefficient, but time jumps should be rare */
548 } 595 }
549 } 596 }
550 } 597 }
551} 598}
552 599
553static void 600static void
554time_update () 601time_update (void)
555{ 602{
556 int i; 603 int i;
557 604
558 ev_now = ev_time (); 605 ev_now = ev_time ();
559 606
593int ev_loop_done; 640int ev_loop_done;
594 641
595void ev_loop (int flags) 642void ev_loop (int flags)
596{ 643{
597 double block; 644 double block;
598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
599 646
600 do 647 do
601 { 648 {
602 /* queue check watchers (and execute them) */ 649 /* queue check watchers (and execute them) */
603 if (preparecnt) 650 if (preparecnt)
708} 755}
709 756
710/*****************************************************************************/ 757/*****************************************************************************/
711 758
712void 759void
713evio_start (struct ev_io *w) 760ev_io_start (struct ev_io *w)
714{ 761{
715 if (ev_is_active (w)) 762 if (ev_is_active (w))
716 return; 763 return;
717 764
718 int fd = w->fd; 765 int fd = w->fd;
719 766
720 ev_start ((W)w, 1); 767 ev_start ((W)w, 1);
721 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 768 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
722 wlist_add ((WL *)&anfds[fd].head, (WL)w); 769 wlist_add ((WL *)&anfds[fd].head, (WL)w);
723 770
724 ++fdchangecnt; 771 fd_change (fd);
725 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
726 fdchanges [fdchangecnt - 1] = fd;
727} 772}
728 773
729void 774void
730evio_stop (struct ev_io *w) 775ev_io_stop (struct ev_io *w)
731{ 776{
732 ev_clear ((W)w); 777 ev_clear ((W)w);
733 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
734 return; 779 return;
735 780
736 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
737 ev_stop ((W)w); 782 ev_stop ((W)w);
738 783
739 ++fdchangecnt; 784 fd_change (w->fd);
740 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
741 fdchanges [fdchangecnt - 1] = w->fd;
742} 785}
743 786
744void 787void
745evtimer_start (struct ev_timer *w) 788ev_timer_start (struct ev_timer *w)
746{ 789{
747 if (ev_is_active (w)) 790 if (ev_is_active (w))
748 return; 791 return;
749 792
750 w->at += now; 793 w->at += now;
756 timers [timercnt - 1] = w; 799 timers [timercnt - 1] = w;
757 upheap ((WT *)timers, timercnt - 1); 800 upheap ((WT *)timers, timercnt - 1);
758} 801}
759 802
760void 803void
761evtimer_stop (struct ev_timer *w) 804ev_timer_stop (struct ev_timer *w)
762{ 805{
763 ev_clear ((W)w); 806 ev_clear ((W)w);
764 if (!ev_is_active (w)) 807 if (!ev_is_active (w))
765 return; 808 return;
766 809
774 817
775 ev_stop ((W)w); 818 ev_stop ((W)w);
776} 819}
777 820
778void 821void
779evtimer_again (struct ev_timer *w) 822ev_timer_again (struct ev_timer *w)
780{ 823{
781 if (ev_is_active (w)) 824 if (ev_is_active (w))
782 { 825 {
783 if (w->repeat) 826 if (w->repeat)
784 { 827 {
785 w->at = now + w->repeat; 828 w->at = now + w->repeat;
786 downheap ((WT *)timers, timercnt, w->active - 1); 829 downheap ((WT *)timers, timercnt, w->active - 1);
787 } 830 }
788 else 831 else
789 evtimer_stop (w); 832 ev_timer_stop (w);
790 } 833 }
791 else if (w->repeat) 834 else if (w->repeat)
792 evtimer_start (w); 835 ev_timer_start (w);
793} 836}
794 837
795void 838void
796evperiodic_start (struct ev_periodic *w) 839ev_periodic_start (struct ev_periodic *w)
797{ 840{
798 if (ev_is_active (w)) 841 if (ev_is_active (w))
799 return; 842 return;
800 843
801 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.));
809 periodics [periodiccnt - 1] = w; 852 periodics [periodiccnt - 1] = w;
810 upheap ((WT *)periodics, periodiccnt - 1); 853 upheap ((WT *)periodics, periodiccnt - 1);
811} 854}
812 855
813void 856void
814evperiodic_stop (struct ev_periodic *w) 857ev_periodic_stop (struct ev_periodic *w)
815{ 858{
816 ev_clear ((W)w); 859 ev_clear ((W)w);
817 if (!ev_is_active (w)) 860 if (!ev_is_active (w))
818 return; 861 return;
819 862
825 868
826 ev_stop ((W)w); 869 ev_stop ((W)w);
827} 870}
828 871
829void 872void
830evsignal_start (struct ev_signal *w) 873ev_signal_start (struct ev_signal *w)
831{ 874{
832 if (ev_is_active (w)) 875 if (ev_is_active (w))
833 return; 876 return;
834 877
835 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
845 sigaction (w->signum, &sa, 0); 888 sigaction (w->signum, &sa, 0);
846 } 889 }
847} 890}
848 891
849void 892void
850evsignal_stop (struct ev_signal *w) 893ev_signal_stop (struct ev_signal *w)
851{ 894{
852 ev_clear ((W)w); 895 ev_clear ((W)w);
853 if (!ev_is_active (w)) 896 if (!ev_is_active (w))
854 return; 897 return;
855 898
858 901
859 if (!signals [w->signum - 1].head) 902 if (!signals [w->signum - 1].head)
860 signal (w->signum, SIG_DFL); 903 signal (w->signum, SIG_DFL);
861} 904}
862 905
906void
863void evidle_start (struct ev_idle *w) 907ev_idle_start (struct ev_idle *w)
864{ 908{
865 if (ev_is_active (w)) 909 if (ev_is_active (w))
866 return; 910 return;
867 911
868 ev_start ((W)w, ++idlecnt); 912 ev_start ((W)w, ++idlecnt);
869 array_needsize (idles, idlemax, idlecnt, ); 913 array_needsize (idles, idlemax, idlecnt, );
870 idles [idlecnt - 1] = w; 914 idles [idlecnt - 1] = w;
871} 915}
872 916
917void
873void evidle_stop (struct ev_idle *w) 918ev_idle_stop (struct ev_idle *w)
874{ 919{
875 ev_clear ((W)w); 920 ev_clear ((W)w);
876 if (ev_is_active (w)) 921 if (ev_is_active (w))
877 return; 922 return;
878 923
879 idles [w->active - 1] = idles [--idlecnt]; 924 idles [w->active - 1] = idles [--idlecnt];
880 ev_stop ((W)w); 925 ev_stop ((W)w);
881} 926}
882 927
928void
883void evprepare_start (struct ev_prepare *w) 929ev_prepare_start (struct ev_prepare *w)
884{ 930{
885 if (ev_is_active (w)) 931 if (ev_is_active (w))
886 return; 932 return;
887 933
888 ev_start ((W)w, ++preparecnt); 934 ev_start ((W)w, ++preparecnt);
889 array_needsize (prepares, preparemax, preparecnt, ); 935 array_needsize (prepares, preparemax, preparecnt, );
890 prepares [preparecnt - 1] = w; 936 prepares [preparecnt - 1] = w;
891} 937}
892 938
939void
893void evprepare_stop (struct ev_prepare *w) 940ev_prepare_stop (struct ev_prepare *w)
894{ 941{
895 ev_clear ((W)w); 942 ev_clear ((W)w);
896 if (ev_is_active (w)) 943 if (ev_is_active (w))
897 return; 944 return;
898 945
899 prepares [w->active - 1] = prepares [--preparecnt]; 946 prepares [w->active - 1] = prepares [--preparecnt];
900 ev_stop ((W)w); 947 ev_stop ((W)w);
901} 948}
902 949
950void
903void evcheck_start (struct ev_check *w) 951ev_check_start (struct ev_check *w)
904{ 952{
905 if (ev_is_active (w)) 953 if (ev_is_active (w))
906 return; 954 return;
907 955
908 ev_start ((W)w, ++checkcnt); 956 ev_start ((W)w, ++checkcnt);
909 array_needsize (checks, checkmax, checkcnt, ); 957 array_needsize (checks, checkmax, checkcnt, );
910 checks [checkcnt - 1] = w; 958 checks [checkcnt - 1] = w;
911} 959}
912 960
961void
913void evcheck_stop (struct ev_check *w) 962ev_check_stop (struct ev_check *w)
914{ 963{
915 ev_clear ((W)w); 964 ev_clear ((W)w);
916 if (ev_is_active (w)) 965 if (ev_is_active (w))
917 return; 966 return;
918 967
919 checks [w->active - 1] = checks [--checkcnt]; 968 checks [w->active - 1] = checks [--checkcnt];
920 ev_stop ((W)w); 969 ev_stop ((W)w);
921} 970}
922 971
972void
923void evchild_start (struct ev_child *w) 973ev_child_start (struct ev_child *w)
924{ 974{
925 if (ev_is_active (w)) 975 if (ev_is_active (w))
926 return; 976 return;
927 977
928 ev_start ((W)w, 1); 978 ev_start ((W)w, 1);
929 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 979 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
930} 980}
931 981
982void
932void evchild_stop (struct ev_child *w) 983ev_child_stop (struct ev_child *w)
933{ 984{
934 ev_clear ((W)w); 985 ev_clear ((W)w);
935 if (ev_is_active (w)) 986 if (ev_is_active (w))
936 return; 987 return;
937 988
953once_cb (struct ev_once *once, int revents) 1004once_cb (struct ev_once *once, int revents)
954{ 1005{
955 void (*cb)(int revents, void *arg) = once->cb; 1006 void (*cb)(int revents, void *arg) = once->cb;
956 void *arg = once->arg; 1007 void *arg = once->arg;
957 1008
958 evio_stop (&once->io); 1009 ev_io_stop (&once->io);
959 evtimer_stop (&once->to); 1010 ev_timer_stop (&once->to);
960 free (once); 1011 free (once);
961 1012
962 cb (revents, arg); 1013 cb (revents, arg);
963} 1014}
964 1015
978ev_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)
979{ 1030{
980 struct ev_once *once = malloc (sizeof (struct ev_once)); 1031 struct ev_once *once = malloc (sizeof (struct ev_once));
981 1032
982 if (!once) 1033 if (!once)
983 cb (EV_ERROR, arg); 1034 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
984 else 1035 else
985 { 1036 {
986 once->cb = cb; 1037 once->cb = cb;
987 once->arg = arg; 1038 once->arg = arg;
988 1039
989 evw_init (&once->io, once_cb_io); 1040 ev_watcher_init (&once->io, once_cb_io);
990
991 if (fd >= 0) 1041 if (fd >= 0)
992 { 1042 {
993 evio_set (&once->io, fd, events); 1043 ev_io_set (&once->io, fd, events);
994 evio_start (&once->io); 1044 ev_io_start (&once->io);
995 } 1045 }
996 1046
997 evw_init (&once->to, once_cb_to); 1047 ev_watcher_init (&once->to, once_cb_to);
998
999 if (timeout >= 0.) 1048 if (timeout >= 0.)
1000 { 1049 {
1001 evtimer_set (&once->to, timeout, 0.); 1050 ev_timer_set (&once->to, timeout, 0.);
1002 evtimer_start (&once->to); 1051 ev_timer_start (&once->to);
1003 } 1052 }
1004 } 1053 }
1005} 1054}
1006 1055
1007/*****************************************************************************/ 1056/*****************************************************************************/
1018 1067
1019static void 1068static void
1020ocb (struct ev_timer *w, int revents) 1069ocb (struct ev_timer *w, int revents)
1021{ 1070{
1022 //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);
1023 evtimer_stop (w); 1072 ev_timer_stop (w);
1024 evtimer_start (w); 1073 ev_timer_start (w);
1025} 1074}
1026 1075
1027static void 1076static void
1028scb (struct ev_signal *w, int revents) 1077scb (struct ev_signal *w, int revents)
1029{ 1078{
1030 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1079 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1031 evio_stop (&wio); 1080 ev_io_stop (&wio);
1032 evio_start (&wio); 1081 ev_io_start (&wio);
1033} 1082}
1034 1083
1035static void 1084static void
1036gcb (struct ev_signal *w, int revents) 1085gcb (struct ev_signal *w, int revents)
1037{ 1086{
1041 1090
1042int main (void) 1091int main (void)
1043{ 1092{
1044 ev_init (0); 1093 ev_init (0);
1045 1094
1046 evio_init (&wio, sin_cb, 0, EV_READ); 1095 ev_io_init (&wio, sin_cb, 0, EV_READ);
1047 evio_start (&wio); 1096 ev_io_start (&wio);
1048 1097
1049 struct ev_timer t[10000]; 1098 struct ev_timer t[10000];
1050 1099
1051#if 0 1100#if 0
1052 int i; 1101 int i;
1053 for (i = 0; i < 10000; ++i) 1102 for (i = 0; i < 10000; ++i)
1054 { 1103 {
1055 struct ev_timer *w = t + i; 1104 struct ev_timer *w = t + i;
1056 evw_init (w, ocb, i); 1105 ev_watcher_init (w, ocb, i);
1057 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1106 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1058 evtimer_start (w); 1107 ev_timer_start (w);
1059 if (drand48 () < 0.5) 1108 if (drand48 () < 0.5)
1060 evtimer_stop (w); 1109 ev_timer_stop (w);
1061 } 1110 }
1062#endif 1111#endif
1063 1112
1064 struct ev_timer t1; 1113 struct ev_timer t1;
1065 evtimer_init (&t1, ocb, 5, 10); 1114 ev_timer_init (&t1, ocb, 5, 10);
1066 evtimer_start (&t1); 1115 ev_timer_start (&t1);
1067 1116
1068 struct ev_signal sig; 1117 struct ev_signal sig;
1069 evsignal_init (&sig, scb, SIGQUIT); 1118 ev_signal_init (&sig, scb, SIGQUIT);
1070 evsignal_start (&sig); 1119 ev_signal_start (&sig);
1071 1120
1072 struct ev_check cw; 1121 struct ev_check cw;
1073 evcheck_init (&cw, gcb); 1122 ev_check_init (&cw, gcb);
1074 evcheck_start (&cw); 1123 ev_check_start (&cw);
1075 1124
1076 struct ev_idle iw; 1125 struct ev_idle iw;
1077 evidle_init (&iw, gcb); 1126 ev_idle_init (&iw, gcb);
1078 evidle_start (&iw); 1127 ev_idle_start (&iw);
1079 1128
1080 ev_loop (0); 1129 ev_loop (0);
1081 1130
1082 return 0; 1131 return 0;
1083} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines