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.57 by root, Sun Nov 4 16:43:53 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
709void
710ev_default_destroy (void)
711{
606#if EV_MULTIPLICITY 712#if EV_MULTIPLICITY
607 713 struct ev_loop *loop = default_loop;
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
622ev_loop_delete (EV_P)
623{
624 /*TODO*/
625 free (loop);
626}
627
628#else
629
630int
631ev_init (int methods)
632{
633 return loop_init (methods);
634}
635
636#endif 714#endif
637 715
638/*****************************************************************************/ 716 ev_ref (EV_A); /* child watcher */
717 ev_signal_stop (EV_A_ &childev);
639 718
640void 719 ev_ref (EV_A); /* signal watcher */
641ev_fork_prepare (void) 720 ev_io_stop (EV_A_ &sigev);
642{
643 /* nop */
644}
645 721
646void 722 close (sigpipe [0]); sigpipe [0] = 0;
647ev_fork_parent (void) 723 close (sigpipe [1]); sigpipe [1] = 0;
648{
649 /* nop */
650}
651 724
725 loop_destroy (EV_A);
726}
727
652void 728void
653ev_fork_child (void) 729ev_default_fork (EV_P)
654{ 730{
655 /*TODO*/ 731 loop_fork (EV_A);
656#if !EV_MULTIPLICITY
657#if EV_USE_EPOLL
658 if (method == EVMETHOD_EPOLL)
659 epoll_postfork_child (EV_A);
660#endif
661 732
662 ev_io_stop (EV_A_ &sigev); 733 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 734 close (sigpipe [0]);
664 close (sigpipe [1]); 735 close (sigpipe [1]);
665 pipe (sigpipe); 736 pipe (sigpipe);
737
738 ev_ref (EV_A); /* signal watcher */
666 siginit (EV_A); 739 siginit (EV_A);
667#endif
668} 740}
669 741
670/*****************************************************************************/ 742/*****************************************************************************/
671 743
672static void 744static void
1083 } 1155 }
1084 1156
1085 ev_stop (EV_A_ (W)w); 1157 ev_stop (EV_A_ (W)w);
1086} 1158}
1087 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
1088#ifndef SA_RESTART 1226#ifndef SA_RESTART
1089# define SA_RESTART 0 1227# define SA_RESTART 0
1090#endif 1228#endif
1091 1229
1092void 1230void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1231ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1232{
1233#if EV_MULTIPLICITY
1234 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1235#endif
1095 if (ev_is_active (w)) 1236 if (ev_is_active (w))
1096 return; 1237 return;
1097 1238
1098 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));
1099 1240
1124 if (!signals [w->signum - 1].head) 1265 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1266 signal (w->signum, SIG_DFL);
1126} 1267}
1127 1268
1128void 1269void
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) 1270ev_child_start (EV_P_ struct ev_child *w)
1196{ 1271{
1272#if EV_MULTIPLICITY
1273 assert (("child watchers are only supported in the default loop", loop == default_loop));
1274#endif
1197 if (ev_is_active (w)) 1275 if (ev_is_active (w))
1198 return; 1276 return;
1199 1277
1200 ev_start (EV_A_ (W)w, 1); 1278 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1279 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1273 ev_timer_start (EV_A_ &once->to); 1351 ev_timer_start (EV_A_ &once->to);
1274 } 1352 }
1275 } 1353 }
1276} 1354}
1277 1355
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