… | |
… | |
4 | <head> |
4 | <head> |
5 | <title>libev</title> |
5 | <title>libev</title> |
6 | <meta name="description" content="Pod documentation for libev" /> |
6 | <meta name="description" content="Pod documentation for libev" /> |
7 | <meta name="inputfile" content="<standard input>" /> |
7 | <meta name="inputfile" content="<standard input>" /> |
8 | <meta name="outputfile" content="<standard output>" /> |
8 | <meta name="outputfile" content="<standard output>" /> |
9 | <meta name="created" content="Fri Dec 7 21:19:14 2007" /> |
9 | <meta name="created" content="Sat Dec 8 16:30:24 2007" /> |
10 | <meta name="generator" content="Pod::Xhtml 1.57" /> |
10 | <meta name="generator" content="Pod::Xhtml 1.57" /> |
11 | <link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head> |
11 | <link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head> |
12 | <body> |
12 | <body> |
13 | <div class="pod"> |
13 | <div class="pod"> |
14 | <!-- INDEX START --> |
14 | <!-- INDEX START --> |
… | |
… | |
769 | <dt>bool ev_is_pending (ev_TYPE *watcher)</dt> |
769 | <dt>bool ev_is_pending (ev_TYPE *watcher)</dt> |
770 | <dd> |
770 | <dd> |
771 | <p>Returns a true value iff the watcher is pending, (i.e. it has outstanding |
771 | <p>Returns a true value iff the watcher is pending, (i.e. it has outstanding |
772 | events but its callback has not yet been invoked). As long as a watcher |
772 | events but its callback has not yet been invoked). As long as a watcher |
773 | is pending (but not active) you must not call an init function on it (but |
773 | is pending (but not active) you must not call an init function on it (but |
774 | <code>ev_TYPE_set</code> is safe) and you must make sure the watcher is available to |
774 | <code>ev_TYPE_set</code> is safe), you must not change its priority, and you must |
775 | libev (e.g. you cnanot <code>free ()</code> it).</p> |
775 | make sure the watcher is available to libev (e.g. you cannot <code>free ()</code> |
|
|
776 | it).</p> |
776 | </dd> |
777 | </dd> |
777 | <dt>callback ev_cb (ev_TYPE *watcher)</dt> |
778 | <dt>callback ev_cb (ev_TYPE *watcher)</dt> |
778 | <dd> |
779 | <dd> |
779 | <p>Returns the callback currently set on the watcher.</p> |
780 | <p>Returns the callback currently set on the watcher.</p> |
780 | </dd> |
781 | </dd> |
… | |
… | |
795 | invocation after new events have been received. This is useful, for |
796 | invocation after new events have been received. This is useful, for |
796 | example, to reduce latency after idling, or more often, to bind two |
797 | example, to reduce latency after idling, or more often, to bind two |
797 | watchers on the same event and make sure one is called first.</p> |
798 | watchers on the same event and make sure one is called first.</p> |
798 | <p>If you need to suppress invocation when higher priority events are pending |
799 | <p>If you need to suppress invocation when higher priority events are pending |
799 | you need to look at <code>ev_idle</code> watchers, which provide this functionality.</p> |
800 | you need to look at <code>ev_idle</code> watchers, which provide this functionality.</p> |
|
|
801 | <p>You <i>must not</i> change the priority of a watcher as long as it is active or |
|
|
802 | pending.</p> |
800 | <p>The default priority used by watchers when no priority has been set is |
803 | <p>The default priority used by watchers when no priority has been set is |
801 | always <code>0</code>, which is supposed to not be too high and not be too low :).</p> |
804 | always <code>0</code>, which is supposed to not be too high and not be too low :).</p> |
802 | <p>Setting a priority outside the range of <code>EV_MINPRI</code> to <code>EV_MAXPRI</code> is |
805 | <p>Setting a priority outside the range of <code>EV_MINPRI</code> to <code>EV_MAXPRI</code> is |
803 | fine, as long as you do not mind that the priority value you query might |
806 | fine, as long as you do not mind that the priority value you query might |
804 | or might not have been adjusted to be within valid range.</p> |
807 | or might not have been adjusted to be within valid range.</p> |
|
|
808 | </dd> |
|
|
809 | <dt>ev_invoke (loop, ev_TYPE *watcher, int revents)</dt> |
|
|
810 | <dd> |
|
|
811 | <p>Invoke the <code>watcher</code> with the given <code>loop</code> and <code>revents</code>. Neither |
|
|
812 | <code>loop</code> nor <code>revents</code> need to be valid as long as the watcher callback |
|
|
813 | can deal with that fact.</p> |
|
|
814 | </dd> |
|
|
815 | <dt>int ev_clear_pending (loop, ev_TYPE *watcher)</dt> |
|
|
816 | <dd> |
|
|
817 | <p>If the watcher is pending, this function returns clears its pending status |
|
|
818 | and returns its <code>revents</code> bitset (as if its callback was invoked). If the |
|
|
819 | watcher isn't pending it does nothing and returns <code>0</code>.</p> |
805 | </dd> |
820 | </dd> |
806 | </dl> |
821 | </dl> |
807 | |
822 | |
808 | |
823 | |
809 | |
824 | |
… | |
… | |
1477 | <p>Initialises and configures the prepare or check watcher - they have no |
1492 | <p>Initialises and configures the prepare or check watcher - they have no |
1478 | parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code> |
1493 | parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code> |
1479 | macros, but using them is utterly, utterly and completely pointless.</p> |
1494 | macros, but using them is utterly, utterly and completely pointless.</p> |
1480 | </dd> |
1495 | </dd> |
1481 | </dl> |
1496 | </dl> |
1482 | <p>Example: To include a library such as adns, you would add IO watchers |
1497 | <p>There are a number of principal ways to embed other event loops or modules |
1483 | and a timeout watcher in a prepare handler, as required by libadns, and |
1498 | into libev. Here are some ideas on how to include libadns into libev |
|
|
1499 | (there is a Perl module named <code>EV::ADNS</code> that does this, which you could |
|
|
1500 | use for an actually working example. Another Perl module named <code>EV::Glib</code> |
|
|
1501 | embeds a Glib main context into libev, and finally, <code>Glib::EV</code> embeds EV |
|
|
1502 | into the Glib event loop).</p> |
|
|
1503 | <p>Method 1: Add IO watchers and a timeout watcher in a prepare handler, |
1484 | in a check watcher, destroy them and call into libadns. What follows is |
1504 | and in a check watcher, destroy them and call into libadns. What follows |
1485 | pseudo-code only of course:</p> |
1505 | is pseudo-code only of course. This requires you to either use a low |
|
|
1506 | priority for the check watcher or use <code>ev_clear_pending</code> explicitly, as |
|
|
1507 | the callbacks for the IO/timeout watchers might not have been called yet.</p> |
1486 | <pre> static ev_io iow [nfd]; |
1508 | <pre> static ev_io iow [nfd]; |
1487 | static ev_timer tw; |
1509 | static ev_timer tw; |
1488 | |
1510 | |
1489 | static void |
1511 | static void |
1490 | io_cb (ev_loop *loop, ev_io *w, int revents) |
1512 | io_cb (ev_loop *loop, ev_io *w, int revents) |
1491 | { |
1513 | { |
1492 | // set the relevant poll flags |
|
|
1493 | // could also call adns_processreadable etc. here |
|
|
1494 | struct pollfd *fd = (struct pollfd *)w->data; |
|
|
1495 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1496 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1497 | } |
1514 | } |
1498 | |
1515 | |
1499 | // create io watchers for each fd and a timer before blocking |
1516 | // create io watchers for each fd and a timer before blocking |
1500 | static void |
1517 | static void |
1501 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
1518 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
… | |
… | |
1507 | |
1524 | |
1508 | /* the callback is illegal, but won't be called as we stop during check */ |
1525 | /* the callback is illegal, but won't be called as we stop during check */ |
1509 | ev_timer_init (&tw, 0, timeout * 1e-3); |
1526 | ev_timer_init (&tw, 0, timeout * 1e-3); |
1510 | ev_timer_start (loop, &tw); |
1527 | ev_timer_start (loop, &tw); |
1511 | |
1528 | |
1512 | // create on ev_io per pollfd |
1529 | // create one ev_io per pollfd |
1513 | for (int i = 0; i < nfd; ++i) |
1530 | for (int i = 0; i < nfd; ++i) |
1514 | { |
1531 | { |
1515 | ev_io_init (iow + i, io_cb, fds [i].fd, |
1532 | ev_io_init (iow + i, io_cb, fds [i].fd, |
1516 | ((fds [i].events & POLLIN ? EV_READ : 0) |
1533 | ((fds [i].events & POLLIN ? EV_READ : 0) |
1517 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
1534 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
1518 | |
1535 | |
1519 | fds [i].revents = 0; |
1536 | fds [i].revents = 0; |
1520 | iow [i].data = fds + i; |
|
|
1521 | ev_io_start (loop, iow + i); |
1537 | ev_io_start (loop, iow + i); |
1522 | } |
1538 | } |
1523 | } |
1539 | } |
1524 | |
1540 | |
1525 | // stop all watchers after blocking |
1541 | // stop all watchers after blocking |
… | |
… | |
1527 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
1543 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
1528 | { |
1544 | { |
1529 | ev_timer_stop (loop, &tw); |
1545 | ev_timer_stop (loop, &tw); |
1530 | |
1546 | |
1531 | for (int i = 0; i < nfd; ++i) |
1547 | for (int i = 0; i < nfd; ++i) |
|
|
1548 | { |
|
|
1549 | // set the relevant poll flags |
|
|
1550 | // could also call adns_processreadable etc. here |
|
|
1551 | struct pollfd *fd = fds + i; |
|
|
1552 | int revents = ev_clear_pending (iow + i); |
|
|
1553 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1554 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1555 | |
|
|
1556 | // now stop the watcher |
1532 | ev_io_stop (loop, iow + i); |
1557 | ev_io_stop (loop, iow + i); |
|
|
1558 | } |
1533 | |
1559 | |
1534 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
1560 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
|
|
1561 | } |
|
|
1562 | |
|
|
1563 | </pre> |
|
|
1564 | <p>Method 2: This would be just like method 1, but you run <code>adns_afterpoll</code> |
|
|
1565 | in the prepare watcher and would dispose of the check watcher.</p> |
|
|
1566 | <p>Method 3: If the module to be embedded supports explicit event |
|
|
1567 | notification (adns does), you can also make use of the actual watcher |
|
|
1568 | callbacks, and only destroy/create the watchers in the prepare watcher.</p> |
|
|
1569 | <pre> static void |
|
|
1570 | timer_cb (EV_P_ ev_timer *w, int revents) |
|
|
1571 | { |
|
|
1572 | adns_state ads = (adns_state)w->data; |
|
|
1573 | update_now (EV_A); |
|
|
1574 | |
|
|
1575 | adns_processtimeouts (ads, &tv_now); |
|
|
1576 | } |
|
|
1577 | |
|
|
1578 | static void |
|
|
1579 | io_cb (EV_P_ ev_io *w, int revents) |
|
|
1580 | { |
|
|
1581 | adns_state ads = (adns_state)w->data; |
|
|
1582 | update_now (EV_A); |
|
|
1583 | |
|
|
1584 | if (revents & EV_READ ) adns_processreadable (ads, w->fd, &tv_now); |
|
|
1585 | if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now); |
|
|
1586 | } |
|
|
1587 | |
|
|
1588 | // do not ever call adns_afterpoll |
|
|
1589 | |
|
|
1590 | </pre> |
|
|
1591 | <p>Method 4: Do not use a prepare or check watcher because the module you |
|
|
1592 | want to embed is too inflexible to support it. Instead, youc na override |
|
|
1593 | their poll function. The drawback with this solution is that the main |
|
|
1594 | loop is now no longer controllable by EV. The <code>Glib::EV</code> module does |
|
|
1595 | this.</p> |
|
|
1596 | <pre> static gint |
|
|
1597 | event_poll_func (GPollFD *fds, guint nfds, gint timeout) |
|
|
1598 | { |
|
|
1599 | int got_events = 0; |
|
|
1600 | |
|
|
1601 | for (n = 0; n < nfds; ++n) |
|
|
1602 | // create/start io watcher that sets the relevant bits in fds[n] and increment got_events |
|
|
1603 | |
|
|
1604 | if (timeout >= 0) |
|
|
1605 | // create/start timer |
|
|
1606 | |
|
|
1607 | // poll |
|
|
1608 | ev_loop (EV_A_ 0); |
|
|
1609 | |
|
|
1610 | // stop timer again |
|
|
1611 | if (timeout >= 0) |
|
|
1612 | ev_timer_stop (EV_A_ &to); |
|
|
1613 | |
|
|
1614 | // stop io watchers again - their callbacks should have set |
|
|
1615 | for (n = 0; n < nfds; ++n) |
|
|
1616 | ev_io_stop (EV_A_ iow [n]); |
|
|
1617 | |
|
|
1618 | return got_events; |
1535 | } |
1619 | } |
1536 | |
1620 | |
1537 | |
1621 | |
1538 | |
1622 | |
1539 | |
1623 | |
… | |
… | |
1806 | ev::io iow; |
1890 | ev::io iow; |
1807 | iow.set <myclass, &myclass::io_cb> (&obj); |
1891 | iow.set <myclass, &myclass::io_cb> (&obj); |
1808 | |
1892 | |
1809 | </pre> |
1893 | </pre> |
1810 | </dd> |
1894 | </dd> |
1811 | <dt>w->set (void (*function)(watcher &w, int), void *data = 0)</dt> |
1895 | <dt>w->set<function> (void *data = 0)</dt> |
1812 | <dd> |
1896 | <dd> |
1813 | <p>Also sets a callback, but uses a static method or plain function as |
1897 | <p>Also sets a callback, but uses a static method or plain function as |
1814 | callback. The optional <code>data</code> argument will be stored in the watcher's |
1898 | callback. The optional <code>data</code> argument will be stored in the watcher's |
1815 | <code>data</code> member and is free for you to use.</p> |
1899 | <code>data</code> member and is free for you to use.</p> |
|
|
1900 | <p>The prototype of the <code>function</code> must be <code>void (*)(ev::TYPE &w, int)</code>.</p> |
1816 | <p>See the method-<code>set</code> above for more details.</p> |
1901 | <p>See the method-<code>set</code> above for more details.</p> |
|
|
1902 | <p>Example:</p> |
|
|
1903 | <pre> static void io_cb (ev::io &w, int revents) { } |
|
|
1904 | iow.set <io_cb> (); |
|
|
1905 | |
|
|
1906 | </pre> |
1817 | </dd> |
1907 | </dd> |
1818 | <dt>w->set (struct ev_loop *)</dt> |
1908 | <dt>w->set (struct ev_loop *)</dt> |
1819 | <dd> |
1909 | <dd> |
1820 | <p>Associates a different <code>struct ev_loop</code> with this watcher. You can only |
1910 | <p>Associates a different <code>struct ev_loop</code> with this watcher. You can only |
1821 | do this when the watcher is inactive (and not pending either).</p> |
1911 | do this when the watcher is inactive (and not pending either).</p> |