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.59 by root, Sun Nov 4 18:15:16 2007 UTC

58 58
59#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
61#endif 61#endif
62 62
63#ifndef EV_USEV_POLL 63#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 65#endif
66 66
67#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
69#endif 69#endif
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{
394static ANSIG *signals; 409static ANSIG *signals;
395static int signalmax; 410static int signalmax;
396 411
397static int sigpipe [2]; 412static int sigpipe [2];
398static sig_atomic_t volatile gotsig; 413static sig_atomic_t volatile gotsig;
414static struct ev_io sigev;
399 415
400static void 416static void
401signals_init (ANSIG *base, int count) 417signals_init (ANSIG *base, int count)
402{ 418{
403 while (count--) 419 while (count--)
460} 476}
461 477
462/*****************************************************************************/ 478/*****************************************************************************/
463 479
464#ifndef WIN32 480#ifndef WIN32
481
482static struct ev_child *childs [PID_HASHSIZE];
483static struct ev_signal childev;
465 484
466#ifndef WCONTINUED 485#ifndef WCONTINUED
467# define WCONTINUED 0 486# define WCONTINUED 0
468#endif 487#endif
469 488
505# include "ev_kqueue.c" 524# include "ev_kqueue.c"
506#endif 525#endif
507#if EV_USE_EPOLL 526#if EV_USE_EPOLL
508# include "ev_epoll.c" 527# include "ev_epoll.c"
509#endif 528#endif
510#if EV_USEV_POLL 529#if EV_USE_POLL
511# include "ev_poll.c" 530# include "ev_poll.c"
512#endif 531#endif
513#if EV_USE_SELECT 532#if EV_USE_SELECT
514# include "ev_select.c" 533# include "ev_select.c"
515#endif 534#endif
542ev_method (EV_P) 561ev_method (EV_P)
543{ 562{
544 return method; 563 return method;
545} 564}
546 565
547inline int 566static void
548loop_init (EV_P_ int methods) 567loop_init (EV_P_ int methods)
549{ 568{
550 if (!method) 569 if (!method)
551 { 570 {
552#if EV_USE_MONOTONIC 571#if EV_USE_MONOTONIC
560 rt_now = ev_time (); 579 rt_now = ev_time ();
561 mn_now = get_clock (); 580 mn_now = get_clock ();
562 now_floor = mn_now; 581 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 582 rtmn_diff = rt_now - mn_now;
564 583
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 584 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 585 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 586 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 587 else
572 methods = EVMETHOD_ANY; 588 methods = EVMETHOD_ANY;
573 589
574 method = 0; 590 method = 0;
575#if EV_USE_KQUEUE 591#if EV_USE_KQUEUE
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 592 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
577#endif 593#endif
578#if EV_USE_EPOLL 594#if EV_USE_EPOLL
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 595 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
580#endif 596#endif
581#if EV_USEV_POLL 597#if EV_USE_POLL
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 598 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 599#endif
584#if EV_USE_SELECT 600#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 601 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 602#endif
603 }
604}
587 605
606void
607loop_destroy (EV_P)
608{
609#if EV_USE_KQUEUE
610 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
611#endif
612#if EV_USE_EPOLL
613 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
614#endif
615#if EV_USE_POLL
616 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
617#endif
618#if EV_USE_SELECT
619 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
620#endif
621
622 method = 0;
623 /*TODO*/
624}
625
626void
627loop_fork (EV_P)
628{
629 /*TODO*/
630#if EV_USE_EPOLL
631 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
632#endif
633#if EV_USE_KQUEUE
634 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
635#endif
636}
637
638#if EV_MULTIPLICITY
639struct ev_loop *
640ev_loop_new (int methods)
641{
642 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
643
644 loop_init (EV_A_ methods);
645
646 if (ev_methods (EV_A))
647 return loop;
648
649 return 0;
650}
651
652void
653ev_loop_destroy (EV_P)
654{
655 loop_destroy (EV_A);
656 free (loop);
657}
658
659void
660ev_loop_fork (EV_P)
661{
662 loop_fork (EV_A);
663}
664
665#endif
666
667#if EV_MULTIPLICITY
668struct ev_loop default_loop_struct;
669static struct ev_loop *default_loop;
670
671struct ev_loop *
672#else
673static int default_loop;
674
675int
676#endif
677ev_default_loop (int methods)
678{
679 if (sigpipe [0] == sigpipe [1])
680 if (pipe (sigpipe))
681 return 0;
682
683 if (!default_loop)
684 {
685#if EV_MULTIPLICITY
686 struct ev_loop *loop = default_loop = &default_loop_struct;
687#else
688 default_loop = 1;
689#endif
690
691 loop_init (EV_A_ methods);
692
588 if (method) 693 if (ev_method (EV_A))
589 { 694 {
590 ev_watcher_init (&sigev, sigcb); 695 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI); 696 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 697 siginit (EV_A);
593 698
596 ev_set_priority (&childev, EV_MAXPRI); 701 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 702 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 703 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 704#endif
600 } 705 }
706 else
707 default_loop = 0;
601 } 708 }
602 709
603 return method; 710 return default_loop;
604} 711}
605 712
713void
714ev_default_destroy (void)
715{
606#if EV_MULTIPLICITY 716#if EV_MULTIPLICITY
607 717 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 718#endif
637 719
638/*****************************************************************************/ 720 ev_ref (EV_A); /* child watcher */
721 ev_signal_stop (EV_A_ &childev);
639 722
640void 723 ev_ref (EV_A); /* signal watcher */
641ev_fork_prepare (void) 724 ev_io_stop (EV_A_ &sigev);
642{
643 /* nop */
644}
645 725
646void 726 close (sigpipe [0]); sigpipe [0] = 0;
647ev_fork_parent (void) 727 close (sigpipe [1]); sigpipe [1] = 0;
648{
649 /* nop */
650}
651 728
729 loop_destroy (EV_A);
730}
731
652void 732void
653ev_fork_child (void) 733ev_default_fork (EV_P)
654{ 734{
655 /*TODO*/ 735 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 736
662 ev_io_stop (EV_A_ &sigev); 737 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 738 close (sigpipe [0]);
664 close (sigpipe [1]); 739 close (sigpipe [1]);
665 pipe (sigpipe); 740 pipe (sigpipe);
741
742 ev_ref (EV_A); /* signal watcher */
666 siginit (EV_A); 743 siginit (EV_A);
667#endif
668} 744}
669 745
670/*****************************************************************************/ 746/*****************************************************************************/
671 747
672static void 748static void
1083 } 1159 }
1084 1160
1085 ev_stop (EV_A_ (W)w); 1161 ev_stop (EV_A_ (W)w);
1086} 1162}
1087 1163
1164void
1165ev_idle_start (EV_P_ struct ev_idle *w)
1166{
1167 if (ev_is_active (w))
1168 return;
1169
1170 ev_start (EV_A_ (W)w, ++idlecnt);
1171 array_needsize (idles, idlemax, idlecnt, );
1172 idles [idlecnt - 1] = w;
1173}
1174
1175void
1176ev_idle_stop (EV_P_ struct ev_idle *w)
1177{
1178 ev_clear_pending (EV_A_ (W)w);
1179 if (ev_is_active (w))
1180 return;
1181
1182 idles [w->active - 1] = idles [--idlecnt];
1183 ev_stop (EV_A_ (W)w);
1184}
1185
1186void
1187ev_prepare_start (EV_P_ struct ev_prepare *w)
1188{
1189 if (ev_is_active (w))
1190 return;
1191
1192 ev_start (EV_A_ (W)w, ++preparecnt);
1193 array_needsize (prepares, preparemax, preparecnt, );
1194 prepares [preparecnt - 1] = w;
1195}
1196
1197void
1198ev_prepare_stop (EV_P_ struct ev_prepare *w)
1199{
1200 ev_clear_pending (EV_A_ (W)w);
1201 if (ev_is_active (w))
1202 return;
1203
1204 prepares [w->active - 1] = prepares [--preparecnt];
1205 ev_stop (EV_A_ (W)w);
1206}
1207
1208void
1209ev_check_start (EV_P_ struct ev_check *w)
1210{
1211 if (ev_is_active (w))
1212 return;
1213
1214 ev_start (EV_A_ (W)w, ++checkcnt);
1215 array_needsize (checks, checkmax, checkcnt, );
1216 checks [checkcnt - 1] = w;
1217}
1218
1219void
1220ev_check_stop (EV_P_ struct ev_check *w)
1221{
1222 ev_clear_pending (EV_A_ (W)w);
1223 if (ev_is_active (w))
1224 return;
1225
1226 checks [w->active - 1] = checks [--checkcnt];
1227 ev_stop (EV_A_ (W)w);
1228}
1229
1088#ifndef SA_RESTART 1230#ifndef SA_RESTART
1089# define SA_RESTART 0 1231# define SA_RESTART 0
1090#endif 1232#endif
1091 1233
1092void 1234void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1235ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1236{
1237#if EV_MULTIPLICITY
1238 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1239#endif
1095 if (ev_is_active (w)) 1240 if (ev_is_active (w))
1096 return; 1241 return;
1097 1242
1098 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1243 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1099 1244
1124 if (!signals [w->signum - 1].head) 1269 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1270 signal (w->signum, SIG_DFL);
1126} 1271}
1127 1272
1128void 1273void
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) 1274ev_child_start (EV_P_ struct ev_child *w)
1196{ 1275{
1276#if EV_MULTIPLICITY
1277 assert (("child watchers are only supported in the default loop", loop == default_loop));
1278#endif
1197 if (ev_is_active (w)) 1279 if (ev_is_active (w))
1198 return; 1280 return;
1199 1281
1200 ev_start (EV_A_ (W)w, 1); 1282 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1283 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1273 ev_timer_start (EV_A_ &once->to); 1355 ev_timer_start (EV_A_ &once->to);
1274 } 1356 }
1275 } 1357 }
1276} 1358}
1277 1359
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