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

Comparing libev/ev.c (file contents):
Revision 1.29 by root, Thu Nov 1 08:10:03 2007 UTC vs.
Revision 1.37 by root, Thu Nov 1 13:33:12 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
45#include <sys/wait.h> 47#include <sys/wait.h>
46#include <sys/time.h> 48#include <sys/time.h>
47#include <time.h> 49#include <time.h>
48 50
49#ifndef EV_USE_MONOTONIC 51#ifndef EV_USE_MONOTONIC
50# ifdef CLOCK_MONOTONIC
51# define EV_USE_MONOTONIC 1 52# define EV_USE_MONOTONIC 1
52# endif 53#endif
54
55#ifndef CLOCK_MONOTONIC
56# undef EV_USE_MONOTONIC
57# define EV_USE_MONOTONIC 0
53#endif 58#endif
54 59
55#ifndef EV_USE_SELECT 60#ifndef EV_USE_SELECT
56# define EV_USE_SELECT 1 61# define EV_USE_SELECT 1
57#endif 62#endif
58 63
59#ifndef EV_USE_EPOLL 64#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 65# define EV_USE_EPOLL 0
61#endif 66#endif
62 67
68#ifndef CLOCK_REALTIME
69# define EV_USE_REALTIME 0
70#endif
63#ifndef EV_USE_REALTIME 71#ifndef EV_USE_REALTIME
64# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 72# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
65#endif 73#endif
66 74
67#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 75#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
68#define MAX_BLOCKTIME 59.731 76#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
69#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 77#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
78#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
70 79
71#include "ev.h" 80#include "ev.h"
72 81
73typedef struct ev_watcher *W; 82typedef struct ev_watcher *W;
74typedef struct ev_watcher_list *WL; 83typedef struct ev_watcher_list *WL;
113#endif 122#endif
114 123
115 return ev_time (); 124 return ev_time ();
116} 125}
117 126
118#define array_nextsize(n) (((n) << 1) | 4 & ~3) 127#define array_roundsize(base,n) ((n) | 4 & ~3)
119#define array_prevsize(n) (((n) >> 1) | 4 & ~3)
120 128
121#define array_needsize(base,cur,cnt,init) \ 129#define array_needsize(base,cur,cnt,init) \
122 if ((cnt) > cur) \ 130 if ((cnt) > cur) \
123 { \ 131 { \
124 int newcnt = cur; \ 132 int newcnt = cur; \
125 do \ 133 do \
126 { \ 134 { \
127 newcnt = array_nextsize (newcnt); \ 135 newcnt = array_roundsize (base, newcnt << 1); \
128 } \ 136 } \
129 while ((cnt) > newcnt); \ 137 while ((cnt) > newcnt); \
130 \ 138 \
131 base = realloc (base, sizeof (*base) * (newcnt)); \ 139 base = realloc (base, sizeof (*base) * (newcnt)); \
132 init (base + cur, newcnt - cur); \ 140 init (base + cur, newcnt - cur); \
136/*****************************************************************************/ 144/*****************************************************************************/
137 145
138typedef struct 146typedef struct
139{ 147{
140 struct ev_io *head; 148 struct ev_io *head;
141 int events; 149 unsigned char events;
150 unsigned char reify;
142} ANFD; 151} ANFD;
143 152
144static ANFD *anfds; 153static ANFD *anfds;
145static int anfdmax; 154static int anfdmax;
146 155
149{ 158{
150 while (count--) 159 while (count--)
151 { 160 {
152 base->head = 0; 161 base->head = 0;
153 base->events = EV_NONE; 162 base->events = EV_NONE;
163 base->reify = 0;
164
154 ++base; 165 ++base;
155 } 166 }
156} 167}
157 168
158typedef struct 169typedef struct
165static int pendingmax, pendingcnt; 176static int pendingmax, pendingcnt;
166 177
167static void 178static void
168event (W w, int events) 179event (W w, int events)
169{ 180{
170 if (w->active) 181 if (w->pending)
182 {
183 pendings [w->pending - 1].events |= events;
184 return;
171 { 185 }
186
172 w->pending = ++pendingcnt; 187 w->pending = ++pendingcnt;
173 array_needsize (pendings, pendingmax, pendingcnt, ); 188 array_needsize (pendings, pendingmax, pendingcnt, );
174 pendings [pendingcnt - 1].w = w; 189 pendings [pendingcnt - 1].w = w;
175 pendings [pendingcnt - 1].events = events; 190 pendings [pendingcnt - 1].events = events;
176 }
177} 191}
178 192
179static void 193static void
180queue_events (W *events, int eventcnt, int type) 194queue_events (W *events, int eventcnt, int type)
181{ 195{
219 int events = 0; 233 int events = 0;
220 234
221 for (w = anfd->head; w; w = w->next) 235 for (w = anfd->head; w; w = w->next)
222 events |= w->events; 236 events |= w->events;
223 237
224 anfd->events &= ~EV_REIFY; 238 anfd->reify = 0;
225 239
226 if (anfd->events != events) 240 if (anfd->events != events)
227 { 241 {
228 method_modify (fd, anfd->events, events); 242 method_modify (fd, anfd->events, events);
229 anfd->events = events; 243 anfd->events = events;
234} 248}
235 249
236static void 250static void
237fd_change (int fd) 251fd_change (int fd)
238{ 252{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 253 if (anfds [fd].reify || fdchangecnt < 0)
240 return; 254 return;
241 255
242 anfds [fd].events |= EV_REIFY; 256 anfds [fd].reify = 1;
243 257
244 ++fdchangecnt; 258 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 259 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd; 260 fdchanges [fdchangecnt - 1] = fd;
247} 261}
255 for (fd = 0; fd < anfdmax; ++fd) 269 for (fd = 0; fd < anfdmax; ++fd)
256 if (anfds [fd].events) 270 if (anfds [fd].events)
257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 271 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
258 while (anfds [fd].head) 272 while (anfds [fd].head)
259 { 273 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
261 ev_io_stop (anfds [fd].head); 274 ev_io_stop (anfds [fd].head);
275 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
262 } 276 }
263} 277}
264 278
265/*****************************************************************************/ 279/*****************************************************************************/
266 280
314/*****************************************************************************/ 328/*****************************************************************************/
315 329
316typedef struct 330typedef struct
317{ 331{
318 struct ev_signal *head; 332 struct ev_signal *head;
319 sig_atomic_t gotsig; 333 sig_atomic_t volatile gotsig;
320} ANSIG; 334} ANSIG;
321 335
322static ANSIG *signals; 336static ANSIG *signals;
323static int signalmax; 337static int signalmax;
324 338
325static int sigpipe [2]; 339static int sigpipe [2];
326static sig_atomic_t gotsig; 340static sig_atomic_t volatile gotsig;
327static struct ev_io sigev; 341static struct ev_io sigev;
328 342
329static void 343static void
330signals_init (ANSIG *base, int count) 344signals_init (ANSIG *base, int count)
331{ 345{
332 while (count--) 346 while (count--)
333 { 347 {
334 base->head = 0; 348 base->head = 0;
335 base->gotsig = 0; 349 base->gotsig = 0;
350
336 ++base; 351 ++base;
337 } 352 }
338} 353}
339 354
340static void 355static void
343 signals [signum - 1].gotsig = 1; 358 signals [signum - 1].gotsig = 1;
344 359
345 if (!gotsig) 360 if (!gotsig)
346 { 361 {
347 gotsig = 1; 362 gotsig = 1;
348 write (sigpipe [1], &gotsig, 1); 363 write (sigpipe [1], &signum, 1);
349 } 364 }
350} 365}
351 366
352static void 367static void
353sigcb (struct ev_io *iow, int revents) 368sigcb (struct ev_io *iow, int revents)
354{ 369{
355 struct ev_signal *w; 370 struct ev_signal *w;
356 int sig; 371 int sig;
357 372
373 read (sigpipe [0], &revents, 1);
358 gotsig = 0; 374 gotsig = 0;
359 read (sigpipe [0], &revents, 1);
360 375
361 for (sig = signalmax; sig--; ) 376 for (sig = signalmax; sig--; )
362 if (signals [sig].gotsig) 377 if (signals [sig].gotsig)
363 { 378 {
364 signals [sig].gotsig = 0; 379 signals [sig].gotsig = 0;
479} 494}
480 495
481/*****************************************************************************/ 496/*****************************************************************************/
482 497
483void 498void
484ev_prefork (void) 499ev_fork_prepare (void)
485{ 500{
486 /* nop */ 501 /* nop */
487} 502}
488 503
489void 504void
490ev_postfork_parent (void) 505ev_fork_parent (void)
491{ 506{
492 /* nop */ 507 /* nop */
493} 508}
494 509
495void 510void
496ev_postfork_child (void) 511ev_fork_child (void)
497{ 512{
498#if EV_USE_EPOLL 513#if EV_USE_EPOLL
499 if (ev_method == EVMETHOD_EPOLL) 514 if (ev_method == EVMETHOD_EPOLL)
500 epoll_postfork_child (); 515 epoll_postfork_child ();
501#endif 516#endif
529{ 544{
530 while (timercnt && timers [0]->at <= now) 545 while (timercnt && timers [0]->at <= now)
531 { 546 {
532 struct ev_timer *w = timers [0]; 547 struct ev_timer *w = timers [0];
533 548
534 event ((W)w, EV_TIMEOUT);
535
536 /* first reschedule or stop timer */ 549 /* first reschedule or stop timer */
537 if (w->repeat) 550 if (w->repeat)
538 { 551 {
552 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
539 w->at = now + w->repeat; 553 w->at = now + w->repeat;
540 assert (("timer timeout in the past, negative repeat?", w->at > now));
541 downheap ((WT *)timers, timercnt, 0); 554 downheap ((WT *)timers, timercnt, 0);
542 } 555 }
543 else 556 else
544 ev_timer_stop (w); /* nonrepeating: stop timer */ 557 ev_timer_stop (w); /* nonrepeating: stop timer */
558
559 event ((W)w, EV_TIMEOUT);
545 } 560 }
546} 561}
547 562
548static void 563static void
549periodics_reify (void) 564periodics_reify (void)
554 569
555 /* first reschedule or stop timer */ 570 /* first reschedule or stop timer */
556 if (w->interval) 571 if (w->interval)
557 { 572 {
558 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 573 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
559 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 574 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
560 downheap ((WT *)periodics, periodiccnt, 0); 575 downheap ((WT *)periodics, periodiccnt, 0);
561 } 576 }
562 else 577 else
563 ev_periodic_stop (w); /* nonrepeating: stop timer */ 578 ev_periodic_stop (w); /* nonrepeating: stop timer */
564 579
565 event ((W)w, EV_TIMEOUT); 580 event ((W)w, EV_PERIODIC);
566 } 581 }
567} 582}
568 583
569static void 584static void
570periodics_reschedule (ev_tstamp diff) 585periodics_reschedule (ev_tstamp diff)
725 head = &(*head)->next; 740 head = &(*head)->next;
726 } 741 }
727} 742}
728 743
729static void 744static void
730ev_clear (W w) 745ev_clear_pending (W w)
731{ 746{
732 if (w->pending) 747 if (w->pending)
733 { 748 {
734 pendings [w->pending - 1].w = 0; 749 pendings [w->pending - 1].w = 0;
735 w->pending = 0; 750 w->pending = 0;
751/*****************************************************************************/ 766/*****************************************************************************/
752 767
753void 768void
754ev_io_start (struct ev_io *w) 769ev_io_start (struct ev_io *w)
755{ 770{
756 if (ev_is_active (w))
757 return;
758
759 int fd = w->fd; 771 int fd = w->fd;
772
773 if (ev_is_active (w))
774 return;
775
776 assert (("ev_io_start called with negative fd", fd >= 0));
760 777
761 ev_start ((W)w, 1); 778 ev_start ((W)w, 1);
762 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 779 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
763 wlist_add ((WL *)&anfds[fd].head, (WL)w); 780 wlist_add ((WL *)&anfds[fd].head, (WL)w);
764 781
766} 783}
767 784
768void 785void
769ev_io_stop (struct ev_io *w) 786ev_io_stop (struct ev_io *w)
770{ 787{
771 ev_clear ((W)w); 788 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 789 if (!ev_is_active (w))
773 return; 790 return;
774 791
775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 792 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
776 ev_stop ((W)w); 793 ev_stop ((W)w);
784 if (ev_is_active (w)) 801 if (ev_is_active (w))
785 return; 802 return;
786 803
787 w->at += now; 804 w->at += now;
788 805
789 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 806 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
790 807
791 ev_start ((W)w, ++timercnt); 808 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, ); 809 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w; 810 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1); 811 upheap ((WT *)timers, timercnt - 1);
795} 812}
796 813
797void 814void
798ev_timer_stop (struct ev_timer *w) 815ev_timer_stop (struct ev_timer *w)
799{ 816{
800 ev_clear ((W)w); 817 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 818 if (!ev_is_active (w))
802 return; 819 return;
803 820
804 if (w->active < timercnt--) 821 if (w->active < timercnt--)
805 { 822 {
833ev_periodic_start (struct ev_periodic *w) 850ev_periodic_start (struct ev_periodic *w)
834{ 851{
835 if (ev_is_active (w)) 852 if (ev_is_active (w))
836 return; 853 return;
837 854
838 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 855 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
839 856
840 /* this formula differs from the one in periodic_reify because we do not always round up */ 857 /* this formula differs from the one in periodic_reify because we do not always round up */
841 if (w->interval) 858 if (w->interval)
842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 859 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
843 860
848} 865}
849 866
850void 867void
851ev_periodic_stop (struct ev_periodic *w) 868ev_periodic_stop (struct ev_periodic *w)
852{ 869{
853 ev_clear ((W)w); 870 ev_clear_pending ((W)w);
854 if (!ev_is_active (w)) 871 if (!ev_is_active (w))
855 return; 872 return;
856 873
857 if (w->active < periodiccnt--) 874 if (w->active < periodiccnt--)
858 { 875 {
866void 883void
867ev_signal_start (struct ev_signal *w) 884ev_signal_start (struct ev_signal *w)
868{ 885{
869 if (ev_is_active (w)) 886 if (ev_is_active (w))
870 return; 887 return;
888
889 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
871 890
872 ev_start ((W)w, 1); 891 ev_start ((W)w, 1);
873 array_needsize (signals, signalmax, w->signum, signals_init); 892 array_needsize (signals, signalmax, w->signum, signals_init);
874 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 893 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
875 894
884} 903}
885 904
886void 905void
887ev_signal_stop (struct ev_signal *w) 906ev_signal_stop (struct ev_signal *w)
888{ 907{
889 ev_clear ((W)w); 908 ev_clear_pending ((W)w);
890 if (!ev_is_active (w)) 909 if (!ev_is_active (w))
891 return; 910 return;
892 911
893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 912 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
894 ev_stop ((W)w); 913 ev_stop ((W)w);
909} 928}
910 929
911void 930void
912ev_idle_stop (struct ev_idle *w) 931ev_idle_stop (struct ev_idle *w)
913{ 932{
914 ev_clear ((W)w); 933 ev_clear_pending ((W)w);
915 if (ev_is_active (w)) 934 if (ev_is_active (w))
916 return; 935 return;
917 936
918 idles [w->active - 1] = idles [--idlecnt]; 937 idles [w->active - 1] = idles [--idlecnt];
919 ev_stop ((W)w); 938 ev_stop ((W)w);
931} 950}
932 951
933void 952void
934ev_prepare_stop (struct ev_prepare *w) 953ev_prepare_stop (struct ev_prepare *w)
935{ 954{
936 ev_clear ((W)w); 955 ev_clear_pending ((W)w);
937 if (ev_is_active (w)) 956 if (ev_is_active (w))
938 return; 957 return;
939 958
940 prepares [w->active - 1] = prepares [--preparecnt]; 959 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w); 960 ev_stop ((W)w);
953} 972}
954 973
955void 974void
956ev_check_stop (struct ev_check *w) 975ev_check_stop (struct ev_check *w)
957{ 976{
958 ev_clear ((W)w); 977 ev_clear_pending ((W)w);
959 if (ev_is_active (w)) 978 if (ev_is_active (w))
960 return; 979 return;
961 980
962 checks [w->active - 1] = checks [--checkcnt]; 981 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w); 982 ev_stop ((W)w);
974} 993}
975 994
976void 995void
977ev_child_stop (struct ev_child *w) 996ev_child_stop (struct ev_child *w)
978{ 997{
979 ev_clear ((W)w); 998 ev_clear_pending ((W)w);
980 if (ev_is_active (w)) 999 if (ev_is_active (w))
981 return; 1000 return;
982 1001
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1002 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w); 1003 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines