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

Comparing libev/ev.c (file contents):
Revision 1.59 by root, Sun Nov 4 18:15:16 2007 UTC vs.
Revision 1.64 by root, Sun Nov 4 23:14:11 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>
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
276 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 306 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
277 events |= w->events; 307 events |= w->events;
278 308
279 anfd->reify = 0; 309 anfd->reify = 0;
280 310
281 if (anfd->events != events)
282 {
283 method_modify (EV_A_ fd, anfd->events, events); 311 method_modify (EV_A_ fd, anfd->events, events);
284 anfd->events = events; 312 anfd->events = events;
285 }
286 } 313 }
287 314
288 fdchangecnt = 0; 315 fdchangecnt = 0;
289} 316}
290 317
327 354
328/* called on ENOMEM in select/poll to kill some fds and retry */ 355/* called on ENOMEM in select/poll to kill some fds and retry */
329static void 356static void
330fd_enomem (EV_P) 357fd_enomem (EV_P)
331{ 358{
332 int fd = anfdmax; 359 int fd;
333 360
334 while (fd--) 361 for (fd = anfdmax; fd--; )
335 if (anfds [fd].events) 362 if (anfds [fd].events)
336 { 363 {
337 close (fd); 364 close (fd);
338 fd_kill (EV_A_ fd); 365 fd_kill (EV_A_ fd);
339 return; 366 return;
349 /* this should be highly optimised to not do anything but set a flag */ 376 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd) 377 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events) 378 if (anfds [fd].events)
352 { 379 {
353 anfds [fd].events = 0; 380 anfds [fd].events = 0;
354 fd_change (fd); 381 fd_change (EV_A_ fd);
355 } 382 }
356} 383}
357 384
358/*****************************************************************************/ 385/*****************************************************************************/
359 386
363 WT w = heap [k]; 390 WT w = heap [k];
364 391
365 while (k && heap [k >> 1]->at > w->at) 392 while (k && heap [k >> 1]->at > w->at)
366 { 393 {
367 heap [k] = heap [k >> 1]; 394 heap [k] = heap [k >> 1];
368 heap [k]->active = k + 1; 395 ((W)heap [k])->active = k + 1;
369 k >>= 1; 396 k >>= 1;
370 } 397 }
371 398
372 heap [k] = w; 399 heap [k] = w;
373 heap [k]->active = k + 1; 400 ((W)heap [k])->active = k + 1;
374 401
375} 402}
376 403
377static void 404static void
378downheap (WT *heap, int N, int k) 405downheap (WT *heap, int N, int k)
388 415
389 if (w->at <= heap [j]->at) 416 if (w->at <= heap [j]->at)
390 break; 417 break;
391 418
392 heap [k] = heap [j]; 419 heap [k] = heap [j];
393 heap [k]->active = k + 1; 420 ((W)heap [k])->active = k + 1;
394 k = j; 421 k = j;
395 } 422 }
396 423
397 heap [k] = w; 424 heap [k] = w;
398 heap [k]->active = k + 1; 425 ((W)heap [k])->active = k + 1;
399} 426}
400 427
401/*****************************************************************************/ 428/*****************************************************************************/
402 429
403typedef struct 430typedef struct
492 struct ev_child *w; 519 struct ev_child *w;
493 520
494 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 521 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
495 if (w->pid == pid || !w->pid) 522 if (w->pid == pid || !w->pid)
496 { 523 {
497 w->priority = sw->priority; /* need to do it *now* */ 524 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
498 w->rpid = pid; 525 w->rpid = pid;
499 w->rstatus = status; 526 w->rstatus = status;
500 event (EV_A_ (W)w, EV_CHILD); 527 event (EV_A_ (W)w, EV_CHILD);
501 } 528 }
502} 529}
503 530
504static void 531static void
586 methods = atoi (getenv ("LIBEV_METHODS")); 613 methods = atoi (getenv ("LIBEV_METHODS"));
587 else 614 else
588 methods = EVMETHOD_ANY; 615 methods = EVMETHOD_ANY;
589 616
590 method = 0; 617 method = 0;
618#if EV_USE_WIN32
619 if (!method && (methods & EVMETHOD_WIN32 )) method = win32_init (EV_A_ methods);
620#endif
591#if EV_USE_KQUEUE 621#if EV_USE_KQUEUE
592 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 622 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
593#endif 623#endif
594#if EV_USE_EPOLL 624#if EV_USE_EPOLL
595 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 625 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
604} 634}
605 635
606void 636void
607loop_destroy (EV_P) 637loop_destroy (EV_P)
608{ 638{
639#if EV_USE_WIN32
640 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
641#endif
609#if EV_USE_KQUEUE 642#if EV_USE_KQUEUE
610 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 643 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
611#endif 644#endif
612#if EV_USE_EPOLL 645#if EV_USE_EPOLL
613 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A); 646 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
641{ 674{
642 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 675 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
643 676
644 loop_init (EV_A_ methods); 677 loop_init (EV_A_ methods);
645 678
646 if (ev_methods (EV_A)) 679 if (ev_method (EV_A))
647 return loop; 680 return loop;
648 681
649 return 0; 682 return 0;
650} 683}
651 684
728 761
729 loop_destroy (EV_A); 762 loop_destroy (EV_A);
730} 763}
731 764
732void 765void
733ev_default_fork (EV_P) 766ev_default_fork (void)
734{ 767{
768#if EV_MULTIPLICITY
769 struct ev_loop *loop = default_loop;
770#endif
771
735 loop_fork (EV_A); 772 loop_fork (EV_A);
736 773
737 ev_io_stop (EV_A_ &sigev); 774 ev_io_stop (EV_A_ &sigev);
738 close (sigpipe [0]); 775 close (sigpipe [0]);
739 close (sigpipe [1]); 776 close (sigpipe [1]);
756 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 793 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
757 794
758 if (p->w) 795 if (p->w)
759 { 796 {
760 p->w->pending = 0; 797 p->w->pending = 0;
798
761 p->w->cb (EV_A_ p->w, p->events); 799 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events);
762 } 800 }
763 } 801 }
764} 802}
765 803
766static void 804static void
767timers_reify (EV_P) 805timers_reify (EV_P)
768{ 806{
769 while (timercnt && timers [0]->at <= mn_now) 807 while (timercnt && ((WT)timers [0])->at <= mn_now)
770 { 808 {
771 struct ev_timer *w = timers [0]; 809 struct ev_timer *w = timers [0];
810
811 assert (("inactive timer on timer heap detected", ev_is_active (w)));
772 812
773 /* first reschedule or stop timer */ 813 /* first reschedule or stop timer */
774 if (w->repeat) 814 if (w->repeat)
775 { 815 {
776 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 816 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
777 w->at = mn_now + w->repeat; 817 ((WT)w)->at = mn_now + w->repeat;
778 downheap ((WT *)timers, timercnt, 0); 818 downheap ((WT *)timers, timercnt, 0);
779 } 819 }
780 else 820 else
781 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 821 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
782 822
785} 825}
786 826
787static void 827static void
788periodics_reify (EV_P) 828periodics_reify (EV_P)
789{ 829{
790 while (periodiccnt && periodics [0]->at <= rt_now) 830 while (periodiccnt && ((WT)periodics [0])->at <= rt_now)
791 { 831 {
792 struct ev_periodic *w = periodics [0]; 832 struct ev_periodic *w = periodics [0];
833
834 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
793 835
794 /* first reschedule or stop timer */ 836 /* first reschedule or stop timer */
795 if (w->interval) 837 if (w->interval)
796 { 838 {
797 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 839 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
798 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 840 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
799 downheap ((WT *)periodics, periodiccnt, 0); 841 downheap ((WT *)periodics, periodiccnt, 0);
800 } 842 }
801 else 843 else
802 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */ 844 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
803 845
815 { 857 {
816 struct ev_periodic *w = periodics [i]; 858 struct ev_periodic *w = periodics [i];
817 859
818 if (w->interval) 860 if (w->interval)
819 { 861 {
820 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval; 862 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
821 863
822 if (fabs (diff) >= 1e-4) 864 if (fabs (diff) >= 1e-4)
823 { 865 {
824 ev_periodic_stop (EV_A_ w); 866 ev_periodic_stop (EV_A_ w);
825 ev_periodic_start (EV_A_ w); 867 ev_periodic_start (EV_A_ w);
886 { 928 {
887 periodics_reschedule (EV_A); 929 periodics_reschedule (EV_A);
888 930
889 /* adjust timers. this is easy, as the offset is the same for all */ 931 /* adjust timers. this is easy, as the offset is the same for all */
890 for (i = 0; i < timercnt; ++i) 932 for (i = 0; i < timercnt; ++i)
891 timers [i]->at += rt_now - mn_now; 933 ((WT)timers [i])->at += rt_now - mn_now;
892 } 934 }
893 935
894 mn_now = rt_now; 936 mn_now = rt_now;
895 } 937 }
896} 938}
947 { 989 {
948 block = MAX_BLOCKTIME; 990 block = MAX_BLOCKTIME;
949 991
950 if (timercnt) 992 if (timercnt)
951 { 993 {
952 ev_tstamp to = timers [0]->at - mn_now + method_fudge; 994 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
953 if (block > to) block = to; 995 if (block > to) block = to;
954 } 996 }
955 997
956 if (periodiccnt) 998 if (periodiccnt)
957 { 999 {
958 ev_tstamp to = periodics [0]->at - rt_now + method_fudge; 1000 ev_tstamp to = ((WT)periodics [0])->at - rt_now + method_fudge;
959 if (block > to) block = to; 1001 if (block > to) block = to;
960 } 1002 }
961 1003
962 if (block < 0.) block = 0.; 1004 if (block < 0.) block = 0.;
963 } 1005 }
1080ev_timer_start (EV_P_ struct ev_timer *w) 1122ev_timer_start (EV_P_ struct ev_timer *w)
1081{ 1123{
1082 if (ev_is_active (w)) 1124 if (ev_is_active (w))
1083 return; 1125 return;
1084 1126
1085 w->at += mn_now; 1127 ((WT)w)->at += mn_now;
1086 1128
1087 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1129 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1088 1130
1089 ev_start (EV_A_ (W)w, ++timercnt); 1131 ev_start (EV_A_ (W)w, ++timercnt);
1090 array_needsize (timers, timermax, timercnt, ); 1132 array_needsize (timers, timermax, timercnt, );
1091 timers [timercnt - 1] = w; 1133 timers [timercnt - 1] = w;
1092 upheap ((WT *)timers, timercnt - 1); 1134 upheap ((WT *)timers, timercnt - 1);
1135
1136 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1093} 1137}
1094 1138
1095void 1139void
1096ev_timer_stop (EV_P_ struct ev_timer *w) 1140ev_timer_stop (EV_P_ struct ev_timer *w)
1097{ 1141{
1098 ev_clear_pending (EV_A_ (W)w); 1142 ev_clear_pending (EV_A_ (W)w);
1099 if (!ev_is_active (w)) 1143 if (!ev_is_active (w))
1100 return; 1144 return;
1101 1145
1146 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1147
1102 if (w->active < timercnt--) 1148 if (((W)w)->active < timercnt--)
1103 { 1149 {
1104 timers [w->active - 1] = timers [timercnt]; 1150 timers [((W)w)->active - 1] = timers [timercnt];
1105 downheap ((WT *)timers, timercnt, w->active - 1); 1151 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1106 } 1152 }
1107 1153
1108 w->at = w->repeat; 1154 ((WT)w)->at = w->repeat;
1109 1155
1110 ev_stop (EV_A_ (W)w); 1156 ev_stop (EV_A_ (W)w);
1111} 1157}
1112 1158
1113void 1159void
1115{ 1161{
1116 if (ev_is_active (w)) 1162 if (ev_is_active (w))
1117 { 1163 {
1118 if (w->repeat) 1164 if (w->repeat)
1119 { 1165 {
1120 w->at = mn_now + w->repeat; 1166 ((WT)w)->at = mn_now + w->repeat;
1121 downheap ((WT *)timers, timercnt, w->active - 1); 1167 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1122 } 1168 }
1123 else 1169 else
1124 ev_timer_stop (EV_A_ w); 1170 ev_timer_stop (EV_A_ w);
1125 } 1171 }
1126 else if (w->repeat) 1172 else if (w->repeat)
1135 1181
1136 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1182 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1137 1183
1138 /* this formula differs from the one in periodic_reify because we do not always round up */ 1184 /* this formula differs from the one in periodic_reify because we do not always round up */
1139 if (w->interval) 1185 if (w->interval)
1140 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval; 1186 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1141 1187
1142 ev_start (EV_A_ (W)w, ++periodiccnt); 1188 ev_start (EV_A_ (W)w, ++periodiccnt);
1143 array_needsize (periodics, periodicmax, periodiccnt, ); 1189 array_needsize (periodics, periodicmax, periodiccnt, );
1144 periodics [periodiccnt - 1] = w; 1190 periodics [periodiccnt - 1] = w;
1145 upheap ((WT *)periodics, periodiccnt - 1); 1191 upheap ((WT *)periodics, periodiccnt - 1);
1192
1193 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1146} 1194}
1147 1195
1148void 1196void
1149ev_periodic_stop (EV_P_ struct ev_periodic *w) 1197ev_periodic_stop (EV_P_ struct ev_periodic *w)
1150{ 1198{
1151 ev_clear_pending (EV_A_ (W)w); 1199 ev_clear_pending (EV_A_ (W)w);
1152 if (!ev_is_active (w)) 1200 if (!ev_is_active (w))
1153 return; 1201 return;
1154 1202
1203 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1204
1155 if (w->active < periodiccnt--) 1205 if (((W)w)->active < periodiccnt--)
1156 { 1206 {
1157 periodics [w->active - 1] = periodics [periodiccnt]; 1207 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1158 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1208 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1159 } 1209 }
1160 1210
1161 ev_stop (EV_A_ (W)w); 1211 ev_stop (EV_A_ (W)w);
1162} 1212}
1163 1213
1177{ 1227{
1178 ev_clear_pending (EV_A_ (W)w); 1228 ev_clear_pending (EV_A_ (W)w);
1179 if (ev_is_active (w)) 1229 if (ev_is_active (w))
1180 return; 1230 return;
1181 1231
1182 idles [w->active - 1] = idles [--idlecnt]; 1232 idles [((W)w)->active - 1] = idles [--idlecnt];
1183 ev_stop (EV_A_ (W)w); 1233 ev_stop (EV_A_ (W)w);
1184} 1234}
1185 1235
1186void 1236void
1187ev_prepare_start (EV_P_ struct ev_prepare *w) 1237ev_prepare_start (EV_P_ struct ev_prepare *w)
1199{ 1249{
1200 ev_clear_pending (EV_A_ (W)w); 1250 ev_clear_pending (EV_A_ (W)w);
1201 if (ev_is_active (w)) 1251 if (ev_is_active (w))
1202 return; 1252 return;
1203 1253
1204 prepares [w->active - 1] = prepares [--preparecnt]; 1254 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1205 ev_stop (EV_A_ (W)w); 1255 ev_stop (EV_A_ (W)w);
1206} 1256}
1207 1257
1208void 1258void
1209ev_check_start (EV_P_ struct ev_check *w) 1259ev_check_start (EV_P_ struct ev_check *w)
1221{ 1271{
1222 ev_clear_pending (EV_A_ (W)w); 1272 ev_clear_pending (EV_A_ (W)w);
1223 if (ev_is_active (w)) 1273 if (ev_is_active (w))
1224 return; 1274 return;
1225 1275
1226 checks [w->active - 1] = checks [--checkcnt]; 1276 checks [((W)w)->active - 1] = checks [--checkcnt];
1227 ev_stop (EV_A_ (W)w); 1277 ev_stop (EV_A_ (W)w);
1228} 1278}
1229 1279
1230#ifndef SA_RESTART 1280#ifndef SA_RESTART
1231# define SA_RESTART 0 1281# define SA_RESTART 0
1244 1294
1245 ev_start (EV_A_ (W)w, 1); 1295 ev_start (EV_A_ (W)w, 1);
1246 array_needsize (signals, signalmax, w->signum, signals_init); 1296 array_needsize (signals, signalmax, w->signum, signals_init);
1247 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1297 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1248 1298
1249 if (!w->next) 1299 if (!((WL)w)->next)
1250 { 1300 {
1251 struct sigaction sa; 1301 struct sigaction sa;
1252 sa.sa_handler = sighandler; 1302 sa.sa_handler = sighandler;
1253 sigfillset (&sa.sa_mask); 1303 sigfillset (&sa.sa_mask);
1254 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1304 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines