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

Comparing libev/ev.c (file contents):
Revision 1.53 by root, Sat Nov 3 22:31:11 2007 UTC vs.
Revision 1.57 by root, Sun Nov 4 16:43:53 2007 UTC

113 113
114typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
115typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
116typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
117 117
118static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
119
118/*****************************************************************************/ 120/*****************************************************************************/
119 121
120typedef struct 122typedef struct
121{ 123{
122 struct ev_watcher_list *head; 124 struct ev_watcher_list *head;
128{ 130{
129 W w; 131 W w;
130 int events; 132 int events;
131} ANPENDING; 133} ANPENDING;
132 134
133#ifdef EV_MULTIPLICITY 135#if EV_MULTIPLICITY
136
134struct ev_loop 137struct ev_loop
135{ 138{
136# define VAR(name,decl) decl 139# define VAR(name,decl) decl;
137# include "ev_vars.h" 140# include "ev_vars.h"
138}; 141};
142# undef VAR
143# include "ev_wrap.h"
144
139#else 145#else
146
140# define VAR(name,decl) static decl 147# define VAR(name,decl) static decl;
141# include "ev_vars.h" 148# include "ev_vars.h"
142#endif
143#undef VAR 149# undef VAR
150
151#endif
144 152
145/*****************************************************************************/ 153/*****************************************************************************/
146 154
147inline ev_tstamp 155inline ev_tstamp
148ev_time (void) 156ev_time (void)
330 fd_kill (EV_A_ fd); 338 fd_kill (EV_A_ fd);
331 return; 339 return;
332 } 340 }
333} 341}
334 342
343/* susually called after fork if method needs to re-arm all fds from scratch */
344static void
345fd_rearm_all (EV_P)
346{
347 int fd;
348
349 /* this should be highly optimised to not do anything but set a flag */
350 for (fd = 0; fd < anfdmax; ++fd)
351 if (anfds [fd].events)
352 {
353 anfds [fd].events = 0;
354 fd_change (fd);
355 }
356}
357
335/*****************************************************************************/ 358/*****************************************************************************/
336 359
337static void 360static void
338upheap (WT *timers, int k) 361upheap (WT *heap, int k)
339{ 362{
340 WT w = timers [k]; 363 WT w = heap [k];
341 364
342 while (k && timers [k >> 1]->at > w->at) 365 while (k && heap [k >> 1]->at > w->at)
343 { 366 {
344 timers [k] = timers [k >> 1]; 367 heap [k] = heap [k >> 1];
345 timers [k]->active = k + 1; 368 heap [k]->active = k + 1;
346 k >>= 1; 369 k >>= 1;
347 } 370 }
348 371
349 timers [k] = w; 372 heap [k] = w;
350 timers [k]->active = k + 1; 373 heap [k]->active = k + 1;
351 374
352} 375}
353 376
354static void 377static void
355downheap (WT *timers, int N, int k) 378downheap (WT *heap, int N, int k)
356{ 379{
357 WT w = timers [k]; 380 WT w = heap [k];
358 381
359 while (k < (N >> 1)) 382 while (k < (N >> 1))
360 { 383 {
361 int j = k << 1; 384 int j = k << 1;
362 385
363 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 386 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
364 ++j; 387 ++j;
365 388
366 if (w->at <= timers [j]->at) 389 if (w->at <= heap [j]->at)
367 break; 390 break;
368 391
369 timers [k] = timers [j]; 392 heap [k] = heap [j];
370 timers [k]->active = k + 1; 393 heap [k]->active = k + 1;
371 k = j; 394 k = j;
372 } 395 }
373 396
374 timers [k] = w; 397 heap [k] = w;
375 timers [k]->active = k + 1; 398 heap [k]->active = k + 1;
376} 399}
377 400
378/*****************************************************************************/ 401/*****************************************************************************/
379 402
380typedef struct 403typedef struct
445 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 468 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
446 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 469 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
447#endif 470#endif
448 471
449 ev_io_set (&sigev, sigpipe [0], EV_READ); 472 ev_io_set (&sigev, sigpipe [0], EV_READ);
450 ev_io_start (&sigev); 473 ev_io_start (EV_A_ &sigev);
451 ev_unref (EV_A); /* child watcher should not keep loop alive */ 474 ev_unref (EV_A); /* child watcher should not keep loop alive */
452} 475}
453 476
454/*****************************************************************************/ 477/*****************************************************************************/
455 478
534ev_method (EV_P) 557ev_method (EV_P)
535{ 558{
536 return method; 559 return method;
537} 560}
538 561
539int 562static void
540ev_init (EV_P_ int methods) 563loop_init (EV_P_ int methods)
541{ 564{
542#ifdef EV_MULTIPLICITY
543 memset (loop, 0, sizeof (struct ev_loop));
544#endif
545
546 if (!method) 565 if (!method)
547 { 566 {
548#if EV_USE_MONOTONIC 567#if EV_USE_MONOTONIC
549 { 568 {
550 struct timespec ts; 569 struct timespec ts;
554#endif 573#endif
555 574
556 rt_now = ev_time (); 575 rt_now = ev_time ();
557 mn_now = get_clock (); 576 mn_now = get_clock ();
558 now_floor = mn_now; 577 now_floor = mn_now;
559 diff = rt_now - mn_now; 578 rtmn_diff = rt_now - mn_now;
560
561 if (pipe (sigpipe))
562 return 0;
563 579
564 if (methods == EVMETHOD_AUTO) 580 if (methods == EVMETHOD_AUTO)
565 if (!enable_secure () && getenv ("LIBmethodS")) 581 if (!enable_secure () && getenv ("LIBEV_METHODS"))
566 methods = atoi (getenv ("LIBmethodS")); 582 methods = atoi (getenv ("LIBEV_METHODS"));
567 else 583 else
568 methods = EVMETHOD_ANY; 584 methods = EVMETHOD_ANY;
569 585
570 method = 0; 586 method = 0;
571#if EV_USE_KQUEUE 587#if EV_USE_KQUEUE
578 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 594 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
579#endif 595#endif
580#if EV_USE_SELECT 596#if EV_USE_SELECT
581 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 597 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
582#endif 598#endif
599 }
600}
583 601
602void
603loop_destroy (EV_P)
604{
605#if EV_USE_KQUEUE
606 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
607#endif
608#if EV_USE_EPOLL
609 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
610#endif
611#if EV_USEV_POLL
612 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
613#endif
614#if EV_USE_SELECT
615 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
616#endif
617
618 method = 0;
619 /*TODO*/
620}
621
622void
623loop_fork (EV_P)
624{
625 /*TODO*/
626#if EV_USE_EPOLL
627 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
628#endif
629#if EV_USE_KQUEUE
630 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
631#endif
632}
633
634#if EV_MULTIPLICITY
635struct ev_loop *
636ev_loop_new (int methods)
637{
638 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
639
640 loop_init (EV_A_ methods);
641
642 if (ev_methods (EV_A))
643 return loop;
644
645 return 0;
646}
647
648void
649ev_loop_destroy (EV_P)
650{
651 loop_destroy (EV_A);
652 free (loop);
653}
654
655void
656ev_loop_fork (EV_P)
657{
658 loop_fork (EV_A);
659}
660
661#endif
662
663#if EV_MULTIPLICITY
664struct ev_loop default_loop_struct;
665static struct ev_loop *default_loop;
666
667struct ev_loop *
668#else
669static int default_loop;
670
671int
672#endif
673ev_default_loop (int methods)
674{
675 if (sigpipe [0] == sigpipe [1])
676 if (pipe (sigpipe))
677 return 0;
678
679 if (!default_loop)
680 {
681#if EV_MULTIPLICITY
682 struct ev_loop *loop = default_loop = &default_loop_struct;
683#else
684 default_loop = 1;
685#endif
686
687 loop_init (EV_A_ methods);
688
584 if (method) 689 if (ev_method (EV_A))
585 { 690 {
586 ev_watcher_init (&sigev, sigcb); 691 ev_watcher_init (&sigev, sigcb);
587 ev_set_priority (&sigev, EV_MAXPRI); 692 ev_set_priority (&sigev, EV_MAXPRI);
588 siginit (EV_A); 693 siginit (EV_A);
589 694
592 ev_set_priority (&childev, EV_MAXPRI); 697 ev_set_priority (&childev, EV_MAXPRI);
593 ev_signal_start (EV_A_ &childev); 698 ev_signal_start (EV_A_ &childev);
594 ev_unref (EV_A); /* child watcher should not keep loop alive */ 699 ev_unref (EV_A); /* child watcher should not keep loop alive */
595#endif 700#endif
596 } 701 }
702 else
703 default_loop = 0;
597 } 704 }
598 705
599 return method; 706 return default_loop;
600} 707}
601 708
602/*****************************************************************************/
603
604void 709void
605ev_fork_prepare (void) 710ev_default_destroy (void)
606{ 711{
607 /* nop */ 712#if EV_MULTIPLICITY
608} 713 struct ev_loop *loop = default_loop;
609
610void
611ev_fork_parent (void)
612{
613 /* nop */
614}
615
616void
617ev_fork_child (void)
618{
619#if EV_USE_EPOLL
620 if (method == EVMETHOD_EPOLL)
621 epoll_postfork_child ();
622#endif 714#endif
623 715
716 ev_ref (EV_A); /* child watcher */
717 ev_signal_stop (EV_A_ &childev);
718
719 ev_ref (EV_A); /* signal watcher */
624 ev_io_stop (&sigev); 720 ev_io_stop (EV_A_ &sigev);
721
722 close (sigpipe [0]); sigpipe [0] = 0;
723 close (sigpipe [1]); sigpipe [1] = 0;
724
725 loop_destroy (EV_A);
726}
727
728void
729ev_default_fork (EV_P)
730{
731 loop_fork (EV_A);
732
733 ev_io_stop (EV_A_ &sigev);
625 close (sigpipe [0]); 734 close (sigpipe [0]);
626 close (sigpipe [1]); 735 close (sigpipe [1]);
627 pipe (sigpipe); 736 pipe (sigpipe);
737
738 ev_ref (EV_A); /* signal watcher */
628 siginit (); 739 siginit (EV_A);
629} 740}
630 741
631/*****************************************************************************/ 742/*****************************************************************************/
632 743
633static void 744static void
663 downheap ((WT *)timers, timercnt, 0); 774 downheap ((WT *)timers, timercnt, 0);
664 } 775 }
665 else 776 else
666 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 777 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
667 778
668 event ((W)w, EV_TIMEOUT); 779 event (EV_A_ (W)w, EV_TIMEOUT);
669 } 780 }
670} 781}
671 782
672static void 783static void
673periodics_reify (EV_P) 784periodics_reify (EV_P)
689 event (EV_A_ (W)w, EV_PERIODIC); 800 event (EV_A_ (W)w, EV_PERIODIC);
690 } 801 }
691} 802}
692 803
693static void 804static void
694periodics_reschedule (EV_P_ ev_tstamp diff) 805periodics_reschedule (EV_P)
695{ 806{
696 int i; 807 int i;
697 808
698 /* adjust periodics after time jump */ 809 /* adjust periodics after time jump */
699 for (i = 0; i < periodiccnt; ++i) 810 for (i = 0; i < periodiccnt; ++i)
720{ 831{
721 mn_now = get_clock (); 832 mn_now = get_clock ();
722 833
723 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5)) 834 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
724 { 835 {
725 rt_now = mn_now + diff; 836 rt_now = rtmn_diff + mn_now;
726 return 0; 837 return 0;
727 } 838 }
728 else 839 else
729 { 840 {
730 now_floor = mn_now; 841 now_floor = mn_now;
741#if EV_USE_MONOTONIC 852#if EV_USE_MONOTONIC
742 if (expect_true (have_monotonic)) 853 if (expect_true (have_monotonic))
743 { 854 {
744 if (time_update_monotonic (EV_A)) 855 if (time_update_monotonic (EV_A))
745 { 856 {
746 ev_tstamp odiff = diff; 857 ev_tstamp odiff = rtmn_diff;
747 858
748 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 859 for (i = 4; --i; ) /* loop a few times, before making important decisions */
749 { 860 {
750 diff = rt_now - mn_now; 861 rtmn_diff = rt_now - mn_now;
751 862
752 if (fabs (odiff - diff) < MIN_TIMEJUMP) 863 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
753 return; /* all is well */ 864 return; /* all is well */
754 865
755 rt_now = ev_time (); 866 rt_now = ev_time ();
756 mn_now = get_clock (); 867 mn_now = get_clock ();
757 now_floor = mn_now; 868 now_floor = mn_now;
758 } 869 }
759 870
760 periodics_reschedule (EV_A_ diff - odiff); 871 periodics_reschedule (EV_A);
761 /* no timer adjustment, as the monotonic clock doesn't jump */ 872 /* no timer adjustment, as the monotonic clock doesn't jump */
873 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
762 } 874 }
763 } 875 }
764 else 876 else
765#endif 877#endif
766 { 878 {
767 rt_now = ev_time (); 879 rt_now = ev_time ();
768 880
769 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 881 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
770 { 882 {
771 periodics_reschedule (EV_A_ rt_now - mn_now); 883 periodics_reschedule (EV_A);
772 884
773 /* adjust timers. this is easy, as the offset is the same for all */ 885 /* adjust timers. this is easy, as the offset is the same for all */
774 for (i = 0; i < timercnt; ++i) 886 for (i = 0; i < timercnt; ++i)
775 timers [i]->at += diff; 887 timers [i]->at += rt_now - mn_now;
776 } 888 }
777 889
778 mn_now = rt_now; 890 mn_now = rt_now;
779 } 891 }
780} 892}
1043 } 1155 }
1044 1156
1045 ev_stop (EV_A_ (W)w); 1157 ev_stop (EV_A_ (W)w);
1046} 1158}
1047 1159
1160void
1161ev_idle_start (EV_P_ struct ev_idle *w)
1162{
1163 if (ev_is_active (w))
1164 return;
1165
1166 ev_start (EV_A_ (W)w, ++idlecnt);
1167 array_needsize (idles, idlemax, idlecnt, );
1168 idles [idlecnt - 1] = w;
1169}
1170
1171void
1172ev_idle_stop (EV_P_ struct ev_idle *w)
1173{
1174 ev_clear_pending (EV_A_ (W)w);
1175 if (ev_is_active (w))
1176 return;
1177
1178 idles [w->active - 1] = idles [--idlecnt];
1179 ev_stop (EV_A_ (W)w);
1180}
1181
1182void
1183ev_prepare_start (EV_P_ struct ev_prepare *w)
1184{
1185 if (ev_is_active (w))
1186 return;
1187
1188 ev_start (EV_A_ (W)w, ++preparecnt);
1189 array_needsize (prepares, preparemax, preparecnt, );
1190 prepares [preparecnt - 1] = w;
1191}
1192
1193void
1194ev_prepare_stop (EV_P_ struct ev_prepare *w)
1195{
1196 ev_clear_pending (EV_A_ (W)w);
1197 if (ev_is_active (w))
1198 return;
1199
1200 prepares [w->active - 1] = prepares [--preparecnt];
1201 ev_stop (EV_A_ (W)w);
1202}
1203
1204void
1205ev_check_start (EV_P_ struct ev_check *w)
1206{
1207 if (ev_is_active (w))
1208 return;
1209
1210 ev_start (EV_A_ (W)w, ++checkcnt);
1211 array_needsize (checks, checkmax, checkcnt, );
1212 checks [checkcnt - 1] = w;
1213}
1214
1215void
1216ev_check_stop (EV_P_ struct ev_check *w)
1217{
1218 ev_clear_pending (EV_A_ (W)w);
1219 if (ev_is_active (w))
1220 return;
1221
1222 checks [w->active - 1] = checks [--checkcnt];
1223 ev_stop (EV_A_ (W)w);
1224}
1225
1048#ifndef SA_RESTART 1226#ifndef SA_RESTART
1049# define SA_RESTART 0 1227# define SA_RESTART 0
1050#endif 1228#endif
1051 1229
1052void 1230void
1053ev_signal_start (EV_P_ struct ev_signal *w) 1231ev_signal_start (EV_P_ struct ev_signal *w)
1054{ 1232{
1233#if EV_MULTIPLICITY
1234 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1235#endif
1055 if (ev_is_active (w)) 1236 if (ev_is_active (w))
1056 return; 1237 return;
1057 1238
1058 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1239 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1059 1240
1084 if (!signals [w->signum - 1].head) 1265 if (!signals [w->signum - 1].head)
1085 signal (w->signum, SIG_DFL); 1266 signal (w->signum, SIG_DFL);
1086} 1267}
1087 1268
1088void 1269void
1089ev_idle_start (EV_P_ struct ev_idle *w)
1090{
1091 if (ev_is_active (w))
1092 return;
1093
1094 ev_start (EV_A_ (W)w, ++idlecnt);
1095 array_needsize (idles, idlemax, idlecnt, );
1096 idles [idlecnt - 1] = w;
1097}
1098
1099void
1100ev_idle_stop (EV_P_ struct ev_idle *w)
1101{
1102 ev_clear_pending (EV_A_ (W)w);
1103 if (ev_is_active (w))
1104 return;
1105
1106 idles [w->active - 1] = idles [--idlecnt];
1107 ev_stop (EV_A_ (W)w);
1108}
1109
1110void
1111ev_prepare_start (EV_P_ struct ev_prepare *w)
1112{
1113 if (ev_is_active (w))
1114 return;
1115
1116 ev_start (EV_A_ (W)w, ++preparecnt);
1117 array_needsize (prepares, preparemax, preparecnt, );
1118 prepares [preparecnt - 1] = w;
1119}
1120
1121void
1122ev_prepare_stop (EV_P_ struct ev_prepare *w)
1123{
1124 ev_clear_pending (EV_A_ (W)w);
1125 if (ev_is_active (w))
1126 return;
1127
1128 prepares [w->active - 1] = prepares [--preparecnt];
1129 ev_stop (EV_A_ (W)w);
1130}
1131
1132void
1133ev_check_start (EV_P_ struct ev_check *w)
1134{
1135 if (ev_is_active (w))
1136 return;
1137
1138 ev_start (EV_A_ (W)w, ++checkcnt);
1139 array_needsize (checks, checkmax, checkcnt, );
1140 checks [checkcnt - 1] = w;
1141}
1142
1143void
1144ev_check_stop (EV_P_ struct ev_check *w)
1145{
1146 ev_clear_pending (EV_A_ (W)w);
1147 if (ev_is_active (w))
1148 return;
1149
1150 checks [w->active - 1] = checks [--checkcnt];
1151 ev_stop (EV_A_ (W)w);
1152}
1153
1154void
1155ev_child_start (EV_P_ struct ev_child *w) 1270ev_child_start (EV_P_ struct ev_child *w)
1156{ 1271{
1272#if EV_MULTIPLICITY
1273 assert (("child watchers are only supported in the default loop", loop == default_loop));
1274#endif
1157 if (ev_is_active (w)) 1275 if (ev_is_active (w))
1158 return; 1276 return;
1159 1277
1160 ev_start (EV_A_ (W)w, 1); 1278 ev_start (EV_A_ (W)w, 1);
1161 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1279 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1233 ev_timer_start (EV_A_ &once->to); 1351 ev_timer_start (EV_A_ &once->to);
1234 } 1352 }
1235 } 1353 }
1236} 1354}
1237 1355
1238/*****************************************************************************/
1239
1240#if 0
1241
1242struct ev_io wio;
1243
1244static void
1245sin_cb (struct ev_io *w, int revents)
1246{
1247 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1248}
1249
1250static void
1251ocb (struct ev_timer *w, int revents)
1252{
1253 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1254 ev_timer_stop (w);
1255 ev_timer_start (w);
1256}
1257
1258static void
1259scb (struct ev_signal *w, int revents)
1260{
1261 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1262 ev_io_stop (&wio);
1263 ev_io_start (&wio);
1264}
1265
1266static void
1267gcb (struct ev_signal *w, int revents)
1268{
1269 fprintf (stderr, "generic %x\n", revents);
1270
1271}
1272
1273int main (void)
1274{
1275 ev_init (0);
1276
1277 ev_io_init (&wio, sin_cb, 0, EV_READ);
1278 ev_io_start (&wio);
1279
1280 struct ev_timer t[10000];
1281
1282#if 0
1283 int i;
1284 for (i = 0; i < 10000; ++i)
1285 {
1286 struct ev_timer *w = t + i;
1287 ev_watcher_init (w, ocb, i);
1288 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1289 ev_timer_start (w);
1290 if (drand48 () < 0.5)
1291 ev_timer_stop (w);
1292 }
1293#endif
1294
1295 struct ev_timer t1;
1296 ev_timer_init (&t1, ocb, 5, 10);
1297 ev_timer_start (&t1);
1298
1299 struct ev_signal sig;
1300 ev_signal_init (&sig, scb, SIGQUIT);
1301 ev_signal_start (&sig);
1302
1303 struct ev_check cw;
1304 ev_check_init (&cw, gcb);
1305 ev_check_start (&cw);
1306
1307 struct ev_idle iw;
1308 ev_idle_init (&iw, gcb);
1309 ev_idle_start (&iw);
1310
1311 ev_loop (0);
1312
1313 return 0;
1314}
1315
1316#endif
1317
1318
1319
1320

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines