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.57 by root, Sun Nov 4 16:43:53 2007 UTC

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{
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;
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; 706 return default_loop;
616} 707}
617 708
618void 709void
619ev_loop_delete (EV_P) 710ev_default_destroy (void)
620{ 711{
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 712#if EV_MULTIPLICITY
654#if EV_USE_EPOLL 713 struct ev_loop *loop = default_loop;
655 if (method == EVMETHOD_EPOLL)
656 epoll_postfork_child (EV_A);
657#endif 714#endif
715
716 ev_ref (EV_A); /* child watcher */
717 ev_signal_stop (EV_A_ &childev);
718
719 ev_ref (EV_A); /* signal watcher */
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);
658 732
659 ev_io_stop (EV_A_ &sigev); 733 ev_io_stop (EV_A_ &sigev);
660 close (sigpipe [0]); 734 close (sigpipe [0]);
661 close (sigpipe [1]); 735 close (sigpipe [1]);
662 pipe (sigpipe); 736 pipe (sigpipe);
737
738 ev_ref (EV_A); /* signal watcher */
663 siginit (EV_A); 739 siginit (EV_A);
664#endif
665} 740}
666 741
667/*****************************************************************************/ 742/*****************************************************************************/
668 743
669static void 744static void
1080 } 1155 }
1081 1156
1082 ev_stop (EV_A_ (W)w); 1157 ev_stop (EV_A_ (W)w);
1083} 1158}
1084 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
1085#ifndef SA_RESTART 1226#ifndef SA_RESTART
1086# define SA_RESTART 0 1227# define SA_RESTART 0
1087#endif 1228#endif
1088 1229
1089void 1230void
1090ev_signal_start (EV_P_ struct ev_signal *w) 1231ev_signal_start (EV_P_ struct ev_signal *w)
1091{ 1232{
1233#if EV_MULTIPLICITY
1234 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1235#endif
1092 if (ev_is_active (w)) 1236 if (ev_is_active (w))
1093 return; 1237 return;
1094 1238
1095 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));
1096 1240
1121 if (!signals [w->signum - 1].head) 1265 if (!signals [w->signum - 1].head)
1122 signal (w->signum, SIG_DFL); 1266 signal (w->signum, SIG_DFL);
1123} 1267}
1124 1268
1125void 1269void
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) 1270ev_child_start (EV_P_ struct ev_child *w)
1193{ 1271{
1272#if EV_MULTIPLICITY
1273 assert (("child watchers are only supported in the default loop", loop == default_loop));
1274#endif
1194 if (ev_is_active (w)) 1275 if (ev_is_active (w))
1195 return; 1276 return;
1196 1277
1197 ev_start (EV_A_ (W)w, 1); 1278 ev_start (EV_A_ (W)w, 1);
1198 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1279 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1270 ev_timer_start (EV_A_ &once->to); 1351 ev_timer_start (EV_A_ &once->to);
1271 } 1352 }
1272 } 1353 }
1273} 1354}
1274 1355
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