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

Comparing libev/ev.c (file contents):
Revision 1.131 by root, Fri Nov 23 05:43:45 2007 UTC vs.
Revision 1.136 by root, Sat Nov 24 07:14:26 2007 UTC

32#ifdef __cplusplus 32#ifdef __cplusplus
33extern "C" { 33extern "C" {
34#endif 34#endif
35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# ifdef EV_CONFIG_H
38# include EV_CONFIG_H
39# else
37# include "config.h" 40# include "config.h"
41# endif
38 42
39# if HAVE_CLOCK_GETTIME 43# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC 44# ifndef EV_USE_MONOTONIC
41# define EV_USE_MONOTONIC 1 45# define EV_USE_MONOTONIC 1
42# endif 46# endif
198#define ABSPRI(w) ((w)->priority - EV_MINPRI) 202#define ABSPRI(w) ((w)->priority - EV_MINPRI)
199 203
200#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
201#define EMPTY2(a,b) /* used to suppress some warnings */ 205#define EMPTY2(a,b) /* used to suppress some warnings */
202 206
203typedef struct ev_watcher *W; 207typedef ev_watcher *W;
204typedef struct ev_watcher_list *WL; 208typedef ev_watcher_list *WL;
205typedef struct ev_watcher_time *WT; 209typedef ev_watcher_time *WT;
206 210
207static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
208 212
209#ifdef _WIN32 213#ifdef _WIN32
210# include "ev_win32.c" 214# include "ev_win32.c"
412 416
413inline void 417inline void
414fd_event (EV_P_ int fd, int revents) 418fd_event (EV_P_ int fd, int revents)
415{ 419{
416 ANFD *anfd = anfds + fd; 420 ANFD *anfd = anfds + fd;
417 struct ev_io *w; 421 ev_io *w;
418 422
419 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 423 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
420 { 424 {
421 int ev = w->events & revents; 425 int ev = w->events & revents;
422 426
423 if (ev) 427 if (ev)
424 ev_feed_event (EV_A_ (W)w, ev); 428 ev_feed_event (EV_A_ (W)w, ev);
440 444
441 for (i = 0; i < fdchangecnt; ++i) 445 for (i = 0; i < fdchangecnt; ++i)
442 { 446 {
443 int fd = fdchanges [i]; 447 int fd = fdchanges [i];
444 ANFD *anfd = anfds + fd; 448 ANFD *anfd = anfds + fd;
445 struct ev_io *w; 449 ev_io *w;
446 450
447 int events = 0; 451 int events = 0;
448 452
449 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 453 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
450 events |= w->events; 454 events |= w->events;
451 455
452#if EV_SELECT_IS_WINSOCKET 456#if EV_SELECT_IS_WINSOCKET
453 if (events) 457 if (events)
454 { 458 {
481} 485}
482 486
483static void 487static void
484fd_kill (EV_P_ int fd) 488fd_kill (EV_P_ int fd)
485{ 489{
486 struct ev_io *w; 490 ev_io *w;
487 491
488 while ((w = (struct ev_io *)anfds [fd].head)) 492 while ((w = (ev_io *)anfds [fd].head))
489 { 493 {
490 ev_io_stop (EV_A_ w); 494 ev_io_stop (EV_A_ w);
491 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 495 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
492 } 496 }
493} 497}
604static ANSIG *signals; 608static ANSIG *signals;
605static int signalmax; 609static int signalmax;
606 610
607static int sigpipe [2]; 611static int sigpipe [2];
608static sig_atomic_t volatile gotsig; 612static sig_atomic_t volatile gotsig;
609static struct ev_io sigev; 613static ev_io sigev;
610 614
611static void 615static void
612signals_init (ANSIG *base, int count) 616signals_init (ANSIG *base, int count)
613{ 617{
614 while (count--) 618 while (count--)
657 for (w = signals [signum].head; w; w = w->next) 661 for (w = signals [signum].head; w; w = w->next)
658 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 662 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
659} 663}
660 664
661static void 665static void
662sigcb (EV_P_ struct ev_io *iow, int revents) 666sigcb (EV_P_ ev_io *iow, int revents)
663{ 667{
664 int signum; 668 int signum;
665 669
666 read (sigpipe [0], &revents, 1); 670 read (sigpipe [0], &revents, 1);
667 gotsig = 0; 671 gotsig = 0;
694 ev_unref (EV_A); /* child watcher should not keep loop alive */ 698 ev_unref (EV_A); /* child watcher should not keep loop alive */
695} 699}
696 700
697/*****************************************************************************/ 701/*****************************************************************************/
698 702
699static struct ev_child *childs [PID_HASHSIZE]; 703static ev_child *childs [PID_HASHSIZE];
700 704
701#ifndef _WIN32 705#ifndef _WIN32
702 706
703static struct ev_signal childev; 707static ev_signal childev;
704 708
705#ifndef WCONTINUED 709#ifndef WCONTINUED
706# define WCONTINUED 0 710# define WCONTINUED 0
707#endif 711#endif
708 712
709static void 713static void
710child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status) 714child_reap (EV_P_ ev_signal *sw, int chain, int pid, int status)
711{ 715{
712 struct ev_child *w; 716 ev_child *w;
713 717
714 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next) 718 for (w = (ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
715 if (w->pid == pid || !w->pid) 719 if (w->pid == pid || !w->pid)
716 { 720 {
717 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 721 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
718 w->rpid = pid; 722 w->rpid = pid;
719 w->rstatus = status; 723 w->rstatus = status;
720 ev_feed_event (EV_A_ (W)w, EV_CHILD); 724 ev_feed_event (EV_A_ (W)w, EV_CHILD);
721 } 725 }
722} 726}
723 727
724static void 728static void
725childcb (EV_P_ struct ev_signal *sw, int revents) 729childcb (EV_P_ ev_signal *sw, int revents)
726{ 730{
727 int pid, status; 731 int pid, status;
728 732
729 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 733 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
730 { 734 {
731 /* make sure we are called again until all childs have been reaped */ 735 /* make sure we are called again until all childs have been reaped */
736 /* we need to do it this way so that the callback gets called before we continue */
732 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL); 737 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
733 738
734 child_reap (EV_A_ sw, pid, pid, status); 739 child_reap (EV_A_ sw, pid, pid, status);
735 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ 740 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but feed_event catches that */
736 } 741 }
737} 742}
738 743
739#endif 744#endif
740 745
811 816
812 return flags; 817 return flags;
813} 818}
814 819
815unsigned int 820unsigned int
821ev_embeddable_backends (void)
822{
823 return EVBACKEND_EPOLL
824 | EVBACKEND_KQUEUE
825 | EVBACKEND_PORT;
826}
827
828unsigned int
816ev_backend (EV_P) 829ev_backend (EV_P)
817{ 830{
818 return backend; 831 return backend;
819} 832}
820 833
1072inline void 1085inline void
1073timers_reify (EV_P) 1086timers_reify (EV_P)
1074{ 1087{
1075 while (timercnt && ((WT)timers [0])->at <= mn_now) 1088 while (timercnt && ((WT)timers [0])->at <= mn_now)
1076 { 1089 {
1077 struct ev_timer *w = timers [0]; 1090 ev_timer *w = timers [0];
1078 1091
1079 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1092 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1080 1093
1081 /* first reschedule or stop timer */ 1094 /* first reschedule or stop timer */
1082 if (w->repeat) 1095 if (w->repeat)
1100inline void 1113inline void
1101periodics_reify (EV_P) 1114periodics_reify (EV_P)
1102{ 1115{
1103 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1116 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1104 { 1117 {
1105 struct ev_periodic *w = periodics [0]; 1118 ev_periodic *w = periodics [0];
1106 1119
1107 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1120 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1108 1121
1109 /* first reschedule or stop timer */ 1122 /* first reschedule or stop timer */
1110 if (w->reschedule_cb) 1123 if (w->reschedule_cb)
1132 int i; 1145 int i;
1133 1146
1134 /* adjust periodics after time jump */ 1147 /* adjust periodics after time jump */
1135 for (i = 0; i < periodiccnt; ++i) 1148 for (i = 0; i < periodiccnt; ++i)
1136 { 1149 {
1137 struct ev_periodic *w = periodics [i]; 1150 ev_periodic *w = periodics [i];
1138 1151
1139 if (w->reschedule_cb) 1152 if (w->reschedule_cb)
1140 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1153 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1141 else if (w->interval) 1154 else if (w->interval)
1142 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1155 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1232static int loop_done; 1245static int loop_done;
1233 1246
1234void 1247void
1235ev_loop (EV_P_ int flags) 1248ev_loop (EV_P_ int flags)
1236{ 1249{
1237 double block;
1238 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1250 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1251 ? EVUNLOOP_ONE
1252 : EVUNLOOP_CANCEL;
1239 1253
1240 while (activecnt) 1254 while (activecnt)
1241 { 1255 {
1242 /* queue check watchers (and execute them) */ 1256 /* queue check watchers (and execute them) */
1243 if (expect_false (preparecnt)) 1257 if (expect_false (preparecnt))
1252 1266
1253 /* update fd-related kernel structures */ 1267 /* update fd-related kernel structures */
1254 fd_reify (EV_A); 1268 fd_reify (EV_A);
1255 1269
1256 /* calculate blocking time */ 1270 /* calculate blocking time */
1271 {
1272 double block;
1257 1273
1258 /* we only need this for !monotonic clock or timers, but as we basically 1274 if (flags & EVLOOP_NONBLOCK || idlecnt)
1259 always have timers, we just calculate it always */ 1275 block = 0.; /* do not block at all */
1276 else
1277 {
1278 /* update time to cancel out callback processing overhead */
1260#if EV_USE_MONOTONIC 1279#if EV_USE_MONOTONIC
1261 if (expect_true (have_monotonic)) 1280 if (expect_true (have_monotonic))
1262 time_update_monotonic (EV_A); 1281 time_update_monotonic (EV_A);
1263 else 1282 else
1264#endif 1283#endif
1265 { 1284 {
1266 ev_rt_now = ev_time (); 1285 ev_rt_now = ev_time ();
1267 mn_now = ev_rt_now; 1286 mn_now = ev_rt_now;
1268 } 1287 }
1269 1288
1270 if (flags & EVLOOP_NONBLOCK || idlecnt)
1271 block = 0.;
1272 else
1273 {
1274 block = MAX_BLOCKTIME; 1289 block = MAX_BLOCKTIME;
1275 1290
1276 if (timercnt) 1291 if (timercnt)
1277 { 1292 {
1278 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1293 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1279 if (block > to) block = to; 1294 if (block > to) block = to;
1280 } 1295 }
1281 1296
1282#if EV_PERIODICS 1297#if EV_PERIODICS
1283 if (periodiccnt) 1298 if (periodiccnt)
1284 { 1299 {
1285 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1300 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1286 if (block > to) block = to; 1301 if (block > to) block = to;
1287 } 1302 }
1288#endif 1303#endif
1289 1304
1290 if (expect_false (block < 0.)) block = 0.; 1305 if (expect_false (block < 0.)) block = 0.;
1291 } 1306 }
1292 1307
1293 backend_poll (EV_A_ block); 1308 backend_poll (EV_A_ block);
1309 }
1294 1310
1295 /* update ev_rt_now, do magic */ 1311 /* update ev_rt_now, do magic */
1296 time_update (EV_A); 1312 time_update (EV_A);
1297 1313
1298 /* queue pending timers and reschedule them */ 1314 /* queue pending timers and reschedule them */
1313 1329
1314 if (expect_false (loop_done)) 1330 if (expect_false (loop_done))
1315 break; 1331 break;
1316 } 1332 }
1317 1333
1318 if (loop_done != 2) 1334 if (loop_done == EVUNLOOP_ONE)
1319 loop_done = 0; 1335 loop_done = EVUNLOOP_CANCEL;
1320} 1336}
1321 1337
1322void 1338void
1323ev_unloop (EV_P_ int how) 1339ev_unloop (EV_P_ int how)
1324{ 1340{
1377} 1393}
1378 1394
1379/*****************************************************************************/ 1395/*****************************************************************************/
1380 1396
1381void 1397void
1382ev_io_start (EV_P_ struct ev_io *w) 1398ev_io_start (EV_P_ ev_io *w)
1383{ 1399{
1384 int fd = w->fd; 1400 int fd = w->fd;
1385 1401
1386 if (expect_false (ev_is_active (w))) 1402 if (expect_false (ev_is_active (w)))
1387 return; 1403 return;
1394 1410
1395 fd_change (EV_A_ fd); 1411 fd_change (EV_A_ fd);
1396} 1412}
1397 1413
1398void 1414void
1399ev_io_stop (EV_P_ struct ev_io *w) 1415ev_io_stop (EV_P_ ev_io *w)
1400{ 1416{
1401 ev_clear_pending (EV_A_ (W)w); 1417 ev_clear_pending (EV_A_ (W)w);
1402 if (expect_false (!ev_is_active (w))) 1418 if (expect_false (!ev_is_active (w)))
1403 return; 1419 return;
1404 1420
1409 1425
1410 fd_change (EV_A_ w->fd); 1426 fd_change (EV_A_ w->fd);
1411} 1427}
1412 1428
1413void 1429void
1414ev_timer_start (EV_P_ struct ev_timer *w) 1430ev_timer_start (EV_P_ ev_timer *w)
1415{ 1431{
1416 if (expect_false (ev_is_active (w))) 1432 if (expect_false (ev_is_active (w)))
1417 return; 1433 return;
1418 1434
1419 ((WT)w)->at += mn_now; 1435 ((WT)w)->at += mn_now;
1420 1436
1421 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1437 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1422 1438
1423 ev_start (EV_A_ (W)w, ++timercnt); 1439 ev_start (EV_A_ (W)w, ++timercnt);
1424 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1440 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1425 timers [timercnt - 1] = w; 1441 timers [timercnt - 1] = w;
1426 upheap ((WT *)timers, timercnt - 1); 1442 upheap ((WT *)timers, timercnt - 1);
1427 1443
1428 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1444 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1429} 1445}
1430 1446
1431void 1447void
1432ev_timer_stop (EV_P_ struct ev_timer *w) 1448ev_timer_stop (EV_P_ ev_timer *w)
1433{ 1449{
1434 ev_clear_pending (EV_A_ (W)w); 1450 ev_clear_pending (EV_A_ (W)w);
1435 if (expect_false (!ev_is_active (w))) 1451 if (expect_false (!ev_is_active (w)))
1436 return; 1452 return;
1437 1453
1447 1463
1448 ev_stop (EV_A_ (W)w); 1464 ev_stop (EV_A_ (W)w);
1449} 1465}
1450 1466
1451void 1467void
1452ev_timer_again (EV_P_ struct ev_timer *w) 1468ev_timer_again (EV_P_ ev_timer *w)
1453{ 1469{
1454 if (ev_is_active (w)) 1470 if (ev_is_active (w))
1455 { 1471 {
1456 if (w->repeat) 1472 if (w->repeat)
1457 { 1473 {
1468 } 1484 }
1469} 1485}
1470 1486
1471#if EV_PERIODICS 1487#if EV_PERIODICS
1472void 1488void
1473ev_periodic_start (EV_P_ struct ev_periodic *w) 1489ev_periodic_start (EV_P_ ev_periodic *w)
1474{ 1490{
1475 if (expect_false (ev_is_active (w))) 1491 if (expect_false (ev_is_active (w)))
1476 return; 1492 return;
1477 1493
1478 if (w->reschedule_cb) 1494 if (w->reschedule_cb)
1483 /* this formula differs from the one in periodic_reify because we do not always round up */ 1499 /* this formula differs from the one in periodic_reify because we do not always round up */
1484 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1500 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1485 } 1501 }
1486 1502
1487 ev_start (EV_A_ (W)w, ++periodiccnt); 1503 ev_start (EV_A_ (W)w, ++periodiccnt);
1488 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1504 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1489 periodics [periodiccnt - 1] = w; 1505 periodics [periodiccnt - 1] = w;
1490 upheap ((WT *)periodics, periodiccnt - 1); 1506 upheap ((WT *)periodics, periodiccnt - 1);
1491 1507
1492 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1508 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1493} 1509}
1494 1510
1495void 1511void
1496ev_periodic_stop (EV_P_ struct ev_periodic *w) 1512ev_periodic_stop (EV_P_ ev_periodic *w)
1497{ 1513{
1498 ev_clear_pending (EV_A_ (W)w); 1514 ev_clear_pending (EV_A_ (W)w);
1499 if (expect_false (!ev_is_active (w))) 1515 if (expect_false (!ev_is_active (w)))
1500 return; 1516 return;
1501 1517
1509 1525
1510 ev_stop (EV_A_ (W)w); 1526 ev_stop (EV_A_ (W)w);
1511} 1527}
1512 1528
1513void 1529void
1514ev_periodic_again (EV_P_ struct ev_periodic *w) 1530ev_periodic_again (EV_P_ ev_periodic *w)
1515{ 1531{
1516 /* TODO: use adjustheap and recalculation */ 1532 /* TODO: use adjustheap and recalculation */
1517 ev_periodic_stop (EV_A_ w); 1533 ev_periodic_stop (EV_A_ w);
1518 ev_periodic_start (EV_A_ w); 1534 ev_periodic_start (EV_A_ w);
1519} 1535}
1520#endif 1536#endif
1521 1537
1522void 1538void
1523ev_idle_start (EV_P_ struct ev_idle *w) 1539ev_idle_start (EV_P_ ev_idle *w)
1524{ 1540{
1525 if (expect_false (ev_is_active (w))) 1541 if (expect_false (ev_is_active (w)))
1526 return; 1542 return;
1527 1543
1528 ev_start (EV_A_ (W)w, ++idlecnt); 1544 ev_start (EV_A_ (W)w, ++idlecnt);
1529 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1545 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1530 idles [idlecnt - 1] = w; 1546 idles [idlecnt - 1] = w;
1531} 1547}
1532 1548
1533void 1549void
1534ev_idle_stop (EV_P_ struct ev_idle *w) 1550ev_idle_stop (EV_P_ ev_idle *w)
1535{ 1551{
1536 ev_clear_pending (EV_A_ (W)w); 1552 ev_clear_pending (EV_A_ (W)w);
1537 if (expect_false (!ev_is_active (w))) 1553 if (expect_false (!ev_is_active (w)))
1538 return; 1554 return;
1539 1555
1540 idles [((W)w)->active - 1] = idles [--idlecnt]; 1556 idles [((W)w)->active - 1] = idles [--idlecnt];
1541 ev_stop (EV_A_ (W)w); 1557 ev_stop (EV_A_ (W)w);
1542} 1558}
1543 1559
1544void 1560void
1545ev_prepare_start (EV_P_ struct ev_prepare *w) 1561ev_prepare_start (EV_P_ ev_prepare *w)
1546{ 1562{
1547 if (expect_false (ev_is_active (w))) 1563 if (expect_false (ev_is_active (w)))
1548 return; 1564 return;
1549 1565
1550 ev_start (EV_A_ (W)w, ++preparecnt); 1566 ev_start (EV_A_ (W)w, ++preparecnt);
1551 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1567 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1552 prepares [preparecnt - 1] = w; 1568 prepares [preparecnt - 1] = w;
1553} 1569}
1554 1570
1555void 1571void
1556ev_prepare_stop (EV_P_ struct ev_prepare *w) 1572ev_prepare_stop (EV_P_ ev_prepare *w)
1557{ 1573{
1558 ev_clear_pending (EV_A_ (W)w); 1574 ev_clear_pending (EV_A_ (W)w);
1559 if (expect_false (!ev_is_active (w))) 1575 if (expect_false (!ev_is_active (w)))
1560 return; 1576 return;
1561 1577
1562 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1578 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1563 ev_stop (EV_A_ (W)w); 1579 ev_stop (EV_A_ (W)w);
1564} 1580}
1565 1581
1566void 1582void
1567ev_check_start (EV_P_ struct ev_check *w) 1583ev_check_start (EV_P_ ev_check *w)
1568{ 1584{
1569 if (expect_false (ev_is_active (w))) 1585 if (expect_false (ev_is_active (w)))
1570 return; 1586 return;
1571 1587
1572 ev_start (EV_A_ (W)w, ++checkcnt); 1588 ev_start (EV_A_ (W)w, ++checkcnt);
1573 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1589 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1574 checks [checkcnt - 1] = w; 1590 checks [checkcnt - 1] = w;
1575} 1591}
1576 1592
1577void 1593void
1578ev_check_stop (EV_P_ struct ev_check *w) 1594ev_check_stop (EV_P_ ev_check *w)
1579{ 1595{
1580 ev_clear_pending (EV_A_ (W)w); 1596 ev_clear_pending (EV_A_ (W)w);
1581 if (expect_false (!ev_is_active (w))) 1597 if (expect_false (!ev_is_active (w)))
1582 return; 1598 return;
1583 1599
1588#ifndef SA_RESTART 1604#ifndef SA_RESTART
1589# define SA_RESTART 0 1605# define SA_RESTART 0
1590#endif 1606#endif
1591 1607
1592void 1608void
1593ev_signal_start (EV_P_ struct ev_signal *w) 1609ev_signal_start (EV_P_ ev_signal *w)
1594{ 1610{
1595#if EV_MULTIPLICITY 1611#if EV_MULTIPLICITY
1596 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1612 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1597#endif 1613#endif
1598 if (expect_false (ev_is_active (w))) 1614 if (expect_false (ev_is_active (w)))
1617#endif 1633#endif
1618 } 1634 }
1619} 1635}
1620 1636
1621void 1637void
1622ev_signal_stop (EV_P_ struct ev_signal *w) 1638ev_signal_stop (EV_P_ ev_signal *w)
1623{ 1639{
1624 ev_clear_pending (EV_A_ (W)w); 1640 ev_clear_pending (EV_A_ (W)w);
1625 if (expect_false (!ev_is_active (w))) 1641 if (expect_false (!ev_is_active (w)))
1626 return; 1642 return;
1627 1643
1631 if (!signals [w->signum - 1].head) 1647 if (!signals [w->signum - 1].head)
1632 signal (w->signum, SIG_DFL); 1648 signal (w->signum, SIG_DFL);
1633} 1649}
1634 1650
1635void 1651void
1636ev_child_start (EV_P_ struct ev_child *w) 1652ev_child_start (EV_P_ ev_child *w)
1637{ 1653{
1638#if EV_MULTIPLICITY 1654#if EV_MULTIPLICITY
1639 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1655 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1640#endif 1656#endif
1641 if (expect_false (ev_is_active (w))) 1657 if (expect_false (ev_is_active (w)))
1644 ev_start (EV_A_ (W)w, 1); 1660 ev_start (EV_A_ (W)w, 1);
1645 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1661 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1646} 1662}
1647 1663
1648void 1664void
1649ev_child_stop (EV_P_ struct ev_child *w) 1665ev_child_stop (EV_P_ ev_child *w)
1650{ 1666{
1651 ev_clear_pending (EV_A_ (W)w); 1667 ev_clear_pending (EV_A_ (W)w);
1652 if (expect_false (!ev_is_active (w))) 1668 if (expect_false (!ev_is_active (w)))
1653 return; 1669 return;
1654 1670
1655 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1671 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1656 ev_stop (EV_A_ (W)w); 1672 ev_stop (EV_A_ (W)w);
1657} 1673}
1658 1674
1675#if EV_MULTIPLICITY
1676void
1677ev_embed_loop (EV_P_ ev_embed *w)
1678{
1679 ev_loop (w->loop, EVLOOP_NONBLOCK);
1680}
1681
1682static void
1683embed_cb (EV_P_ ev_io *io, int revents)
1684{
1685 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1686
1687 if (ev_cb (w))
1688 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1689 else
1690 ev_embed_loop (loop, w);
1691}
1692
1693void
1694ev_embed_start (EV_P_ ev_embed *w)
1695{
1696 if (expect_false (ev_is_active (w)))
1697 return;
1698
1699 {
1700 struct ev_loop *loop = w->loop;
1701 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1702 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1703 }
1704
1705 ev_set_priority (&w->io, ev_priority (w));
1706 ev_io_start (EV_A_ &w->io);
1707 ev_start (EV_A_ (W)w, 1);
1708}
1709
1710void
1711ev_embed_stop (EV_P_ ev_embed *w)
1712{
1713 ev_clear_pending (EV_A_ (W)w);
1714 if (expect_false (!ev_is_active (w)))
1715 return;
1716
1717 ev_io_stop (EV_A_ &w->io);
1718 ev_stop (EV_A_ (W)w);
1719}
1720#endif
1721
1659/*****************************************************************************/ 1722/*****************************************************************************/
1660 1723
1661struct ev_once 1724struct ev_once
1662{ 1725{
1663 struct ev_io io; 1726 ev_io io;
1664 struct ev_timer to; 1727 ev_timer to;
1665 void (*cb)(int revents, void *arg); 1728 void (*cb)(int revents, void *arg);
1666 void *arg; 1729 void *arg;
1667}; 1730};
1668 1731
1669static void 1732static void
1678 1741
1679 cb (revents, arg); 1742 cb (revents, arg);
1680} 1743}
1681 1744
1682static void 1745static void
1683once_cb_io (EV_P_ struct ev_io *w, int revents) 1746once_cb_io (EV_P_ ev_io *w, int revents)
1684{ 1747{
1685 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1748 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1686} 1749}
1687 1750
1688static void 1751static void
1689once_cb_to (EV_P_ struct ev_timer *w, int revents) 1752once_cb_to (EV_P_ ev_timer *w, int revents)
1690{ 1753{
1691 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1754 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1692} 1755}
1693 1756
1694void 1757void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines