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

Comparing libev/ev.c (file contents):
Revision 1.54 by root, Sun Nov 4 00:24:16 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
130{ 130{
131 W w; 131 W w;
132 int events; 132 int events;
133} ANPENDING; 133} ANPENDING;
134 134
135#ifdef EV_MULTIPLICITY 135#if EV_MULTIPLICITY
136 136
137struct ev_loop 137struct ev_loop
138{ 138{
139# define VAR(name,decl) decl; 139# define VAR(name,decl) decl;
140# include "ev_vars.h" 140# include "ev_vars.h"
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
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;
604}
605
606#ifdef 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 loop_init (EV_A_ methods);
614
615 return loop; 710 return default_loop;
616} 711}
617 712
618void 713void
619ev_loop_delete (EV_P) 714ev_default_destroy (void)
620{ 715{
621 /*TODO*/
622 free (loop);
623}
624
625#else
626
627int
628ev_init (int methods)
629{
630 loop_init ();
631}
632
633#endif
634
635/*****************************************************************************/
636
637void
638ev_fork_prepare (void)
639{
640 /* nop */
641}
642
643void
644ev_fork_parent (void)
645{
646 /* nop */
647}
648
649void
650ev_fork_child (void)
651{
652 /*TODO*/
653#if !EV_MULTIPLICITY 716#if EV_MULTIPLICITY
654#if EV_USE_EPOLL 717 struct ev_loop *loop = default_loop;
655 if (method == EVMETHOD_EPOLL)
656 epoll_postfork_child (EV_A);
657#endif 718#endif
719
720 ev_ref (EV_A); /* child watcher */
721 ev_signal_stop (EV_A_ &childev);
722
723 ev_ref (EV_A); /* signal watcher */
724 ev_io_stop (EV_A_ &sigev);
725
726 close (sigpipe [0]); sigpipe [0] = 0;
727 close (sigpipe [1]); sigpipe [1] = 0;
728
729 loop_destroy (EV_A);
730}
731
732void
733ev_default_fork (EV_P)
734{
735 loop_fork (EV_A);
658 736
659 ev_io_stop (EV_A_ &sigev); 737 ev_io_stop (EV_A_ &sigev);
660 close (sigpipe [0]); 738 close (sigpipe [0]);
661 close (sigpipe [1]); 739 close (sigpipe [1]);
662 pipe (sigpipe); 740 pipe (sigpipe);
741
742 ev_ref (EV_A); /* signal watcher */
663 siginit (EV_A); 743 siginit (EV_A);
664#endif
665} 744}
666 745
667/*****************************************************************************/ 746/*****************************************************************************/
668 747
669static void 748static void
1080 } 1159 }
1081 1160
1082 ev_stop (EV_A_ (W)w); 1161 ev_stop (EV_A_ (W)w);
1083} 1162}
1084 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
1085#ifndef SA_RESTART 1230#ifndef SA_RESTART
1086# define SA_RESTART 0 1231# define SA_RESTART 0
1087#endif 1232#endif
1088 1233
1089void 1234void
1090ev_signal_start (EV_P_ struct ev_signal *w) 1235ev_signal_start (EV_P_ struct ev_signal *w)
1091{ 1236{
1237#if EV_MULTIPLICITY
1238 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1239#endif
1092 if (ev_is_active (w)) 1240 if (ev_is_active (w))
1093 return; 1241 return;
1094 1242
1095 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));
1096 1244
1121 if (!signals [w->signum - 1].head) 1269 if (!signals [w->signum - 1].head)
1122 signal (w->signum, SIG_DFL); 1270 signal (w->signum, SIG_DFL);
1123} 1271}
1124 1272
1125void 1273void
1126ev_idle_start (EV_P_ struct ev_idle *w)
1127{
1128 if (ev_is_active (w))
1129 return;
1130
1131 ev_start (EV_A_ (W)w, ++idlecnt);
1132 array_needsize (idles, idlemax, idlecnt, );
1133 idles [idlecnt - 1] = w;
1134}
1135
1136void
1137ev_idle_stop (EV_P_ struct ev_idle *w)
1138{
1139 ev_clear_pending (EV_A_ (W)w);
1140 if (ev_is_active (w))
1141 return;
1142
1143 idles [w->active - 1] = idles [--idlecnt];
1144 ev_stop (EV_A_ (W)w);
1145}
1146
1147void
1148ev_prepare_start (EV_P_ struct ev_prepare *w)
1149{
1150 if (ev_is_active (w))
1151 return;
1152
1153 ev_start (EV_A_ (W)w, ++preparecnt);
1154 array_needsize (prepares, preparemax, preparecnt, );
1155 prepares [preparecnt - 1] = w;
1156}
1157
1158void
1159ev_prepare_stop (EV_P_ struct ev_prepare *w)
1160{
1161 ev_clear_pending (EV_A_ (W)w);
1162 if (ev_is_active (w))
1163 return;
1164
1165 prepares [w->active - 1] = prepares [--preparecnt];
1166 ev_stop (EV_A_ (W)w);
1167}
1168
1169void
1170ev_check_start (EV_P_ struct ev_check *w)
1171{
1172 if (ev_is_active (w))
1173 return;
1174
1175 ev_start (EV_A_ (W)w, ++checkcnt);
1176 array_needsize (checks, checkmax, checkcnt, );
1177 checks [checkcnt - 1] = w;
1178}
1179
1180void
1181ev_check_stop (EV_P_ struct ev_check *w)
1182{
1183 ev_clear_pending (EV_A_ (W)w);
1184 if (ev_is_active (w))
1185 return;
1186
1187 checks [w->active - 1] = checks [--checkcnt];
1188 ev_stop (EV_A_ (W)w);
1189}
1190
1191void
1192ev_child_start (EV_P_ struct ev_child *w) 1274ev_child_start (EV_P_ struct ev_child *w)
1193{ 1275{
1276#if EV_MULTIPLICITY
1277 assert (("child watchers are only supported in the default loop", loop == default_loop));
1278#endif
1194 if (ev_is_active (w)) 1279 if (ev_is_active (w))
1195 return; 1280 return;
1196 1281
1197 ev_start (EV_A_ (W)w, 1); 1282 ev_start (EV_A_ (W)w, 1);
1198 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1283 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1270 ev_timer_start (EV_A_ &once->to); 1355 ev_timer_start (EV_A_ &once->to);
1271 } 1356 }
1272 } 1357 }
1273} 1358}
1274 1359
1275/*****************************************************************************/
1276
1277#if 0
1278
1279struct ev_io wio;
1280
1281static void
1282sin_cb (struct ev_io *w, int revents)
1283{
1284 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1285}
1286
1287static void
1288ocb (struct ev_timer *w, int revents)
1289{
1290 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1291 ev_timer_stop (w);
1292 ev_timer_start (w);
1293}
1294
1295static void
1296scb (struct ev_signal *w, int revents)
1297{
1298 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1299 ev_io_stop (&wio);
1300 ev_io_start (&wio);
1301}
1302
1303static void
1304gcb (struct ev_signal *w, int revents)
1305{
1306 fprintf (stderr, "generic %x\n", revents);
1307
1308}
1309
1310int main (void)
1311{
1312 ev_init (0);
1313
1314 ev_io_init (&wio, sin_cb, 0, EV_READ);
1315 ev_io_start (&wio);
1316
1317 struct ev_timer t[10000];
1318
1319#if 0
1320 int i;
1321 for (i = 0; i < 10000; ++i)
1322 {
1323 struct ev_timer *w = t + i;
1324 ev_watcher_init (w, ocb, i);
1325 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1326 ev_timer_start (w);
1327 if (drand48 () < 0.5)
1328 ev_timer_stop (w);
1329 }
1330#endif
1331
1332 struct ev_timer t1;
1333 ev_timer_init (&t1, ocb, 5, 10);
1334 ev_timer_start (&t1);
1335
1336 struct ev_signal sig;
1337 ev_signal_init (&sig, scb, SIGQUIT);
1338 ev_signal_start (&sig);
1339
1340 struct ev_check cw;
1341 ev_check_init (&cw, gcb);
1342 ev_check_start (&cw);
1343
1344 struct ev_idle iw;
1345 ev_idle_init (&iw, gcb);
1346 ev_idle_start (&iw);
1347
1348 ev_loop (0);
1349
1350 return 0;
1351}
1352
1353#endif
1354
1355
1356
1357

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines