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.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 {
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
1061 { 1074 {
1062 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1075 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1063 1076
1064 if (expect_true (p->w)) 1077 if (expect_true (p->w))
1065 { 1078 {
1079 assert (("non-pending watcher on pending list", p->w->pending));
1080
1066 p->w->pending = 0; 1081 p->w->pending = 0;
1067 EV_CB_INVOKE (p->w, p->events); 1082 EV_CB_INVOKE (p->w, p->events);
1068 } 1083 }
1069 } 1084 }
1070} 1085}
1072inline void 1087inline void
1073timers_reify (EV_P) 1088timers_reify (EV_P)
1074{ 1089{
1075 while (timercnt && ((WT)timers [0])->at <= mn_now) 1090 while (timercnt && ((WT)timers [0])->at <= mn_now)
1076 { 1091 {
1077 struct ev_timer *w = timers [0]; 1092 ev_timer *w = timers [0];
1078 1093
1079 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1094 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1080 1095
1081 /* first reschedule or stop timer */ 1096 /* first reschedule or stop timer */
1082 if (w->repeat) 1097 if (w->repeat)
1100inline void 1115inline void
1101periodics_reify (EV_P) 1116periodics_reify (EV_P)
1102{ 1117{
1103 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1118 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1104 { 1119 {
1105 struct ev_periodic *w = periodics [0]; 1120 ev_periodic *w = periodics [0];
1106 1121
1107 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1122 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1108 1123
1109 /* first reschedule or stop timer */ 1124 /* first reschedule or stop timer */
1110 if (w->reschedule_cb) 1125 if (w->reschedule_cb)
1132 int i; 1147 int i;
1133 1148
1134 /* adjust periodics after time jump */ 1149 /* adjust periodics after time jump */
1135 for (i = 0; i < periodiccnt; ++i) 1150 for (i = 0; i < periodiccnt; ++i)
1136 { 1151 {
1137 struct ev_periodic *w = periodics [i]; 1152 ev_periodic *w = periodics [i];
1138 1153
1139 if (w->reschedule_cb) 1154 if (w->reschedule_cb)
1140 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1155 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1141 else if (w->interval) 1156 else if (w->interval)
1142 ((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;
1176 { 1191 {
1177 if (time_update_monotonic (EV_A)) 1192 if (time_update_monotonic (EV_A))
1178 { 1193 {
1179 ev_tstamp odiff = rtmn_diff; 1194 ev_tstamp odiff = rtmn_diff;
1180 1195
1181 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; )
1182 { 1205 {
1183 rtmn_diff = ev_rt_now - mn_now; 1206 rtmn_diff = ev_rt_now - mn_now;
1184 1207
1185 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1208 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1186 return; /* all is well */ 1209 return; /* all is well */
1232static int loop_done; 1255static int loop_done;
1233 1256
1234void 1257void
1235ev_loop (EV_P_ int flags) 1258ev_loop (EV_P_ int flags)
1236{ 1259{
1237 double block;
1238 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1260 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1261 ? EVUNLOOP_ONE
1262 : EVUNLOOP_CANCEL;
1239 1263
1240 while (activecnt) 1264 while (activecnt)
1241 { 1265 {
1242 /* queue check watchers (and execute them) */ 1266 /* queue check watchers (and execute them) */
1243 if (expect_false (preparecnt)) 1267 if (expect_false (preparecnt))
1252 1276
1253 /* update fd-related kernel structures */ 1277 /* update fd-related kernel structures */
1254 fd_reify (EV_A); 1278 fd_reify (EV_A);
1255 1279
1256 /* calculate blocking time */ 1280 /* calculate blocking time */
1281 {
1282 double block;
1257 1283
1258 /* we only need this for !monotonic clock or timers, but as we basically 1284 if (flags & EVLOOP_NONBLOCK || idlecnt)
1259 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 */
1260#if EV_USE_MONOTONIC 1289#if EV_USE_MONOTONIC
1261 if (expect_true (have_monotonic)) 1290 if (expect_true (have_monotonic))
1262 time_update_monotonic (EV_A); 1291 time_update_monotonic (EV_A);
1263 else 1292 else
1264#endif 1293#endif
1265 { 1294 {
1266 ev_rt_now = ev_time (); 1295 ev_rt_now = ev_time ();
1267 mn_now = ev_rt_now; 1296 mn_now = ev_rt_now;
1268 } 1297 }
1269 1298
1270 if (flags & EVLOOP_NONBLOCK || idlecnt)
1271 block = 0.;
1272 else
1273 {
1274 block = MAX_BLOCKTIME; 1299 block = MAX_BLOCKTIME;
1275 1300
1276 if (timercnt) 1301 if (timercnt)
1277 { 1302 {
1278 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1303 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1279 if (block > to) block = to; 1304 if (block > to) block = to;
1280 } 1305 }
1281 1306
1282#if EV_PERIODICS 1307#if EV_PERIODICS
1283 if (periodiccnt) 1308 if (periodiccnt)
1284 { 1309 {
1285 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;
1286 if (block > to) block = to; 1311 if (block > to) block = to;
1287 } 1312 }
1288#endif 1313#endif
1289 1314
1290 if (expect_false (block < 0.)) block = 0.; 1315 if (expect_false (block < 0.)) block = 0.;
1291 } 1316 }
1292 1317
1293 backend_poll (EV_A_ block); 1318 backend_poll (EV_A_ block);
1319 }
1294 1320
1295 /* update ev_rt_now, do magic */ 1321 /* update ev_rt_now, do magic */
1296 time_update (EV_A); 1322 time_update (EV_A);
1297 1323
1298 /* queue pending timers and reschedule them */ 1324 /* queue pending timers and reschedule them */
1299 timers_reify (EV_A); /* relative timers called last */ 1325 timers_reify (EV_A); /* relative timers called last */
1300#if EV_PERIODICS 1326#if EV_PERIODICS
1301 periodics_reify (EV_A); /* absolute timers called first */ 1327 periodics_reify (EV_A); /* absolute timers called first */
1302#endif 1328#endif
1303 1329
1304 /* queue idle watchers unless io or timers are pending */ 1330 /* queue idle watchers unless other events are pending */
1305 if (idlecnt && !any_pending (EV_A)) 1331 if (idlecnt && !any_pending (EV_A))
1306 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1332 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1307 1333
1308 /* queue check watchers, to be executed first */ 1334 /* queue check watchers, to be executed first */
1309 if (expect_false (checkcnt)) 1335 if (expect_false (checkcnt))
1313 1339
1314 if (expect_false (loop_done)) 1340 if (expect_false (loop_done))
1315 break; 1341 break;
1316 } 1342 }
1317 1343
1318 if (loop_done != 2) 1344 if (loop_done == EVUNLOOP_ONE)
1319 loop_done = 0; 1345 loop_done = EVUNLOOP_CANCEL;
1320} 1346}
1321 1347
1322void 1348void
1323ev_unloop (EV_P_ int how) 1349ev_unloop (EV_P_ int how)
1324{ 1350{
1377} 1403}
1378 1404
1379/*****************************************************************************/ 1405/*****************************************************************************/
1380 1406
1381void 1407void
1382ev_io_start (EV_P_ struct ev_io *w) 1408ev_io_start (EV_P_ ev_io *w)
1383{ 1409{
1384 int fd = w->fd; 1410 int fd = w->fd;
1385 1411
1386 if (expect_false (ev_is_active (w))) 1412 if (expect_false (ev_is_active (w)))
1387 return; 1413 return;
1394 1420
1395 fd_change (EV_A_ fd); 1421 fd_change (EV_A_ fd);
1396} 1422}
1397 1423
1398void 1424void
1399ev_io_stop (EV_P_ struct ev_io *w) 1425ev_io_stop (EV_P_ ev_io *w)
1400{ 1426{
1401 ev_clear_pending (EV_A_ (W)w); 1427 ev_clear_pending (EV_A_ (W)w);
1402 if (expect_false (!ev_is_active (w))) 1428 if (expect_false (!ev_is_active (w)))
1403 return; 1429 return;
1404 1430
1409 1435
1410 fd_change (EV_A_ w->fd); 1436 fd_change (EV_A_ w->fd);
1411} 1437}
1412 1438
1413void 1439void
1414ev_timer_start (EV_P_ struct ev_timer *w) 1440ev_timer_start (EV_P_ ev_timer *w)
1415{ 1441{
1416 if (expect_false (ev_is_active (w))) 1442 if (expect_false (ev_is_active (w)))
1417 return; 1443 return;
1418 1444
1419 ((WT)w)->at += mn_now; 1445 ((WT)w)->at += mn_now;
1420 1446
1421 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.));
1422 1448
1423 ev_start (EV_A_ (W)w, ++timercnt); 1449 ev_start (EV_A_ (W)w, ++timercnt);
1424 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1450 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1425 timers [timercnt - 1] = w; 1451 timers [timercnt - 1] = w;
1426 upheap ((WT *)timers, timercnt - 1); 1452 upheap ((WT *)timers, timercnt - 1);
1427 1453
1428 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1454 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1429} 1455}
1430 1456
1431void 1457void
1432ev_timer_stop (EV_P_ struct ev_timer *w) 1458ev_timer_stop (EV_P_ ev_timer *w)
1433{ 1459{
1434 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1435 if (expect_false (!ev_is_active (w))) 1461 if (expect_false (!ev_is_active (w)))
1436 return; 1462 return;
1437 1463
1447 1473
1448 ev_stop (EV_A_ (W)w); 1474 ev_stop (EV_A_ (W)w);
1449} 1475}
1450 1476
1451void 1477void
1452ev_timer_again (EV_P_ struct ev_timer *w) 1478ev_timer_again (EV_P_ ev_timer *w)
1453{ 1479{
1454 if (ev_is_active (w)) 1480 if (ev_is_active (w))
1455 { 1481 {
1456 if (w->repeat) 1482 if (w->repeat)
1457 { 1483 {
1468 } 1494 }
1469} 1495}
1470 1496
1471#if EV_PERIODICS 1497#if EV_PERIODICS
1472void 1498void
1473ev_periodic_start (EV_P_ struct ev_periodic *w) 1499ev_periodic_start (EV_P_ ev_periodic *w)
1474{ 1500{
1475 if (expect_false (ev_is_active (w))) 1501 if (expect_false (ev_is_active (w)))
1476 return; 1502 return;
1477 1503
1478 if (w->reschedule_cb) 1504 if (w->reschedule_cb)
1483 /* 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 */
1484 ((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;
1485 } 1511 }
1486 1512
1487 ev_start (EV_A_ (W)w, ++periodiccnt); 1513 ev_start (EV_A_ (W)w, ++periodiccnt);
1488 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1514 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1489 periodics [periodiccnt - 1] = w; 1515 periodics [periodiccnt - 1] = w;
1490 upheap ((WT *)periodics, periodiccnt - 1); 1516 upheap ((WT *)periodics, periodiccnt - 1);
1491 1517
1492 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1518 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1493} 1519}
1494 1520
1495void 1521void
1496ev_periodic_stop (EV_P_ struct ev_periodic *w) 1522ev_periodic_stop (EV_P_ ev_periodic *w)
1497{ 1523{
1498 ev_clear_pending (EV_A_ (W)w); 1524 ev_clear_pending (EV_A_ (W)w);
1499 if (expect_false (!ev_is_active (w))) 1525 if (expect_false (!ev_is_active (w)))
1500 return; 1526 return;
1501 1527
1509 1535
1510 ev_stop (EV_A_ (W)w); 1536 ev_stop (EV_A_ (W)w);
1511} 1537}
1512 1538
1513void 1539void
1514ev_periodic_again (EV_P_ struct ev_periodic *w) 1540ev_periodic_again (EV_P_ ev_periodic *w)
1515{ 1541{
1516 /* TODO: use adjustheap and recalculation */ 1542 /* TODO: use adjustheap and recalculation */
1517 ev_periodic_stop (EV_A_ w); 1543 ev_periodic_stop (EV_A_ w);
1518 ev_periodic_start (EV_A_ w); 1544 ev_periodic_start (EV_A_ w);
1519} 1545}
1520#endif 1546#endif
1521 1547
1522void 1548void
1523ev_idle_start (EV_P_ struct ev_idle *w) 1549ev_idle_start (EV_P_ ev_idle *w)
1524{ 1550{
1525 if (expect_false (ev_is_active (w))) 1551 if (expect_false (ev_is_active (w)))
1526 return; 1552 return;
1527 1553
1528 ev_start (EV_A_ (W)w, ++idlecnt); 1554 ev_start (EV_A_ (W)w, ++idlecnt);
1529 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1555 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1530 idles [idlecnt - 1] = w; 1556 idles [idlecnt - 1] = w;
1531} 1557}
1532 1558
1533void 1559void
1534ev_idle_stop (EV_P_ struct ev_idle *w) 1560ev_idle_stop (EV_P_ ev_idle *w)
1535{ 1561{
1536 ev_clear_pending (EV_A_ (W)w); 1562 ev_clear_pending (EV_A_ (W)w);
1537 if (expect_false (!ev_is_active (w))) 1563 if (expect_false (!ev_is_active (w)))
1538 return; 1564 return;
1539 1565
1566 {
1567 int active = ((W)w)->active;
1540 idles [((W)w)->active - 1] = idles [--idlecnt]; 1568 idles [active - 1] = idles [--idlecnt];
1569 ((W)idles [active - 1])->active = active;
1570 }
1571
1541 ev_stop (EV_A_ (W)w); 1572 ev_stop (EV_A_ (W)w);
1542} 1573}
1543 1574
1544void 1575void
1545ev_prepare_start (EV_P_ struct ev_prepare *w) 1576ev_prepare_start (EV_P_ ev_prepare *w)
1546{ 1577{
1547 if (expect_false (ev_is_active (w))) 1578 if (expect_false (ev_is_active (w)))
1548 return; 1579 return;
1549 1580
1550 ev_start (EV_A_ (W)w, ++preparecnt); 1581 ev_start (EV_A_ (W)w, ++preparecnt);
1551 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1582 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1552 prepares [preparecnt - 1] = w; 1583 prepares [preparecnt - 1] = w;
1553} 1584}
1554 1585
1555void 1586void
1556ev_prepare_stop (EV_P_ struct ev_prepare *w) 1587ev_prepare_stop (EV_P_ ev_prepare *w)
1557{ 1588{
1558 ev_clear_pending (EV_A_ (W)w); 1589 ev_clear_pending (EV_A_ (W)w);
1559 if (expect_false (!ev_is_active (w))) 1590 if (expect_false (!ev_is_active (w)))
1560 return; 1591 return;
1561 1592
1593 {
1594 int active = ((W)w)->active;
1562 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1595 prepares [active - 1] = prepares [--preparecnt];
1596 ((W)prepares [active - 1])->active = active;
1597 }
1598
1563 ev_stop (EV_A_ (W)w); 1599 ev_stop (EV_A_ (W)w);
1564} 1600}
1565 1601
1566void 1602void
1567ev_check_start (EV_P_ struct ev_check *w) 1603ev_check_start (EV_P_ ev_check *w)
1568{ 1604{
1569 if (expect_false (ev_is_active (w))) 1605 if (expect_false (ev_is_active (w)))
1570 return; 1606 return;
1571 1607
1572 ev_start (EV_A_ (W)w, ++checkcnt); 1608 ev_start (EV_A_ (W)w, ++checkcnt);
1573 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1609 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1574 checks [checkcnt - 1] = w; 1610 checks [checkcnt - 1] = w;
1575} 1611}
1576 1612
1577void 1613void
1578ev_check_stop (EV_P_ struct ev_check *w) 1614ev_check_stop (EV_P_ ev_check *w)
1579{ 1615{
1580 ev_clear_pending (EV_A_ (W)w); 1616 ev_clear_pending (EV_A_ (W)w);
1581 if (expect_false (!ev_is_active (w))) 1617 if (expect_false (!ev_is_active (w)))
1582 return; 1618 return;
1583 1619
1620 {
1621 int active = ((W)w)->active;
1584 checks [((W)w)->active - 1] = checks [--checkcnt]; 1622 checks [active - 1] = checks [--checkcnt];
1623 ((W)checks [active - 1])->active = active;
1624 }
1625
1585 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1586} 1627}
1587 1628
1588#ifndef SA_RESTART 1629#ifndef SA_RESTART
1589# define SA_RESTART 0 1630# define SA_RESTART 0
1590#endif 1631#endif
1591 1632
1592void 1633void
1593ev_signal_start (EV_P_ struct ev_signal *w) 1634ev_signal_start (EV_P_ ev_signal *w)
1594{ 1635{
1595#if EV_MULTIPLICITY 1636#if EV_MULTIPLICITY
1596 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));
1597#endif 1638#endif
1598 if (expect_false (ev_is_active (w))) 1639 if (expect_false (ev_is_active (w)))
1617#endif 1658#endif
1618 } 1659 }
1619} 1660}
1620 1661
1621void 1662void
1622ev_signal_stop (EV_P_ struct ev_signal *w) 1663ev_signal_stop (EV_P_ ev_signal *w)
1623{ 1664{
1624 ev_clear_pending (EV_A_ (W)w); 1665 ev_clear_pending (EV_A_ (W)w);
1625 if (expect_false (!ev_is_active (w))) 1666 if (expect_false (!ev_is_active (w)))
1626 return; 1667 return;
1627 1668
1631 if (!signals [w->signum - 1].head) 1672 if (!signals [w->signum - 1].head)
1632 signal (w->signum, SIG_DFL); 1673 signal (w->signum, SIG_DFL);
1633} 1674}
1634 1675
1635void 1676void
1636ev_child_start (EV_P_ struct ev_child *w) 1677ev_child_start (EV_P_ ev_child *w)
1637{ 1678{
1638#if EV_MULTIPLICITY 1679#if EV_MULTIPLICITY
1639 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));
1640#endif 1681#endif
1641 if (expect_false (ev_is_active (w))) 1682 if (expect_false (ev_is_active (w)))
1644 ev_start (EV_A_ (W)w, 1); 1685 ev_start (EV_A_ (W)w, 1);
1645 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1686 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1646} 1687}
1647 1688
1648void 1689void
1649ev_child_stop (EV_P_ struct ev_child *w) 1690ev_child_stop (EV_P_ ev_child *w)
1650{ 1691{
1651 ev_clear_pending (EV_A_ (W)w); 1692 ev_clear_pending (EV_A_ (W)w);
1652 if (expect_false (!ev_is_active (w))) 1693 if (expect_false (!ev_is_active (w)))
1653 return; 1694 return;
1654 1695
1655 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1696 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1656 ev_stop (EV_A_ (W)w); 1697 ev_stop (EV_A_ (W)w);
1657} 1698}
1658 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
1659/*****************************************************************************/ 1747/*****************************************************************************/
1660 1748
1661struct ev_once 1749struct ev_once
1662{ 1750{
1663 struct ev_io io; 1751 ev_io io;
1664 struct ev_timer to; 1752 ev_timer to;
1665 void (*cb)(int revents, void *arg); 1753 void (*cb)(int revents, void *arg);
1666 void *arg; 1754 void *arg;
1667}; 1755};
1668 1756
1669static void 1757static void
1678 1766
1679 cb (revents, arg); 1767 cb (revents, arg);
1680} 1768}
1681 1769
1682static void 1770static void
1683once_cb_io (EV_P_ struct ev_io *w, int revents) 1771once_cb_io (EV_P_ ev_io *w, int revents)
1684{ 1772{
1685 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);
1686} 1774}
1687 1775
1688static void 1776static void
1689once_cb_to (EV_P_ struct ev_timer *w, int revents) 1777once_cb_to (EV_P_ ev_timer *w, int revents)
1690{ 1778{
1691 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);
1692} 1780}
1693 1781
1694void 1782void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines