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

Comparing libev/ev.c (file contents):
Revision 1.58 by root, Sun Nov 4 16:52:52 2007 UTC vs.
Revision 1.62 by root, Sun Nov 4 20:38:07 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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_EMBED 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
93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 123#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 124#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 125#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 126/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
97 127
98#ifndef EV_EMBED
99# include "ev.h" 128#include "ev.h"
100#endif
101 129
102#if __GNUC__ >= 3 130#if __GNUC__ >= 3
103# define expect(expr,value) __builtin_expect ((expr),(value)) 131# define expect(expr,value) __builtin_expect ((expr),(value))
104# define inline inline 132# define inline inline
105#else 133#else
329 357
330/* 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 */
331static void 359static void
332fd_enomem (EV_P) 360fd_enomem (EV_P)
333{ 361{
334 int fd = anfdmax; 362 int fd;
335 363
336 while (fd--) 364 for (fd = anfdmax; fd--; )
337 if (anfds [fd].events) 365 if (anfds [fd].events)
338 { 366 {
339 close (fd); 367 close (fd);
340 fd_kill (EV_A_ fd); 368 fd_kill (EV_A_ fd);
341 return; 369 return;
351 /* 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 */
352 for (fd = 0; fd < anfdmax; ++fd) 380 for (fd = 0; fd < anfdmax; ++fd)
353 if (anfds [fd].events) 381 if (anfds [fd].events)
354 { 382 {
355 anfds [fd].events = 0; 383 anfds [fd].events = 0;
356 fd_change (fd); 384 fd_change (EV_A_ fd);
357 } 385 }
358} 386}
359 387
360/*****************************************************************************/ 388/*****************************************************************************/
361 389
365 WT w = heap [k]; 393 WT w = heap [k];
366 394
367 while (k && heap [k >> 1]->at > w->at) 395 while (k && heap [k >> 1]->at > w->at)
368 { 396 {
369 heap [k] = heap [k >> 1]; 397 heap [k] = heap [k >> 1];
370 heap [k]->active = k + 1; 398 ((W)heap [k])->active = k + 1;
371 k >>= 1; 399 k >>= 1;
372 } 400 }
373 401
374 heap [k] = w; 402 heap [k] = w;
375 heap [k]->active = k + 1; 403 ((W)heap [k])->active = k + 1;
376 404
377} 405}
378 406
379static void 407static void
380downheap (WT *heap, int N, int k) 408downheap (WT *heap, int N, int k)
390 418
391 if (w->at <= heap [j]->at) 419 if (w->at <= heap [j]->at)
392 break; 420 break;
393 421
394 heap [k] = heap [j]; 422 heap [k] = heap [j];
395 heap [k]->active = k + 1; 423 ((W)heap [k])->active = k + 1;
396 k = j; 424 k = j;
397 } 425 }
398 426
399 heap [k] = w; 427 heap [k] = w;
400 heap [k]->active = k + 1; 428 ((W)heap [k])->active = k + 1;
401} 429}
402 430
403/*****************************************************************************/ 431/*****************************************************************************/
404 432
405typedef struct 433typedef struct
411static ANSIG *signals; 439static ANSIG *signals;
412static int signalmax; 440static int signalmax;
413 441
414static int sigpipe [2]; 442static int sigpipe [2];
415static sig_atomic_t volatile gotsig; 443static sig_atomic_t volatile gotsig;
444static struct ev_io sigev;
416 445
417static void 446static void
418signals_init (ANSIG *base, int count) 447signals_init (ANSIG *base, int count)
419{ 448{
420 while (count--) 449 while (count--)
477} 506}
478 507
479/*****************************************************************************/ 508/*****************************************************************************/
480 509
481#ifndef WIN32 510#ifndef WIN32
511
512static struct ev_child *childs [PID_HASHSIZE];
513static struct ev_signal childev;
482 514
483#ifndef WCONTINUED 515#ifndef WCONTINUED
484# define WCONTINUED 0 516# define WCONTINUED 0
485#endif 517#endif
486 518
522# include "ev_kqueue.c" 554# include "ev_kqueue.c"
523#endif 555#endif
524#if EV_USE_EPOLL 556#if EV_USE_EPOLL
525# include "ev_epoll.c" 557# include "ev_epoll.c"
526#endif 558#endif
527#if EV_USEV_POLL 559#if EV_USE_POLL
528# include "ev_poll.c" 560# include "ev_poll.c"
529#endif 561#endif
530#if EV_USE_SELECT 562#if EV_USE_SELECT
531# include "ev_select.c" 563# include "ev_select.c"
532#endif 564#endif
584 methods = atoi (getenv ("LIBEV_METHODS")); 616 methods = atoi (getenv ("LIBEV_METHODS"));
585 else 617 else
586 methods = EVMETHOD_ANY; 618 methods = EVMETHOD_ANY;
587 619
588 method = 0; 620 method = 0;
621#if EV_USE_WIN32
622 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
623#endif
589#if EV_USE_KQUEUE 624#if EV_USE_KQUEUE
590 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
591#endif 626#endif
592#if EV_USE_EPOLL 627#if EV_USE_EPOLL
593 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 628 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
594#endif 629#endif
595#if EV_USEV_POLL 630#if EV_USE_POLL
596 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 631 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
597#endif 632#endif
598#if EV_USE_SELECT 633#if EV_USE_SELECT
599 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 634 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
600#endif 635#endif
602} 637}
603 638
604void 639void
605loop_destroy (EV_P) 640loop_destroy (EV_P)
606{ 641{
642#if EV_USE_WIN32
643 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
644#endif
607#if EV_USE_KQUEUE 645#if EV_USE_KQUEUE
608 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 646 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
609#endif 647#endif
610#if EV_USE_EPOLL 648#if EV_USE_EPOLL
611 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 649 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
612#endif 650#endif
613#if EV_USEV_POLL 651#if EV_USE_POLL
614 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 652 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
615#endif 653#endif
616#if EV_USE_SELECT 654#if EV_USE_SELECT
617 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 655 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
618#endif 656#endif
639{ 677{
640 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));
641 679
642 loop_init (EV_A_ methods); 680 loop_init (EV_A_ methods);
643 681
644 if (ev_methods (EV_A)) 682 if (ev_method (EV_A))
645 return loop; 683 return loop;
646 684
647 return 0; 685 return 0;
648} 686}
649 687
726 764
727 loop_destroy (EV_A); 765 loop_destroy (EV_A);
728} 766}
729 767
730void 768void
731ev_default_fork (EV_P) 769ev_default_fork (void)
732{ 770{
771#if EV_MULTIPLICITY
772 struct ev_loop *loop = default_loop;
773#endif
774
733 loop_fork (EV_A); 775 loop_fork (EV_A);
734 776
735 ev_io_stop (EV_A_ &sigev); 777 ev_io_stop (EV_A_ &sigev);
736 close (sigpipe [0]); 778 close (sigpipe [0]);
737 close (sigpipe [1]); 779 close (sigpipe [1]);
765timers_reify (EV_P) 807timers_reify (EV_P)
766{ 808{
767 while (timercnt && timers [0]->at <= mn_now) 809 while (timercnt && timers [0]->at <= mn_now)
768 { 810 {
769 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)));
770 814
771 /* first reschedule or stop timer */ 815 /* first reschedule or stop timer */
772 if (w->repeat) 816 if (w->repeat)
773 { 817 {
774 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.));
787{ 831{
788 while (periodiccnt && periodics [0]->at <= rt_now) 832 while (periodiccnt && periodics [0]->at <= rt_now)
789 { 833 {
790 struct ev_periodic *w = periodics [0]; 834 struct ev_periodic *w = periodics [0];
791 835
836 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
837
792 /* first reschedule or stop timer */ 838 /* first reschedule or stop timer */
793 if (w->interval) 839 if (w->interval)
794 { 840 {
795 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;
796 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));
1086 1132
1087 ev_start (EV_A_ (W)w, ++timercnt); 1133 ev_start (EV_A_ (W)w, ++timercnt);
1088 array_needsize (timers, timermax, timercnt, ); 1134 array_needsize (timers, timermax, timercnt, );
1089 timers [timercnt - 1] = w; 1135 timers [timercnt - 1] = w;
1090 upheap ((WT *)timers, timercnt - 1); 1136 upheap ((WT *)timers, timercnt - 1);
1137
1138 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1091} 1139}
1092 1140
1093void 1141void
1094ev_timer_stop (EV_P_ struct ev_timer *w) 1142ev_timer_stop (EV_P_ struct ev_timer *w)
1095{ 1143{
1096 ev_clear_pending (EV_A_ (W)w); 1144 ev_clear_pending (EV_A_ (W)w);
1097 if (!ev_is_active (w)) 1145 if (!ev_is_active (w))
1098 return; 1146 return;
1099 1147
1148 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1149
1100 if (w->active < timercnt--) 1150 if (((W)w)->active < timercnt--)
1101 { 1151 {
1102 timers [w->active - 1] = timers [timercnt]; 1152 timers [((W)w)->active - 1] = timers [timercnt];
1103 downheap ((WT *)timers, timercnt, w->active - 1); 1153 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1104 } 1154 }
1105 1155
1106 w->at = w->repeat; 1156 w->at = w->repeat;
1107 1157
1108 ev_stop (EV_A_ (W)w); 1158 ev_stop (EV_A_ (W)w);
1114 if (ev_is_active (w)) 1164 if (ev_is_active (w))
1115 { 1165 {
1116 if (w->repeat) 1166 if (w->repeat)
1117 { 1167 {
1118 w->at = mn_now + w->repeat; 1168 w->at = mn_now + w->repeat;
1119 downheap ((WT *)timers, timercnt, w->active - 1); 1169 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1120 } 1170 }
1121 else 1171 else
1122 ev_timer_stop (EV_A_ w); 1172 ev_timer_stop (EV_A_ w);
1123 } 1173 }
1124 else if (w->repeat) 1174 else if (w->repeat)
1139 1189
1140 ev_start (EV_A_ (W)w, ++periodiccnt); 1190 ev_start (EV_A_ (W)w, ++periodiccnt);
1141 array_needsize (periodics, periodicmax, periodiccnt, ); 1191 array_needsize (periodics, periodicmax, periodiccnt, );
1142 periodics [periodiccnt - 1] = w; 1192 periodics [periodiccnt - 1] = w;
1143 upheap ((WT *)periodics, periodiccnt - 1); 1193 upheap ((WT *)periodics, periodiccnt - 1);
1194
1195 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1144} 1196}
1145 1197
1146void 1198void
1147ev_periodic_stop (EV_P_ struct ev_periodic *w) 1199ev_periodic_stop (EV_P_ struct ev_periodic *w)
1148{ 1200{
1149 ev_clear_pending (EV_A_ (W)w); 1201 ev_clear_pending (EV_A_ (W)w);
1150 if (!ev_is_active (w)) 1202 if (!ev_is_active (w))
1151 return; 1203 return;
1152 1204
1205 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1206
1153 if (w->active < periodiccnt--) 1207 if (((W)w)->active < periodiccnt--)
1154 { 1208 {
1155 periodics [w->active - 1] = periodics [periodiccnt]; 1209 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1156 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1210 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1157 } 1211 }
1158 1212
1159 ev_stop (EV_A_ (W)w); 1213 ev_stop (EV_A_ (W)w);
1160} 1214}
1161 1215
1175{ 1229{
1176 ev_clear_pending (EV_A_ (W)w); 1230 ev_clear_pending (EV_A_ (W)w);
1177 if (ev_is_active (w)) 1231 if (ev_is_active (w))
1178 return; 1232 return;
1179 1233
1180 idles [w->active - 1] = idles [--idlecnt]; 1234 idles [((W)w)->active - 1] = idles [--idlecnt];
1181 ev_stop (EV_A_ (W)w); 1235 ev_stop (EV_A_ (W)w);
1182} 1236}
1183 1237
1184void 1238void
1185ev_prepare_start (EV_P_ struct ev_prepare *w) 1239ev_prepare_start (EV_P_ struct ev_prepare *w)
1197{ 1251{
1198 ev_clear_pending (EV_A_ (W)w); 1252 ev_clear_pending (EV_A_ (W)w);
1199 if (ev_is_active (w)) 1253 if (ev_is_active (w))
1200 return; 1254 return;
1201 1255
1202 prepares [w->active - 1] = prepares [--preparecnt]; 1256 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1203 ev_stop (EV_A_ (W)w); 1257 ev_stop (EV_A_ (W)w);
1204} 1258}
1205 1259
1206void 1260void
1207ev_check_start (EV_P_ struct ev_check *w) 1261ev_check_start (EV_P_ struct ev_check *w)
1219{ 1273{
1220 ev_clear_pending (EV_A_ (W)w); 1274 ev_clear_pending (EV_A_ (W)w);
1221 if (ev_is_active (w)) 1275 if (ev_is_active (w))
1222 return; 1276 return;
1223 1277
1224 checks [w->active - 1] = checks [--checkcnt]; 1278 checks [((W)w)->active - 1] = checks [--checkcnt];
1225 ev_stop (EV_A_ (W)w); 1279 ev_stop (EV_A_ (W)w);
1226} 1280}
1227 1281
1228#ifndef SA_RESTART 1282#ifndef SA_RESTART
1229# define SA_RESTART 0 1283# define SA_RESTART 0

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines