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

Comparing libev/ev.c (file contents):
Revision 1.32 by root, Thu Nov 1 09:21:51 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
139/*****************************************************************************/ 144/*****************************************************************************/
140 145
141typedef struct 146typedef struct
142{ 147{
143 struct ev_io *head; 148 struct ev_io *head;
144 int events; 149 unsigned char events;
150 unsigned char reify;
145} ANFD; 151} ANFD;
146 152
147static ANFD *anfds; 153static ANFD *anfds;
148static int anfdmax; 154static int anfdmax;
149 155
152{ 158{
153 while (count--) 159 while (count--)
154 { 160 {
155 base->head = 0; 161 base->head = 0;
156 base->events = EV_NONE; 162 base->events = EV_NONE;
163 base->reify = 0;
164
157 ++base; 165 ++base;
158 } 166 }
159} 167}
160 168
161typedef struct 169typedef struct
225 int events = 0; 233 int events = 0;
226 234
227 for (w = anfd->head; w; w = w->next) 235 for (w = anfd->head; w; w = w->next)
228 events |= w->events; 236 events |= w->events;
229 237
230 anfd->events &= ~EV_REIFY; 238 anfd->reify = 0;
231 239
232 if (anfd->events != events) 240 if (anfd->events != events)
233 { 241 {
234 method_modify (fd, anfd->events, events); 242 method_modify (fd, anfd->events, events);
235 anfd->events = events; 243 anfd->events = events;
240} 248}
241 249
242static void 250static void
243fd_change (int fd) 251fd_change (int fd)
244{ 252{
245 if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) 253 if (anfds [fd].reify || fdchangecnt < 0)
246 return; 254 return;
247 255
248 anfds [fd].events |= EV_REIFY; 256 anfds [fd].reify = 1;
249 257
250 ++fdchangecnt; 258 ++fdchangecnt;
251 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 259 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
252 fdchanges [fdchangecnt - 1] = fd; 260 fdchanges [fdchangecnt - 1] = fd;
253} 261}
262 if (anfds [fd].events) 270 if (anfds [fd].events)
263 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 271 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
264 while (anfds [fd].head) 272 while (anfds [fd].head)
265 { 273 {
266 ev_io_stop (anfds [fd].head); 274 ev_io_stop (anfds [fd].head);
267 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); 275 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
268 } 276 }
269} 277}
270 278
271/*****************************************************************************/ 279/*****************************************************************************/
272 280
320/*****************************************************************************/ 328/*****************************************************************************/
321 329
322typedef struct 330typedef struct
323{ 331{
324 struct ev_signal *head; 332 struct ev_signal *head;
325 sig_atomic_t gotsig; 333 sig_atomic_t volatile gotsig;
326} ANSIG; 334} ANSIG;
327 335
328static ANSIG *signals; 336static ANSIG *signals;
329static int signalmax; 337static int signalmax;
330 338
331static int sigpipe [2]; 339static int sigpipe [2];
332static sig_atomic_t gotsig; 340static sig_atomic_t volatile gotsig;
333static struct ev_io sigev; 341static struct ev_io sigev;
334 342
335static void 343static void
336signals_init (ANSIG *base, int count) 344signals_init (ANSIG *base, int count)
337{ 345{
338 while (count--) 346 while (count--)
339 { 347 {
340 base->head = 0; 348 base->head = 0;
341 base->gotsig = 0; 349 base->gotsig = 0;
350
342 ++base; 351 ++base;
343 } 352 }
344} 353}
345 354
346static void 355static void
349 signals [signum - 1].gotsig = 1; 358 signals [signum - 1].gotsig = 1;
350 359
351 if (!gotsig) 360 if (!gotsig)
352 { 361 {
353 gotsig = 1; 362 gotsig = 1;
354 write (sigpipe [1], &gotsig, 1); 363 write (sigpipe [1], &signum, 1);
355 } 364 }
356} 365}
357 366
358static void 367static void
359sigcb (struct ev_io *iow, int revents) 368sigcb (struct ev_io *iow, int revents)
360{ 369{
361 struct ev_signal *w; 370 struct ev_signal *w;
362 int sig; 371 int signum;
363 372
373 read (sigpipe [0], &revents, 1);
364 gotsig = 0; 374 gotsig = 0;
365 read (sigpipe [0], &revents, 1);
366 375
367 for (sig = signalmax; sig--; ) 376 for (signum = signalmax; signum--; )
368 if (signals [sig].gotsig) 377 if (signals [signum].gotsig)
369 { 378 {
370 signals [sig].gotsig = 0; 379 signals [signum].gotsig = 0;
371 380
372 for (w = signals [sig].head; w; w = w->next) 381 for (w = signals [signum].head; w; w = w->next)
373 event ((W)w, EV_SIGNAL); 382 event ((W)w, EV_SIGNAL);
374 } 383 }
375} 384}
376 385
377static void 386static void
414 struct ev_child *w; 423 struct ev_child *w;
415 int pid, status; 424 int pid, status;
416 425
417 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 426 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
418 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 427 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
419 if (w->pid == pid || w->pid == -1) 428 if (w->pid == pid || !w->pid)
420 { 429 {
421 w->status = status; 430 w->status = status;
422 event ((W)w, EV_CHILD); 431 event ((W)w, EV_CHILD);
423 } 432 }
424} 433}
485} 494}
486 495
487/*****************************************************************************/ 496/*****************************************************************************/
488 497
489void 498void
490ev_prefork (void) 499ev_fork_prepare (void)
491{ 500{
492 /* nop */ 501 /* nop */
493} 502}
494 503
495void 504void
496ev_postfork_parent (void) 505ev_fork_parent (void)
497{ 506{
498 /* nop */ 507 /* nop */
499} 508}
500 509
501void 510void
502ev_postfork_child (void) 511ev_fork_child (void)
503{ 512{
504#if EV_USE_EPOLL 513#if EV_USE_EPOLL
505 if (ev_method == EVMETHOD_EPOLL) 514 if (ev_method == EVMETHOD_EPOLL)
506 epoll_postfork_child (); 515 epoll_postfork_child ();
507#endif 516#endif
538 struct ev_timer *w = timers [0]; 547 struct ev_timer *w = timers [0];
539 548
540 /* first reschedule or stop timer */ 549 /* first reschedule or stop timer */
541 if (w->repeat) 550 if (w->repeat)
542 { 551 {
552 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
543 w->at = now + w->repeat; 553 w->at = now + w->repeat;
544 assert (("timer timeout in the past, negative repeat?", w->at > now));
545 downheap ((WT *)timers, timercnt, 0); 554 downheap ((WT *)timers, timercnt, 0);
546 } 555 }
547 else 556 else
548 ev_timer_stop (w); /* nonrepeating: stop timer */ 557 ev_timer_stop (w); /* nonrepeating: stop timer */
549 558
560 569
561 /* first reschedule or stop timer */ 570 /* first reschedule or stop timer */
562 if (w->interval) 571 if (w->interval)
563 { 572 {
564 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;
565 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));
566 downheap ((WT *)periodics, periodiccnt, 0); 575 downheap ((WT *)periodics, periodiccnt, 0);
567 } 576 }
568 else 577 else
569 ev_periodic_stop (w); /* nonrepeating: stop timer */ 578 ev_periodic_stop (w); /* nonrepeating: stop timer */
570 579
571 event ((W)w, EV_TIMEOUT); 580 event ((W)w, EV_PERIODIC);
572 } 581 }
573} 582}
574 583
575static void 584static void
576periodics_reschedule (ev_tstamp diff) 585periodics_reschedule (ev_tstamp diff)
731 head = &(*head)->next; 740 head = &(*head)->next;
732 } 741 }
733} 742}
734 743
735static void 744static void
736ev_clear (W w) 745ev_clear_pending (W w)
737{ 746{
738 if (w->pending) 747 if (w->pending)
739 { 748 {
740 pendings [w->pending - 1].w = 0; 749 pendings [w->pending - 1].w = 0;
741 w->pending = 0; 750 w->pending = 0;
757/*****************************************************************************/ 766/*****************************************************************************/
758 767
759void 768void
760ev_io_start (struct ev_io *w) 769ev_io_start (struct ev_io *w)
761{ 770{
762 if (ev_is_active (w))
763 return;
764
765 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));
766 777
767 ev_start ((W)w, 1); 778 ev_start ((W)w, 1);
768 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 779 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
769 wlist_add ((WL *)&anfds[fd].head, (WL)w); 780 wlist_add ((WL *)&anfds[fd].head, (WL)w);
770 781
772} 783}
773 784
774void 785void
775ev_io_stop (struct ev_io *w) 786ev_io_stop (struct ev_io *w)
776{ 787{
777 ev_clear ((W)w); 788 ev_clear_pending ((W)w);
778 if (!ev_is_active (w)) 789 if (!ev_is_active (w))
779 return; 790 return;
780 791
781 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 792 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
782 ev_stop ((W)w); 793 ev_stop ((W)w);
790 if (ev_is_active (w)) 801 if (ev_is_active (w))
791 return; 802 return;
792 803
793 w->at += now; 804 w->at += now;
794 805
795 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.));
796 807
797 ev_start ((W)w, ++timercnt); 808 ev_start ((W)w, ++timercnt);
798 array_needsize (timers, timermax, timercnt, ); 809 array_needsize (timers, timermax, timercnt, );
799 timers [timercnt - 1] = w; 810 timers [timercnt - 1] = w;
800 upheap ((WT *)timers, timercnt - 1); 811 upheap ((WT *)timers, timercnt - 1);
801} 812}
802 813
803void 814void
804ev_timer_stop (struct ev_timer *w) 815ev_timer_stop (struct ev_timer *w)
805{ 816{
806 ev_clear ((W)w); 817 ev_clear_pending ((W)w);
807 if (!ev_is_active (w)) 818 if (!ev_is_active (w))
808 return; 819 return;
809 820
810 if (w->active < timercnt--) 821 if (w->active < timercnt--)
811 { 822 {
839ev_periodic_start (struct ev_periodic *w) 850ev_periodic_start (struct ev_periodic *w)
840{ 851{
841 if (ev_is_active (w)) 852 if (ev_is_active (w))
842 return; 853 return;
843 854
844 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.));
845 856
846 /* 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 */
847 if (w->interval) 858 if (w->interval)
848 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 859 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
849 860
854} 865}
855 866
856void 867void
857ev_periodic_stop (struct ev_periodic *w) 868ev_periodic_stop (struct ev_periodic *w)
858{ 869{
859 ev_clear ((W)w); 870 ev_clear_pending ((W)w);
860 if (!ev_is_active (w)) 871 if (!ev_is_active (w))
861 return; 872 return;
862 873
863 if (w->active < periodiccnt--) 874 if (w->active < periodiccnt--)
864 { 875 {
872void 883void
873ev_signal_start (struct ev_signal *w) 884ev_signal_start (struct ev_signal *w)
874{ 885{
875 if (ev_is_active (w)) 886 if (ev_is_active (w))
876 return; 887 return;
888
889 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
877 890
878 ev_start ((W)w, 1); 891 ev_start ((W)w, 1);
879 array_needsize (signals, signalmax, w->signum, signals_init); 892 array_needsize (signals, signalmax, w->signum, signals_init);
880 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 893 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
881 894
890} 903}
891 904
892void 905void
893ev_signal_stop (struct ev_signal *w) 906ev_signal_stop (struct ev_signal *w)
894{ 907{
895 ev_clear ((W)w); 908 ev_clear_pending ((W)w);
896 if (!ev_is_active (w)) 909 if (!ev_is_active (w))
897 return; 910 return;
898 911
899 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 912 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
900 ev_stop ((W)w); 913 ev_stop ((W)w);
915} 928}
916 929
917void 930void
918ev_idle_stop (struct ev_idle *w) 931ev_idle_stop (struct ev_idle *w)
919{ 932{
920 ev_clear ((W)w); 933 ev_clear_pending ((W)w);
921 if (ev_is_active (w)) 934 if (ev_is_active (w))
922 return; 935 return;
923 936
924 idles [w->active - 1] = idles [--idlecnt]; 937 idles [w->active - 1] = idles [--idlecnt];
925 ev_stop ((W)w); 938 ev_stop ((W)w);
937} 950}
938 951
939void 952void
940ev_prepare_stop (struct ev_prepare *w) 953ev_prepare_stop (struct ev_prepare *w)
941{ 954{
942 ev_clear ((W)w); 955 ev_clear_pending ((W)w);
943 if (ev_is_active (w)) 956 if (ev_is_active (w))
944 return; 957 return;
945 958
946 prepares [w->active - 1] = prepares [--preparecnt]; 959 prepares [w->active - 1] = prepares [--preparecnt];
947 ev_stop ((W)w); 960 ev_stop ((W)w);
959} 972}
960 973
961void 974void
962ev_check_stop (struct ev_check *w) 975ev_check_stop (struct ev_check *w)
963{ 976{
964 ev_clear ((W)w); 977 ev_clear_pending ((W)w);
965 if (ev_is_active (w)) 978 if (ev_is_active (w))
966 return; 979 return;
967 980
968 checks [w->active - 1] = checks [--checkcnt]; 981 checks [w->active - 1] = checks [--checkcnt];
969 ev_stop ((W)w); 982 ev_stop ((W)w);
980} 993}
981 994
982void 995void
983ev_child_stop (struct ev_child *w) 996ev_child_stop (struct ev_child *w)
984{ 997{
985 ev_clear ((W)w); 998 ev_clear_pending ((W)w);
986 if (ev_is_active (w)) 999 if (ev_is_active (w))
987 return; 1000 return;
988 1001
989 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1002 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
990 ev_stop ((W)w); 1003 ev_stop ((W)w);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines