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

Comparing libev/ev.c (file contents):
Revision 1.56 by root, Sun Nov 4 15:58:49 2007 UTC vs.
Revision 1.62 by root, Sun Nov 4 20:38:07 2007 UTC

28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#ifndef EV_STANDALONE 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
70 92
71#ifndef EV_USE_KQUEUE 93#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0 94# define EV_USE_KQUEUE 0
95#endif
96
97#ifndef EV_USE_WIN32
98# ifdef WIN32
99# define EV_USE_WIN32 1
100# else
101# define EV_USE_WIN32 0
102# endif
73#endif 103#endif
74 104
75#ifndef EV_USE_REALTIME 105#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1 106# define EV_USE_REALTIME 1
77#endif 107#endif
327 357
328/* called on ENOMEM in select/poll to kill some fds and retry */ 358/* called on ENOMEM in select/poll to kill some fds and retry */
329static void 359static void
330fd_enomem (EV_P) 360fd_enomem (EV_P)
331{ 361{
332 int fd = anfdmax; 362 int fd;
333 363
334 while (fd--) 364 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 365 if (anfds [fd].events)
336 { 366 {
337 close (fd); 367 close (fd);
338 fd_kill (EV_A_ fd); 368 fd_kill (EV_A_ fd);
339 return; 369 return;
349 /* this should be highly optimised to not do anything but set a flag */ 379 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd) 380 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events) 381 if (anfds [fd].events)
352 { 382 {
353 anfds [fd].events = 0; 383 anfds [fd].events = 0;
354 fd_change (fd); 384 fd_change (EV_A_ fd);
355 } 385 }
356} 386}
357 387
358/*****************************************************************************/ 388/*****************************************************************************/
359 389
363 WT w = heap [k]; 393 WT w = heap [k];
364 394
365 while (k && heap [k >> 1]->at > w->at) 395 while (k && heap [k >> 1]->at > w->at)
366 { 396 {
367 heap [k] = heap [k >> 1]; 397 heap [k] = heap [k >> 1];
368 heap [k]->active = k + 1; 398 ((W)heap [k])->active = k + 1;
369 k >>= 1; 399 k >>= 1;
370 } 400 }
371 401
372 heap [k] = w; 402 heap [k] = w;
373 heap [k]->active = k + 1; 403 ((W)heap [k])->active = k + 1;
374 404
375} 405}
376 406
377static void 407static void
378downheap (WT *heap, int N, int k) 408downheap (WT *heap, int N, int k)
388 418
389 if (w->at <= heap [j]->at) 419 if (w->at <= heap [j]->at)
390 break; 420 break;
391 421
392 heap [k] = heap [j]; 422 heap [k] = heap [j];
393 heap [k]->active = k + 1; 423 ((W)heap [k])->active = k + 1;
394 k = j; 424 k = j;
395 } 425 }
396 426
397 heap [k] = w; 427 heap [k] = w;
398 heap [k]->active = k + 1; 428 ((W)heap [k])->active = k + 1;
399} 429}
400 430
401/*****************************************************************************/ 431/*****************************************************************************/
402 432
403typedef struct 433typedef struct
409static ANSIG *signals; 439static ANSIG *signals;
410static int signalmax; 440static int signalmax;
411 441
412static int sigpipe [2]; 442static int sigpipe [2];
413static sig_atomic_t volatile gotsig; 443static sig_atomic_t volatile gotsig;
444static struct ev_io sigev;
414 445
415static void 446static void
416signals_init (ANSIG *base, int count) 447signals_init (ANSIG *base, int count)
417{ 448{
418 while (count--) 449 while (count--)
475} 506}
476 507
477/*****************************************************************************/ 508/*****************************************************************************/
478 509
479#ifndef WIN32 510#ifndef WIN32
511
512static struct ev_child *childs [PID_HASHSIZE];
513static struct ev_signal childev;
480 514
481#ifndef WCONTINUED 515#ifndef WCONTINUED
482# define WCONTINUED 0 516# define WCONTINUED 0
483#endif 517#endif
484 518
520# include "ev_kqueue.c" 554# include "ev_kqueue.c"
521#endif 555#endif
522#if EV_USE_EPOLL 556#if EV_USE_EPOLL
523# include "ev_epoll.c" 557# include "ev_epoll.c"
524#endif 558#endif
525#if EV_USEV_POLL 559#if EV_USE_POLL
526# include "ev_poll.c" 560# include "ev_poll.c"
527#endif 561#endif
528#if EV_USE_SELECT 562#if EV_USE_SELECT
529# include "ev_select.c" 563# include "ev_select.c"
530#endif 564#endif
582 methods = atoi (getenv ("LIBEV_METHODS")); 616 methods = atoi (getenv ("LIBEV_METHODS"));
583 else 617 else
584 methods = EVMETHOD_ANY; 618 methods = EVMETHOD_ANY;
585 619
586 method = 0; 620 method = 0;
621#if EV_USE_WIN32
622 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
623#endif
587#if EV_USE_KQUEUE 624#if EV_USE_KQUEUE
588 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
589#endif 626#endif
590#if EV_USE_EPOLL 627#if EV_USE_EPOLL
591 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
592#endif 629#endif
593#if EV_USEV_POLL 630#if EV_USE_POLL
594 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 631 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
595#endif 632#endif
596#if EV_USE_SELECT 633#if EV_USE_SELECT
597 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 634 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
598#endif 635#endif
600} 637}
601 638
602void 639void
603loop_destroy (EV_P) 640loop_destroy (EV_P)
604{ 641{
642#if EV_USE_WIN32
643 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
644#endif
605#if EV_USE_KQUEUE 645#if EV_USE_KQUEUE
606 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 646 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
607#endif 647#endif
608#if EV_USE_EPOLL 648#if EV_USE_EPOLL
609 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 649 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
610#endif 650#endif
611#if EV_USEV_POLL 651#if EV_USE_POLL
612 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 652 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
613#endif 653#endif
614#if EV_USE_SELECT 654#if EV_USE_SELECT
615 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 655 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
616#endif 656#endif
637{ 677{
638 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 678 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
639 679
640 loop_init (EV_A_ methods); 680 loop_init (EV_A_ methods);
641 681
642 if (ev_methods (EV_A)) 682 if (ev_method (EV_A))
643 return loop; 683 return loop;
644 684
645 return 0; 685 return 0;
646} 686}
647 687
707} 747}
708 748
709void 749void
710ev_default_destroy (void) 750ev_default_destroy (void)
711{ 751{
752#if EV_MULTIPLICITY
712 struct ev_loop *loop = default_loop; 753 struct ev_loop *loop = default_loop;
754#endif
713 755
714 ev_ref (EV_A); /* child watcher */ 756 ev_ref (EV_A); /* child watcher */
715 ev_signal_stop (EV_A_ &childev); 757 ev_signal_stop (EV_A_ &childev);
716 758
717 ev_ref (EV_A); /* signal watcher */ 759 ev_ref (EV_A); /* signal watcher */
722 764
723 loop_destroy (EV_A); 765 loop_destroy (EV_A);
724} 766}
725 767
726void 768void
727ev_default_fork (EV_P) 769ev_default_fork (void)
728{ 770{
771#if EV_MULTIPLICITY
772 struct ev_loop *loop = default_loop;
773#endif
774
729 loop_fork (EV_A); 775 loop_fork (EV_A);
730 776
731 ev_io_stop (EV_A_ &sigev); 777 ev_io_stop (EV_A_ &sigev);
732 close (sigpipe [0]); 778 close (sigpipe [0]);
733 close (sigpipe [1]); 779 close (sigpipe [1]);
761timers_reify (EV_P) 807timers_reify (EV_P)
762{ 808{
763 while (timercnt && timers [0]->at <= mn_now) 809 while (timercnt && timers [0]->at <= mn_now)
764 { 810 {
765 struct ev_timer *w = timers [0]; 811 struct ev_timer *w = timers [0];
812
813 assert (("inactive timer on timer heap detected", ev_is_active (w)));
766 814
767 /* first reschedule or stop timer */ 815 /* first reschedule or stop timer */
768 if (w->repeat) 816 if (w->repeat)
769 { 817 {
770 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 818 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
783{ 831{
784 while (periodiccnt && periodics [0]->at <= rt_now) 832 while (periodiccnt && periodics [0]->at <= rt_now)
785 { 833 {
786 struct ev_periodic *w = periodics [0]; 834 struct ev_periodic *w = periodics [0];
787 835
836 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
837
788 /* first reschedule or stop timer */ 838 /* first reschedule or stop timer */
789 if (w->interval) 839 if (w->interval)
790 { 840 {
791 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 841 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval;
792 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 842 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now));
1082 1132
1083 ev_start (EV_A_ (W)w, ++timercnt); 1133 ev_start (EV_A_ (W)w, ++timercnt);
1084 array_needsize (timers, timermax, timercnt, ); 1134 array_needsize (timers, timermax, timercnt, );
1085 timers [timercnt - 1] = w; 1135 timers [timercnt - 1] = w;
1086 upheap ((WT *)timers, timercnt - 1); 1136 upheap ((WT *)timers, timercnt - 1);
1137
1138 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1087} 1139}
1088 1140
1089void 1141void
1090ev_timer_stop (EV_P_ struct ev_timer *w) 1142ev_timer_stop (EV_P_ struct ev_timer *w)
1091{ 1143{
1092 ev_clear_pending (EV_A_ (W)w); 1144 ev_clear_pending (EV_A_ (W)w);
1093 if (!ev_is_active (w)) 1145 if (!ev_is_active (w))
1094 return; 1146 return;
1095 1147
1148 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1149
1096 if (w->active < timercnt--) 1150 if (((W)w)->active < timercnt--)
1097 { 1151 {
1098 timers [w->active - 1] = timers [timercnt]; 1152 timers [((W)w)->active - 1] = timers [timercnt];
1099 downheap ((WT *)timers, timercnt, w->active - 1); 1153 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1100 } 1154 }
1101 1155
1102 w->at = w->repeat; 1156 w->at = w->repeat;
1103 1157
1104 ev_stop (EV_A_ (W)w); 1158 ev_stop (EV_A_ (W)w);
1110 if (ev_is_active (w)) 1164 if (ev_is_active (w))
1111 { 1165 {
1112 if (w->repeat) 1166 if (w->repeat)
1113 { 1167 {
1114 w->at = mn_now + w->repeat; 1168 w->at = mn_now + w->repeat;
1115 downheap ((WT *)timers, timercnt, w->active - 1); 1169 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1116 } 1170 }
1117 else 1171 else
1118 ev_timer_stop (EV_A_ w); 1172 ev_timer_stop (EV_A_ w);
1119 } 1173 }
1120 else if (w->repeat) 1174 else if (w->repeat)
1135 1189
1136 ev_start (EV_A_ (W)w, ++periodiccnt); 1190 ev_start (EV_A_ (W)w, ++periodiccnt);
1137 array_needsize (periodics, periodicmax, periodiccnt, ); 1191 array_needsize (periodics, periodicmax, periodiccnt, );
1138 periodics [periodiccnt - 1] = w; 1192 periodics [periodiccnt - 1] = w;
1139 upheap ((WT *)periodics, periodiccnt - 1); 1193 upheap ((WT *)periodics, periodiccnt - 1);
1194
1195 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1140} 1196}
1141 1197
1142void 1198void
1143ev_periodic_stop (EV_P_ struct ev_periodic *w) 1199ev_periodic_stop (EV_P_ struct ev_periodic *w)
1144{ 1200{
1145 ev_clear_pending (EV_A_ (W)w); 1201 ev_clear_pending (EV_A_ (W)w);
1146 if (!ev_is_active (w)) 1202 if (!ev_is_active (w))
1147 return; 1203 return;
1148 1204
1205 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1206
1149 if (w->active < periodiccnt--) 1207 if (((W)w)->active < periodiccnt--)
1150 { 1208 {
1151 periodics [w->active - 1] = periodics [periodiccnt]; 1209 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1152 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1210 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1153 } 1211 }
1154 1212
1155 ev_stop (EV_A_ (W)w); 1213 ev_stop (EV_A_ (W)w);
1156} 1214}
1157 1215
1171{ 1229{
1172 ev_clear_pending (EV_A_ (W)w); 1230 ev_clear_pending (EV_A_ (W)w);
1173 if (ev_is_active (w)) 1231 if (ev_is_active (w))
1174 return; 1232 return;
1175 1233
1176 idles [w->active - 1] = idles [--idlecnt]; 1234 idles [((W)w)->active - 1] = idles [--idlecnt];
1177 ev_stop (EV_A_ (W)w); 1235 ev_stop (EV_A_ (W)w);
1178} 1236}
1179 1237
1180void 1238void
1181ev_prepare_start (EV_P_ struct ev_prepare *w) 1239ev_prepare_start (EV_P_ struct ev_prepare *w)
1193{ 1251{
1194 ev_clear_pending (EV_A_ (W)w); 1252 ev_clear_pending (EV_A_ (W)w);
1195 if (ev_is_active (w)) 1253 if (ev_is_active (w))
1196 return; 1254 return;
1197 1255
1198 prepares [w->active - 1] = prepares [--preparecnt]; 1256 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1199 ev_stop (EV_A_ (W)w); 1257 ev_stop (EV_A_ (W)w);
1200} 1258}
1201 1259
1202void 1260void
1203ev_check_start (EV_P_ struct ev_check *w) 1261ev_check_start (EV_P_ struct ev_check *w)
1215{ 1273{
1216 ev_clear_pending (EV_A_ (W)w); 1274 ev_clear_pending (EV_A_ (W)w);
1217 if (ev_is_active (w)) 1275 if (ev_is_active (w))
1218 return; 1276 return;
1219 1277
1220 checks [w->active - 1] = checks [--checkcnt]; 1278 checks [((W)w)->active - 1] = checks [--checkcnt];
1221 ev_stop (EV_A_ (W)w); 1279 ev_stop (EV_A_ (W)w);
1222} 1280}
1223 1281
1224#ifndef SA_RESTART 1282#ifndef SA_RESTART
1225# define SA_RESTART 0 1283# define SA_RESTART 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines