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

Comparing libev/ev.c (file contents):
Revision 1.15 by root, Wed Oct 31 11:56:34 2007 UTC vs.
Revision 1.25 by root, Wed Oct 31 21:34:45 2007 UTC

1/*
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
28 */
29
1#include <math.h> 30#include <math.h>
2#include <stdlib.h> 31#include <stdlib.h>
3#include <unistd.h> 32#include <unistd.h>
4#include <fcntl.h> 33#include <fcntl.h>
5#include <signal.h> 34#include <signal.h>
35#include <stddef.h>
6 36
7#include <stdio.h> 37#include <stdio.h>
8 38
9#include <assert.h> 39#include <assert.h>
10#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
11#include <sys/time.h> 43#include <sys/time.h>
12#include <time.h> 44#include <time.h>
13
14#define HAVE_EPOLL 1
15 45
16#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
17# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
18# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
19# endif 49# endif
31# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
32#endif 62#endif
33 63
34#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
35#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
36 67
37#include "ev.h" 68#include "ev.h"
38 69
39typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
40typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
82} 113}
83 114
84#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
85 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
86 { \ 117 { \
87 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
88 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
89 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
90 cur = newcnt; \ 127 cur = newcnt; \
91 } 128 }
92 129
125static int pendingmax, pendingcnt; 162static int pendingmax, pendingcnt;
126 163
127static void 164static void
128event (W w, int events) 165event (W w, int events)
129{ 166{
167 if (w->active)
168 {
130 w->pending = ++pendingcnt; 169 w->pending = ++pendingcnt;
131 array_needsize (pendings, pendingmax, pendingcnt, ); 170 array_needsize (pendings, pendingmax, pendingcnt, );
132 pendings [pendingcnt - 1].w = w; 171 pendings [pendingcnt - 1].w = w;
133 pendings [pendingcnt - 1].events = events; 172 pendings [pendingcnt - 1].events = events;
173 }
134} 174}
135 175
136static void 176static void
137fd_event (int fd, int events) 177fd_event (int fd, int events)
138{ 178{
153{ 193{
154 int i; 194 int i;
155 195
156 for (i = 0; i < eventcnt; ++i) 196 for (i = 0; i < eventcnt; ++i)
157 event (events [i], type); 197 event (events [i], type);
198}
199
200/* called on EBADF to verify fds */
201static void
202fd_recheck (void)
203{
204 int fd;
205
206 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head)
210 {
211 event ((W)anfds [fd].head, EV_ERROR);
212 evio_stop (anfds [fd].head);
213 }
158} 214}
159 215
160/*****************************************************************************/ 216/*****************************************************************************/
161 217
162static struct ev_timer **timers; 218static struct ev_timer **timers;
280/*****************************************************************************/ 336/*****************************************************************************/
281 337
282static struct ev_idle **idles; 338static struct ev_idle **idles;
283static int idlemax, idlecnt; 339static int idlemax, idlecnt;
284 340
341static struct ev_prepare **prepares;
342static int preparemax, preparecnt;
343
285static struct ev_check **checks; 344static struct ev_check **checks;
286static int checkmax, checkcnt; 345static int checkmax, checkcnt;
287 346
288/*****************************************************************************/ 347/*****************************************************************************/
289 348
349static struct ev_child *childs [PID_HASHSIZE];
350static struct ev_signal childev;
351
352#ifndef WCONTINUED
353# define WCONTINUED 0
354#endif
355
356static void
357childcb (struct ev_signal *sw, int revents)
358{
359 struct ev_child *w;
360 int pid, status;
361
362 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
363 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
364 if (w->pid == pid || w->pid == -1)
365 {
366 w->status = status;
367 event ((W)w, EV_CHILD);
368 }
369}
370
371/*****************************************************************************/
372
290#if HAVE_EPOLL 373#if HAVE_EPOLL
291# include "ev_epoll.c" 374# include "ev_epoll.c"
292#endif 375#endif
293#if HAVE_SELECT 376#if HAVE_SELECT
294# include "ev_select.c" 377# include "ev_select.c"
295#endif 378#endif
296 379
380int
381ev_version_major (void)
382{
383 return EV_VERSION_MAJOR;
384}
385
386int
387ev_version_minor (void)
388{
389 return EV_VERSION_MINOR;
390}
391
297int ev_init (int flags) 392int ev_init (int flags)
298{ 393{
394 if (!ev_method)
395 {
299#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
300 { 397 {
301 struct timespec ts; 398 struct timespec ts;
302 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
303 have_monotonic = 1; 400 have_monotonic = 1;
304 } 401 }
305#endif 402#endif
306 403
307 ev_now = ev_time (); 404 ev_now = ev_time ();
308 now = get_clock (); 405 now = get_clock ();
309 diff = ev_now - now; 406 diff = ev_now - now;
310 407
311 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
312 return 0; 409 return 0;
313 410
314 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
315#if HAVE_EPOLL 412#if HAVE_EPOLL
316 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
317#endif 414#endif
318#if HAVE_SELECT 415#if HAVE_SELECT
319 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
320#endif 417#endif
321 418
322 if (ev_method) 419 if (ev_method)
323 { 420 {
324 evw_init (&sigev, sigcb); 421 evw_init (&sigev, sigcb);
325 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
326 } 427 }
327 428
328 return ev_method; 429 return ev_method;
329} 430}
330 431
331/*****************************************************************************/ 432/*****************************************************************************/
332 433
434void
333void ev_prefork (void) 435ev_prefork (void)
334{ 436{
335 /* nop */ 437 /* nop */
336} 438}
337 439
440void
338void ev_postfork_parent (void) 441ev_postfork_parent (void)
339{ 442{
340 /* nop */ 443 /* nop */
341} 444}
342 445
446void
343void ev_postfork_child (void) 447ev_postfork_child (void)
344{ 448{
345#if HAVE_EPOLL 449#if HAVE_EPOLL
346 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
347 epoll_postfork_child (); 451 epoll_postfork_child ();
348#endif 452#endif
381 485
382 fdchangecnt = 0; 486 fdchangecnt = 0;
383} 487}
384 488
385static void 489static void
386call_pending () 490call_pending (void)
387{ 491{
388 int i; 492 while (pendingcnt)
389
390 for (i = 0; i < pendingcnt; ++i)
391 { 493 {
392 ANPENDING *p = pendings + i; 494 ANPENDING *p = pendings + --pendingcnt;
393 495
394 if (p->w) 496 if (p->w)
395 { 497 {
396 p->w->pending = 0; 498 p->w->pending = 0;
397 p->w->cb (p->w, p->events); 499 p->w->cb (p->w, p->events);
398 } 500 }
399 } 501 }
400
401 pendingcnt = 0;
402} 502}
403 503
404static void 504static void
405timers_reify () 505timers_reify (void)
406{ 506{
407 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
408 { 508 {
409 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
510
511 event ((W)w, EV_TIMEOUT);
410 512
411 /* first reschedule or stop timer */ 513 /* first reschedule or stop timer */
412 if (w->repeat) 514 if (w->repeat)
413 { 515 {
414 w->at = now + w->repeat; 516 w->at = now + w->repeat;
415 assert (("timer timeout in the past, negative repeat?", w->at > now)); 517 assert (("timer timeout in the past, negative repeat?", w->at > now));
416 downheap ((WT *)timers, timercnt, 0); 518 downheap ((WT *)timers, timercnt, 0);
417 } 519 }
418 else 520 else
419 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
420
421 event ((W)w, EV_TIMEOUT);
422 } 522 }
423} 523}
424 524
425static void 525static void
426periodics_reify () 526periodics_reify (void)
427{ 527{
428 while (periodiccnt && periodics [0]->at <= ev_now) 528 while (periodiccnt && periodics [0]->at <= ev_now)
429 { 529 {
430 struct ev_periodic *w = periodics [0]; 530 struct ev_periodic *w = periodics [0];
431 531
467 } 567 }
468 } 568 }
469} 569}
470 570
471static void 571static void
472time_update () 572time_update (void)
473{ 573{
474 int i; 574 int i;
475 575
476 ev_now = ev_time (); 576 ev_now = ev_time ();
477 577
513void ev_loop (int flags) 613void ev_loop (int flags)
514{ 614{
515 double block; 615 double block;
516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
517 617
518 if (checkcnt)
519 {
520 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 call_pending ();
522 }
523
524 do 618 do
525 { 619 {
620 /* queue check watchers (and execute them) */
621 if (preparecnt)
622 {
623 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
624 call_pending ();
625 }
626
526 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
527 fd_reify (); 628 fd_reify ();
528 629
529 /* calculate blocking time */ 630 /* calculate blocking time */
530 631
531 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 632 /* we only need this for !monotonic clockor timers, but as we basically
633 always have timers, we just calculate it always */
532 ev_now = ev_time (); 634 ev_now = ev_time ();
533 635
534 if (flags & EVLOOP_NONBLOCK || idlecnt) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
535 block = 0.; 637 block = 0.;
536 else 638 else
556 658
557 /* update ev_now, do magic */ 659 /* update ev_now, do magic */
558 time_update (); 660 time_update ();
559 661
560 /* queue pending timers and reschedule them */ 662 /* queue pending timers and reschedule them */
663 timers_reify (); /* relative timers called last */
561 periodics_reify (); /* absolute timers first */ 664 periodics_reify (); /* absolute timers called first */
562 timers_reify (); /* relative timers second */
563 665
564 /* queue idle watchers unless io or timers are pending */ 666 /* queue idle watchers unless io or timers are pending */
565 if (!pendingcnt) 667 if (!pendingcnt)
566 queue_events ((W *)idles, idlecnt, EV_IDLE); 668 queue_events ((W *)idles, idlecnt, EV_IDLE);
567 669
568 /* queue check and possibly idle watchers */ 670 /* queue check watchers, to be executed first */
671 if (checkcnt)
569 queue_events ((W *)checks, checkcnt, EV_CHECK); 672 queue_events ((W *)checks, checkcnt, EV_CHECK);
570 673
571 call_pending (); 674 call_pending ();
572 } 675 }
573 while (!ev_loop_done); 676 while (!ev_loop_done);
574 677
599 head = &(*head)->next; 702 head = &(*head)->next;
600 } 703 }
601} 704}
602 705
603static void 706static void
707ev_clear (W w)
708{
709 if (w->pending)
710 {
711 pendings [w->pending - 1].w = 0;
712 w->pending = 0;
713 }
714}
715
716static void
604ev_start (W w, int active) 717ev_start (W w, int active)
605{ 718{
606 w->pending = 0;
607 w->active = active; 719 w->active = active;
608} 720}
609 721
610static void 722static void
611ev_stop (W w) 723ev_stop (W w)
612{ 724{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 725 w->active = 0;
617} 726}
618 727
619/*****************************************************************************/ 728/*****************************************************************************/
620 729
631 wlist_add ((WL *)&anfds[fd].head, (WL)w); 740 wlist_add ((WL *)&anfds[fd].head, (WL)w);
632 741
633 ++fdchangecnt; 742 ++fdchangecnt;
634 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
635 fdchanges [fdchangecnt - 1] = fd; 744 fdchanges [fdchangecnt - 1] = fd;
745
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
636} 748}
637 749
638void 750void
639evio_stop (struct ev_io *w) 751evio_stop (struct ev_io *w)
640{ 752{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
755 ev_clear ((W)w);
641 if (!ev_is_active (w)) 756 if (!ev_is_active (w))
642 return; 757 return;
643 758
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 760 ev_stop ((W)w);
646 761
647 ++fdchangecnt; 762 ++fdchangecnt;
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 763 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd; 764 fdchanges [fdchangecnt - 1] = w->fd;
650} 765}
651
652 766
653void 767void
654evtimer_start (struct ev_timer *w) 768evtimer_start (struct ev_timer *w)
655{ 769{
656 if (ev_is_active (w)) 770 if (ev_is_active (w))
667} 781}
668 782
669void 783void
670evtimer_stop (struct ev_timer *w) 784evtimer_stop (struct ev_timer *w)
671{ 785{
786 ev_clear ((W)w);
672 if (!ev_is_active (w)) 787 if (!ev_is_active (w))
673 return; 788 return;
674 789
675 if (w->active < timercnt--) 790 if (w->active < timercnt--)
676 { 791 {
719} 834}
720 835
721void 836void
722evperiodic_stop (struct ev_periodic *w) 837evperiodic_stop (struct ev_periodic *w)
723{ 838{
839 ev_clear ((W)w);
724 if (!ev_is_active (w)) 840 if (!ev_is_active (w))
725 return; 841 return;
726 842
727 if (w->active < periodiccnt--) 843 if (w->active < periodiccnt--)
728 { 844 {
754} 870}
755 871
756void 872void
757evsignal_stop (struct ev_signal *w) 873evsignal_stop (struct ev_signal *w)
758{ 874{
875 ev_clear ((W)w);
759 if (!ev_is_active (w)) 876 if (!ev_is_active (w))
760 return; 877 return;
761 878
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 879 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 880 ev_stop ((W)w);
776 idles [idlecnt - 1] = w; 893 idles [idlecnt - 1] = w;
777} 894}
778 895
779void evidle_stop (struct ev_idle *w) 896void evidle_stop (struct ev_idle *w)
780{ 897{
898 ev_clear ((W)w);
899 if (ev_is_active (w))
900 return;
901
781 idles [w->active - 1] = idles [--idlecnt]; 902 idles [w->active - 1] = idles [--idlecnt];
903 ev_stop ((W)w);
904}
905
906void evprepare_start (struct ev_prepare *w)
907{
908 if (ev_is_active (w))
909 return;
910
911 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w;
914}
915
916void evprepare_stop (struct ev_prepare *w)
917{
918 ev_clear ((W)w);
919 if (ev_is_active (w))
920 return;
921
922 prepares [w->active - 1] = prepares [--preparecnt];
782 ev_stop ((W)w); 923 ev_stop ((W)w);
783} 924}
784 925
785void evcheck_start (struct ev_check *w) 926void evcheck_start (struct ev_check *w)
786{ 927{
792 checks [checkcnt - 1] = w; 933 checks [checkcnt - 1] = w;
793} 934}
794 935
795void evcheck_stop (struct ev_check *w) 936void evcheck_stop (struct ev_check *w)
796{ 937{
938 ev_clear ((W)w);
939 if (ev_is_active (w))
940 return;
941
797 checks [w->active - 1] = checks [--checkcnt]; 942 checks [w->active - 1] = checks [--checkcnt];
798 ev_stop ((W)w); 943 ev_stop ((W)w);
944}
945
946void evchild_start (struct ev_child *w)
947{
948 if (ev_is_active (w))
949 return;
950
951 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953}
954
955void evchild_stop (struct ev_child *w)
956{
957 ev_clear ((W)w);
958 if (ev_is_active (w))
959 return;
960
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
962 ev_stop ((W)w);
963}
964
965/*****************************************************************************/
966
967struct ev_once
968{
969 struct ev_io io;
970 struct ev_timer to;
971 void (*cb)(int revents, void *arg);
972 void *arg;
973};
974
975static void
976once_cb (struct ev_once *once, int revents)
977{
978 void (*cb)(int revents, void *arg) = once->cb;
979 void *arg = once->arg;
980
981 evio_stop (&once->io);
982 evtimer_stop (&once->to);
983 free (once);
984
985 cb (revents, arg);
986}
987
988static void
989once_cb_io (struct ev_io *w, int revents)
990{
991 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
992}
993
994static void
995once_cb_to (struct ev_timer *w, int revents)
996{
997 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
998}
999
1000void
1001ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1002{
1003 struct ev_once *once = malloc (sizeof (struct ev_once));
1004
1005 if (!once)
1006 cb (EV_ERROR, arg);
1007 else
1008 {
1009 once->cb = cb;
1010 once->arg = arg;
1011
1012 evw_init (&once->io, once_cb_io);
1013
1014 if (fd >= 0)
1015 {
1016 evio_set (&once->io, fd, events);
1017 evio_start (&once->io);
1018 }
1019
1020 evw_init (&once->to, once_cb_to);
1021
1022 if (timeout >= 0.)
1023 {
1024 evtimer_set (&once->to, timeout, 0.);
1025 evtimer_start (&once->to);
1026 }
1027 }
799} 1028}
800 1029
801/*****************************************************************************/ 1030/*****************************************************************************/
802 1031
803#if 0 1032#if 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines