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

Comparing libev/ev.c (file contents):
Revision 1.122 by root, Sat Nov 17 02:00:48 2007 UTC vs.
Revision 1.123 by root, Sat Nov 17 02:23:54 2007 UTC

162# include "ev.h" 162# include "ev.h"
163#endif 163#endif
164 164
165#if __GNUC__ >= 3 165#if __GNUC__ >= 3
166# define expect(expr,value) __builtin_expect ((expr),(value)) 166# define expect(expr,value) __builtin_expect ((expr),(value))
167# define inline inline 167# define inline static inline
168#else 168#else
169# define expect(expr,value) (expr) 169# define expect(expr,value) (expr)
170# define inline static 170# define inline static
171#endif 171#endif
172 172
366void 366void
367ev_feed_event (EV_P_ void *w, int revents) 367ev_feed_event (EV_P_ void *w, int revents)
368{ 368{
369 W w_ = (W)w; 369 W w_ = (W)w;
370 370
371 if (w_->pending) 371 if (expect_false (w_->pending))
372 { 372 {
373 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents; 373 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
374 return; 374 return;
375 } 375 }
376 376
410 fd_event (EV_A_ fd, revents); 410 fd_event (EV_A_ fd, revents);
411} 411}
412 412
413/*****************************************************************************/ 413/*****************************************************************************/
414 414
415static void 415inline void
416fd_reify (EV_P) 416fd_reify (EV_P)
417{ 417{
418 int i; 418 int i;
419 419
420 for (i = 0; i < fdchangecnt; ++i) 420 for (i = 0; i < fdchangecnt; ++i)
447} 447}
448 448
449static void 449static void
450fd_change (EV_P_ int fd) 450fd_change (EV_P_ int fd)
451{ 451{
452 if (anfds [fd].reify) 452 if (expect_false (anfds [fd].reify))
453 return; 453 return;
454 454
455 anfds [fd].reify = 1; 455 anfds [fd].reify = 1;
456 456
457 ++fdchangecnt; 457 ++fdchangecnt;
469 ev_io_stop (EV_A_ w); 469 ev_io_stop (EV_A_ w);
470 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE); 470 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
471 } 471 }
472} 472}
473 473
474static int 474inline int
475fd_valid (int fd) 475fd_valid (int fd)
476{ 476{
477#ifdef _WIN32 477#ifdef _WIN32
478 return _get_osfhandle (fd) != -1; 478 return _get_osfhandle (fd) != -1;
479#else 479#else
648 for (signum = signalmax; signum--; ) 648 for (signum = signalmax; signum--; )
649 if (signals [signum].gotsig) 649 if (signals [signum].gotsig)
650 ev_feed_signal_event (EV_A_ signum + 1); 650 ev_feed_signal_event (EV_A_ signum + 1);
651} 651}
652 652
653inline void 653static void
654fd_intern (int fd) 654fd_intern (int fd)
655{ 655{
656#ifdef _WIN32 656#ifdef _WIN32
657 int arg = 1; 657 int arg = 1;
658 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg); 658 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
1012 EV_CB_INVOKE (p->w, p->events); 1012 EV_CB_INVOKE (p->w, p->events);
1013 } 1013 }
1014 } 1014 }
1015} 1015}
1016 1016
1017static void 1017inline void
1018timers_reify (EV_P) 1018timers_reify (EV_P)
1019{ 1019{
1020 while (timercnt && ((WT)timers [0])->at <= mn_now) 1020 while (timercnt && ((WT)timers [0])->at <= mn_now)
1021 { 1021 {
1022 struct ev_timer *w = timers [0]; 1022 struct ev_timer *w = timers [0];
1040 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1040 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1041 } 1041 }
1042} 1042}
1043 1043
1044#if EV_PERIODICS 1044#if EV_PERIODICS
1045static void 1045inline void
1046periodics_reify (EV_P) 1046periodics_reify (EV_P)
1047{ 1047{
1048 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1048 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1049 { 1049 {
1050 struct ev_periodic *w = periodics [0]; 1050 struct ev_periodic *w = periodics [0];
1109 ev_rt_now = ev_time (); 1109 ev_rt_now = ev_time ();
1110 return 1; 1110 return 1;
1111 } 1111 }
1112} 1112}
1113 1113
1114static void 1114inline void
1115time_update (EV_P) 1115time_update (EV_P)
1116{ 1116{
1117 int i; 1117 int i;
1118 1118
1119#if EV_USE_MONOTONIC 1119#if EV_USE_MONOTONIC
1230 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1230 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1231 if (block > to) block = to; 1231 if (block > to) block = to;
1232 } 1232 }
1233#endif 1233#endif
1234 1234
1235 if (block < 0.) block = 0.; 1235 if (expect_false (block < 0.)) block = 0.;
1236 } 1236 }
1237 1237
1238 method_poll (EV_A_ block); 1238 method_poll (EV_A_ block);
1239 1239
1240 /* update ev_rt_now, do magic */ 1240 /* update ev_rt_now, do magic */
1249 /* queue idle watchers unless io or timers are pending */ 1249 /* queue idle watchers unless io or timers are pending */
1250 if (idlecnt && !any_pending (EV_A)) 1250 if (idlecnt && !any_pending (EV_A))
1251 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1251 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1252 1252
1253 /* queue check watchers, to be executed first */ 1253 /* queue check watchers, to be executed first */
1254 if (checkcnt) 1254 if (expect_false (checkcnt))
1255 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1255 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1256 1256
1257 call_pending (EV_A); 1257 call_pending (EV_A);
1258 1258
1259 if (loop_done) 1259 if (expect_false (loop_done))
1260 break; 1260 break;
1261 } 1261 }
1262 1262
1263 if (loop_done != 2) 1263 if (loop_done != 2)
1264 loop_done = 0; 1264 loop_done = 0;
1326void 1326void
1327ev_io_start (EV_P_ struct ev_io *w) 1327ev_io_start (EV_P_ struct ev_io *w)
1328{ 1328{
1329 int fd = w->fd; 1329 int fd = w->fd;
1330 1330
1331 if (ev_is_active (w)) 1331 if (expect_false (ev_is_active (w)))
1332 return; 1332 return;
1333 1333
1334 assert (("ev_io_start called with negative fd", fd >= 0)); 1334 assert (("ev_io_start called with negative fd", fd >= 0));
1335 1335
1336 ev_start (EV_A_ (W)w, 1); 1336 ev_start (EV_A_ (W)w, 1);
1342 1342
1343void 1343void
1344ev_io_stop (EV_P_ struct ev_io *w) 1344ev_io_stop (EV_P_ struct ev_io *w)
1345{ 1345{
1346 ev_clear_pending (EV_A_ (W)w); 1346 ev_clear_pending (EV_A_ (W)w);
1347 if (!ev_is_active (w)) 1347 if (expect_false (!ev_is_active (w)))
1348 return; 1348 return;
1349 1349
1350 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax)); 1350 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1351 1351
1352 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1352 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1356} 1356}
1357 1357
1358void 1358void
1359ev_timer_start (EV_P_ struct ev_timer *w) 1359ev_timer_start (EV_P_ struct ev_timer *w)
1360{ 1360{
1361 if (ev_is_active (w)) 1361 if (expect_false (ev_is_active (w)))
1362 return; 1362 return;
1363 1363
1364 ((WT)w)->at += mn_now; 1364 ((WT)w)->at += mn_now;
1365 1365
1366 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1366 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1375 1375
1376void 1376void
1377ev_timer_stop (EV_P_ struct ev_timer *w) 1377ev_timer_stop (EV_P_ struct ev_timer *w)
1378{ 1378{
1379 ev_clear_pending (EV_A_ (W)w); 1379 ev_clear_pending (EV_A_ (W)w);
1380 if (!ev_is_active (w)) 1380 if (expect_false (!ev_is_active (w)))
1381 return; 1381 return;
1382 1382
1383 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1383 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1384 1384
1385 if (((W)w)->active < timercnt--) 1385 if (expect_true (((W)w)->active < timercnt--))
1386 { 1386 {
1387 timers [((W)w)->active - 1] = timers [timercnt]; 1387 timers [((W)w)->active - 1] = timers [timercnt];
1388 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1388 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1389 } 1389 }
1390 1390
1415 1415
1416#if EV_PERIODICS 1416#if EV_PERIODICS
1417void 1417void
1418ev_periodic_start (EV_P_ struct ev_periodic *w) 1418ev_periodic_start (EV_P_ struct ev_periodic *w)
1419{ 1419{
1420 if (ev_is_active (w)) 1420 if (expect_false (ev_is_active (w)))
1421 return; 1421 return;
1422 1422
1423 if (w->reschedule_cb) 1423 if (w->reschedule_cb)
1424 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now); 1424 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1425 else if (w->interval) 1425 else if (w->interval)
1439 1439
1440void 1440void
1441ev_periodic_stop (EV_P_ struct ev_periodic *w) 1441ev_periodic_stop (EV_P_ struct ev_periodic *w)
1442{ 1442{
1443 ev_clear_pending (EV_A_ (W)w); 1443 ev_clear_pending (EV_A_ (W)w);
1444 if (!ev_is_active (w)) 1444 if (expect_false (!ev_is_active (w)))
1445 return; 1445 return;
1446 1446
1447 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1447 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1448 1448
1449 if (((W)w)->active < periodiccnt--) 1449 if (expect_true (((W)w)->active < periodiccnt--))
1450 { 1450 {
1451 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1451 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1452 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1452 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1453 } 1453 }
1454 1454
1465#endif 1465#endif
1466 1466
1467void 1467void
1468ev_idle_start (EV_P_ struct ev_idle *w) 1468ev_idle_start (EV_P_ struct ev_idle *w)
1469{ 1469{
1470 if (ev_is_active (w)) 1470 if (expect_false (ev_is_active (w)))
1471 return; 1471 return;
1472 1472
1473 ev_start (EV_A_ (W)w, ++idlecnt); 1473 ev_start (EV_A_ (W)w, ++idlecnt);
1474 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2); 1474 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, EMPTY2);
1475 idles [idlecnt - 1] = w; 1475 idles [idlecnt - 1] = w;
1477 1477
1478void 1478void
1479ev_idle_stop (EV_P_ struct ev_idle *w) 1479ev_idle_stop (EV_P_ struct ev_idle *w)
1480{ 1480{
1481 ev_clear_pending (EV_A_ (W)w); 1481 ev_clear_pending (EV_A_ (W)w);
1482 if (!ev_is_active (w)) 1482 if (expect_false (!ev_is_active (w)))
1483 return; 1483 return;
1484 1484
1485 idles [((W)w)->active - 1] = idles [--idlecnt]; 1485 idles [((W)w)->active - 1] = idles [--idlecnt];
1486 ev_stop (EV_A_ (W)w); 1486 ev_stop (EV_A_ (W)w);
1487} 1487}
1488 1488
1489void 1489void
1490ev_prepare_start (EV_P_ struct ev_prepare *w) 1490ev_prepare_start (EV_P_ struct ev_prepare *w)
1491{ 1491{
1492 if (ev_is_active (w)) 1492 if (expect_false (ev_is_active (w)))
1493 return; 1493 return;
1494 1494
1495 ev_start (EV_A_ (W)w, ++preparecnt); 1495 ev_start (EV_A_ (W)w, ++preparecnt);
1496 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2); 1496 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, EMPTY2);
1497 prepares [preparecnt - 1] = w; 1497 prepares [preparecnt - 1] = w;
1499 1499
1500void 1500void
1501ev_prepare_stop (EV_P_ struct ev_prepare *w) 1501ev_prepare_stop (EV_P_ struct ev_prepare *w)
1502{ 1502{
1503 ev_clear_pending (EV_A_ (W)w); 1503 ev_clear_pending (EV_A_ (W)w);
1504 if (!ev_is_active (w)) 1504 if (expect_false (!ev_is_active (w)))
1505 return; 1505 return;
1506 1506
1507 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1507 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1508 ev_stop (EV_A_ (W)w); 1508 ev_stop (EV_A_ (W)w);
1509} 1509}
1510 1510
1511void 1511void
1512ev_check_start (EV_P_ struct ev_check *w) 1512ev_check_start (EV_P_ struct ev_check *w)
1513{ 1513{
1514 if (ev_is_active (w)) 1514 if (expect_false (ev_is_active (w)))
1515 return; 1515 return;
1516 1516
1517 ev_start (EV_A_ (W)w, ++checkcnt); 1517 ev_start (EV_A_ (W)w, ++checkcnt);
1518 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2); 1518 array_needsize (struct ev_check *, checks, checkmax, checkcnt, EMPTY2);
1519 checks [checkcnt - 1] = w; 1519 checks [checkcnt - 1] = w;
1521 1521
1522void 1522void
1523ev_check_stop (EV_P_ struct ev_check *w) 1523ev_check_stop (EV_P_ struct ev_check *w)
1524{ 1524{
1525 ev_clear_pending (EV_A_ (W)w); 1525 ev_clear_pending (EV_A_ (W)w);
1526 if (!ev_is_active (w)) 1526 if (expect_false (!ev_is_active (w)))
1527 return; 1527 return;
1528 1528
1529 checks [((W)w)->active - 1] = checks [--checkcnt]; 1529 checks [((W)w)->active - 1] = checks [--checkcnt];
1530 ev_stop (EV_A_ (W)w); 1530 ev_stop (EV_A_ (W)w);
1531} 1531}
1538ev_signal_start (EV_P_ struct ev_signal *w) 1538ev_signal_start (EV_P_ struct ev_signal *w)
1539{ 1539{
1540#if EV_MULTIPLICITY 1540#if EV_MULTIPLICITY
1541 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1541 assert (("signal watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1542#endif 1542#endif
1543 if (ev_is_active (w)) 1543 if (expect_false (ev_is_active (w)))
1544 return; 1544 return;
1545 1545
1546 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1546 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1547 1547
1548 ev_start (EV_A_ (W)w, 1); 1548 ev_start (EV_A_ (W)w, 1);
1565 1565
1566void 1566void
1567ev_signal_stop (EV_P_ struct ev_signal *w) 1567ev_signal_stop (EV_P_ struct ev_signal *w)
1568{ 1568{
1569 ev_clear_pending (EV_A_ (W)w); 1569 ev_clear_pending (EV_A_ (W)w);
1570 if (!ev_is_active (w)) 1570 if (expect_false (!ev_is_active (w)))
1571 return; 1571 return;
1572 1572
1573 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1573 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1574 ev_stop (EV_A_ (W)w); 1574 ev_stop (EV_A_ (W)w);
1575 1575
1581ev_child_start (EV_P_ struct ev_child *w) 1581ev_child_start (EV_P_ struct ev_child *w)
1582{ 1582{
1583#if EV_MULTIPLICITY 1583#if EV_MULTIPLICITY
1584 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr)); 1584 assert (("child watchers are only supported in the default loop", loop == ev_default_loop_ptr));
1585#endif 1585#endif
1586 if (ev_is_active (w)) 1586 if (expect_false (ev_is_active (w)))
1587 return; 1587 return;
1588 1588
1589 ev_start (EV_A_ (W)w, 1); 1589 ev_start (EV_A_ (W)w, 1);
1590 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1590 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1591} 1591}
1592 1592
1593void 1593void
1594ev_child_stop (EV_P_ struct ev_child *w) 1594ev_child_stop (EV_P_ struct ev_child *w)
1595{ 1595{
1596 ev_clear_pending (EV_A_ (W)w); 1596 ev_clear_pending (EV_A_ (W)w);
1597 if (!ev_is_active (w)) 1597 if (expect_false (!ev_is_active (w)))
1598 return; 1598 return;
1599 1599
1600 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1600 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1601 ev_stop (EV_A_ (W)w); 1601 ev_stop (EV_A_ (W)w);
1602} 1602}
1639void 1639void
1640ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1640ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1641{ 1641{
1642 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once)); 1642 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1643 1643
1644 if (!once) 1644 if (expect_false (!once))
1645 {
1645 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1646 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1646 else 1647 return;
1647 { 1648 }
1649
1648 once->cb = cb; 1650 once->cb = cb;
1649 once->arg = arg; 1651 once->arg = arg;
1650 1652
1651 ev_init (&once->io, once_cb_io); 1653 ev_init (&once->io, once_cb_io);
1652 if (fd >= 0) 1654 if (fd >= 0)
1653 { 1655 {
1654 ev_io_set (&once->io, fd, events); 1656 ev_io_set (&once->io, fd, events);
1655 ev_io_start (EV_A_ &once->io); 1657 ev_io_start (EV_A_ &once->io);
1656 } 1658 }
1657 1659
1658 ev_init (&once->to, once_cb_to); 1660 ev_init (&once->to, once_cb_to);
1659 if (timeout >= 0.) 1661 if (timeout >= 0.)
1660 { 1662 {
1661 ev_timer_set (&once->to, timeout, 0.); 1663 ev_timer_set (&once->to, timeout, 0.);
1662 ev_timer_start (EV_A_ &once->to); 1664 ev_timer_start (EV_A_ &once->to);
1663 }
1664 } 1665 }
1665} 1666}
1666 1667
1667#ifdef __cplusplus 1668#ifdef __cplusplus
1668} 1669}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines