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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines