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

Comparing libev/ev.c (file contents):
Revision 1.55 by root, Sun Nov 4 00:39:24 2007 UTC vs.
Revision 1.56 by root, Sun Nov 4 15:58:49 2007 UTC

338 fd_kill (EV_A_ fd); 338 fd_kill (EV_A_ fd);
339 return; 339 return;
340 } 340 }
341} 341}
342 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
343/*****************************************************************************/ 358/*****************************************************************************/
344 359
345static void 360static void
346upheap (WT *heap, int k) 361upheap (WT *heap, int k)
347{ 362{
542ev_method (EV_P) 557ev_method (EV_P)
543{ 558{
544 return method; 559 return method;
545} 560}
546 561
547inline int 562static void
548loop_init (EV_P_ int methods) 563loop_init (EV_P_ int methods)
549{ 564{
550 if (!method) 565 if (!method)
551 { 566 {
552#if EV_USE_MONOTONIC 567#if EV_USE_MONOTONIC
560 rt_now = ev_time (); 575 rt_now = ev_time ();
561 mn_now = get_clock (); 576 mn_now = get_clock ();
562 now_floor = mn_now; 577 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 578 rtmn_diff = rt_now - mn_now;
564 579
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 580 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 581 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 582 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 583 else
572 methods = EVMETHOD_ANY; 584 methods = EVMETHOD_ANY;
573 585
574 method = 0; 586 method = 0;
575#if EV_USE_KQUEUE 587#if EV_USE_KQUEUE
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 594 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 595#endif
584#if EV_USE_SELECT 596#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 597 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 598#endif
599 }
600}
587 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
588 if (method) 689 if (ev_method (EV_A))
589 { 690 {
590 ev_watcher_init (&sigev, sigcb); 691 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI); 692 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 693 siginit (EV_A);
593 694
596 ev_set_priority (&childev, EV_MAXPRI); 697 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 698 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 699 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 700#endif
600 } 701 }
702 else
703 default_loop = 0;
601 } 704 }
602 705
603 return method; 706 return default_loop;
604} 707}
605 708
606#if EV_MULTIPLICITY
607
608struct ev_loop *
609ev_loop_new (int methods)
610{
611 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
612
613 if (loop_init (EV_A_ methods))
614 return loop;
615
616 ev_loop_delete (loop);
617
618 return 0;
619}
620
621void 709void
622ev_loop_delete (EV_P) 710ev_default_destroy (void)
623{ 711{
624 /*TODO*/ 712 struct ev_loop *loop = default_loop;
625 free (loop);
626}
627 713
628#else 714 ev_ref (EV_A); /* child watcher */
715 ev_signal_stop (EV_A_ &childev);
629 716
630int 717 ev_ref (EV_A); /* signal watcher */
631ev_init (int methods) 718 ev_io_stop (EV_A_ &sigev);
632{
633 return loop_init (methods);
634}
635 719
636#endif 720 close (sigpipe [0]); sigpipe [0] = 0;
721 close (sigpipe [1]); sigpipe [1] = 0;
637 722
638/*****************************************************************************/ 723 loop_destroy (EV_A);
639
640void
641ev_fork_prepare (void)
642{
643 /* nop */
644} 724}
645 725
646void 726void
647ev_fork_parent (void) 727ev_default_fork (EV_P)
648{ 728{
649 /* nop */ 729 loop_fork (EV_A);
650}
651
652void
653ev_fork_child (void)
654{
655 /*TODO*/
656#if !EV_MULTIPLICITY
657#if EV_USE_EPOLL
658 if (method == EVMETHOD_EPOLL)
659 epoll_postfork_child (EV_A);
660#endif
661 730
662 ev_io_stop (EV_A_ &sigev); 731 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 732 close (sigpipe [0]);
664 close (sigpipe [1]); 733 close (sigpipe [1]);
665 pipe (sigpipe); 734 pipe (sigpipe);
735
736 ev_ref (EV_A); /* signal watcher */
666 siginit (EV_A); 737 siginit (EV_A);
667#endif
668} 738}
669 739
670/*****************************************************************************/ 740/*****************************************************************************/
671 741
672static void 742static void
1083 } 1153 }
1084 1154
1085 ev_stop (EV_A_ (W)w); 1155 ev_stop (EV_A_ (W)w);
1086} 1156}
1087 1157
1158void
1159ev_idle_start (EV_P_ struct ev_idle *w)
1160{
1161 if (ev_is_active (w))
1162 return;
1163
1164 ev_start (EV_A_ (W)w, ++idlecnt);
1165 array_needsize (idles, idlemax, idlecnt, );
1166 idles [idlecnt - 1] = w;
1167}
1168
1169void
1170ev_idle_stop (EV_P_ struct ev_idle *w)
1171{
1172 ev_clear_pending (EV_A_ (W)w);
1173 if (ev_is_active (w))
1174 return;
1175
1176 idles [w->active - 1] = idles [--idlecnt];
1177 ev_stop (EV_A_ (W)w);
1178}
1179
1180void
1181ev_prepare_start (EV_P_ struct ev_prepare *w)
1182{
1183 if (ev_is_active (w))
1184 return;
1185
1186 ev_start (EV_A_ (W)w, ++preparecnt);
1187 array_needsize (prepares, preparemax, preparecnt, );
1188 prepares [preparecnt - 1] = w;
1189}
1190
1191void
1192ev_prepare_stop (EV_P_ struct ev_prepare *w)
1193{
1194 ev_clear_pending (EV_A_ (W)w);
1195 if (ev_is_active (w))
1196 return;
1197
1198 prepares [w->active - 1] = prepares [--preparecnt];
1199 ev_stop (EV_A_ (W)w);
1200}
1201
1202void
1203ev_check_start (EV_P_ struct ev_check *w)
1204{
1205 if (ev_is_active (w))
1206 return;
1207
1208 ev_start (EV_A_ (W)w, ++checkcnt);
1209 array_needsize (checks, checkmax, checkcnt, );
1210 checks [checkcnt - 1] = w;
1211}
1212
1213void
1214ev_check_stop (EV_P_ struct ev_check *w)
1215{
1216 ev_clear_pending (EV_A_ (W)w);
1217 if (ev_is_active (w))
1218 return;
1219
1220 checks [w->active - 1] = checks [--checkcnt];
1221 ev_stop (EV_A_ (W)w);
1222}
1223
1088#ifndef SA_RESTART 1224#ifndef SA_RESTART
1089# define SA_RESTART 0 1225# define SA_RESTART 0
1090#endif 1226#endif
1091 1227
1092void 1228void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1229ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1230{
1231#if EV_MULTIPLICITY
1232 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1233#endif
1095 if (ev_is_active (w)) 1234 if (ev_is_active (w))
1096 return; 1235 return;
1097 1236
1098 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1237 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1099 1238
1124 if (!signals [w->signum - 1].head) 1263 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1264 signal (w->signum, SIG_DFL);
1126} 1265}
1127 1266
1128void 1267void
1129ev_idle_start (EV_P_ struct ev_idle *w)
1130{
1131 if (ev_is_active (w))
1132 return;
1133
1134 ev_start (EV_A_ (W)w, ++idlecnt);
1135 array_needsize (idles, idlemax, idlecnt, );
1136 idles [idlecnt - 1] = w;
1137}
1138
1139void
1140ev_idle_stop (EV_P_ struct ev_idle *w)
1141{
1142 ev_clear_pending (EV_A_ (W)w);
1143 if (ev_is_active (w))
1144 return;
1145
1146 idles [w->active - 1] = idles [--idlecnt];
1147 ev_stop (EV_A_ (W)w);
1148}
1149
1150void
1151ev_prepare_start (EV_P_ struct ev_prepare *w)
1152{
1153 if (ev_is_active (w))
1154 return;
1155
1156 ev_start (EV_A_ (W)w, ++preparecnt);
1157 array_needsize (prepares, preparemax, preparecnt, );
1158 prepares [preparecnt - 1] = w;
1159}
1160
1161void
1162ev_prepare_stop (EV_P_ struct ev_prepare *w)
1163{
1164 ev_clear_pending (EV_A_ (W)w);
1165 if (ev_is_active (w))
1166 return;
1167
1168 prepares [w->active - 1] = prepares [--preparecnt];
1169 ev_stop (EV_A_ (W)w);
1170}
1171
1172void
1173ev_check_start (EV_P_ struct ev_check *w)
1174{
1175 if (ev_is_active (w))
1176 return;
1177
1178 ev_start (EV_A_ (W)w, ++checkcnt);
1179 array_needsize (checks, checkmax, checkcnt, );
1180 checks [checkcnt - 1] = w;
1181}
1182
1183void
1184ev_check_stop (EV_P_ struct ev_check *w)
1185{
1186 ev_clear_pending (EV_A_ (W)w);
1187 if (ev_is_active (w))
1188 return;
1189
1190 checks [w->active - 1] = checks [--checkcnt];
1191 ev_stop (EV_A_ (W)w);
1192}
1193
1194void
1195ev_child_start (EV_P_ struct ev_child *w) 1268ev_child_start (EV_P_ struct ev_child *w)
1196{ 1269{
1270#if EV_MULTIPLICITY
1271 assert (("child watchers are only supported in the default loop", loop == default_loop));
1272#endif
1197 if (ev_is_active (w)) 1273 if (ev_is_active (w))
1198 return; 1274 return;
1199 1275
1200 ev_start (EV_A_ (W)w, 1); 1276 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1277 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1273 ev_timer_start (EV_A_ &once->to); 1349 ev_timer_start (EV_A_ &once->to);
1274 } 1350 }
1275 } 1351 }
1276} 1352}
1277 1353
1278/*****************************************************************************/
1279
1280#if 0
1281
1282struct ev_io wio;
1283
1284static void
1285sin_cb (struct ev_io *w, int revents)
1286{
1287 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1288}
1289
1290static void
1291ocb (struct ev_timer *w, int revents)
1292{
1293 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1294 ev_timer_stop (w);
1295 ev_timer_start (w);
1296}
1297
1298static void
1299scb (struct ev_signal *w, int revents)
1300{
1301 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1302 ev_io_stop (&wio);
1303 ev_io_start (&wio);
1304}
1305
1306static void
1307gcb (struct ev_signal *w, int revents)
1308{
1309 fprintf (stderr, "generic %x\n", revents);
1310
1311}
1312
1313int main (void)
1314{
1315 ev_init (0);
1316
1317 ev_io_init (&wio, sin_cb, 0, EV_READ);
1318 ev_io_start (&wio);
1319
1320 struct ev_timer t[10000];
1321
1322#if 0
1323 int i;
1324 for (i = 0; i < 10000; ++i)
1325 {
1326 struct ev_timer *w = t + i;
1327 ev_watcher_init (w, ocb, i);
1328 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1329 ev_timer_start (w);
1330 if (drand48 () < 0.5)
1331 ev_timer_stop (w);
1332 }
1333#endif
1334
1335 struct ev_timer t1;
1336 ev_timer_init (&t1, ocb, 5, 10);
1337 ev_timer_start (&t1);
1338
1339 struct ev_signal sig;
1340 ev_signal_init (&sig, scb, SIGQUIT);
1341 ev_signal_start (&sig);
1342
1343 struct ev_check cw;
1344 ev_check_init (&cw, gcb);
1345 ev_check_start (&cw);
1346
1347 struct ev_idle iw;
1348 ev_idle_init (&iw, gcb);
1349 ev_idle_start (&iw);
1350
1351 ev_loop (0);
1352
1353 return 0;
1354}
1355
1356#endif
1357
1358
1359
1360

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines