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

Comparing libev/ev.c (file contents):
Revision 1.4 by root, Tue Oct 30 23:10:33 2007 UTC vs.
Revision 1.11 by root, Wed Oct 31 07:40:49 2007 UTC

1#include <math.h> 1#include <math.h>
2#include <stdlib.h> 2#include <stdlib.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <signal.h>
3 6
4#include <stdio.h> 7#include <stdio.h>
5 8
6#include <assert.h> 9#include <assert.h>
7#include <errno.h> 10#include <errno.h>
8#include <sys/time.h> 11#include <sys/time.h>
9#include <time.h> 12#include <time.h>
10 13
14#ifndef HAVE_MONOTONIC
11#ifdef CLOCK_MONOTONIC 15# ifdef CLOCK_MONOTONIC
12# define HAVE_MONOTONIC 1 16# define HAVE_MONOTONIC 1
13#endif 17# endif
18#endif
14 19
15#define HAVE_EPOLL 1 20#ifndef HAVE_SELECT
16#define HAVE_REALTIME 1
17#define HAVE_SELECT 0 21# define HAVE_SELECT 1
22#endif
23
24#ifndef HAVE_EPOLL
25# define HAVE_EPOLL 0
26#endif
27
28#ifndef HAVE_REALTIME
29# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
30#endif
18 31
19#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 32#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
20#define MAX_BLOCKTIME 60. 33#define MAX_BLOCKTIME 60.
21 34
22#include "ev.h" 35#include "ev.h"
27 40
28struct ev_watcher_list { 41struct ev_watcher_list {
29 EV_WATCHER_LIST (ev_watcher_list); 42 EV_WATCHER_LIST (ev_watcher_list);
30}; 43};
31 44
45typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL;
47
32static ev_tstamp now, diff; /* monotonic clock */ 48static ev_tstamp now, diff; /* monotonic clock */
33ev_tstamp ev_now; 49ev_tstamp ev_now;
34int ev_method; 50int ev_method;
35 51
36static int have_monotonic; /* runtime */ 52static int have_monotonic; /* runtime */
37 53
38static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 54static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
39static void (*method_reify)(void); 55static void (*method_modify)(int fd, int oev, int nev);
40static void (*method_poll)(ev_tstamp timeout); 56static void (*method_poll)(ev_tstamp timeout);
57
58/*****************************************************************************/
41 59
42ev_tstamp 60ev_tstamp
43ev_time (void) 61ev_time (void)
44{ 62{
45#if HAVE_REALTIME 63#if HAVE_REALTIME
76 base = realloc (base, sizeof (*base) * (newcnt)); \ 94 base = realloc (base, sizeof (*base) * (newcnt)); \
77 init (base + cur, newcnt - cur); \ 95 init (base + cur, newcnt - cur); \
78 cur = newcnt; \ 96 cur = newcnt; \
79 } 97 }
80 98
99/*****************************************************************************/
100
81typedef struct 101typedef struct
82{ 102{
83 struct ev_io *head; 103 struct ev_io *head;
84 unsigned char wev, rev; /* want, received event set */ 104 unsigned char wev, rev; /* want, received event set */
85} ANFD; 105} ANFD;
101 } 121 }
102} 122}
103 123
104typedef struct 124typedef struct
105{ 125{
106 struct ev_watcher *w; 126 W w;
107 int events; 127 int events;
108} ANPENDING; 128} ANPENDING;
109 129
110static ANPENDING *pendings; 130static ANPENDING *pendings;
111static int pendingmax, pendingcnt; 131static int pendingmax, pendingcnt;
112 132
113static void 133static void
114event (struct ev_watcher *w, int events) 134event (W w, int events)
115{ 135{
116 w->pending = ++pendingcnt; 136 w->pending = ++pendingcnt;
117 array_needsize (pendings, pendingmax, pendingcnt, ); 137 array_needsize (pendings, pendingmax, pendingcnt, );
118 pendings [pendingcnt - 1].w = w; 138 pendings [pendingcnt - 1].w = w;
119 pendings [pendingcnt - 1].events = events; 139 pendings [pendingcnt - 1].events = events;
128 for (w = anfd->head; w; w = w->next) 148 for (w = anfd->head; w; w = w->next)
129 { 149 {
130 int ev = w->events & events; 150 int ev = w->events & events;
131 151
132 if (ev) 152 if (ev)
133 event ((struct ev_watcher *)w, ev); 153 event ((W)w, ev);
134 } 154 }
135} 155}
156
157static void
158queue_events (W *events, int eventcnt, int type)
159{
160 int i;
161
162 for (i = 0; i < eventcnt; ++i)
163 event (events [i], type);
164}
165
166/*****************************************************************************/
136 167
137static struct ev_timer **atimers; 168static struct ev_timer **atimers;
138static int atimermax, atimercnt; 169static int atimermax, atimercnt;
139 170
140static struct ev_timer **rtimers; 171static struct ev_timer **rtimers;
179 210
180 timers [k] = w; 211 timers [k] = w;
181 timers [k]->active = k + 1; 212 timers [k]->active = k + 1;
182} 213}
183 214
184static struct ev_signal **signals; 215/*****************************************************************************/
216
217typedef struct
218{
219 struct ev_signal *head;
220 sig_atomic_t gotsig;
221} ANSIG;
222
223static ANSIG *signals;
185static int signalmax; 224static int signalmax;
186 225
226static int sigpipe [2];
227static sig_atomic_t gotsig;
228static struct ev_io sigev;
229
187static void 230static void
188signals_init (struct ev_signal **base, int count) 231signals_init (ANSIG *base, int count)
189{ 232{
190 while (count--) 233 while (count--)
191 *base++ = 0; 234 {
235 base->head = 0;
236 base->gotsig = 0;
237 ++base;
238 }
192} 239}
240
241static void
242sighandler (int signum)
243{
244 signals [signum - 1].gotsig = 1;
245
246 if (!gotsig)
247 {
248 gotsig = 1;
249 write (sigpipe [1], &gotsig, 1);
250 }
251}
252
253static void
254sigcb (struct ev_io *iow, int revents)
255{
256 struct ev_signal *w;
257 int sig;
258
259 gotsig = 0;
260 read (sigpipe [0], &revents, 1);
261
262 for (sig = signalmax; sig--; )
263 if (signals [sig].gotsig)
264 {
265 signals [sig].gotsig = 0;
266
267 for (w = signals [sig].head; w; w = w->next)
268 event ((W)w, EV_SIGNAL);
269 }
270}
271
272static void
273siginit (void)
274{
275 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
276 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
277
278 /* rather than sort out wether we really need nb, set it */
279 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
280 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
281
282 evio_set (&sigev, sigpipe [0], EV_READ);
283 evio_start (&sigev);
284}
285
286/*****************************************************************************/
287
288static struct ev_idle **idles;
289static int idlemax, idlecnt;
290
291static struct ev_check **checks;
292static int checkmax, checkcnt;
293
294/*****************************************************************************/
193 295
194#if HAVE_EPOLL 296#if HAVE_EPOLL
195# include "ev_epoll.c" 297# include "ev_epoll.c"
196#endif 298#endif
197#if HAVE_SELECT 299#if HAVE_SELECT
210 312
211 ev_now = ev_time (); 313 ev_now = ev_time ();
212 now = get_clock (); 314 now = get_clock ();
213 diff = ev_now - now; 315 diff = ev_now - now;
214 316
317 if (pipe (sigpipe))
318 return 0;
319
320 ev_method = EVMETHOD_NONE;
215#if HAVE_EPOLL 321#if HAVE_EPOLL
216 if (epoll_init (flags)) 322 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
217 return ev_method;
218#endif 323#endif
219#if HAVE_SELECT 324#if HAVE_SELECT
220 if (select_init (flags)) 325 if (ev_method == EVMETHOD_NONE) select_init (flags);
221 return ev_method;
222#endif 326#endif
223 327
224 ev_method = EVMETHOD_NONE; 328 if (ev_method)
329 {
330 evw_init (&sigev, sigcb, 0);
331 siginit ();
332 }
333
225 return ev_method; 334 return ev_method;
226} 335}
227 336
337/*****************************************************************************/
338
228void ev_prefork (void) 339void ev_prefork (void)
229{ 340{
341 /* nop */
230} 342}
231 343
232void ev_postfork_parent (void) 344void ev_postfork_parent (void)
233{ 345{
346 /* nop */
234} 347}
235 348
236void ev_postfork_child (void) 349void ev_postfork_child (void)
237{ 350{
238#if HAVE_EPOLL 351#if HAVE_EPOLL
352 if (ev_method == EVMETHOD_EPOLL)
239 epoll_postfork_child (); 353 epoll_postfork_child ();
240#endif 354#endif
355
356 evio_stop (&sigev);
357 close (sigpipe [0]);
358 close (sigpipe [1]);
359 pipe (sigpipe);
360 siginit ();
361}
362
363/*****************************************************************************/
364
365static void
366fd_reify (void)
367{
368 int i;
369
370 for (i = 0; i < fdchangecnt; ++i)
371 {
372 int fd = fdchanges [i];
373 ANFD *anfd = anfds + fd;
374 struct ev_io *w;
375
376 int wev = 0;
377
378 for (w = anfd->head; w; w = w->next)
379 wev |= w->events;
380
381 if (anfd->wev != wev)
382 {
383 method_modify (fd, anfd->wev, wev);
384 anfd->wev = wev;
385 }
386 }
387
388 fdchangecnt = 0;
241} 389}
242 390
243static void 391static void
244call_pending () 392call_pending ()
245{ 393{
282 { 430 {
283 evtimer_stop (w); /* nonrepeating: stop timer */ 431 evtimer_stop (w); /* nonrepeating: stop timer */
284 --timercnt; /* maybe pass by reference instead? */ 432 --timercnt; /* maybe pass by reference instead? */
285 } 433 }
286 434
287 event ((struct ev_watcher *)w, EV_TIMEOUT); 435 event ((W)w, EV_TIMEOUT);
288 } 436 }
289} 437}
290 438
291static void 439static void
292time_update () 440time_update ()
333void ev_loop (int flags) 481void ev_loop (int flags)
334{ 482{
335 double block; 483 double block;
336 ev_loop_done = flags & EVLOOP_ONESHOT; 484 ev_loop_done = flags & EVLOOP_ONESHOT;
337 485
486 if (checkcnt)
487 {
488 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 call_pending ();
490 }
491
338 do 492 do
339 { 493 {
340 /* update fd-related kernel structures */ 494 /* update fd-related kernel structures */
341 method_reify (); fdchangecnt = 0; 495 fd_reify ();
342 496
343 /* calculate blocking time */ 497 /* calculate blocking time */
344 if (flags & EVLOOP_NONBLOCK) 498 if (flags & EVLOOP_NONBLOCK || idlecnt)
345 block = 0.; 499 block = 0.;
346 else 500 else
347 { 501 {
348 block = MAX_BLOCKTIME; 502 block = MAX_BLOCKTIME;
349 503
365 method_poll (block); 519 method_poll (block);
366 520
367 /* update ev_now, do magic */ 521 /* update ev_now, do magic */
368 time_update (); 522 time_update ();
369 523
370 /* put pending timers into pendign queue and reschedule them */ 524 /* queue pending timers and reschedule them */
371 /* absolute timers first */ 525 /* absolute timers first */
372 timers_reify (atimers, atimercnt, ev_now); 526 timers_reify (atimers, atimercnt, ev_now);
373 /* relative timers second */ 527 /* relative timers second */
374 timers_reify (rtimers, rtimercnt, now); 528 timers_reify (rtimers, rtimercnt, now);
375 529
530 /* queue idle watchers unless io or timers are pending */
531 if (!pendingcnt)
532 queue_events ((W *)idles, idlecnt, EV_IDLE);
533
534 /* queue check and possibly idle watchers */
535 queue_events ((W *)checks, checkcnt, EV_CHECK);
536
376 call_pending (); 537 call_pending ();
377 } 538 }
378 while (!ev_loop_done); 539 while (!ev_loop_done);
379} 540}
380 541
542/*****************************************************************************/
543
381static void 544static void
382wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 545wlist_add (WL *head, WL elem)
383{ 546{
384 elem->next = *head; 547 elem->next = *head;
385 *head = elem; 548 *head = elem;
386} 549}
387 550
388static void 551static void
389wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 552wlist_del (WL *head, WL elem)
390{ 553{
391 while (*head) 554 while (*head)
392 { 555 {
393 if (*head == elem) 556 if (*head == elem)
394 { 557 {
399 head = &(*head)->next; 562 head = &(*head)->next;
400 } 563 }
401} 564}
402 565
403static void 566static void
404ev_start (struct ev_watcher *w, int active) 567ev_start (W w, int active)
405{ 568{
406 w->pending = 0; 569 w->pending = 0;
407 w->active = active; 570 w->active = active;
408} 571}
409 572
410static void 573static void
411ev_stop (struct ev_watcher *w) 574ev_stop (W w)
412{ 575{
413 if (w->pending) 576 if (w->pending)
414 pendings [w->pending - 1].w = 0; 577 pendings [w->pending - 1].w = 0;
415 578
416 w->active = 0; 579 w->active = 0;
417 /* nop */
418} 580}
581
582/*****************************************************************************/
419 583
420void 584void
421evio_start (struct ev_io *w) 585evio_start (struct ev_io *w)
422{ 586{
423 if (ev_is_active (w)) 587 if (ev_is_active (w))
424 return; 588 return;
425 589
426 int fd = w->fd; 590 int fd = w->fd;
427 591
428 ev_start ((struct ev_watcher *)w, 1); 592 ev_start ((W)w, 1);
429 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 593 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
430 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 594 wlist_add ((WL *)&anfds[fd].head, (WL)w);
431 595
432 ++fdchangecnt; 596 ++fdchangecnt;
433 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 597 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
434 fdchanges [fdchangecnt - 1] = fd; 598 fdchanges [fdchangecnt - 1] = fd;
435} 599}
438evio_stop (struct ev_io *w) 602evio_stop (struct ev_io *w)
439{ 603{
440 if (!ev_is_active (w)) 604 if (!ev_is_active (w))
441 return; 605 return;
442 606
443 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 607 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
444 ev_stop ((struct ev_watcher *)w); 608 ev_stop ((W)w);
445 609
446 ++fdchangecnt; 610 ++fdchangecnt;
447 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 611 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
448 fdchanges [fdchangecnt - 1] = w->fd; 612 fdchanges [fdchangecnt - 1] = w->fd;
449} 613}
458 { 622 {
459 /* this formula differs from the one in timer_reify becuse we do not round up */ 623 /* this formula differs from the one in timer_reify becuse we do not round up */
460 if (w->repeat) 624 if (w->repeat)
461 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
462 626
463 ev_start ((struct ev_watcher *)w, ++atimercnt); 627 ev_start ((W)w, ++atimercnt);
464 array_needsize (atimers, atimermax, atimercnt, ); 628 array_needsize (atimers, atimermax, atimercnt, );
465 atimers [atimercnt - 1] = w; 629 atimers [atimercnt - 1] = w;
466 upheap (atimers, atimercnt - 1); 630 upheap (atimers, atimercnt - 1);
467 } 631 }
468 else 632 else
469 { 633 {
470 w->at += now; 634 w->at += now;
471 635
472 ev_start ((struct ev_watcher *)w, ++rtimercnt); 636 ev_start ((W)w, ++rtimercnt);
473 array_needsize (rtimers, rtimermax, rtimercnt, ); 637 array_needsize (rtimers, rtimermax, rtimercnt, );
474 rtimers [rtimercnt - 1] = w; 638 rtimers [rtimercnt - 1] = w;
475 upheap (rtimers, rtimercnt - 1); 639 upheap (rtimers, rtimercnt - 1);
476 } 640 }
477 641
498 rtimers [w->active - 1] = rtimers [rtimercnt]; 662 rtimers [w->active - 1] = rtimers [rtimercnt];
499 downheap (rtimers, rtimercnt, w->active - 1); 663 downheap (rtimers, rtimercnt, w->active - 1);
500 } 664 }
501 } 665 }
502 666
503 ev_stop ((struct ev_watcher *)w); 667 ev_stop ((W)w);
504} 668}
505 669
506void 670void
507evsignal_start (struct ev_signal *w) 671evsignal_start (struct ev_signal *w)
508{ 672{
509 if (ev_is_active (w)) 673 if (ev_is_active (w))
510 return; 674 return;
511 675
512 ev_start ((struct ev_watcher *)w, 1); 676 ev_start ((W)w, 1);
513 array_needsize (signals, signalmax, w->signum, signals_init); 677 array_needsize (signals, signalmax, w->signum, signals_init);
514 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 678 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
679
680 if (!w->next)
681 {
682 struct sigaction sa;
683 sa.sa_handler = sighandler;
684 sigfillset (&sa.sa_mask);
685 sa.sa_flags = 0;
686 sigaction (w->signum, &sa, 0);
687 }
515} 688}
516 689
517void 690void
518evsignal_stop (struct ev_signal *w) 691evsignal_stop (struct ev_signal *w)
519{ 692{
520 if (!ev_is_active (w)) 693 if (!ev_is_active (w))
521 return; 694 return;
522 695
523 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); 696 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
524 ev_stop ((struct ev_watcher *)w); 697 ev_stop ((W)w);
525}
526 698
699 if (!signals [w->signum - 1].head)
700 signal (w->signum, SIG_DFL);
701}
702
703void evidle_start (struct ev_idle *w)
704{
705 if (ev_is_active (w))
706 return;
707
708 ev_start ((W)w, ++idlecnt);
709 array_needsize (idles, idlemax, idlecnt, );
710 idles [idlecnt - 1] = w;
711}
712
713void evidle_stop (struct ev_idle *w)
714{
715 idles [w->active - 1] = idles [--idlecnt];
716 ev_stop ((W)w);
717}
718
719void evcheck_start (struct ev_check *w)
720{
721 if (ev_is_active (w))
722 return;
723
724 ev_start ((W)w, ++checkcnt);
725 array_needsize (checks, checkmax, checkcnt, );
726 checks [checkcnt - 1] = w;
727}
728
729void evcheck_stop (struct ev_check *w)
730{
731 checks [w->active - 1] = checks [--checkcnt];
732 ev_stop ((W)w);
733}
734
527/*****************************************************************************/ 735/*****************************************************************************/
736
528#if 1 737#if 0
529 738
530static void 739static void
531sin_cb (struct ev_io *w, int revents) 740sin_cb (struct ev_io *w, int revents)
532{ 741{
533 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
539 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 748 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
540 evtimer_stop (w); 749 evtimer_stop (w);
541 evtimer_start (w); 750 evtimer_start (w);
542} 751}
543 752
753static void
754scb (struct ev_signal *w, int revents)
755{
756 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
757}
758
759static void
760gcb (struct ev_signal *w, int revents)
761{
762 fprintf (stderr, "generic %x\n", revents);
763}
764
544int main (void) 765int main (void)
545{ 766{
546 struct ev_io sin; 767 struct ev_io sin;
547 768
548 ev_init (0); 769 ev_init (0);
551 evio_set (&sin, 0, EV_READ); 772 evio_set (&sin, 0, EV_READ);
552 evio_start (&sin); 773 evio_start (&sin);
553 774
554 struct ev_timer t[10000]; 775 struct ev_timer t[10000];
555 776
556#if 1 777#if 0
557 int i; 778 int i;
558 for (i = 0; i < 10000; ++i) 779 for (i = 0; i < 10000; ++i)
559 { 780 {
560 struct ev_timer *w = t + i; 781 struct ev_timer *w = t + i;
561 evw_init (w, ocb, i); 782 evw_init (w, ocb, i);
569 struct ev_timer t1; 790 struct ev_timer t1;
570 evw_init (&t1, ocb, 0); 791 evw_init (&t1, ocb, 0);
571 evtimer_set_abs (&t1, 5, 10); 792 evtimer_set_abs (&t1, 5, 10);
572 evtimer_start (&t1); 793 evtimer_start (&t1);
573 794
795 struct ev_signal sig;
796 evw_init (&sig, scb, 65535);
797 evsignal_set (&sig, SIGQUIT);
798 evsignal_start (&sig);
799
800 struct ev_check cw;
801 evw_init (&cw, gcb, 0);
802 evcheck_start (&cw);
803
804 struct ev_idle iw;
805 evw_init (&iw, gcb, 0);
806 evidle_start (&iw);
807
574 ev_loop (0); 808 ev_loop (0);
575 809
576 return 0; 810 return 0;
577} 811}
578 812

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines