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

Comparing libev/ev.c (file contents):
Revision 1.20 by root, Wed Oct 31 18:28:00 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>
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
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
58#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
59# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
60#endif 68#endif
61 69
62#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) */
63#define MAX_BLOCKTIME 60. 71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
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 */
64 74
65#include "ev.h" 75#include "ev.h"
66 76
67typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
68typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
81/*****************************************************************************/ 91/*****************************************************************************/
82 92
83ev_tstamp 93ev_tstamp
84ev_time (void) 94ev_time (void)
85{ 95{
86#if HAVE_REALTIME 96#if EV_USE_REALTIME
87 struct timespec ts; 97 struct timespec ts;
88 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
89 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
90#else 100#else
91 struct timeval tv; 101 struct timeval tv;
95} 105}
96 106
97static ev_tstamp 107static ev_tstamp
98get_clock (void) 108get_clock (void)
99{ 109{
100#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
101 if (have_monotonic) 111 if (have_monotonic)
102 { 112 {
103 struct timespec ts; 113 struct timespec ts;
104 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
105 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
107#endif 117#endif
108 118
109 return ev_time (); 119 return ev_time ();
110} 120}
111 121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
123
112#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
113 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
114 { \ 126 { \
115 int newcnt = cur ? cur << 1 : 16; \ 127 int newcnt = cur; \
128 do \
129 { \
130 newcnt = array_roundsize (base, newcnt << 1); \
131 } \
132 while ((cnt) > newcnt); \
133 \
116 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
117 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
118 cur = newcnt; \ 136 cur = newcnt; \
119 } 137 }
120 138
121/*****************************************************************************/ 139/*****************************************************************************/
122 140
123typedef struct 141typedef struct
124{ 142{
125 struct ev_io *head; 143 struct ev_io *head;
126 unsigned char wev, rev; /* want, received event set */ 144 int events;
127} ANFD; 145} ANFD;
128 146
129static ANFD *anfds; 147static ANFD *anfds;
130static int anfdmax; 148static int anfdmax;
131 149
132static int *fdchanges;
133static int fdchangemax, fdchangecnt;
134
135static void 150static void
136anfds_init (ANFD *base, int count) 151anfds_init (ANFD *base, int count)
137{ 152{
138 while (count--) 153 while (count--)
139 { 154 {
140 base->head = 0; 155 base->head = 0;
141 base->wev = base->rev = EV_NONE; 156 base->events = EV_NONE;
142 ++base; 157 ++base;
143 } 158 }
144} 159}
145 160
146typedef struct 161typedef struct
153static int pendingmax, pendingcnt; 168static int pendingmax, pendingcnt;
154 169
155static void 170static void
156event (W w, int events) 171event (W w, int events)
157{ 172{
158 if (w->active) 173 if (w->pending)
174 {
175 pendings [w->pending - 1].events |= events;
176 return;
159 { 177 }
178
160 w->pending = ++pendingcnt; 179 w->pending = ++pendingcnt;
161 array_needsize (pendings, pendingmax, pendingcnt, ); 180 array_needsize (pendings, pendingmax, pendingcnt, );
162 pendings [pendingcnt - 1].w = w; 181 pendings [pendingcnt - 1].w = w;
163 pendings [pendingcnt - 1].events = events; 182 pendings [pendingcnt - 1].events = events;
164 } 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);
165} 192}
166 193
167static void 194static void
168fd_event (int fd, int events) 195fd_event (int fd, int events)
169{ 196{
177 if (ev) 204 if (ev)
178 event ((W)w, ev); 205 event ((W)w, ev);
179 } 206 }
180} 207}
181 208
209/*****************************************************************************/
210
211static int *fdchanges;
212static int fdchangemax, fdchangecnt;
213
182static void 214static void
183queue_events (W *events, int eventcnt, int type) 215fd_reify (void)
184{ 216{
185 int i; 217 int i;
186 218
187 for (i = 0; i < eventcnt; ++i) 219 for (i = 0; i < fdchangecnt; ++i)
188 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;
189} 253}
190 254
191/* called on EBADF to verify fds */ 255/* called on EBADF to verify fds */
192static void 256static void
193fd_recheck () 257fd_recheck (void)
194{ 258{
195 int fd; 259 int fd;
196 260
197 for (fd = 0; fd < anfdmax; ++fd) 261 for (fd = 0; fd < anfdmax; ++fd)
198 if (anfds [fd].wev) 262 if (anfds [fd].events)
199 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
200 while (anfds [fd].head) 264 while (anfds [fd].head)
265 {
201 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 }
202} 269}
203 270
204/*****************************************************************************/ 271/*****************************************************************************/
205 272
206static struct ev_timer **timers; 273static struct ev_timer **timers;
315 382
316 /* rather than sort out wether we really need nb, set it */ 383 /* rather than sort out wether we really need nb, set it */
317 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 384 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
318 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 385 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
319 386
320 evio_set (&sigev, sigpipe [0], EV_READ); 387 ev_io_set (&sigev, sigpipe [0], EV_READ);
321 evio_start (&sigev); 388 ev_io_start (&sigev);
322} 389}
323 390
324/*****************************************************************************/ 391/*****************************************************************************/
325 392
326static struct ev_idle **idles; 393static struct ev_idle **idles;
332static struct ev_check **checks; 399static struct ev_check **checks;
333static int checkmax, checkcnt; 400static int checkmax, checkcnt;
334 401
335/*****************************************************************************/ 402/*****************************************************************************/
336 403
404static struct ev_child *childs [PID_HASHSIZE];
405static struct ev_signal childev;
406
407#ifndef WCONTINUED
408# define WCONTINUED 0
409#endif
410
411static void
412childcb (struct ev_signal *sw, int revents)
413{
414 struct ev_child *w;
415 int pid, status;
416
417 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
418 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
419 if (w->pid == pid || w->pid == -1)
420 {
421 w->status = status;
422 event ((W)w, EV_CHILD);
423 }
424}
425
426/*****************************************************************************/
427
337#if HAVE_EPOLL 428#if EV_USE_EPOLL
338# include "ev_epoll.c" 429# include "ev_epoll.c"
339#endif 430#endif
340#if HAVE_SELECT 431#if EV_USE_SELECT
341# include "ev_select.c" 432# include "ev_select.c"
342#endif 433#endif
343 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
344int ev_init (int flags) 447int ev_init (int flags)
345{ 448{
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) 449 if (!ev_method)
450 {
451#if EV_USE_MONOTONIC
370 { 452 {
453 struct timespec ts;
454 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
455 have_monotonic = 1;
456 }
457#endif
458
459 ev_now = ev_time ();
460 now = get_clock ();
461 diff = ev_now - now;
462
463 if (pipe (sigpipe))
464 return 0;
465
466 ev_method = EVMETHOD_NONE;
467#if EV_USE_EPOLL
468 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
469#endif
470#if EV_USE_SELECT
471 if (ev_method == EVMETHOD_NONE) select_init (flags);
472#endif
473
474 if (ev_method)
475 {
371 evw_init (&sigev, sigcb); 476 ev_watcher_init (&sigev, sigcb);
372 siginit (); 477 siginit ();
478
479 ev_signal_init (&childev, childcb, SIGCHLD);
480 ev_signal_start (&childev);
481 }
373 } 482 }
374 483
375 return ev_method; 484 return ev_method;
376} 485}
377 486
378/*****************************************************************************/ 487/*****************************************************************************/
379 488
489void
380void ev_prefork (void) 490ev_prefork (void)
381{ 491{
382 /* nop */ 492 /* nop */
383} 493}
384 494
495void
385void ev_postfork_parent (void) 496ev_postfork_parent (void)
386{ 497{
387 /* nop */ 498 /* nop */
388} 499}
389 500
501void
390void ev_postfork_child (void) 502ev_postfork_child (void)
391{ 503{
392#if HAVE_EPOLL 504#if EV_USE_EPOLL
393 if (ev_method == EVMETHOD_EPOLL) 505 if (ev_method == EVMETHOD_EPOLL)
394 epoll_postfork_child (); 506 epoll_postfork_child ();
395#endif 507#endif
396 508
397 evio_stop (&sigev); 509 ev_io_stop (&sigev);
398 close (sigpipe [0]); 510 close (sigpipe [0]);
399 close (sigpipe [1]); 511 close (sigpipe [1]);
400 pipe (sigpipe); 512 pipe (sigpipe);
401 siginit (); 513 siginit ();
402} 514}
403 515
404/*****************************************************************************/ 516/*****************************************************************************/
405 517
406static void 518static 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 () 519call_pending (void)
434{ 520{
435 while (pendingcnt) 521 while (pendingcnt)
436 { 522 {
437 ANPENDING *p = pendings + --pendingcnt; 523 ANPENDING *p = pendings + --pendingcnt;
438 524
443 } 529 }
444 } 530 }
445} 531}
446 532
447static void 533static void
448timers_reify () 534timers_reify (void)
449{ 535{
450 while (timercnt && timers [0]->at <= now) 536 while (timercnt && timers [0]->at <= now)
451 { 537 {
452 struct ev_timer *w = timers [0]; 538 struct ev_timer *w = timers [0];
453
454 event ((W)w, EV_TIMEOUT);
455 539
456 /* first reschedule or stop timer */ 540 /* first reschedule or stop timer */
457 if (w->repeat) 541 if (w->repeat)
458 { 542 {
459 w->at = now + w->repeat; 543 w->at = now + w->repeat;
460 assert (("timer timeout in the past, negative repeat?", w->at > now)); 544 assert (("timer timeout in the past, negative repeat?", w->at > now));
461 downheap ((WT *)timers, timercnt, 0); 545 downheap ((WT *)timers, timercnt, 0);
462 } 546 }
463 else 547 else
464 evtimer_stop (w); /* nonrepeating: stop timer */ 548 ev_timer_stop (w); /* nonrepeating: stop timer */
465 }
466}
467 549
550 event ((W)w, EV_TIMEOUT);
551 }
552}
553
468static void 554static void
469periodics_reify () 555periodics_reify (void)
470{ 556{
471 while (periodiccnt && periodics [0]->at <= ev_now) 557 while (periodiccnt && periodics [0]->at <= ev_now)
472 { 558 {
473 struct ev_periodic *w = periodics [0]; 559 struct ev_periodic *w = periodics [0];
474 560
478 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;
479 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));
480 downheap ((WT *)periodics, periodiccnt, 0); 566 downheap ((WT *)periodics, periodiccnt, 0);
481 } 567 }
482 else 568 else
483 evperiodic_stop (w); /* nonrepeating: stop timer */ 569 ev_periodic_stop (w); /* nonrepeating: stop timer */
484 570
485 event ((W)w, EV_TIMEOUT); 571 event ((W)w, EV_TIMEOUT);
486 } 572 }
487} 573}
488 574
500 { 586 {
501 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;
502 588
503 if (fabs (diff) >= 1e-4) 589 if (fabs (diff) >= 1e-4)
504 { 590 {
505 evperiodic_stop (w); 591 ev_periodic_stop (w);
506 evperiodic_start (w); 592 ev_periodic_start (w);
507 593
508 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 594 i = 0; /* restart loop, inefficient, but time jumps should be rare */
509 } 595 }
510 } 596 }
511 } 597 }
512} 598}
513 599
514static void 600static void
515time_update () 601time_update (void)
516{ 602{
517 int i; 603 int i;
518 604
519 ev_now = ev_time (); 605 ev_now = ev_time ();
520 606
554int ev_loop_done; 640int ev_loop_done;
555 641
556void ev_loop (int flags) 642void ev_loop (int flags)
557{ 643{
558 double block; 644 double block;
559 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 645 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
560 646
561 do 647 do
562 { 648 {
563 /* queue check watchers (and execute them) */ 649 /* queue check watchers (and execute them) */
564 if (checkcnt) 650 if (preparecnt)
565 { 651 {
566 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 652 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
567 call_pending (); 653 call_pending ();
568 } 654 }
569 655
570 /* update fd-related kernel structures */ 656 /* update fd-related kernel structures */
571 fd_reify (); 657 fd_reify ();
572 658
573 /* calculate blocking time */ 659 /* calculate blocking time */
574 660
575 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 661 /* we only need this for !monotonic clockor timers, but as we basically
662 always have timers, we just calculate it always */
576 ev_now = ev_time (); 663 ev_now = ev_time ();
577 664
578 if (flags & EVLOOP_NONBLOCK || idlecnt) 665 if (flags & EVLOOP_NONBLOCK || idlecnt)
579 block = 0.; 666 block = 0.;
580 else 667 else
668} 755}
669 756
670/*****************************************************************************/ 757/*****************************************************************************/
671 758
672void 759void
673evio_start (struct ev_io *w) 760ev_io_start (struct ev_io *w)
674{ 761{
675 if (ev_is_active (w)) 762 if (ev_is_active (w))
676 return; 763 return;
677 764
678 int fd = w->fd; 765 int fd = w->fd;
679 766
680 ev_start ((W)w, 1); 767 ev_start ((W)w, 1);
681 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 768 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
682 wlist_add ((WL *)&anfds[fd].head, (WL)w); 769 wlist_add ((WL *)&anfds[fd].head, (WL)w);
683 770
684 ++fdchangecnt; 771 fd_change (fd);
685 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
686 fdchanges [fdchangecnt - 1] = fd;
687} 772}
688 773
689void 774void
690evio_stop (struct ev_io *w) 775ev_io_stop (struct ev_io *w)
691{ 776{
692 ev_clear ((W)w); 777 ev_clear ((W)w);
693 if (!ev_is_active (w)) 778 if (!ev_is_active (w))
694 return; 779 return;
695 780
696 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
697 ev_stop ((W)w); 782 ev_stop ((W)w);
698 783
699 ++fdchangecnt; 784 fd_change (w->fd);
700 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
701 fdchanges [fdchangecnt - 1] = w->fd;
702} 785}
703 786
704void 787void
705evtimer_start (struct ev_timer *w) 788ev_timer_start (struct ev_timer *w)
706{ 789{
707 if (ev_is_active (w)) 790 if (ev_is_active (w))
708 return; 791 return;
709 792
710 w->at += now; 793 w->at += now;
716 timers [timercnt - 1] = w; 799 timers [timercnt - 1] = w;
717 upheap ((WT *)timers, timercnt - 1); 800 upheap ((WT *)timers, timercnt - 1);
718} 801}
719 802
720void 803void
721evtimer_stop (struct ev_timer *w) 804ev_timer_stop (struct ev_timer *w)
722{ 805{
723 ev_clear ((W)w); 806 ev_clear ((W)w);
724 if (!ev_is_active (w)) 807 if (!ev_is_active (w))
725 return; 808 return;
726 809
734 817
735 ev_stop ((W)w); 818 ev_stop ((W)w);
736} 819}
737 820
738void 821void
739evtimer_again (struct ev_timer *w) 822ev_timer_again (struct ev_timer *w)
740{ 823{
741 if (ev_is_active (w)) 824 if (ev_is_active (w))
742 { 825 {
743 if (w->repeat) 826 if (w->repeat)
744 { 827 {
745 w->at = now + w->repeat; 828 w->at = now + w->repeat;
746 downheap ((WT *)timers, timercnt, w->active - 1); 829 downheap ((WT *)timers, timercnt, w->active - 1);
747 } 830 }
748 else 831 else
749 evtimer_stop (w); 832 ev_timer_stop (w);
750 } 833 }
751 else if (w->repeat) 834 else if (w->repeat)
752 evtimer_start (w); 835 ev_timer_start (w);
753} 836}
754 837
755void 838void
756evperiodic_start (struct ev_periodic *w) 839ev_periodic_start (struct ev_periodic *w)
757{ 840{
758 if (ev_is_active (w)) 841 if (ev_is_active (w))
759 return; 842 return;
760 843
761 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.));
769 periodics [periodiccnt - 1] = w; 852 periodics [periodiccnt - 1] = w;
770 upheap ((WT *)periodics, periodiccnt - 1); 853 upheap ((WT *)periodics, periodiccnt - 1);
771} 854}
772 855
773void 856void
774evperiodic_stop (struct ev_periodic *w) 857ev_periodic_stop (struct ev_periodic *w)
775{ 858{
776 ev_clear ((W)w); 859 ev_clear ((W)w);
777 if (!ev_is_active (w)) 860 if (!ev_is_active (w))
778 return; 861 return;
779 862
785 868
786 ev_stop ((W)w); 869 ev_stop ((W)w);
787} 870}
788 871
789void 872void
790evsignal_start (struct ev_signal *w) 873ev_signal_start (struct ev_signal *w)
791{ 874{
792 if (ev_is_active (w)) 875 if (ev_is_active (w))
793 return; 876 return;
794 877
795 ev_start ((W)w, 1); 878 ev_start ((W)w, 1);
805 sigaction (w->signum, &sa, 0); 888 sigaction (w->signum, &sa, 0);
806 } 889 }
807} 890}
808 891
809void 892void
810evsignal_stop (struct ev_signal *w) 893ev_signal_stop (struct ev_signal *w)
811{ 894{
812 ev_clear ((W)w); 895 ev_clear ((W)w);
813 if (!ev_is_active (w)) 896 if (!ev_is_active (w))
814 return; 897 return;
815 898
818 901
819 if (!signals [w->signum - 1].head) 902 if (!signals [w->signum - 1].head)
820 signal (w->signum, SIG_DFL); 903 signal (w->signum, SIG_DFL);
821} 904}
822 905
906void
823void evidle_start (struct ev_idle *w) 907ev_idle_start (struct ev_idle *w)
824{ 908{
825 if (ev_is_active (w)) 909 if (ev_is_active (w))
826 return; 910 return;
827 911
828 ev_start ((W)w, ++idlecnt); 912 ev_start ((W)w, ++idlecnt);
829 array_needsize (idles, idlemax, idlecnt, ); 913 array_needsize (idles, idlemax, idlecnt, );
830 idles [idlecnt - 1] = w; 914 idles [idlecnt - 1] = w;
831} 915}
832 916
917void
833void evidle_stop (struct ev_idle *w) 918ev_idle_stop (struct ev_idle *w)
834{ 919{
835 ev_clear ((W)w); 920 ev_clear ((W)w);
836 if (ev_is_active (w)) 921 if (ev_is_active (w))
837 return; 922 return;
838 923
839 idles [w->active - 1] = idles [--idlecnt]; 924 idles [w->active - 1] = idles [--idlecnt];
840 ev_stop ((W)w); 925 ev_stop ((W)w);
841} 926}
842 927
928void
843void evprepare_start (struct ev_prepare *w) 929ev_prepare_start (struct ev_prepare *w)
844{ 930{
845 if (ev_is_active (w)) 931 if (ev_is_active (w))
846 return; 932 return;
847 933
848 ev_start ((W)w, ++preparecnt); 934 ev_start ((W)w, ++preparecnt);
849 array_needsize (prepares, preparemax, preparecnt, ); 935 array_needsize (prepares, preparemax, preparecnt, );
850 prepares [preparecnt - 1] = w; 936 prepares [preparecnt - 1] = w;
851} 937}
852 938
939void
853void evprepare_stop (struct ev_prepare *w) 940ev_prepare_stop (struct ev_prepare *w)
854{ 941{
855 ev_clear ((W)w); 942 ev_clear ((W)w);
856 if (ev_is_active (w)) 943 if (ev_is_active (w))
857 return; 944 return;
858 945
859 prepares [w->active - 1] = prepares [--preparecnt]; 946 prepares [w->active - 1] = prepares [--preparecnt];
860 ev_stop ((W)w); 947 ev_stop ((W)w);
861} 948}
862 949
950void
863void evcheck_start (struct ev_check *w) 951ev_check_start (struct ev_check *w)
864{ 952{
865 if (ev_is_active (w)) 953 if (ev_is_active (w))
866 return; 954 return;
867 955
868 ev_start ((W)w, ++checkcnt); 956 ev_start ((W)w, ++checkcnt);
869 array_needsize (checks, checkmax, checkcnt, ); 957 array_needsize (checks, checkmax, checkcnt, );
870 checks [checkcnt - 1] = w; 958 checks [checkcnt - 1] = w;
871} 959}
872 960
961void
873void evcheck_stop (struct ev_check *w) 962ev_check_stop (struct ev_check *w)
874{ 963{
875 ev_clear ((W)w); 964 ev_clear ((W)w);
876 if (ev_is_active (w)) 965 if (ev_is_active (w))
877 return; 966 return;
878 967
879 checks [w->active - 1] = checks [--checkcnt]; 968 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w);
970}
971
972void
973ev_child_start (struct ev_child *w)
974{
975 if (ev_is_active (w))
976 return;
977
978 ev_start ((W)w, 1);
979 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
980}
981
982void
983ev_child_stop (struct ev_child *w)
984{
985 ev_clear ((W)w);
986 if (ev_is_active (w))
987 return;
988
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
880 ev_stop ((W)w); 990 ev_stop ((W)w);
881} 991}
882 992
883/*****************************************************************************/ 993/*****************************************************************************/
884 994
894once_cb (struct ev_once *once, int revents) 1004once_cb (struct ev_once *once, int revents)
895{ 1005{
896 void (*cb)(int revents, void *arg) = once->cb; 1006 void (*cb)(int revents, void *arg) = once->cb;
897 void *arg = once->arg; 1007 void *arg = once->arg;
898 1008
899 evio_stop (&once->io); 1009 ev_io_stop (&once->io);
900 evtimer_stop (&once->to); 1010 ev_timer_stop (&once->to);
901 free (once); 1011 free (once);
902 1012
903 cb (revents, arg); 1013 cb (revents, arg);
904} 1014}
905 1015
919ev_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)
920{ 1030{
921 struct ev_once *once = malloc (sizeof (struct ev_once)); 1031 struct ev_once *once = malloc (sizeof (struct ev_once));
922 1032
923 if (!once) 1033 if (!once)
924 cb (EV_ERROR, arg); 1034 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
925 else 1035 else
926 { 1036 {
927 once->cb = cb; 1037 once->cb = cb;
928 once->arg = arg; 1038 once->arg = arg;
929 1039
930 evw_init (&once->io, once_cb_io); 1040 ev_watcher_init (&once->io, once_cb_io);
931
932 if (fd >= 0) 1041 if (fd >= 0)
933 { 1042 {
934 evio_set (&once->io, fd, events); 1043 ev_io_set (&once->io, fd, events);
935 evio_start (&once->io); 1044 ev_io_start (&once->io);
936 } 1045 }
937 1046
938 evw_init (&once->to, once_cb_to); 1047 ev_watcher_init (&once->to, once_cb_to);
939
940 if (timeout >= 0.) 1048 if (timeout >= 0.)
941 { 1049 {
942 evtimer_set (&once->to, timeout, 0.); 1050 ev_timer_set (&once->to, timeout, 0.);
943 evtimer_start (&once->to); 1051 ev_timer_start (&once->to);
944 } 1052 }
945 } 1053 }
946} 1054}
947 1055
948/*****************************************************************************/ 1056/*****************************************************************************/
959 1067
960static void 1068static void
961ocb (struct ev_timer *w, int revents) 1069ocb (struct ev_timer *w, int revents)
962{ 1070{
963 //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);
964 evtimer_stop (w); 1072 ev_timer_stop (w);
965 evtimer_start (w); 1073 ev_timer_start (w);
966} 1074}
967 1075
968static void 1076static void
969scb (struct ev_signal *w, int revents) 1077scb (struct ev_signal *w, int revents)
970{ 1078{
971 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1079 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
972 evio_stop (&wio); 1080 ev_io_stop (&wio);
973 evio_start (&wio); 1081 ev_io_start (&wio);
974} 1082}
975 1083
976static void 1084static void
977gcb (struct ev_signal *w, int revents) 1085gcb (struct ev_signal *w, int revents)
978{ 1086{
982 1090
983int main (void) 1091int main (void)
984{ 1092{
985 ev_init (0); 1093 ev_init (0);
986 1094
987 evio_init (&wio, sin_cb, 0, EV_READ); 1095 ev_io_init (&wio, sin_cb, 0, EV_READ);
988 evio_start (&wio); 1096 ev_io_start (&wio);
989 1097
990 struct ev_timer t[10000]; 1098 struct ev_timer t[10000];
991 1099
992#if 0 1100#if 0
993 int i; 1101 int i;
994 for (i = 0; i < 10000; ++i) 1102 for (i = 0; i < 10000; ++i)
995 { 1103 {
996 struct ev_timer *w = t + i; 1104 struct ev_timer *w = t + i;
997 evw_init (w, ocb, i); 1105 ev_watcher_init (w, ocb, i);
998 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1106 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
999 evtimer_start (w); 1107 ev_timer_start (w);
1000 if (drand48 () < 0.5) 1108 if (drand48 () < 0.5)
1001 evtimer_stop (w); 1109 ev_timer_stop (w);
1002 } 1110 }
1003#endif 1111#endif
1004 1112
1005 struct ev_timer t1; 1113 struct ev_timer t1;
1006 evtimer_init (&t1, ocb, 5, 10); 1114 ev_timer_init (&t1, ocb, 5, 10);
1007 evtimer_start (&t1); 1115 ev_timer_start (&t1);
1008 1116
1009 struct ev_signal sig; 1117 struct ev_signal sig;
1010 evsignal_init (&sig, scb, SIGQUIT); 1118 ev_signal_init (&sig, scb, SIGQUIT);
1011 evsignal_start (&sig); 1119 ev_signal_start (&sig);
1012 1120
1013 struct ev_check cw; 1121 struct ev_check cw;
1014 evcheck_init (&cw, gcb); 1122 ev_check_init (&cw, gcb);
1015 evcheck_start (&cw); 1123 ev_check_start (&cw);
1016 1124
1017 struct ev_idle iw; 1125 struct ev_idle iw;
1018 evidle_init (&iw, gcb); 1126 ev_idle_init (&iw, gcb);
1019 evidle_start (&iw); 1127 ev_idle_start (&iw);
1020 1128
1021 ev_loop (0); 1129 ev_loop (0);
1022 1130
1023 return 0; 1131 return 0;
1024} 1132}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines