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.60 by root, Sun Nov 4 18:29:44 2007 UTC

28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31#ifndef EV_STANDALONE 31#ifndef EV_STANDALONE
32# include "config.h" 32# include "config.h"
33
34# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1
36# define EV_USE_REALTIME 1
37# endif
38
39# if HAVE_SELECT && HAVE_SYS_SELECT_H
40# define EV_USE_SELECT 1
41# endif
42
43# if HAVE_POLL && HAVE_POLL_H
44# define EV_USE_POLL 1
45# endif
46
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1
49# endif
50
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1
53# endif
54
33#endif 55#endif
34 56
35#include <math.h> 57#include <math.h>
36#include <stdlib.h> 58#include <stdlib.h>
37#include <unistd.h> 59#include <unistd.h>
58 80
59#ifndef EV_USE_SELECT 81#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1 82# define EV_USE_SELECT 1
61#endif 83#endif
62 84
63#ifndef EV_USEV_POLL 85#ifndef EV_USE_POLL
64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */ 86# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif 87#endif
66 88
67#ifndef EV_USE_EPOLL 89#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0 90# define EV_USE_EPOLL 0
69#endif 91#endif
130{ 152{
131 W w; 153 W w;
132 int events; 154 int events;
133} ANPENDING; 155} ANPENDING;
134 156
135#ifdef EV_MULTIPLICITY 157#if EV_MULTIPLICITY
136 158
137struct ev_loop 159struct ev_loop
138{ 160{
139# define VAR(name,decl) decl; 161# define VAR(name,decl) decl;
140# include "ev_vars.h" 162# include "ev_vars.h"
338 fd_kill (EV_A_ fd); 360 fd_kill (EV_A_ fd);
339 return; 361 return;
340 } 362 }
341} 363}
342 364
365/* susually called after fork if method needs to re-arm all fds from scratch */
366static void
367fd_rearm_all (EV_P)
368{
369 int fd;
370
371 /* this should be highly optimised to not do anything but set a flag */
372 for (fd = 0; fd < anfdmax; ++fd)
373 if (anfds [fd].events)
374 {
375 anfds [fd].events = 0;
376 fd_change (EV_A_ fd);
377 }
378}
379
343/*****************************************************************************/ 380/*****************************************************************************/
344 381
345static void 382static void
346upheap (WT *heap, int k) 383upheap (WT *heap, int k)
347{ 384{
394static ANSIG *signals; 431static ANSIG *signals;
395static int signalmax; 432static int signalmax;
396 433
397static int sigpipe [2]; 434static int sigpipe [2];
398static sig_atomic_t volatile gotsig; 435static sig_atomic_t volatile gotsig;
436static struct ev_io sigev;
399 437
400static void 438static void
401signals_init (ANSIG *base, int count) 439signals_init (ANSIG *base, int count)
402{ 440{
403 while (count--) 441 while (count--)
460} 498}
461 499
462/*****************************************************************************/ 500/*****************************************************************************/
463 501
464#ifndef WIN32 502#ifndef WIN32
503
504static struct ev_child *childs [PID_HASHSIZE];
505static struct ev_signal childev;
465 506
466#ifndef WCONTINUED 507#ifndef WCONTINUED
467# define WCONTINUED 0 508# define WCONTINUED 0
468#endif 509#endif
469 510
505# include "ev_kqueue.c" 546# include "ev_kqueue.c"
506#endif 547#endif
507#if EV_USE_EPOLL 548#if EV_USE_EPOLL
508# include "ev_epoll.c" 549# include "ev_epoll.c"
509#endif 550#endif
510#if EV_USEV_POLL 551#if EV_USE_POLL
511# include "ev_poll.c" 552# include "ev_poll.c"
512#endif 553#endif
513#if EV_USE_SELECT 554#if EV_USE_SELECT
514# include "ev_select.c" 555# include "ev_select.c"
515#endif 556#endif
560 rt_now = ev_time (); 601 rt_now = ev_time ();
561 mn_now = get_clock (); 602 mn_now = get_clock ();
562 now_floor = mn_now; 603 now_floor = mn_now;
563 rtmn_diff = rt_now - mn_now; 604 rtmn_diff = rt_now - mn_now;
564 605
565 if (pipe (sigpipe))
566 return 0;
567
568 if (methods == EVMETHOD_AUTO) 606 if (methods == EVMETHOD_AUTO)
569 if (!enable_secure () && getenv ("LIBmethodS")) 607 if (!enable_secure () && getenv ("LIBEV_METHODS"))
570 methods = atoi (getenv ("LIBmethodS")); 608 methods = atoi (getenv ("LIBEV_METHODS"));
571 else 609 else
572 methods = EVMETHOD_ANY; 610 methods = EVMETHOD_ANY;
573 611
574 method = 0; 612 method = 0;
575#if EV_USE_KQUEUE 613#if EV_USE_KQUEUE
576 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods); 614 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
577#endif 615#endif
578#if EV_USE_EPOLL 616#if EV_USE_EPOLL
579 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods); 617 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
580#endif 618#endif
581#if EV_USEV_POLL 619#if EV_USE_POLL
582 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods); 620 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
583#endif 621#endif
584#if EV_USE_SELECT 622#if EV_USE_SELECT
585 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods); 623 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
586#endif 624#endif
625 }
626}
587 627
628void
629loop_destroy (EV_P)
630{
631#if EV_USE_KQUEUE
632 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
633#endif
634#if EV_USE_EPOLL
635 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
636#endif
637#if EV_USE_POLL
638 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
639#endif
640#if EV_USE_SELECT
641 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
642#endif
643
644 method = 0;
645 /*TODO*/
646}
647
648void
649loop_fork (EV_P)
650{
651 /*TODO*/
652#if EV_USE_EPOLL
653 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
654#endif
655#if EV_USE_KQUEUE
656 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
657#endif
658}
659
660#if EV_MULTIPLICITY
661struct ev_loop *
662ev_loop_new (int methods)
663{
664 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop));
665
666 loop_init (EV_A_ methods);
667
668 if (ev_method (EV_A))
669 return loop;
670
671 return 0;
672}
673
674void
675ev_loop_destroy (EV_P)
676{
677 loop_destroy (EV_A);
678 free (loop);
679}
680
681void
682ev_loop_fork (EV_P)
683{
684 loop_fork (EV_A);
685}
686
687#endif
688
689#if EV_MULTIPLICITY
690struct ev_loop default_loop_struct;
691static struct ev_loop *default_loop;
692
693struct ev_loop *
694#else
695static int default_loop;
696
697int
698#endif
699ev_default_loop (int methods)
700{
701 if (sigpipe [0] == sigpipe [1])
702 if (pipe (sigpipe))
703 return 0;
704
705 if (!default_loop)
706 {
707#if EV_MULTIPLICITY
708 struct ev_loop *loop = default_loop = &default_loop_struct;
709#else
710 default_loop = 1;
711#endif
712
713 loop_init (EV_A_ methods);
714
588 if (method) 715 if (ev_method (EV_A))
589 { 716 {
590 ev_watcher_init (&sigev, sigcb); 717 ev_watcher_init (&sigev, sigcb);
591 ev_set_priority (&sigev, EV_MAXPRI); 718 ev_set_priority (&sigev, EV_MAXPRI);
592 siginit (EV_A); 719 siginit (EV_A);
593 720
596 ev_set_priority (&childev, EV_MAXPRI); 723 ev_set_priority (&childev, EV_MAXPRI);
597 ev_signal_start (EV_A_ &childev); 724 ev_signal_start (EV_A_ &childev);
598 ev_unref (EV_A); /* child watcher should not keep loop alive */ 725 ev_unref (EV_A); /* child watcher should not keep loop alive */
599#endif 726#endif
600 } 727 }
728 else
729 default_loop = 0;
601 } 730 }
602 731
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; 732 return default_loop;
616} 733}
617 734
618void 735void
619ev_loop_delete (EV_P) 736ev_default_destroy (void)
620{ 737{
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 738#if EV_MULTIPLICITY
654#if EV_USE_EPOLL 739 struct ev_loop *loop = default_loop;
655 if (method == EVMETHOD_EPOLL)
656 epoll_postfork_child (EV_A);
657#endif 740#endif
741
742 ev_ref (EV_A); /* child watcher */
743 ev_signal_stop (EV_A_ &childev);
744
745 ev_ref (EV_A); /* signal watcher */
746 ev_io_stop (EV_A_ &sigev);
747
748 close (sigpipe [0]); sigpipe [0] = 0;
749 close (sigpipe [1]); sigpipe [1] = 0;
750
751 loop_destroy (EV_A);
752}
753
754void
755ev_default_fork (void)
756{
757#if EV_MULTIPLICITY
758 struct ev_loop *loop = default_loop;
759#endif
760
761 loop_fork (EV_A);
658 762
659 ev_io_stop (EV_A_ &sigev); 763 ev_io_stop (EV_A_ &sigev);
660 close (sigpipe [0]); 764 close (sigpipe [0]);
661 close (sigpipe [1]); 765 close (sigpipe [1]);
662 pipe (sigpipe); 766 pipe (sigpipe);
767
768 ev_ref (EV_A); /* signal watcher */
663 siginit (EV_A); 769 siginit (EV_A);
664#endif
665} 770}
666 771
667/*****************************************************************************/ 772/*****************************************************************************/
668 773
669static void 774static void
1080 } 1185 }
1081 1186
1082 ev_stop (EV_A_ (W)w); 1187 ev_stop (EV_A_ (W)w);
1083} 1188}
1084 1189
1190void
1191ev_idle_start (EV_P_ struct ev_idle *w)
1192{
1193 if (ev_is_active (w))
1194 return;
1195
1196 ev_start (EV_A_ (W)w, ++idlecnt);
1197 array_needsize (idles, idlemax, idlecnt, );
1198 idles [idlecnt - 1] = w;
1199}
1200
1201void
1202ev_idle_stop (EV_P_ struct ev_idle *w)
1203{
1204 ev_clear_pending (EV_A_ (W)w);
1205 if (ev_is_active (w))
1206 return;
1207
1208 idles [w->active - 1] = idles [--idlecnt];
1209 ev_stop (EV_A_ (W)w);
1210}
1211
1212void
1213ev_prepare_start (EV_P_ struct ev_prepare *w)
1214{
1215 if (ev_is_active (w))
1216 return;
1217
1218 ev_start (EV_A_ (W)w, ++preparecnt);
1219 array_needsize (prepares, preparemax, preparecnt, );
1220 prepares [preparecnt - 1] = w;
1221}
1222
1223void
1224ev_prepare_stop (EV_P_ struct ev_prepare *w)
1225{
1226 ev_clear_pending (EV_A_ (W)w);
1227 if (ev_is_active (w))
1228 return;
1229
1230 prepares [w->active - 1] = prepares [--preparecnt];
1231 ev_stop (EV_A_ (W)w);
1232}
1233
1234void
1235ev_check_start (EV_P_ struct ev_check *w)
1236{
1237 if (ev_is_active (w))
1238 return;
1239
1240 ev_start (EV_A_ (W)w, ++checkcnt);
1241 array_needsize (checks, checkmax, checkcnt, );
1242 checks [checkcnt - 1] = w;
1243}
1244
1245void
1246ev_check_stop (EV_P_ struct ev_check *w)
1247{
1248 ev_clear_pending (EV_A_ (W)w);
1249 if (ev_is_active (w))
1250 return;
1251
1252 checks [w->active - 1] = checks [--checkcnt];
1253 ev_stop (EV_A_ (W)w);
1254}
1255
1085#ifndef SA_RESTART 1256#ifndef SA_RESTART
1086# define SA_RESTART 0 1257# define SA_RESTART 0
1087#endif 1258#endif
1088 1259
1089void 1260void
1090ev_signal_start (EV_P_ struct ev_signal *w) 1261ev_signal_start (EV_P_ struct ev_signal *w)
1091{ 1262{
1263#if EV_MULTIPLICITY
1264 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1265#endif
1092 if (ev_is_active (w)) 1266 if (ev_is_active (w))
1093 return; 1267 return;
1094 1268
1095 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1269 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1096 1270
1121 if (!signals [w->signum - 1].head) 1295 if (!signals [w->signum - 1].head)
1122 signal (w->signum, SIG_DFL); 1296 signal (w->signum, SIG_DFL);
1123} 1297}
1124 1298
1125void 1299void
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) 1300ev_child_start (EV_P_ struct ev_child *w)
1193{ 1301{
1302#if EV_MULTIPLICITY
1303 assert (("child watchers are only supported in the default loop", loop == default_loop));
1304#endif
1194 if (ev_is_active (w)) 1305 if (ev_is_active (w))
1195 return; 1306 return;
1196 1307
1197 ev_start (EV_A_ (W)w, 1); 1308 ev_start (EV_A_ (W)w, 1);
1198 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1309 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1270 ev_timer_start (EV_A_ &once->to); 1381 ev_timer_start (EV_A_ &once->to);
1271 } 1382 }
1272 } 1383 }
1273} 1384}
1274 1385
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