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

Comparing libev/ev.c (file contents):
Revision 1.120 by root, Fri Nov 16 01:54:25 2007 UTC vs.
Revision 1.123 by root, Sat Nov 17 02:23:54 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines