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.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;
113#endif 119#endif
114 120
115 return ev_time (); 121 return ev_time ();
116} 122}
117 123
118#define array_nextsize(n) (((n) << 1) | 4 & ~3) 124#define array_roundsize(base,n) ((n) | 4 & ~3)
119#define array_prevsize(n) (((n) >> 1) | 4 & ~3)
120 125
121#define array_needsize(base,cur,cnt,init) \ 126#define array_needsize(base,cur,cnt,init) \
122 if ((cnt) > cur) \ 127 if ((cnt) > cur) \
123 { \ 128 { \
124 int newcnt = cur; \ 129 int newcnt = cur; \
125 do \ 130 do \
126 { \ 131 { \
127 newcnt = array_nextsize (newcnt); \ 132 newcnt = array_roundsize (base, newcnt << 1); \
128 } \ 133 } \
129 while ((cnt) > newcnt); \ 134 while ((cnt) > newcnt); \
130 \ 135 \
131 base = realloc (base, sizeof (*base) * (newcnt)); \ 136 base = realloc (base, sizeof (*base) * (newcnt)); \
132 init (base + cur, newcnt - cur); \ 137 init (base + cur, newcnt - cur); \
136/*****************************************************************************/ 141/*****************************************************************************/
137 142
138typedef struct 143typedef struct
139{ 144{
140 struct ev_io *head; 145 struct ev_io *head;
141 int events; 146 unsigned char events;
147 unsigned char reify;
142} ANFD; 148} ANFD;
143 149
144static ANFD *anfds; 150static ANFD *anfds;
145static int anfdmax; 151static int anfdmax;
146 152
149{ 155{
150 while (count--) 156 while (count--)
151 { 157 {
152 base->head = 0; 158 base->head = 0;
153 base->events = EV_NONE; 159 base->events = EV_NONE;
160 base->reify = 0;
161
154 ++base; 162 ++base;
155 } 163 }
156} 164}
157 165
158typedef struct 166typedef struct
165static int pendingmax, pendingcnt; 173static int pendingmax, pendingcnt;
166 174
167static void 175static void
168event (W w, int events) 176event (W w, int events)
169{ 177{
170 if (w->active) 178 if (w->pending)
179 {
180 pendings [w->pending - 1].events |= events;
181 return;
171 { 182 }
183
172 w->pending = ++pendingcnt; 184 w->pending = ++pendingcnt;
173 array_needsize (pendings, pendingmax, pendingcnt, ); 185 array_needsize (pendings, pendingmax, pendingcnt, );
174 pendings [pendingcnt - 1].w = w; 186 pendings [pendingcnt - 1].w = w;
175 pendings [pendingcnt - 1].events = events; 187 pendings [pendingcnt - 1].events = events;
176 }
177} 188}
178 189
179static void 190static void
180queue_events (W *events, int eventcnt, int type) 191queue_events (W *events, int eventcnt, int type)
181{ 192{
219 int events = 0; 230 int events = 0;
220 231
221 for (w = anfd->head; w; w = w->next) 232 for (w = anfd->head; w; w = w->next)
222 events |= w->events; 233 events |= w->events;
223 234
224 anfd->events &= ~EV_REIFY; 235 anfd->reify = 0;
225 236
226 if (anfd->events != events) 237 if (anfd->events != events)
227 { 238 {
228 method_modify (fd, anfd->events, events); 239 method_modify (fd, anfd->events, events);
229 anfd->events = events; 240 anfd->events = events;
234} 245}
235 246
236static void 247static void
237fd_change (int fd) 248fd_change (int fd)
238{ 249{
239 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 250 if (anfds [fd].reify || fdchangecnt < 0)
240 return; 251 return;
241 252
242 anfds [fd].events |= EV_REIFY; 253 anfds [fd].reify = 1;
243 254
244 ++fdchangecnt; 255 ++fdchangecnt;
245 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 256 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
246 fdchanges [fdchangecnt - 1] = fd; 257 fdchanges [fdchangecnt - 1] = fd;
247} 258}
255 for (fd = 0; fd < anfdmax; ++fd) 266 for (fd = 0; fd < anfdmax; ++fd)
256 if (anfds [fd].events) 267 if (anfds [fd].events)
257 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
258 while (anfds [fd].head) 269 while (anfds [fd].head)
259 { 270 {
260 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
261 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);
262 } 273 }
263} 274}
264 275
265/*****************************************************************************/ 276/*****************************************************************************/
266 277
314/*****************************************************************************/ 325/*****************************************************************************/
315 326
316typedef struct 327typedef struct
317{ 328{
318 struct ev_signal *head; 329 struct ev_signal *head;
319 sig_atomic_t gotsig; 330 sig_atomic_t volatile gotsig;
320} ANSIG; 331} ANSIG;
321 332
322static ANSIG *signals; 333static ANSIG *signals;
323static int signalmax; 334static int signalmax;
324 335
325static int sigpipe [2]; 336static int sigpipe [2];
326static sig_atomic_t gotsig; 337static sig_atomic_t volatile gotsig;
327static struct ev_io sigev; 338static struct ev_io sigev;
328 339
329static void 340static void
330signals_init (ANSIG *base, int count) 341signals_init (ANSIG *base, int count)
331{ 342{
332 while (count--) 343 while (count--)
333 { 344 {
334 base->head = 0; 345 base->head = 0;
335 base->gotsig = 0; 346 base->gotsig = 0;
347
336 ++base; 348 ++base;
337 } 349 }
338} 350}
339 351
340static void 352static void
343 signals [signum - 1].gotsig = 1; 355 signals [signum - 1].gotsig = 1;
344 356
345 if (!gotsig) 357 if (!gotsig)
346 { 358 {
347 gotsig = 1; 359 gotsig = 1;
348 write (sigpipe [1], &gotsig, 1); 360 write (sigpipe [1], &signum, 1);
349 } 361 }
350} 362}
351 363
352static void 364static void
353sigcb (struct ev_io *iow, int revents) 365sigcb (struct ev_io *iow, int revents)
354{ 366{
355 struct ev_signal *w; 367 struct ev_signal *w;
356 int sig; 368 int sig;
357 369
370 read (sigpipe [0], &revents, 1);
358 gotsig = 0; 371 gotsig = 0;
359 read (sigpipe [0], &revents, 1);
360 372
361 for (sig = signalmax; sig--; ) 373 for (sig = signalmax; sig--; )
362 if (signals [sig].gotsig) 374 if (signals [sig].gotsig)
363 { 375 {
364 signals [sig].gotsig = 0; 376 signals [sig].gotsig = 0;
479} 491}
480 492
481/*****************************************************************************/ 493/*****************************************************************************/
482 494
483void 495void
484ev_prefork (void) 496ev_fork_prepare (void)
485{ 497{
486 /* nop */ 498 /* nop */
487} 499}
488 500
489void 501void
490ev_postfork_parent (void) 502ev_fork_parent (void)
491{ 503{
492 /* nop */ 504 /* nop */
493} 505}
494 506
495void 507void
496ev_postfork_child (void) 508ev_fork_child (void)
497{ 509{
498#if EV_USE_EPOLL 510#if EV_USE_EPOLL
499 if (ev_method == EVMETHOD_EPOLL) 511 if (ev_method == EVMETHOD_EPOLL)
500 epoll_postfork_child (); 512 epoll_postfork_child ();
501#endif 513#endif
529{ 541{
530 while (timercnt && timers [0]->at <= now) 542 while (timercnt && timers [0]->at <= now)
531 { 543 {
532 struct ev_timer *w = timers [0]; 544 struct ev_timer *w = timers [0];
533 545
534 event ((W)w, EV_TIMEOUT);
535
536 /* first reschedule or stop timer */ 546 /* first reschedule or stop timer */
537 if (w->repeat) 547 if (w->repeat)
538 { 548 {
549 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
539 w->at = now + w->repeat; 550 w->at = now + w->repeat;
540 assert (("timer timeout in the past, negative repeat?", w->at > now));
541 downheap ((WT *)timers, timercnt, 0); 551 downheap ((WT *)timers, timercnt, 0);
542 } 552 }
543 else 553 else
544 ev_timer_stop (w); /* nonrepeating: stop timer */ 554 ev_timer_stop (w); /* nonrepeating: stop timer */
555
556 event ((W)w, EV_TIMEOUT);
545 } 557 }
546} 558}
547 559
548static void 560static void
549periodics_reify (void) 561periodics_reify (void)
554 566
555 /* first reschedule or stop timer */ 567 /* first reschedule or stop timer */
556 if (w->interval) 568 if (w->interval)
557 { 569 {
558 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;
559 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));
560 downheap ((WT *)periodics, periodiccnt, 0); 572 downheap ((WT *)periodics, periodiccnt, 0);
561 } 573 }
562 else 574 else
563 ev_periodic_stop (w); /* nonrepeating: stop timer */ 575 ev_periodic_stop (w); /* nonrepeating: stop timer */
564 576
565 event ((W)w, EV_TIMEOUT); 577 event ((W)w, EV_PERIODIC);
566 } 578 }
567} 579}
568 580
569static void 581static void
570periodics_reschedule (ev_tstamp diff) 582periodics_reschedule (ev_tstamp diff)
725 head = &(*head)->next; 737 head = &(*head)->next;
726 } 738 }
727} 739}
728 740
729static void 741static void
730ev_clear (W w) 742ev_clear_pending (W w)
731{ 743{
732 if (w->pending) 744 if (w->pending)
733 { 745 {
734 pendings [w->pending - 1].w = 0; 746 pendings [w->pending - 1].w = 0;
735 w->pending = 0; 747 w->pending = 0;
755{ 767{
756 if (ev_is_active (w)) 768 if (ev_is_active (w))
757 return; 769 return;
758 770
759 int fd = w->fd; 771 int fd = w->fd;
772
773 assert (("ev_io_start called with negative fd", fd >= 0));
760 774
761 ev_start ((W)w, 1); 775 ev_start ((W)w, 1);
762 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 776 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
763 wlist_add ((WL *)&anfds[fd].head, (WL)w); 777 wlist_add ((WL *)&anfds[fd].head, (WL)w);
764 778
766} 780}
767 781
768void 782void
769ev_io_stop (struct ev_io *w) 783ev_io_stop (struct ev_io *w)
770{ 784{
771 ev_clear ((W)w); 785 ev_clear_pending ((W)w);
772 if (!ev_is_active (w)) 786 if (!ev_is_active (w))
773 return; 787 return;
774 788
775 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 789 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
776 ev_stop ((W)w); 790 ev_stop ((W)w);
784 if (ev_is_active (w)) 798 if (ev_is_active (w))
785 return; 799 return;
786 800
787 w->at += now; 801 w->at += now;
788 802
789 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.));
790 804
791 ev_start ((W)w, ++timercnt); 805 ev_start ((W)w, ++timercnt);
792 array_needsize (timers, timermax, timercnt, ); 806 array_needsize (timers, timermax, timercnt, );
793 timers [timercnt - 1] = w; 807 timers [timercnt - 1] = w;
794 upheap ((WT *)timers, timercnt - 1); 808 upheap ((WT *)timers, timercnt - 1);
795} 809}
796 810
797void 811void
798ev_timer_stop (struct ev_timer *w) 812ev_timer_stop (struct ev_timer *w)
799{ 813{
800 ev_clear ((W)w); 814 ev_clear_pending ((W)w);
801 if (!ev_is_active (w)) 815 if (!ev_is_active (w))
802 return; 816 return;
803 817
804 if (w->active < timercnt--) 818 if (w->active < timercnt--)
805 { 819 {
833ev_periodic_start (struct ev_periodic *w) 847ev_periodic_start (struct ev_periodic *w)
834{ 848{
835 if (ev_is_active (w)) 849 if (ev_is_active (w))
836 return; 850 return;
837 851
838 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.));
839 853
840 /* 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 */
841 if (w->interval) 855 if (w->interval)
842 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 856 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
843 857
848} 862}
849 863
850void 864void
851ev_periodic_stop (struct ev_periodic *w) 865ev_periodic_stop (struct ev_periodic *w)
852{ 866{
853 ev_clear ((W)w); 867 ev_clear_pending ((W)w);
854 if (!ev_is_active (w)) 868 if (!ev_is_active (w))
855 return; 869 return;
856 870
857 if (w->active < periodiccnt--) 871 if (w->active < periodiccnt--)
858 { 872 {
866void 880void
867ev_signal_start (struct ev_signal *w) 881ev_signal_start (struct ev_signal *w)
868{ 882{
869 if (ev_is_active (w)) 883 if (ev_is_active (w))
870 return; 884 return;
885
886 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
871 887
872 ev_start ((W)w, 1); 888 ev_start ((W)w, 1);
873 array_needsize (signals, signalmax, w->signum, signals_init); 889 array_needsize (signals, signalmax, w->signum, signals_init);
874 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 890 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
875 891
884} 900}
885 901
886void 902void
887ev_signal_stop (struct ev_signal *w) 903ev_signal_stop (struct ev_signal *w)
888{ 904{
889 ev_clear ((W)w); 905 ev_clear_pending ((W)w);
890 if (!ev_is_active (w)) 906 if (!ev_is_active (w))
891 return; 907 return;
892 908
893 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 909 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
894 ev_stop ((W)w); 910 ev_stop ((W)w);
909} 925}
910 926
911void 927void
912ev_idle_stop (struct ev_idle *w) 928ev_idle_stop (struct ev_idle *w)
913{ 929{
914 ev_clear ((W)w); 930 ev_clear_pending ((W)w);
915 if (ev_is_active (w)) 931 if (ev_is_active (w))
916 return; 932 return;
917 933
918 idles [w->active - 1] = idles [--idlecnt]; 934 idles [w->active - 1] = idles [--idlecnt];
919 ev_stop ((W)w); 935 ev_stop ((W)w);
931} 947}
932 948
933void 949void
934ev_prepare_stop (struct ev_prepare *w) 950ev_prepare_stop (struct ev_prepare *w)
935{ 951{
936 ev_clear ((W)w); 952 ev_clear_pending ((W)w);
937 if (ev_is_active (w)) 953 if (ev_is_active (w))
938 return; 954 return;
939 955
940 prepares [w->active - 1] = prepares [--preparecnt]; 956 prepares [w->active - 1] = prepares [--preparecnt];
941 ev_stop ((W)w); 957 ev_stop ((W)w);
953} 969}
954 970
955void 971void
956ev_check_stop (struct ev_check *w) 972ev_check_stop (struct ev_check *w)
957{ 973{
958 ev_clear ((W)w); 974 ev_clear_pending ((W)w);
959 if (ev_is_active (w)) 975 if (ev_is_active (w))
960 return; 976 return;
961 977
962 checks [w->active - 1] = checks [--checkcnt]; 978 checks [w->active - 1] = checks [--checkcnt];
963 ev_stop ((W)w); 979 ev_stop ((W)w);
974} 990}
975 991
976void 992void
977ev_child_stop (struct ev_child *w) 993ev_child_stop (struct ev_child *w)
978{ 994{
979 ev_clear ((W)w); 995 ev_clear_pending ((W)w);
980 if (ev_is_active (w)) 996 if (ev_is_active (w))
981 return; 997 return;
982 998
983 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 999 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
984 ev_stop ((W)w); 1000 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines