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.61 by root, Sun Nov 4 19:45:09 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
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
542ev_method (EV_P) 583ev_method (EV_P)
543{ 584{
544 return method; 585 return method;
545} 586}
546 587
547inline int 588static void
548loop_init (EV_P_ int methods) 589loop_init (EV_P_ int methods)
549{ 590{
550 if (!method) 591 if (!method)
551 { 592 {
552#if EV_USE_MONOTONIC 593#if EV_USE_MONOTONIC
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; 732 return default_loop;
604} 733}
605 734
735void
736ev_default_destroy (void)
737{
606#if EV_MULTIPLICITY 738#if EV_MULTIPLICITY
607 739 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 740#endif
637 741
638/*****************************************************************************/ 742 ev_ref (EV_A); /* child watcher */
743 ev_signal_stop (EV_A_ &childev);
639 744
640void 745 ev_ref (EV_A); /* signal watcher */
641ev_fork_prepare (void) 746 ev_io_stop (EV_A_ &sigev);
642{
643 /* nop */
644}
645 747
646void 748 close (sigpipe [0]); sigpipe [0] = 0;
647ev_fork_parent (void) 749 close (sigpipe [1]); sigpipe [1] = 0;
648{
649 /* nop */
650}
651 750
751 loop_destroy (EV_A);
752}
753
652void 754void
653ev_fork_child (void) 755ev_default_fork (void)
654{ 756{
655 /*TODO*/
656#if !EV_MULTIPLICITY 757#if EV_MULTIPLICITY
657#if EV_USE_EPOLL 758 struct ev_loop *loop = default_loop;
658 if (method == EVMETHOD_EPOLL)
659 epoll_postfork_child (EV_A);
660#endif 759#endif
760
761 loop_fork (EV_A);
661 762
662 ev_io_stop (EV_A_ &sigev); 763 ev_io_stop (EV_A_ &sigev);
663 close (sigpipe [0]); 764 close (sigpipe [0]);
664 close (sigpipe [1]); 765 close (sigpipe [1]);
665 pipe (sigpipe); 766 pipe (sigpipe);
767
768 ev_ref (EV_A); /* signal watcher */
666 siginit (EV_A); 769 siginit (EV_A);
667#endif
668} 770}
669 771
670/*****************************************************************************/ 772/*****************************************************************************/
671 773
672static void 774static void
691timers_reify (EV_P) 793timers_reify (EV_P)
692{ 794{
693 while (timercnt && timers [0]->at <= mn_now) 795 while (timercnt && timers [0]->at <= mn_now)
694 { 796 {
695 struct ev_timer *w = timers [0]; 797 struct ev_timer *w = timers [0];
798
799 assert (("inactive timer on timer heap detected", ev_is_active (w)));
696 800
697 /* first reschedule or stop timer */ 801 /* first reschedule or stop timer */
698 if (w->repeat) 802 if (w->repeat)
699 { 803 {
700 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 804 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
713{ 817{
714 while (periodiccnt && periodics [0]->at <= rt_now) 818 while (periodiccnt && periodics [0]->at <= rt_now)
715 { 819 {
716 struct ev_periodic *w = periodics [0]; 820 struct ev_periodic *w = periodics [0];
717 821
822 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
823
718 /* first reschedule or stop timer */ 824 /* first reschedule or stop timer */
719 if (w->interval) 825 if (w->interval)
720 { 826 {
721 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval; 827 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval;
722 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now)); 828 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now));
1083 } 1189 }
1084 1190
1085 ev_stop (EV_A_ (W)w); 1191 ev_stop (EV_A_ (W)w);
1086} 1192}
1087 1193
1194void
1195ev_idle_start (EV_P_ struct ev_idle *w)
1196{
1197 if (ev_is_active (w))
1198 return;
1199
1200 ev_start (EV_A_ (W)w, ++idlecnt);
1201 array_needsize (idles, idlemax, idlecnt, );
1202 idles [idlecnt - 1] = w;
1203}
1204
1205void
1206ev_idle_stop (EV_P_ struct ev_idle *w)
1207{
1208 ev_clear_pending (EV_A_ (W)w);
1209 if (ev_is_active (w))
1210 return;
1211
1212 idles [w->active - 1] = idles [--idlecnt];
1213 ev_stop (EV_A_ (W)w);
1214}
1215
1216void
1217ev_prepare_start (EV_P_ struct ev_prepare *w)
1218{
1219 if (ev_is_active (w))
1220 return;
1221
1222 ev_start (EV_A_ (W)w, ++preparecnt);
1223 array_needsize (prepares, preparemax, preparecnt, );
1224 prepares [preparecnt - 1] = w;
1225}
1226
1227void
1228ev_prepare_stop (EV_P_ struct ev_prepare *w)
1229{
1230 ev_clear_pending (EV_A_ (W)w);
1231 if (ev_is_active (w))
1232 return;
1233
1234 prepares [w->active - 1] = prepares [--preparecnt];
1235 ev_stop (EV_A_ (W)w);
1236}
1237
1238void
1239ev_check_start (EV_P_ struct ev_check *w)
1240{
1241 if (ev_is_active (w))
1242 return;
1243
1244 ev_start (EV_A_ (W)w, ++checkcnt);
1245 array_needsize (checks, checkmax, checkcnt, );
1246 checks [checkcnt - 1] = w;
1247}
1248
1249void
1250ev_check_stop (EV_P_ struct ev_check *w)
1251{
1252 ev_clear_pending (EV_A_ (W)w);
1253 if (ev_is_active (w))
1254 return;
1255
1256 checks [w->active - 1] = checks [--checkcnt];
1257 ev_stop (EV_A_ (W)w);
1258}
1259
1088#ifndef SA_RESTART 1260#ifndef SA_RESTART
1089# define SA_RESTART 0 1261# define SA_RESTART 0
1090#endif 1262#endif
1091 1263
1092void 1264void
1093ev_signal_start (EV_P_ struct ev_signal *w) 1265ev_signal_start (EV_P_ struct ev_signal *w)
1094{ 1266{
1267#if EV_MULTIPLICITY
1268 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1269#endif
1095 if (ev_is_active (w)) 1270 if (ev_is_active (w))
1096 return; 1271 return;
1097 1272
1098 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1273 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1099 1274
1124 if (!signals [w->signum - 1].head) 1299 if (!signals [w->signum - 1].head)
1125 signal (w->signum, SIG_DFL); 1300 signal (w->signum, SIG_DFL);
1126} 1301}
1127 1302
1128void 1303void
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) 1304ev_child_start (EV_P_ struct ev_child *w)
1196{ 1305{
1306#if EV_MULTIPLICITY
1307 assert (("child watchers are only supported in the default loop", loop == default_loop));
1308#endif
1197 if (ev_is_active (w)) 1309 if (ev_is_active (w))
1198 return; 1310 return;
1199 1311
1200 ev_start (EV_A_ (W)w, 1); 1312 ev_start (EV_A_ (W)w, 1);
1201 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1313 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1273 ev_timer_start (EV_A_ &once->to); 1385 ev_timer_start (EV_A_ &once->to);
1274 } 1386 }
1275 } 1387 }
1276} 1388}
1277 1389
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