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

Comparing libev/ev.c (file contents):
Revision 1.30 by root, Thu Nov 1 08:28:33 2007 UTC vs.
Revision 1.36 by root, Thu Nov 1 13:11:11 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
58 60
59#ifndef EV_USE_EPOLL 61#ifndef EV_USE_EPOLL
60# define EV_USE_EPOLL 0 62# define EV_USE_EPOLL 0
61#endif 63#endif
62 64
65#ifndef CLOCK_REALTIME
66# define EV_USE_REALTIME 0
67#endif
63#ifndef EV_USE_REALTIME 68#ifndef EV_USE_REALTIME
64# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ 69# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
65#endif 70#endif
66 71
67#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 72#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
68#define MAX_BLOCKTIME 59.731 73#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 */ 74#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
75#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
70 76
71#include "ev.h" 77#include "ev.h"
72 78
73typedef struct ev_watcher *W; 79typedef struct ev_watcher *W;
74typedef struct ev_watcher_list *WL; 80typedef struct ev_watcher_list *WL;
135/*****************************************************************************/ 141/*****************************************************************************/
136 142
137typedef struct 143typedef struct
138{ 144{
139 struct ev_io *head; 145 struct ev_io *head;
140 int events; 146 unsigned char events;
147 unsigned char reify;
141} ANFD; 148} ANFD;
142 149
143static ANFD *anfds; 150static ANFD *anfds;
144static int anfdmax; 151static int anfdmax;
145 152
148{ 155{
149 while (count--) 156 while (count--)
150 { 157 {
151 base->head = 0; 158 base->head = 0;
152 base->events = EV_NONE; 159 base->events = EV_NONE;
160 base->reify = 0;
161
153 ++base; 162 ++base;
154 } 163 }
155} 164}
156 165
157typedef struct 166typedef struct
164static int pendingmax, pendingcnt; 173static int pendingmax, pendingcnt;
165 174
166static void 175static void
167event (W w, int events) 176event (W w, int events)
168{ 177{
178 if (w->pending)
179 {
180 pendings [w->pending - 1].events |= events;
181 return;
182 }
183
169 w->pending = ++pendingcnt; 184 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, ); 185 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w; 186 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 187 pendings [pendingcnt - 1].events = events;
173} 188}
215 int events = 0; 230 int events = 0;
216 231
217 for (w = anfd->head; w; w = w->next) 232 for (w = anfd->head; w; w = w->next)
218 events |= w->events; 233 events |= w->events;
219 234
220 anfd->events &= ~EV_REIFY; 235 anfd->reify = 0;
221 236
222 if (anfd->events != events) 237 if (anfd->events != events)
223 { 238 {
224 method_modify (fd, anfd->events, events); 239 method_modify (fd, anfd->events, events);
225 anfd->events = events; 240 anfd->events = events;
230} 245}
231 246
232static void 247static void
233fd_change (int fd) 248fd_change (int fd)
234{ 249{
235 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 250 if (anfds [fd].reify || fdchangecnt < 0)
236 return; 251 return;
237 252
238 anfds [fd].events |= EV_REIFY; 253 anfds [fd].reify = 1;
239 254
240 ++fdchangecnt; 255 ++fdchangecnt;
241 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 256 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
242 fdchanges [fdchangecnt - 1] = fd; 257 fdchanges [fdchangecnt - 1] = fd;
243} 258}
251 for (fd = 0; fd < anfdmax; ++fd) 266 for (fd = 0; fd < anfdmax; ++fd)
252 if (anfds [fd].events) 267 if (anfds [fd].events)
253 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
254 while (anfds [fd].head) 269 while (anfds [fd].head)
255 { 270 {
256 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
257 ev_io_stop (anfds [fd].head); 271 ev_io_stop (anfds [fd].head);
272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
258 } 273 }
259} 274}
260 275
261/*****************************************************************************/ 276/*****************************************************************************/
262 277
310/*****************************************************************************/ 325/*****************************************************************************/
311 326
312typedef struct 327typedef struct
313{ 328{
314 struct ev_signal *head; 329 struct ev_signal *head;
315 sig_atomic_t gotsig; 330 sig_atomic_t volatile gotsig;
316} ANSIG; 331} ANSIG;
317 332
318static ANSIG *signals; 333static ANSIG *signals;
319static int signalmax; 334static int signalmax;
320 335
321static int sigpipe [2]; 336static int sigpipe [2];
322static sig_atomic_t gotsig; 337static sig_atomic_t volatile gotsig;
323static struct ev_io sigev; 338static struct ev_io sigev;
324 339
325static void 340static void
326signals_init (ANSIG *base, int count) 341signals_init (ANSIG *base, int count)
327{ 342{
328 while (count--) 343 while (count--)
329 { 344 {
330 base->head = 0; 345 base->head = 0;
331 base->gotsig = 0; 346 base->gotsig = 0;
347
332 ++base; 348 ++base;
333 } 349 }
334} 350}
335 351
336static void 352static void
339 signals [signum - 1].gotsig = 1; 355 signals [signum - 1].gotsig = 1;
340 356
341 if (!gotsig) 357 if (!gotsig)
342 { 358 {
343 gotsig = 1; 359 gotsig = 1;
344 write (sigpipe [1], &gotsig, 1); 360 write (sigpipe [1], &signum, 1);
345 } 361 }
346} 362}
347 363
348static void 364static void
349sigcb (struct ev_io *iow, int revents) 365sigcb (struct ev_io *iow, int revents)
350{ 366{
351 struct ev_signal *w; 367 struct ev_signal *w;
352 int sig; 368 int sig;
353 369
370 read (sigpipe [0], &revents, 1);
354 gotsig = 0; 371 gotsig = 0;
355 read (sigpipe [0], &revents, 1);
356 372
357 for (sig = signalmax; sig--; ) 373 for (sig = signalmax; sig--; )
358 if (signals [sig].gotsig) 374 if (signals [sig].gotsig)
359 { 375 {
360 signals [sig].gotsig = 0; 376 signals [sig].gotsig = 0;
475} 491}
476 492
477/*****************************************************************************/ 493/*****************************************************************************/
478 494
479void 495void
480ev_prefork (void) 496ev_fork_prepare (void)
481{ 497{
482 /* nop */ 498 /* nop */
483} 499}
484 500
485void 501void
486ev_postfork_parent (void) 502ev_fork_parent (void)
487{ 503{
488 /* nop */ 504 /* nop */
489} 505}
490 506
491void 507void
492ev_postfork_child (void) 508ev_fork_child (void)
493{ 509{
494#if EV_USE_EPOLL 510#if EV_USE_EPOLL
495 if (ev_method == EVMETHOD_EPOLL) 511 if (ev_method == EVMETHOD_EPOLL)
496 epoll_postfork_child (); 512 epoll_postfork_child ();
497#endif 513#endif
528 struct ev_timer *w = timers [0]; 544 struct ev_timer *w = timers [0];
529 545
530 /* first reschedule or stop timer */ 546 /* first reschedule or stop timer */
531 if (w->repeat) 547 if (w->repeat)
532 { 548 {
549 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 550 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 551 downheap ((WT *)timers, timercnt, 0);
536 } 552 }
537 else 553 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 554 ev_timer_stop (w); /* nonrepeating: stop timer */
539 555
550 566
551 /* first reschedule or stop timer */ 567 /* first reschedule or stop timer */
552 if (w->interval) 568 if (w->interval)
553 { 569 {
554 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 570 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
555 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 571 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
556 downheap ((WT *)periodics, periodiccnt, 0); 572 downheap ((WT *)periodics, periodiccnt, 0);
557 } 573 }
558 else 574 else
559 ev_periodic_stop (w); /* nonrepeating: stop timer */ 575 ev_periodic_stop (w); /* nonrepeating: stop timer */
560 576
561 event ((W)w, EV_TIMEOUT); 577 event ((W)w, EV_PERIODIC);
562 } 578 }
563} 579}
564 580
565static void 581static void
566periodics_reschedule (ev_tstamp diff) 582periodics_reschedule (ev_tstamp diff)
721 head = &(*head)->next; 737 head = &(*head)->next;
722 } 738 }
723} 739}
724 740
725static void 741static void
726ev_clear (W w) 742ev_clear_pending (W w)
727{ 743{
728 if (w->pending) 744 if (w->pending)
729 { 745 {
730 pendings [w->pending - 1].w = 0; 746 pendings [w->pending - 1].w = 0;
731 w->pending = 0; 747 w->pending = 0;
751{ 767{
752 if (ev_is_active (w)) 768 if (ev_is_active (w))
753 return; 769 return;
754 770
755 int fd = w->fd; 771 int fd = w->fd;
772
773 assert (("ev_io_start called with negative fd", fd >= 0));
756 774
757 ev_start ((W)w, 1); 775 ev_start ((W)w, 1);
758 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 776 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
759 wlist_add ((WL *)&anfds[fd].head, (WL)w); 777 wlist_add ((WL *)&anfds[fd].head, (WL)w);
760 778
762} 780}
763 781
764void 782void
765ev_io_stop (struct ev_io *w) 783ev_io_stop (struct ev_io *w)
766{ 784{
767 ev_clear ((W)w); 785 ev_clear_pending ((W)w);
768 if (!ev_is_active (w)) 786 if (!ev_is_active (w))
769 return; 787 return;
770 788
771 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 789 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
772 ev_stop ((W)w); 790 ev_stop ((W)w);
780 if (ev_is_active (w)) 798 if (ev_is_active (w))
781 return; 799 return;
782 800
783 w->at += now; 801 w->at += now;
784 802
785 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 803 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
786 804
787 ev_start ((W)w, ++timercnt); 805 ev_start ((W)w, ++timercnt);
788 array_needsize (timers, timermax, timercnt, ); 806 array_needsize (timers, timermax, timercnt, );
789 timers [timercnt - 1] = w; 807 timers [timercnt - 1] = w;
790 upheap ((WT *)timers, timercnt - 1); 808 upheap ((WT *)timers, timercnt - 1);
791} 809}
792 810
793void 811void
794ev_timer_stop (struct ev_timer *w) 812ev_timer_stop (struct ev_timer *w)
795{ 813{
796 ev_clear ((W)w); 814 ev_clear_pending ((W)w);
797 if (!ev_is_active (w)) 815 if (!ev_is_active (w))
798 return; 816 return;
799 817
800 if (w->active < timercnt--) 818 if (w->active < timercnt--)
801 { 819 {
829ev_periodic_start (struct ev_periodic *w) 847ev_periodic_start (struct ev_periodic *w)
830{ 848{
831 if (ev_is_active (w)) 849 if (ev_is_active (w))
832 return; 850 return;
833 851
834 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 852 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
835 853
836 /* this formula differs from the one in periodic_reify because we do not always round up */ 854 /* this formula differs from the one in periodic_reify because we do not always round up */
837 if (w->interval) 855 if (w->interval)
838 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 856 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
839 857
844} 862}
845 863
846void 864void
847ev_periodic_stop (struct ev_periodic *w) 865ev_periodic_stop (struct ev_periodic *w)
848{ 866{
849 ev_clear ((W)w); 867 ev_clear_pending ((W)w);
850 if (!ev_is_active (w)) 868 if (!ev_is_active (w))
851 return; 869 return;
852 870
853 if (w->active < periodiccnt--) 871 if (w->active < periodiccnt--)
854 { 872 {
862void 880void
863ev_signal_start (struct ev_signal *w) 881ev_signal_start (struct ev_signal *w)
864{ 882{
865 if (ev_is_active (w)) 883 if (ev_is_active (w))
866 return; 884 return;
885
886 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
867 887
868 ev_start ((W)w, 1); 888 ev_start ((W)w, 1);
869 array_needsize (signals, signalmax, w->signum, signals_init); 889 array_needsize (signals, signalmax, w->signum, signals_init);
870 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 890 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
871 891
880} 900}
881 901
882void 902void
883ev_signal_stop (struct ev_signal *w) 903ev_signal_stop (struct ev_signal *w)
884{ 904{
885 ev_clear ((W)w); 905 ev_clear_pending ((W)w);
886 if (!ev_is_active (w)) 906 if (!ev_is_active (w))
887 return; 907 return;
888 908
889 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 909 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
890 ev_stop ((W)w); 910 ev_stop ((W)w);
905} 925}
906 926
907void 927void
908ev_idle_stop (struct ev_idle *w) 928ev_idle_stop (struct ev_idle *w)
909{ 929{
910 ev_clear ((W)w); 930 ev_clear_pending ((W)w);
911 if (ev_is_active (w)) 931 if (ev_is_active (w))
912 return; 932 return;
913 933
914 idles [w->active - 1] = idles [--idlecnt]; 934 idles [w->active - 1] = idles [--idlecnt];
915 ev_stop ((W)w); 935 ev_stop ((W)w);
927} 947}
928 948
929void 949void
930ev_prepare_stop (struct ev_prepare *w) 950ev_prepare_stop (struct ev_prepare *w)
931{ 951{
932 ev_clear ((W)w); 952 ev_clear_pending ((W)w);
933 if (ev_is_active (w)) 953 if (ev_is_active (w))
934 return; 954 return;
935 955
936 prepares [w->active - 1] = prepares [--preparecnt]; 956 prepares [w->active - 1] = prepares [--preparecnt];
937 ev_stop ((W)w); 957 ev_stop ((W)w);
949} 969}
950 970
951void 971void
952ev_check_stop (struct ev_check *w) 972ev_check_stop (struct ev_check *w)
953{ 973{
954 ev_clear ((W)w); 974 ev_clear_pending ((W)w);
955 if (ev_is_active (w)) 975 if (ev_is_active (w))
956 return; 976 return;
957 977
958 checks [w->active - 1] = checks [--checkcnt]; 978 checks [w->active - 1] = checks [--checkcnt];
959 ev_stop ((W)w); 979 ev_stop ((W)w);
970} 990}
971 991
972void 992void
973ev_child_stop (struct ev_child *w) 993ev_child_stop (struct ev_child *w)
974{ 994{
975 ev_clear ((W)w); 995 ev_clear_pending ((W)w);
976 if (ev_is_active (w)) 996 if (ev_is_active (w))
977 return; 997 return;
978 998
979 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 999 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
980 ev_stop ((W)w); 1000 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines