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

Comparing libev/ev.c (file contents):
Revision 1.132 by root, Fri Nov 23 10:36:30 2007 UTC vs.
Revision 1.139 by root, Sun Nov 25 09:24:37 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 {
812 816
813 return flags; 817 return flags;
814} 818}
815 819
816unsigned int 820unsigned int
821ev_embeddable_backends (void)
822{
823 return EVBACKEND_EPOLL
824 | EVBACKEND_KQUEUE
825 | EVBACKEND_PORT;
826}
827
828unsigned int
817ev_backend (EV_P) 829ev_backend (EV_P)
818{ 830{
819 return backend; 831 return backend;
820} 832}
821 833
1062 { 1074 {
1063 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1075 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1064 1076
1065 if (expect_true (p->w)) 1077 if (expect_true (p->w))
1066 { 1078 {
1079 assert (("non-pending watcher on pending list", p->w->pending));
1080
1067 p->w->pending = 0; 1081 p->w->pending = 0;
1068 EV_CB_INVOKE (p->w, p->events); 1082 EV_CB_INVOKE (p->w, p->events);
1069 } 1083 }
1070 } 1084 }
1071} 1085}
1073inline void 1087inline void
1074timers_reify (EV_P) 1088timers_reify (EV_P)
1075{ 1089{
1076 while (timercnt && ((WT)timers [0])->at <= mn_now) 1090 while (timercnt && ((WT)timers [0])->at <= mn_now)
1077 { 1091 {
1078 struct ev_timer *w = timers [0]; 1092 ev_timer *w = timers [0];
1079 1093
1080 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1094 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1081 1095
1082 /* first reschedule or stop timer */ 1096 /* first reschedule or stop timer */
1083 if (w->repeat) 1097 if (w->repeat)
1101inline void 1115inline void
1102periodics_reify (EV_P) 1116periodics_reify (EV_P)
1103{ 1117{
1104 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1118 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1105 { 1119 {
1106 struct ev_periodic *w = periodics [0]; 1120 ev_periodic *w = periodics [0];
1107 1121
1108 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1122 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1109 1123
1110 /* first reschedule or stop timer */ 1124 /* first reschedule or stop timer */
1111 if (w->reschedule_cb) 1125 if (w->reschedule_cb)
1133 int i; 1147 int i;
1134 1148
1135 /* adjust periodics after time jump */ 1149 /* adjust periodics after time jump */
1136 for (i = 0; i < periodiccnt; ++i) 1150 for (i = 0; i < periodiccnt; ++i)
1137 { 1151 {
1138 struct ev_periodic *w = periodics [i]; 1152 ev_periodic *w = periodics [i];
1139 1153
1140 if (w->reschedule_cb) 1154 if (w->reschedule_cb)
1141 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1155 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1142 else if (w->interval) 1156 else if (w->interval)
1143 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1157 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1177 { 1191 {
1178 if (time_update_monotonic (EV_A)) 1192 if (time_update_monotonic (EV_A))
1179 { 1193 {
1180 ev_tstamp odiff = rtmn_diff; 1194 ev_tstamp odiff = rtmn_diff;
1181 1195
1182 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1196 /* loop a few times, before making important decisions.
1197 * on the choice of "4": one iteration isn't enough,
1198 * in case we get preempted during the calls to
1199 * ev_time and get_clock. a second call is almost guarenteed
1200 * to succeed in that case, though. and looping a few more times
1201 * doesn't hurt either as we only do this on time-jumps or
1202 * in the unlikely event of getting preempted here.
1203 */
1204 for (i = 4; --i; )
1183 { 1205 {
1184 rtmn_diff = ev_rt_now - mn_now; 1206 rtmn_diff = ev_rt_now - mn_now;
1185 1207
1186 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1208 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1187 return; /* all is well */ 1209 return; /* all is well */
1233static int loop_done; 1255static int loop_done;
1234 1256
1235void 1257void
1236ev_loop (EV_P_ int flags) 1258ev_loop (EV_P_ int flags)
1237{ 1259{
1238 double block;
1239 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1260 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1261 ? EVUNLOOP_ONE
1262 : EVUNLOOP_CANCEL;
1240 1263
1241 while (activecnt) 1264 while (activecnt)
1242 { 1265 {
1243 /* queue check watchers (and execute them) */ 1266 /* queue check watchers (and execute them) */
1244 if (expect_false (preparecnt)) 1267 if (expect_false (preparecnt))
1253 1276
1254 /* update fd-related kernel structures */ 1277 /* update fd-related kernel structures */
1255 fd_reify (EV_A); 1278 fd_reify (EV_A);
1256 1279
1257 /* calculate blocking time */ 1280 /* calculate blocking time */
1281 {
1282 double block;
1258 1283
1259 /* we only need this for !monotonic clock or timers, but as we basically 1284 if (flags & EVLOOP_NONBLOCK || idlecnt)
1260 always have timers, we just calculate it always */ 1285 block = 0.; /* do not block at all */
1286 else
1287 {
1288 /* update time to cancel out callback processing overhead */
1261#if EV_USE_MONOTONIC 1289#if EV_USE_MONOTONIC
1262 if (expect_true (have_monotonic)) 1290 if (expect_true (have_monotonic))
1263 time_update_monotonic (EV_A); 1291 time_update_monotonic (EV_A);
1264 else 1292 else
1265#endif 1293#endif
1266 { 1294 {
1267 ev_rt_now = ev_time (); 1295 ev_rt_now = ev_time ();
1268 mn_now = ev_rt_now; 1296 mn_now = ev_rt_now;
1269 } 1297 }
1270 1298
1271 if (flags & EVLOOP_NONBLOCK || idlecnt)
1272 block = 0.;
1273 else
1274 {
1275 block = MAX_BLOCKTIME; 1299 block = MAX_BLOCKTIME;
1276 1300
1277 if (timercnt) 1301 if (timercnt)
1278 { 1302 {
1279 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1303 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1280 if (block > to) block = to; 1304 if (block > to) block = to;
1281 } 1305 }
1282 1306
1283#if EV_PERIODICS 1307#if EV_PERIODICS
1284 if (periodiccnt) 1308 if (periodiccnt)
1285 { 1309 {
1286 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge; 1310 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + backend_fudge;
1287 if (block > to) block = to; 1311 if (block > to) block = to;
1288 } 1312 }
1289#endif 1313#endif
1290 1314
1291 if (expect_false (block < 0.)) block = 0.; 1315 if (expect_false (block < 0.)) block = 0.;
1292 } 1316 }
1293 1317
1294 backend_poll (EV_A_ block); 1318 backend_poll (EV_A_ block);
1319 }
1295 1320
1296 /* update ev_rt_now, do magic */ 1321 /* update ev_rt_now, do magic */
1297 time_update (EV_A); 1322 time_update (EV_A);
1298 1323
1299 /* queue pending timers and reschedule them */ 1324 /* queue pending timers and reschedule them */
1300 timers_reify (EV_A); /* relative timers called last */ 1325 timers_reify (EV_A); /* relative timers called last */
1301#if EV_PERIODICS 1326#if EV_PERIODICS
1302 periodics_reify (EV_A); /* absolute timers called first */ 1327 periodics_reify (EV_A); /* absolute timers called first */
1303#endif 1328#endif
1304 1329
1305 /* queue idle watchers unless io or timers are pending */ 1330 /* queue idle watchers unless other events are pending */
1306 if (idlecnt && !any_pending (EV_A)) 1331 if (idlecnt && !any_pending (EV_A))
1307 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1332 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1308 1333
1309 /* queue check watchers, to be executed first */ 1334 /* queue check watchers, to be executed first */
1310 if (expect_false (checkcnt)) 1335 if (expect_false (checkcnt))
1314 1339
1315 if (expect_false (loop_done)) 1340 if (expect_false (loop_done))
1316 break; 1341 break;
1317 } 1342 }
1318 1343
1319 if (loop_done != 2) 1344 if (loop_done == EVUNLOOP_ONE)
1320 loop_done = 0; 1345 loop_done = EVUNLOOP_CANCEL;
1321} 1346}
1322 1347
1323void 1348void
1324ev_unloop (EV_P_ int how) 1349ev_unloop (EV_P_ int how)
1325{ 1350{
1378} 1403}
1379 1404
1380/*****************************************************************************/ 1405/*****************************************************************************/
1381 1406
1382void 1407void
1383ev_io_start (EV_P_ struct ev_io *w) 1408ev_io_start (EV_P_ ev_io *w)
1384{ 1409{
1385 int fd = w->fd; 1410 int fd = w->fd;
1386 1411
1387 if (expect_false (ev_is_active (w))) 1412 if (expect_false (ev_is_active (w)))
1388 return; 1413 return;
1395 1420
1396 fd_change (EV_A_ fd); 1421 fd_change (EV_A_ fd);
1397} 1422}
1398 1423
1399void 1424void
1400ev_io_stop (EV_P_ struct ev_io *w) 1425ev_io_stop (EV_P_ ev_io *w)
1401{ 1426{
1402 ev_clear_pending (EV_A_ (W)w); 1427 ev_clear_pending (EV_A_ (W)w);
1403 if (expect_false (!ev_is_active (w))) 1428 if (expect_false (!ev_is_active (w)))
1404 return; 1429 return;
1405 1430
1410 1435
1411 fd_change (EV_A_ w->fd); 1436 fd_change (EV_A_ w->fd);
1412} 1437}
1413 1438
1414void 1439void
1415ev_timer_start (EV_P_ struct ev_timer *w) 1440ev_timer_start (EV_P_ ev_timer *w)
1416{ 1441{
1417 if (expect_false (ev_is_active (w))) 1442 if (expect_false (ev_is_active (w)))
1418 return; 1443 return;
1419 1444
1420 ((WT)w)->at += mn_now; 1445 ((WT)w)->at += mn_now;
1421 1446
1422 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1447 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1423 1448
1424 ev_start (EV_A_ (W)w, ++timercnt); 1449 ev_start (EV_A_ (W)w, ++timercnt);
1425 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1450 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1426 timers [timercnt - 1] = w; 1451 timers [timercnt - 1] = w;
1427 upheap ((WT *)timers, timercnt - 1); 1452 upheap ((WT *)timers, timercnt - 1);
1428 1453
1429 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1454 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1430} 1455}
1431 1456
1432void 1457void
1433ev_timer_stop (EV_P_ struct ev_timer *w) 1458ev_timer_stop (EV_P_ ev_timer *w)
1434{ 1459{
1435 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1436 if (expect_false (!ev_is_active (w))) 1461 if (expect_false (!ev_is_active (w)))
1437 return; 1462 return;
1438 1463
1448 1473
1449 ev_stop (EV_A_ (W)w); 1474 ev_stop (EV_A_ (W)w);
1450} 1475}
1451 1476
1452void 1477void
1453ev_timer_again (EV_P_ struct ev_timer *w) 1478ev_timer_again (EV_P_ ev_timer *w)
1454{ 1479{
1455 if (ev_is_active (w)) 1480 if (ev_is_active (w))
1456 { 1481 {
1457 if (w->repeat) 1482 if (w->repeat)
1458 { 1483 {
1469 } 1494 }
1470} 1495}
1471 1496
1472#if EV_PERIODICS 1497#if EV_PERIODICS
1473void 1498void
1474ev_periodic_start (EV_P_ struct ev_periodic *w) 1499ev_periodic_start (EV_P_ ev_periodic *w)
1475{ 1500{
1476 if (expect_false (ev_is_active (w))) 1501 if (expect_false (ev_is_active (w)))
1477 return; 1502 return;
1478 1503
1479 if (w->reschedule_cb) 1504 if (w->reschedule_cb)
1484 /* this formula differs from the one in periodic_reify because we do not always round up */ 1509 /* this formula differs from the one in periodic_reify because we do not always round up */
1485 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval; 1510 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1486 } 1511 }
1487 1512
1488 ev_start (EV_A_ (W)w, ++periodiccnt); 1513 ev_start (EV_A_ (W)w, ++periodiccnt);
1489 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1514 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1490 periodics [periodiccnt - 1] = w; 1515 periodics [periodiccnt - 1] = w;
1491 upheap ((WT *)periodics, periodiccnt - 1); 1516 upheap ((WT *)periodics, periodiccnt - 1);
1492 1517
1493 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1518 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1494} 1519}
1495 1520
1496void 1521void
1497ev_periodic_stop (EV_P_ struct ev_periodic *w) 1522ev_periodic_stop (EV_P_ ev_periodic *w)
1498{ 1523{
1499 ev_clear_pending (EV_A_ (W)w); 1524 ev_clear_pending (EV_A_ (W)w);
1500 if (expect_false (!ev_is_active (w))) 1525 if (expect_false (!ev_is_active (w)))
1501 return; 1526 return;
1502 1527
1510 1535
1511 ev_stop (EV_A_ (W)w); 1536 ev_stop (EV_A_ (W)w);
1512} 1537}
1513 1538
1514void 1539void
1515ev_periodic_again (EV_P_ struct ev_periodic *w) 1540ev_periodic_again (EV_P_ ev_periodic *w)
1516{ 1541{
1517 /* TODO: use adjustheap and recalculation */ 1542 /* TODO: use adjustheap and recalculation */
1518 ev_periodic_stop (EV_A_ w); 1543 ev_periodic_stop (EV_A_ w);
1519 ev_periodic_start (EV_A_ w); 1544 ev_periodic_start (EV_A_ w);
1520} 1545}
1521#endif 1546#endif
1522 1547
1523void 1548void
1524ev_idle_start (EV_P_ struct ev_idle *w) 1549ev_idle_start (EV_P_ ev_idle *w)
1525{ 1550{
1526 if (expect_false (ev_is_active (w))) 1551 if (expect_false (ev_is_active (w)))
1527 return; 1552 return;
1528 1553
1529 ev_start (EV_A_ (W)w, ++idlecnt); 1554 ev_start (EV_A_ (W)w, ++idlecnt);
1530 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1555 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1531 idles [idlecnt - 1] = w; 1556 idles [idlecnt - 1] = w;
1532} 1557}
1533 1558
1534void 1559void
1535ev_idle_stop (EV_P_ struct ev_idle *w) 1560ev_idle_stop (EV_P_ ev_idle *w)
1536{ 1561{
1537 ev_clear_pending (EV_A_ (W)w); 1562 ev_clear_pending (EV_A_ (W)w);
1538 if (expect_false (!ev_is_active (w))) 1563 if (expect_false (!ev_is_active (w)))
1539 return; 1564 return;
1540 1565
1566 {
1567 int active = ((W)w)->active;
1541 idles [((W)w)->active - 1] = idles [--idlecnt]; 1568 idles [active - 1] = idles [--idlecnt];
1569 ((W)idles [active - 1])->active = active;
1570 }
1571
1542 ev_stop (EV_A_ (W)w); 1572 ev_stop (EV_A_ (W)w);
1543} 1573}
1544 1574
1545void 1575void
1546ev_prepare_start (EV_P_ struct ev_prepare *w) 1576ev_prepare_start (EV_P_ ev_prepare *w)
1547{ 1577{
1548 if (expect_false (ev_is_active (w))) 1578 if (expect_false (ev_is_active (w)))
1549 return; 1579 return;
1550 1580
1551 ev_start (EV_A_ (W)w, ++preparecnt); 1581 ev_start (EV_A_ (W)w, ++preparecnt);
1552 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1582 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1553 prepares [preparecnt - 1] = w; 1583 prepares [preparecnt - 1] = w;
1554} 1584}
1555 1585
1556void 1586void
1557ev_prepare_stop (EV_P_ struct ev_prepare *w) 1587ev_prepare_stop (EV_P_ ev_prepare *w)
1558{ 1588{
1559 ev_clear_pending (EV_A_ (W)w); 1589 ev_clear_pending (EV_A_ (W)w);
1560 if (expect_false (!ev_is_active (w))) 1590 if (expect_false (!ev_is_active (w)))
1561 return; 1591 return;
1562 1592
1593 {
1594 int active = ((W)w)->active;
1563 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1595 prepares [active - 1] = prepares [--preparecnt];
1596 ((W)prepares [active - 1])->active = active;
1597 }
1598
1564 ev_stop (EV_A_ (W)w); 1599 ev_stop (EV_A_ (W)w);
1565} 1600}
1566 1601
1567void 1602void
1568ev_check_start (EV_P_ struct ev_check *w) 1603ev_check_start (EV_P_ ev_check *w)
1569{ 1604{
1570 if (expect_false (ev_is_active (w))) 1605 if (expect_false (ev_is_active (w)))
1571 return; 1606 return;
1572 1607
1573 ev_start (EV_A_ (W)w, ++checkcnt); 1608 ev_start (EV_A_ (W)w, ++checkcnt);
1574 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1609 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1575 checks [checkcnt - 1] = w; 1610 checks [checkcnt - 1] = w;
1576} 1611}
1577 1612
1578void 1613void
1579ev_check_stop (EV_P_ struct ev_check *w) 1614ev_check_stop (EV_P_ ev_check *w)
1580{ 1615{
1581 ev_clear_pending (EV_A_ (W)w); 1616 ev_clear_pending (EV_A_ (W)w);
1582 if (expect_false (!ev_is_active (w))) 1617 if (expect_false (!ev_is_active (w)))
1583 return; 1618 return;
1584 1619
1620 {
1621 int active = ((W)w)->active;
1585 checks [((W)w)->active - 1] = checks [--checkcnt]; 1622 checks [active - 1] = checks [--checkcnt];
1623 ((W)checks [active - 1])->active = active;
1624 }
1625
1586 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1587} 1627}
1588 1628
1589#ifndef SA_RESTART 1629#ifndef SA_RESTART
1590# define SA_RESTART 0 1630# define SA_RESTART 0
1591#endif 1631#endif
1592 1632
1593void 1633void
1594ev_signal_start (EV_P_ struct ev_signal *w) 1634ev_signal_start (EV_P_ ev_signal *w)
1595{ 1635{
1596#if EV_MULTIPLICITY 1636#if EV_MULTIPLICITY
1597 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1637 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1598#endif 1638#endif
1599 if (expect_false (ev_is_active (w))) 1639 if (expect_false (ev_is_active (w)))
1618#endif 1658#endif
1619 } 1659 }
1620} 1660}
1621 1661
1622void 1662void
1623ev_signal_stop (EV_P_ struct ev_signal *w) 1663ev_signal_stop (EV_P_ ev_signal *w)
1624{ 1664{
1625 ev_clear_pending (EV_A_ (W)w); 1665 ev_clear_pending (EV_A_ (W)w);
1626 if (expect_false (!ev_is_active (w))) 1666 if (expect_false (!ev_is_active (w)))
1627 return; 1667 return;
1628 1668
1632 if (!signals [w->signum - 1].head) 1672 if (!signals [w->signum - 1].head)
1633 signal (w->signum, SIG_DFL); 1673 signal (w->signum, SIG_DFL);
1634} 1674}
1635 1675
1636void 1676void
1637ev_child_start (EV_P_ struct ev_child *w) 1677ev_child_start (EV_P_ ev_child *w)
1638{ 1678{
1639#if EV_MULTIPLICITY 1679#if EV_MULTIPLICITY
1640 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1680 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1641#endif 1681#endif
1642 if (expect_false (ev_is_active (w))) 1682 if (expect_false (ev_is_active (w)))
1645 ev_start (EV_A_ (W)w, 1); 1685 ev_start (EV_A_ (W)w, 1);
1646 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1686 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1647} 1687}
1648 1688
1649void 1689void
1650ev_child_stop (EV_P_ struct ev_child *w) 1690ev_child_stop (EV_P_ ev_child *w)
1651{ 1691{
1652 ev_clear_pending (EV_A_ (W)w); 1692 ev_clear_pending (EV_A_ (W)w);
1653 if (expect_false (!ev_is_active (w))) 1693 if (expect_false (!ev_is_active (w)))
1654 return; 1694 return;
1655 1695
1656 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1696 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1657 ev_stop (EV_A_ (W)w); 1697 ev_stop (EV_A_ (W)w);
1658} 1698}
1659 1699
1700#if EV_MULTIPLICITY
1701void
1702ev_embed_sweep (EV_P_ ev_embed *w)
1703{
1704 ev_loop (w->loop, EVLOOP_NONBLOCK);
1705}
1706
1707static void
1708embed_cb (EV_P_ ev_io *io, int revents)
1709{
1710 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
1711
1712 if (ev_cb (w))
1713 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1714 else
1715 ev_embed_sweep (loop, w);
1716}
1717
1718void
1719ev_embed_start (EV_P_ ev_embed *w)
1720{
1721 if (expect_false (ev_is_active (w)))
1722 return;
1723
1724 {
1725 struct ev_loop *loop = w->loop;
1726 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1727 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1728 }
1729
1730 ev_set_priority (&w->io, ev_priority (w));
1731 ev_io_start (EV_A_ &w->io);
1732 ev_start (EV_A_ (W)w, 1);
1733}
1734
1735void
1736ev_embed_stop (EV_P_ ev_embed *w)
1737{
1738 ev_clear_pending (EV_A_ (W)w);
1739 if (expect_false (!ev_is_active (w)))
1740 return;
1741
1742 ev_io_stop (EV_A_ &w->io);
1743 ev_stop (EV_A_ (W)w);
1744}
1745#endif
1746
1660/*****************************************************************************/ 1747/*****************************************************************************/
1661 1748
1662struct ev_once 1749struct ev_once
1663{ 1750{
1664 struct ev_io io; 1751 ev_io io;
1665 struct ev_timer to; 1752 ev_timer to;
1666 void (*cb)(int revents, void *arg); 1753 void (*cb)(int revents, void *arg);
1667 void *arg; 1754 void *arg;
1668}; 1755};
1669 1756
1670static void 1757static void
1679 1766
1680 cb (revents, arg); 1767 cb (revents, arg);
1681} 1768}
1682 1769
1683static void 1770static void
1684once_cb_io (EV_P_ struct ev_io *w, int revents) 1771once_cb_io (EV_P_ ev_io *w, int revents)
1685{ 1772{
1686 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1773 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1687} 1774}
1688 1775
1689static void 1776static void
1690once_cb_to (EV_P_ struct ev_timer *w, int revents) 1777once_cb_to (EV_P_ ev_timer *w, int revents)
1691{ 1778{
1692 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1779 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1693} 1780}
1694 1781
1695void 1782void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines