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

Comparing libev/ev.c (file contents):
Revision 1.134 by root, Fri Nov 23 19:13:33 2007 UTC vs.
Revision 1.139 by root, Sun Nov 25 09:24:37 2007 UTC

202#define ABSPRI(w) ((w)->priority - EV_MINPRI) 202#define ABSPRI(w) ((w)->priority - EV_MINPRI)
203 203
204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */ 204#define EMPTY0 /* required for microsofts broken pseudo-c compiler */
205#define EMPTY2(a,b) /* used to suppress some warnings */ 205#define EMPTY2(a,b) /* used to suppress some warnings */
206 206
207typedef struct ev_watcher *W; 207typedef ev_watcher *W;
208typedef struct ev_watcher_list *WL; 208typedef ev_watcher_list *WL;
209typedef struct ev_watcher_time *WT; 209typedef ev_watcher_time *WT;
210 210
211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 211static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
212 212
213#ifdef _WIN32 213#ifdef _WIN32
214# include "ev_win32.c" 214# include "ev_win32.c"
397 { 397 {
398 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 398 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
399 return; 399 return;
400 } 400 }
401 401
402 if (expect_false (!w_->cb))
403 return;
404
405 w_->pending = ++pendingcnt [ABSPRI (w_)]; 402 w_->pending = ++pendingcnt [ABSPRI (w_)];
406 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2); 403 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], EMPTY2);
407 pendings [ABSPRI (w_)][w_->pending - 1].w = w_; 404 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
408 pendings [ABSPRI (w_)][w_->pending - 1].events = revents; 405 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
409} 406}
419 416
420inline void 417inline void
421fd_event (EV_P_ int fd, int revents) 418fd_event (EV_P_ int fd, int revents)
422{ 419{
423 ANFD *anfd = anfds + fd; 420 ANFD *anfd = anfds + fd;
424 struct ev_io *w; 421 ev_io *w;
425 422
426 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)
427 { 424 {
428 int ev = w->events & revents; 425 int ev = w->events & revents;
429 426
430 if (ev) 427 if (ev)
431 ev_feed_event (EV_A_ (W)w, ev); 428 ev_feed_event (EV_A_ (W)w, ev);
447 444
448 for (i = 0; i < fdchangecnt; ++i) 445 for (i = 0; i < fdchangecnt; ++i)
449 { 446 {
450 int fd = fdchanges [i]; 447 int fd = fdchanges [i];
451 ANFD *anfd = anfds + fd; 448 ANFD *anfd = anfds + fd;
452 struct ev_io *w; 449 ev_io *w;
453 450
454 int events = 0; 451 int events = 0;
455 452
456 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)
457 events |= w->events; 454 events |= w->events;
458 455
459#if EV_SELECT_IS_WINSOCKET 456#if EV_SELECT_IS_WINSOCKET
460 if (events) 457 if (events)
461 { 458 {
488} 485}
489 486
490static void 487static void
491fd_kill (EV_P_ int fd) 488fd_kill (EV_P_ int fd)
492{ 489{
493 struct ev_io *w; 490 ev_io *w;
494 491
495 while ((w = (struct ev_io *)anfds [fd].head)) 492 while ((w = (ev_io *)anfds [fd].head))
496 { 493 {
497 ev_io_stop (EV_A_ w); 494 ev_io_stop (EV_A_ w);
498 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);
499 } 496 }
500} 497}
611static ANSIG *signals; 608static ANSIG *signals;
612static int signalmax; 609static int signalmax;
613 610
614static int sigpipe [2]; 611static int sigpipe [2];
615static sig_atomic_t volatile gotsig; 612static sig_atomic_t volatile gotsig;
616static struct ev_io sigev; 613static ev_io sigev;
617 614
618static void 615static void
619signals_init (ANSIG *base, int count) 616signals_init (ANSIG *base, int count)
620{ 617{
621 while (count--) 618 while (count--)
664 for (w = signals [signum].head; w; w = w->next) 661 for (w = signals [signum].head; w; w = w->next)
665 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 662 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
666} 663}
667 664
668static void 665static void
669sigcb (EV_P_ struct ev_io *iow, int revents) 666sigcb (EV_P_ ev_io *iow, int revents)
670{ 667{
671 int signum; 668 int signum;
672 669
673 read (sigpipe [0], &revents, 1); 670 read (sigpipe [0], &revents, 1);
674 gotsig = 0; 671 gotsig = 0;
701 ev_unref (EV_A); /* child watcher should not keep loop alive */ 698 ev_unref (EV_A); /* child watcher should not keep loop alive */
702} 699}
703 700
704/*****************************************************************************/ 701/*****************************************************************************/
705 702
706static struct ev_child *childs [PID_HASHSIZE]; 703static ev_child *childs [PID_HASHSIZE];
707 704
708#ifndef _WIN32 705#ifndef _WIN32
709 706
710static struct ev_signal childev; 707static ev_signal childev;
711 708
712#ifndef WCONTINUED 709#ifndef WCONTINUED
713# define WCONTINUED 0 710# define WCONTINUED 0
714#endif 711#endif
715 712
716static void 713static void
717child_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)
718{ 715{
719 struct ev_child *w; 716 ev_child *w;
720 717
721 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)
722 if (w->pid == pid || !w->pid) 719 if (w->pid == pid || !w->pid)
723 { 720 {
724 ev_priority (w) = ev_priority (sw); /* need to do it *now* */ 721 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
725 w->rpid = pid; 722 w->rpid = pid;
726 w->rstatus = status; 723 w->rstatus = status;
727 ev_feed_event (EV_A_ (W)w, EV_CHILD); 724 ev_feed_event (EV_A_ (W)w, EV_CHILD);
728 } 725 }
729} 726}
730 727
731static void 728static void
732childcb (EV_P_ struct ev_signal *sw, int revents) 729childcb (EV_P_ ev_signal *sw, int revents)
733{ 730{
734 int pid, status; 731 int pid, status;
735 732
736 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) 733 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
737 { 734 {
1077 { 1074 {
1078 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 1075 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
1079 1076
1080 if (expect_true (p->w)) 1077 if (expect_true (p->w))
1081 { 1078 {
1079 assert (("non-pending watcher on pending list", p->w->pending));
1080
1082 p->w->pending = 0; 1081 p->w->pending = 0;
1083 EV_CB_INVOKE (p->w, p->events); 1082 EV_CB_INVOKE (p->w, p->events);
1084 } 1083 }
1085 } 1084 }
1086} 1085}
1088inline void 1087inline void
1089timers_reify (EV_P) 1088timers_reify (EV_P)
1090{ 1089{
1091 while (timercnt && ((WT)timers [0])->at <= mn_now) 1090 while (timercnt && ((WT)timers [0])->at <= mn_now)
1092 { 1091 {
1093 struct ev_timer *w = timers [0]; 1092 ev_timer *w = timers [0];
1094 1093
1095 assert (("inactive timer on timer heap detected", ev_is_active (w))); 1094 assert (("inactive timer on timer heap detected", ev_is_active (w)));
1096 1095
1097 /* first reschedule or stop timer */ 1096 /* first reschedule or stop timer */
1098 if (w->repeat) 1097 if (w->repeat)
1116inline void 1115inline void
1117periodics_reify (EV_P) 1116periodics_reify (EV_P)
1118{ 1117{
1119 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1118 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1120 { 1119 {
1121 struct ev_periodic *w = periodics [0]; 1120 ev_periodic *w = periodics [0];
1122 1121
1123 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 1122 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1124 1123
1125 /* first reschedule or stop timer */ 1124 /* first reschedule or stop timer */
1126 if (w->reschedule_cb) 1125 if (w->reschedule_cb)
1148 int i; 1147 int i;
1149 1148
1150 /* adjust periodics after time jump */ 1149 /* adjust periodics after time jump */
1151 for (i = 0; i < periodiccnt; ++i) 1150 for (i = 0; i < periodiccnt; ++i)
1152 { 1151 {
1153 struct ev_periodic *w = periodics [i]; 1152 ev_periodic *w = periodics [i];
1154 1153
1155 if (w->reschedule_cb) 1154 if (w->reschedule_cb)
1156 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1155 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1157 else if (w->interval) 1156 else if (w->interval)
1158 ((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;
1192 { 1191 {
1193 if (time_update_monotonic (EV_A)) 1192 if (time_update_monotonic (EV_A))
1194 { 1193 {
1195 ev_tstamp odiff = rtmn_diff; 1194 ev_tstamp odiff = rtmn_diff;
1196 1195
1197 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; )
1198 { 1205 {
1199 rtmn_diff = ev_rt_now - mn_now; 1206 rtmn_diff = ev_rt_now - mn_now;
1200 1207
1201 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP) 1208 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
1202 return; /* all is well */ 1209 return; /* all is well */
1248static int loop_done; 1255static int loop_done;
1249 1256
1250void 1257void
1251ev_loop (EV_P_ int flags) 1258ev_loop (EV_P_ int flags)
1252{ 1259{
1253 double block;
1254 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1260 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1261 ? EVUNLOOP_ONE
1262 : EVUNLOOP_CANCEL;
1255 1263
1256 while (activecnt) 1264 while (activecnt)
1257 { 1265 {
1258 /* queue check watchers (and execute them) */ 1266 /* queue check watchers (and execute them) */
1259 if (expect_false (preparecnt)) 1267 if (expect_false (preparecnt))
1268 1276
1269 /* update fd-related kernel structures */ 1277 /* update fd-related kernel structures */
1270 fd_reify (EV_A); 1278 fd_reify (EV_A);
1271 1279
1272 /* calculate blocking time */ 1280 /* calculate blocking time */
1281 {
1282 double block;
1273 1283
1274 /* we only need this for !monotonic clock or timers, but as we basically 1284 if (flags & EVLOOP_NONBLOCK || idlecnt)
1275 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 */
1276#if EV_USE_MONOTONIC 1289#if EV_USE_MONOTONIC
1277 if (expect_true (have_monotonic)) 1290 if (expect_true (have_monotonic))
1278 time_update_monotonic (EV_A); 1291 time_update_monotonic (EV_A);
1279 else 1292 else
1280#endif 1293#endif
1281 { 1294 {
1282 ev_rt_now = ev_time (); 1295 ev_rt_now = ev_time ();
1283 mn_now = ev_rt_now; 1296 mn_now = ev_rt_now;
1284 } 1297 }
1285 1298
1286 if (flags & EVLOOP_NONBLOCK || idlecnt)
1287 block = 0.;
1288 else
1289 {
1290 block = MAX_BLOCKTIME; 1299 block = MAX_BLOCKTIME;
1291 1300
1292 if (timercnt) 1301 if (timercnt)
1293 { 1302 {
1294 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge; 1303 ev_tstamp to = ((WT)timers [0])->at - mn_now + backend_fudge;
1295 if (block > to) block = to; 1304 if (block > to) block = to;
1296 } 1305 }
1297 1306
1298#if EV_PERIODICS 1307#if EV_PERIODICS
1299 if (periodiccnt) 1308 if (periodiccnt)
1300 { 1309 {
1301 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;
1302 if (block > to) block = to; 1311 if (block > to) block = to;
1303 } 1312 }
1304#endif 1313#endif
1305 1314
1306 if (expect_false (block < 0.)) block = 0.; 1315 if (expect_false (block < 0.)) block = 0.;
1307 } 1316 }
1308 1317
1309 backend_poll (EV_A_ block); 1318 backend_poll (EV_A_ block);
1319 }
1310 1320
1311 /* update ev_rt_now, do magic */ 1321 /* update ev_rt_now, do magic */
1312 time_update (EV_A); 1322 time_update (EV_A);
1313 1323
1314 /* queue pending timers and reschedule them */ 1324 /* queue pending timers and reschedule them */
1315 timers_reify (EV_A); /* relative timers called last */ 1325 timers_reify (EV_A); /* relative timers called last */
1316#if EV_PERIODICS 1326#if EV_PERIODICS
1317 periodics_reify (EV_A); /* absolute timers called first */ 1327 periodics_reify (EV_A); /* absolute timers called first */
1318#endif 1328#endif
1319 1329
1320 /* queue idle watchers unless io or timers are pending */ 1330 /* queue idle watchers unless other events are pending */
1321 if (idlecnt && !any_pending (EV_A)) 1331 if (idlecnt && !any_pending (EV_A))
1322 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1332 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1323 1333
1324 /* queue check watchers, to be executed first */ 1334 /* queue check watchers, to be executed first */
1325 if (expect_false (checkcnt)) 1335 if (expect_false (checkcnt))
1329 1339
1330 if (expect_false (loop_done)) 1340 if (expect_false (loop_done))
1331 break; 1341 break;
1332 } 1342 }
1333 1343
1334 if (loop_done != 2) 1344 if (loop_done == EVUNLOOP_ONE)
1335 loop_done = 0; 1345 loop_done = EVUNLOOP_CANCEL;
1336} 1346}
1337 1347
1338void 1348void
1339ev_unloop (EV_P_ int how) 1349ev_unloop (EV_P_ int how)
1340{ 1350{
1393} 1403}
1394 1404
1395/*****************************************************************************/ 1405/*****************************************************************************/
1396 1406
1397void 1407void
1398ev_io_start (EV_P_ struct ev_io *w) 1408ev_io_start (EV_P_ ev_io *w)
1399{ 1409{
1400 int fd = w->fd; 1410 int fd = w->fd;
1401 1411
1402 if (expect_false (ev_is_active (w))) 1412 if (expect_false (ev_is_active (w)))
1403 return; 1413 return;
1410 1420
1411 fd_change (EV_A_ fd); 1421 fd_change (EV_A_ fd);
1412} 1422}
1413 1423
1414void 1424void
1415ev_io_stop (EV_P_ struct ev_io *w) 1425ev_io_stop (EV_P_ ev_io *w)
1416{ 1426{
1417 ev_clear_pending (EV_A_ (W)w); 1427 ev_clear_pending (EV_A_ (W)w);
1418 if (expect_false (!ev_is_active (w))) 1428 if (expect_false (!ev_is_active (w)))
1419 return; 1429 return;
1420 1430
1425 1435
1426 fd_change (EV_A_ w->fd); 1436 fd_change (EV_A_ w->fd);
1427} 1437}
1428 1438
1429void 1439void
1430ev_timer_start (EV_P_ struct ev_timer *w) 1440ev_timer_start (EV_P_ ev_timer *w)
1431{ 1441{
1432 if (expect_false (ev_is_active (w))) 1442 if (expect_false (ev_is_active (w)))
1433 return; 1443 return;
1434 1444
1435 ((WT)w)->at += mn_now; 1445 ((WT)w)->at += mn_now;
1436 1446
1437 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.));
1438 1448
1439 ev_start (EV_A_ (W)w, ++timercnt); 1449 ev_start (EV_A_ (W)w, ++timercnt);
1440 array_needsize (struct ev_timer *, timers, timermax, timercnt, EMPTY2); 1450 array_needsize (ev_timer *, timers, timermax, timercnt, EMPTY2);
1441 timers [timercnt - 1] = w; 1451 timers [timercnt - 1] = w;
1442 upheap ((WT *)timers, timercnt - 1); 1452 upheap ((WT *)timers, timercnt - 1);
1443 1453
1444 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1454 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1445} 1455}
1446 1456
1447void 1457void
1448ev_timer_stop (EV_P_ struct ev_timer *w) 1458ev_timer_stop (EV_P_ ev_timer *w)
1449{ 1459{
1450 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1451 if (expect_false (!ev_is_active (w))) 1461 if (expect_false (!ev_is_active (w)))
1452 return; 1462 return;
1453 1463
1463 1473
1464 ev_stop (EV_A_ (W)w); 1474 ev_stop (EV_A_ (W)w);
1465} 1475}
1466 1476
1467void 1477void
1468ev_timer_again (EV_P_ struct ev_timer *w) 1478ev_timer_again (EV_P_ ev_timer *w)
1469{ 1479{
1470 if (ev_is_active (w)) 1480 if (ev_is_active (w))
1471 { 1481 {
1472 if (w->repeat) 1482 if (w->repeat)
1473 { 1483 {
1484 } 1494 }
1485} 1495}
1486 1496
1487#if EV_PERIODICS 1497#if EV_PERIODICS
1488void 1498void
1489ev_periodic_start (EV_P_ struct ev_periodic *w) 1499ev_periodic_start (EV_P_ ev_periodic *w)
1490{ 1500{
1491 if (expect_false (ev_is_active (w))) 1501 if (expect_false (ev_is_active (w)))
1492 return; 1502 return;
1493 1503
1494 if (w->reschedule_cb) 1504 if (w->reschedule_cb)
1499 /* 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 */
1500 ((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;
1501 } 1511 }
1502 1512
1503 ev_start (EV_A_ (W)w, ++periodiccnt); 1513 ev_start (EV_A_ (W)w, ++periodiccnt);
1504 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2); 1514 array_needsize (ev_periodic *, periodics, periodicmax, periodiccnt, EMPTY2);
1505 periodics [periodiccnt - 1] = w; 1515 periodics [periodiccnt - 1] = w;
1506 upheap ((WT *)periodics, periodiccnt - 1); 1516 upheap ((WT *)periodics, periodiccnt - 1);
1507 1517
1508 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1518 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1509} 1519}
1510 1520
1511void 1521void
1512ev_periodic_stop (EV_P_ struct ev_periodic *w) 1522ev_periodic_stop (EV_P_ ev_periodic *w)
1513{ 1523{
1514 ev_clear_pending (EV_A_ (W)w); 1524 ev_clear_pending (EV_A_ (W)w);
1515 if (expect_false (!ev_is_active (w))) 1525 if (expect_false (!ev_is_active (w)))
1516 return; 1526 return;
1517 1527
1525 1535
1526 ev_stop (EV_A_ (W)w); 1536 ev_stop (EV_A_ (W)w);
1527} 1537}
1528 1538
1529void 1539void
1530ev_periodic_again (EV_P_ struct ev_periodic *w) 1540ev_periodic_again (EV_P_ ev_periodic *w)
1531{ 1541{
1532 /* TODO: use adjustheap and recalculation */ 1542 /* TODO: use adjustheap and recalculation */
1533 ev_periodic_stop (EV_A_ w); 1543 ev_periodic_stop (EV_A_ w);
1534 ev_periodic_start (EV_A_ w); 1544 ev_periodic_start (EV_A_ w);
1535} 1545}
1536#endif 1546#endif
1537 1547
1538void 1548void
1539ev_idle_start (EV_P_ struct ev_idle *w) 1549ev_idle_start (EV_P_ ev_idle *w)
1540{ 1550{
1541 if (expect_false (ev_is_active (w))) 1551 if (expect_false (ev_is_active (w)))
1542 return; 1552 return;
1543 1553
1544 ev_start (EV_A_ (W)w, ++idlecnt); 1554 ev_start (EV_A_ (W)w, ++idlecnt);
1545 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1555 array_needsize (ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1546 idles [idlecnt - 1] = w; 1556 idles [idlecnt - 1] = w;
1547} 1557}
1548 1558
1549void 1559void
1550ev_idle_stop (EV_P_ struct ev_idle *w) 1560ev_idle_stop (EV_P_ ev_idle *w)
1551{ 1561{
1552 ev_clear_pending (EV_A_ (W)w); 1562 ev_clear_pending (EV_A_ (W)w);
1553 if (expect_false (!ev_is_active (w))) 1563 if (expect_false (!ev_is_active (w)))
1554 return; 1564 return;
1555 1565
1566 {
1567 int active = ((W)w)->active;
1556 idles [((W)w)->active - 1] = idles [--idlecnt]; 1568 idles [active - 1] = idles [--idlecnt];
1569 ((W)idles [active - 1])->active = active;
1570 }
1571
1557 ev_stop (EV_A_ (W)w); 1572 ev_stop (EV_A_ (W)w);
1558} 1573}
1559 1574
1560void 1575void
1561ev_prepare_start (EV_P_ struct ev_prepare *w) 1576ev_prepare_start (EV_P_ ev_prepare *w)
1562{ 1577{
1563 if (expect_false (ev_is_active (w))) 1578 if (expect_false (ev_is_active (w)))
1564 return; 1579 return;
1565 1580
1566 ev_start (EV_A_ (W)w, ++preparecnt); 1581 ev_start (EV_A_ (W)w, ++preparecnt);
1567 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1582 array_needsize (ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1568 prepares [preparecnt - 1] = w; 1583 prepares [preparecnt - 1] = w;
1569} 1584}
1570 1585
1571void 1586void
1572ev_prepare_stop (EV_P_ struct ev_prepare *w) 1587ev_prepare_stop (EV_P_ ev_prepare *w)
1573{ 1588{
1574 ev_clear_pending (EV_A_ (W)w); 1589 ev_clear_pending (EV_A_ (W)w);
1575 if (expect_false (!ev_is_active (w))) 1590 if (expect_false (!ev_is_active (w)))
1576 return; 1591 return;
1577 1592
1593 {
1594 int active = ((W)w)->active;
1578 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1595 prepares [active - 1] = prepares [--preparecnt];
1596 ((W)prepares [active - 1])->active = active;
1597 }
1598
1579 ev_stop (EV_A_ (W)w); 1599 ev_stop (EV_A_ (W)w);
1580} 1600}
1581 1601
1582void 1602void
1583ev_check_start (EV_P_ struct ev_check *w) 1603ev_check_start (EV_P_ ev_check *w)
1584{ 1604{
1585 if (expect_false (ev_is_active (w))) 1605 if (expect_false (ev_is_active (w)))
1586 return; 1606 return;
1587 1607
1588 ev_start (EV_A_ (W)w, ++checkcnt); 1608 ev_start (EV_A_ (W)w, ++checkcnt);
1589 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1609 array_needsize (ev_check *, checks, checkmax, checkcnt, EMPTY2);
1590 checks [checkcnt - 1] = w; 1610 checks [checkcnt - 1] = w;
1591} 1611}
1592 1612
1593void 1613void
1594ev_check_stop (EV_P_ struct ev_check *w) 1614ev_check_stop (EV_P_ ev_check *w)
1595{ 1615{
1596 ev_clear_pending (EV_A_ (W)w); 1616 ev_clear_pending (EV_A_ (W)w);
1597 if (expect_false (!ev_is_active (w))) 1617 if (expect_false (!ev_is_active (w)))
1598 return; 1618 return;
1599 1619
1620 {
1621 int active = ((W)w)->active;
1600 checks [((W)w)->active - 1] = checks [--checkcnt]; 1622 checks [active - 1] = checks [--checkcnt];
1623 ((W)checks [active - 1])->active = active;
1624 }
1625
1601 ev_stop (EV_A_ (W)w); 1626 ev_stop (EV_A_ (W)w);
1602} 1627}
1603 1628
1604#ifndef SA_RESTART 1629#ifndef SA_RESTART
1605# define SA_RESTART 0 1630# define SA_RESTART 0
1606#endif 1631#endif
1607 1632
1608void 1633void
1609ev_signal_start (EV_P_ struct ev_signal *w) 1634ev_signal_start (EV_P_ ev_signal *w)
1610{ 1635{
1611#if EV_MULTIPLICITY 1636#if EV_MULTIPLICITY
1612 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));
1613#endif 1638#endif
1614 if (expect_false (ev_is_active (w))) 1639 if (expect_false (ev_is_active (w)))
1633#endif 1658#endif
1634 } 1659 }
1635} 1660}
1636 1661
1637void 1662void
1638ev_signal_stop (EV_P_ struct ev_signal *w) 1663ev_signal_stop (EV_P_ ev_signal *w)
1639{ 1664{
1640 ev_clear_pending (EV_A_ (W)w); 1665 ev_clear_pending (EV_A_ (W)w);
1641 if (expect_false (!ev_is_active (w))) 1666 if (expect_false (!ev_is_active (w)))
1642 return; 1667 return;
1643 1668
1647 if (!signals [w->signum - 1].head) 1672 if (!signals [w->signum - 1].head)
1648 signal (w->signum, SIG_DFL); 1673 signal (w->signum, SIG_DFL);
1649} 1674}
1650 1675
1651void 1676void
1652ev_child_start (EV_P_ struct ev_child *w) 1677ev_child_start (EV_P_ ev_child *w)
1653{ 1678{
1654#if EV_MULTIPLICITY 1679#if EV_MULTIPLICITY
1655 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));
1656#endif 1681#endif
1657 if (expect_false (ev_is_active (w))) 1682 if (expect_false (ev_is_active (w)))
1660 ev_start (EV_A_ (W)w, 1); 1685 ev_start (EV_A_ (W)w, 1);
1661 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1686 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1662} 1687}
1663 1688
1664void 1689void
1665ev_child_stop (EV_P_ struct ev_child *w) 1690ev_child_stop (EV_P_ ev_child *w)
1666{ 1691{
1667 ev_clear_pending (EV_A_ (W)w); 1692 ev_clear_pending (EV_A_ (W)w);
1668 if (expect_false (!ev_is_active (w))) 1693 if (expect_false (!ev_is_active (w)))
1669 return; 1694 return;
1670 1695
1671 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1696 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1672 ev_stop (EV_A_ (W)w); 1697 ev_stop (EV_A_ (W)w);
1673} 1698}
1674 1699
1675#if EV_MULTIPLICITY 1700#if EV_MULTIPLICITY
1676static void 1701void
1677embed_cb (EV_P_ struct ev_io *io, int revents) 1702ev_embed_sweep (EV_P_ ev_embed *w)
1678{ 1703{
1679 struct ev_embed *w = (struct ev_embed *)(((char *)io) - offsetof (struct ev_embed, io));
1680
1681 ev_feed_event (EV_A_ (W)w, EV_EMBED);
1682 ev_loop (w->loop, EVLOOP_NONBLOCK); 1704 ev_loop (w->loop, EVLOOP_NONBLOCK);
1683} 1705}
1684 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
1685void 1718void
1686ev_embed_start (EV_P_ struct ev_embed *w) 1719ev_embed_start (EV_P_ ev_embed *w)
1687{ 1720{
1688 if (expect_false (ev_is_active (w))) 1721 if (expect_false (ev_is_active (w)))
1689 return; 1722 return;
1690 1723
1691 { 1724 {
1692 struct ev_loop *loop = w->loop; 1725 struct ev_loop *loop = w->loop;
1693 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ())); 1726 assert (("loop to be embedded is not embeddable", backend & ev_embeddable_backends ()));
1694 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ); 1727 ev_io_init (&w->io, embed_cb, backend_fd, EV_READ);
1695 } 1728 }
1696 1729
1730 ev_set_priority (&w->io, ev_priority (w));
1697 ev_io_start (EV_A_ &w->io); 1731 ev_io_start (EV_A_ &w->io);
1698 ev_start (EV_A_ (W)w, 1); 1732 ev_start (EV_A_ (W)w, 1);
1699} 1733}
1700 1734
1701void 1735void
1702ev_embed_stop (EV_P_ struct ev_embed *w) 1736ev_embed_stop (EV_P_ ev_embed *w)
1703{ 1737{
1704 ev_clear_pending (EV_A_ (W)w); 1738 ev_clear_pending (EV_A_ (W)w);
1705 if (expect_false (!ev_is_active (w))) 1739 if (expect_false (!ev_is_active (w)))
1706 return; 1740 return;
1707 1741
1712 1746
1713/*****************************************************************************/ 1747/*****************************************************************************/
1714 1748
1715struct ev_once 1749struct ev_once
1716{ 1750{
1717 struct ev_io io; 1751 ev_io io;
1718 struct ev_timer to; 1752 ev_timer to;
1719 void (*cb)(int revents, void *arg); 1753 void (*cb)(int revents, void *arg);
1720 void *arg; 1754 void *arg;
1721}; 1755};
1722 1756
1723static void 1757static void
1732 1766
1733 cb (revents, arg); 1767 cb (revents, arg);
1734} 1768}
1735 1769
1736static void 1770static void
1737once_cb_io (EV_P_ struct ev_io *w, int revents) 1771once_cb_io (EV_P_ ev_io *w, int revents)
1738{ 1772{
1739 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);
1740} 1774}
1741 1775
1742static void 1776static void
1743once_cb_to (EV_P_ struct ev_timer *w, int revents) 1777once_cb_to (EV_P_ ev_timer *w, int revents)
1744{ 1778{
1745 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);
1746} 1780}
1747 1781
1748void 1782void

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines