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.39 by root, Thu Nov 1 17:17:32 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;
135/*****************************************************************************/ 144/*****************************************************************************/
136 145
137typedef struct 146typedef struct
138{ 147{
139 struct ev_io *head; 148 struct ev_io *head;
140 int events; 149 unsigned char events;
150 unsigned char reify;
141} ANFD; 151} ANFD;
142 152
143static ANFD *anfds; 153static ANFD *anfds;
144static int anfdmax; 154static int anfdmax;
145 155
148{ 158{
149 while (count--) 159 while (count--)
150 { 160 {
151 base->head = 0; 161 base->head = 0;
152 base->events = EV_NONE; 162 base->events = EV_NONE;
163 base->reify = 0;
164
153 ++base; 165 ++base;
154 } 166 }
155} 167}
156 168
157typedef struct 169typedef struct
164static int pendingmax, pendingcnt; 176static int pendingmax, pendingcnt;
165 177
166static void 178static void
167event (W w, int events) 179event (W w, int events)
168{ 180{
181 if (w->pending)
182 {
183 pendings [w->pending - 1].events |= events;
184 return;
185 }
186
169 w->pending = ++pendingcnt; 187 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, ); 188 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w; 189 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 190 pendings [pendingcnt - 1].events = events;
173} 191}
215 int events = 0; 233 int events = 0;
216 234
217 for (w = anfd->head; w; w = w->next) 235 for (w = anfd->head; w; w = w->next)
218 events |= w->events; 236 events |= w->events;
219 237
220 anfd->events &= ~EV_REIFY; 238 anfd->reify = 0;
221 239
222 if (anfd->events != events) 240 if (anfd->events != events)
223 { 241 {
224 method_modify (fd, anfd->events, events); 242 method_modify (fd, anfd->events, events);
225 anfd->events = events; 243 anfd->events = events;
230} 248}
231 249
232static void 250static void
233fd_change (int fd) 251fd_change (int fd)
234{ 252{
235 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 253 if (anfds [fd].reify || fdchangecnt < 0)
236 return; 254 return;
237 255
238 anfds [fd].events |= EV_REIFY; 256 anfds [fd].reify = 1;
239 257
240 ++fdchangecnt; 258 ++fdchangecnt;
241 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 259 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
242 fdchanges [fdchangecnt - 1] = fd; 260 fdchanges [fdchangecnt - 1] = fd;
243} 261}
251 for (fd = 0; fd < anfdmax; ++fd) 269 for (fd = 0; fd < anfdmax; ++fd)
252 if (anfds [fd].events) 270 if (anfds [fd].events)
253 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 271 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
254 while (anfds [fd].head) 272 while (anfds [fd].head)
255 { 273 {
256 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT);
257 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);
258 } 276 }
259} 277}
260 278
261/*****************************************************************************/ 279/*****************************************************************************/
262 280
310/*****************************************************************************/ 328/*****************************************************************************/
311 329
312typedef struct 330typedef struct
313{ 331{
314 struct ev_signal *head; 332 struct ev_signal *head;
315 sig_atomic_t gotsig; 333 sig_atomic_t volatile gotsig;
316} ANSIG; 334} ANSIG;
317 335
318static ANSIG *signals; 336static ANSIG *signals;
319static int signalmax; 337static int signalmax;
320 338
321static int sigpipe [2]; 339static int sigpipe [2];
322static sig_atomic_t gotsig; 340static sig_atomic_t volatile gotsig;
323static struct ev_io sigev; 341static struct ev_io sigev;
324 342
325static void 343static void
326signals_init (ANSIG *base, int count) 344signals_init (ANSIG *base, int count)
327{ 345{
328 while (count--) 346 while (count--)
329 { 347 {
330 base->head = 0; 348 base->head = 0;
331 base->gotsig = 0; 349 base->gotsig = 0;
350
332 ++base; 351 ++base;
333 } 352 }
334} 353}
335 354
336static void 355static void
339 signals [signum - 1].gotsig = 1; 358 signals [signum - 1].gotsig = 1;
340 359
341 if (!gotsig) 360 if (!gotsig)
342 { 361 {
343 gotsig = 1; 362 gotsig = 1;
344 write (sigpipe [1], &gotsig, 1); 363 write (sigpipe [1], &signum, 1);
345 } 364 }
346} 365}
347 366
348static void 367static void
349sigcb (struct ev_io *iow, int revents) 368sigcb (struct ev_io *iow, int revents)
350{ 369{
351 struct ev_signal *w; 370 struct ev_signal *w;
352 int sig; 371 int signum;
353 372
373 read (sigpipe [0], &revents, 1);
354 gotsig = 0; 374 gotsig = 0;
355 read (sigpipe [0], &revents, 1);
356 375
357 for (sig = signalmax; sig--; ) 376 for (signum = signalmax; signum--; )
358 if (signals [sig].gotsig) 377 if (signals [signum].gotsig)
359 { 378 {
360 signals [sig].gotsig = 0; 379 signals [signum].gotsig = 0;
361 380
362 for (w = signals [sig].head; w; w = w->next) 381 for (w = signals [signum].head; w; w = w->next)
363 event ((W)w, EV_SIGNAL); 382 event ((W)w, EV_SIGNAL);
364 } 383 }
365} 384}
366 385
367static void 386static void
404 struct ev_child *w; 423 struct ev_child *w;
405 int pid, status; 424 int pid, status;
406 425
407 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 426 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
408 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 427 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
409 if (w->pid == pid || w->pid == -1) 428 if (w->pid == pid || !w->pid)
410 { 429 {
411 w->status = status; 430 w->status = status;
412 event ((W)w, EV_CHILD); 431 event ((W)w, EV_CHILD);
413 } 432 }
414} 433}
475} 494}
476 495
477/*****************************************************************************/ 496/*****************************************************************************/
478 497
479void 498void
480ev_prefork (void) 499ev_fork_prepare (void)
481{ 500{
482 /* nop */ 501 /* nop */
483} 502}
484 503
485void 504void
486ev_postfork_parent (void) 505ev_fork_parent (void)
487{ 506{
488 /* nop */ 507 /* nop */
489} 508}
490 509
491void 510void
492ev_postfork_child (void) 511ev_fork_child (void)
493{ 512{
494#if EV_USE_EPOLL 513#if EV_USE_EPOLL
495 if (ev_method == EVMETHOD_EPOLL) 514 if (ev_method == EVMETHOD_EPOLL)
496 epoll_postfork_child (); 515 epoll_postfork_child ();
497#endif 516#endif
528 struct ev_timer *w = timers [0]; 547 struct ev_timer *w = timers [0];
529 548
530 /* first reschedule or stop timer */ 549 /* first reschedule or stop timer */
531 if (w->repeat) 550 if (w->repeat)
532 { 551 {
552 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 553 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 554 downheap ((WT *)timers, timercnt, 0);
536 } 555 }
537 else 556 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 557 ev_timer_stop (w); /* nonrepeating: stop timer */
539 558
550 569
551 /* first reschedule or stop timer */ 570 /* first reschedule or stop timer */
552 if (w->interval) 571 if (w->interval)
553 { 572 {
554 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;
555 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));
556 downheap ((WT *)periodics, periodiccnt, 0); 575 downheap ((WT *)periodics, periodiccnt, 0);
557 } 576 }
558 else 577 else
559 ev_periodic_stop (w); /* nonrepeating: stop timer */ 578 ev_periodic_stop (w); /* nonrepeating: stop timer */
560 579
561 event ((W)w, EV_TIMEOUT); 580 event ((W)w, EV_PERIODIC);
562 } 581 }
563} 582}
564 583
565static void 584static void
566periodics_reschedule (ev_tstamp diff) 585periodics_reschedule (ev_tstamp diff)
721 head = &(*head)->next; 740 head = &(*head)->next;
722 } 741 }
723} 742}
724 743
725static void 744static void
726ev_clear (W w) 745ev_clear_pending (W w)
727{ 746{
728 if (w->pending) 747 if (w->pending)
729 { 748 {
730 pendings [w->pending - 1].w = 0; 749 pendings [w->pending - 1].w = 0;
731 w->pending = 0; 750 w->pending = 0;
747/*****************************************************************************/ 766/*****************************************************************************/
748 767
749void 768void
750ev_io_start (struct ev_io *w) 769ev_io_start (struct ev_io *w)
751{ 770{
752 if (ev_is_active (w))
753 return;
754
755 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));
756 777
757 ev_start ((W)w, 1); 778 ev_start ((W)w, 1);
758 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 779 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
759 wlist_add ((WL *)&anfds[fd].head, (WL)w); 780 wlist_add ((WL *)&anfds[fd].head, (WL)w);
760 781
762} 783}
763 784
764void 785void
765ev_io_stop (struct ev_io *w) 786ev_io_stop (struct ev_io *w)
766{ 787{
767 ev_clear ((W)w); 788 ev_clear_pending ((W)w);
768 if (!ev_is_active (w)) 789 if (!ev_is_active (w))
769 return; 790 return;
770 791
771 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 792 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
772 ev_stop ((W)w); 793 ev_stop ((W)w);
780 if (ev_is_active (w)) 801 if (ev_is_active (w))
781 return; 802 return;
782 803
783 w->at += now; 804 w->at += now;
784 805
785 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.));
786 807
787 ev_start ((W)w, ++timercnt); 808 ev_start ((W)w, ++timercnt);
788 array_needsize (timers, timermax, timercnt, ); 809 array_needsize (timers, timermax, timercnt, );
789 timers [timercnt - 1] = w; 810 timers [timercnt - 1] = w;
790 upheap ((WT *)timers, timercnt - 1); 811 upheap ((WT *)timers, timercnt - 1);
791} 812}
792 813
793void 814void
794ev_timer_stop (struct ev_timer *w) 815ev_timer_stop (struct ev_timer *w)
795{ 816{
796 ev_clear ((W)w); 817 ev_clear_pending ((W)w);
797 if (!ev_is_active (w)) 818 if (!ev_is_active (w))
798 return; 819 return;
799 820
800 if (w->active < timercnt--) 821 if (w->active < timercnt--)
801 { 822 {
829ev_periodic_start (struct ev_periodic *w) 850ev_periodic_start (struct ev_periodic *w)
830{ 851{
831 if (ev_is_active (w)) 852 if (ev_is_active (w))
832 return; 853 return;
833 854
834 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.));
835 856
836 /* 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 */
837 if (w->interval) 858 if (w->interval)
838 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 859 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
839 860
844} 865}
845 866
846void 867void
847ev_periodic_stop (struct ev_periodic *w) 868ev_periodic_stop (struct ev_periodic *w)
848{ 869{
849 ev_clear ((W)w); 870 ev_clear_pending ((W)w);
850 if (!ev_is_active (w)) 871 if (!ev_is_active (w))
851 return; 872 return;
852 873
853 if (w->active < periodiccnt--) 874 if (w->active < periodiccnt--)
854 { 875 {
862void 883void
863ev_signal_start (struct ev_signal *w) 884ev_signal_start (struct ev_signal *w)
864{ 885{
865 if (ev_is_active (w)) 886 if (ev_is_active (w))
866 return; 887 return;
888
889 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
867 890
868 ev_start ((W)w, 1); 891 ev_start ((W)w, 1);
869 array_needsize (signals, signalmax, w->signum, signals_init); 892 array_needsize (signals, signalmax, w->signum, signals_init);
870 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 893 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
871 894
880} 903}
881 904
882void 905void
883ev_signal_stop (struct ev_signal *w) 906ev_signal_stop (struct ev_signal *w)
884{ 907{
885 ev_clear ((W)w); 908 ev_clear_pending ((W)w);
886 if (!ev_is_active (w)) 909 if (!ev_is_active (w))
887 return; 910 return;
888 911
889 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 912 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
890 ev_stop ((W)w); 913 ev_stop ((W)w);
905} 928}
906 929
907void 930void
908ev_idle_stop (struct ev_idle *w) 931ev_idle_stop (struct ev_idle *w)
909{ 932{
910 ev_clear ((W)w); 933 ev_clear_pending ((W)w);
911 if (ev_is_active (w)) 934 if (ev_is_active (w))
912 return; 935 return;
913 936
914 idles [w->active - 1] = idles [--idlecnt]; 937 idles [w->active - 1] = idles [--idlecnt];
915 ev_stop ((W)w); 938 ev_stop ((W)w);
927} 950}
928 951
929void 952void
930ev_prepare_stop (struct ev_prepare *w) 953ev_prepare_stop (struct ev_prepare *w)
931{ 954{
932 ev_clear ((W)w); 955 ev_clear_pending ((W)w);
933 if (ev_is_active (w)) 956 if (ev_is_active (w))
934 return; 957 return;
935 958
936 prepares [w->active - 1] = prepares [--preparecnt]; 959 prepares [w->active - 1] = prepares [--preparecnt];
937 ev_stop ((W)w); 960 ev_stop ((W)w);
949} 972}
950 973
951void 974void
952ev_check_stop (struct ev_check *w) 975ev_check_stop (struct ev_check *w)
953{ 976{
954 ev_clear ((W)w); 977 ev_clear_pending ((W)w);
955 if (ev_is_active (w)) 978 if (ev_is_active (w))
956 return; 979 return;
957 980
958 checks [w->active - 1] = checks [--checkcnt]; 981 checks [w->active - 1] = checks [--checkcnt];
959 ev_stop ((W)w); 982 ev_stop ((W)w);
970} 993}
971 994
972void 995void
973ev_child_stop (struct ev_child *w) 996ev_child_stop (struct ev_child *w)
974{ 997{
975 ev_clear ((W)w); 998 ev_clear_pending ((W)w);
976 if (ev_is_active (w)) 999 if (ev_is_active (w))
977 return; 1000 return;
978 1001
979 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1002 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
980 ev_stop ((W)w); 1003 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines