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

Comparing libev/ev.html (file contents):
Revision 1.68 by root, Fri Dec 7 20:13:09 2007 UTC vs.
Revision 1.72 by root, Sat Dec 8 15:30:29 2007 UTC

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="&lt;standard input&gt;" /> 7 <meta name="inputfile" content="&lt;standard input&gt;" />
8 <meta name="outputfile" content="&lt;standard output&gt;" /> 8 <meta name="outputfile" content="&lt;standard output&gt;" />
9 <meta name="created" content="Fri Dec 7 21:13:07 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
772events but its callback has not yet been invoked). As long as a watcher 772events but its callback has not yet been invoked). As long as a watcher
773is pending (but not active) you must not call an init function on it (but 773is 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
775libev (e.g. you cnanot <code>free ()</code> it).</p> 775make sure the watcher is available to libev (e.g. you cannot <code>free ()</code>
776it).</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>
795invocation after new events have been received. This is useful, for 796invocation after new events have been received. This is useful, for
796example, to reduce latency after idling, or more often, to bind two 797example, to reduce latency after idling, or more often, to bind two
797watchers on the same event and make sure one is called first.</p> 798watchers 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
799you need to look at <code>ev_idle</code> watchers, which provide this functionality.</p> 800you 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
802pending.</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
801always <code>0</code>, which is supposed to not be too high and not be too low :).</p> 804always <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
803fine, as long as you do not mind that the priority value you query might 806fine, as long as you do not mind that the priority value you query might
804or might not have been adjusted to be within valid range.</p> 807or 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
813can 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
818and returns its <code>revents</code> bitset (as if its callback was invoked). If the
819watcher 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
1478parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code> 1493parameters of any kind. There are <code>ev_prepare_set</code> and <code>ev_check_set</code>
1479macros, but using them is utterly, utterly and completely pointless.</p> 1494macros, 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
1483and a timeout watcher in a prepare handler, as required by libadns, and 1498into 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
1500use for an actually working example. Another Perl module named <code>EV::Glib</code>
1501embeds a Glib main context into libev, and finally, <code>Glib::EV</code> embeds EV
1502into the Glib event loop).</p>
1503<p>Method 1: Add IO watchers and a timeout watcher in a prepare handler,
1484in a check watcher, destroy them and call into libadns. What follows is 1504and in a check watcher, destroy them and call into libadns. What follows
1485pseudo-code only of course:</p> 1505is pseudo-code only of course. This requires you to either use a low
1506priority for the check watcher or use <code>ev_clear_pending</code> explicitly, as
1507the 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-&gt;data;
1495 if (revents &amp; EV_READ ) fd-&gt;revents |= fd-&gt;events &amp; POLLIN;
1496 if (revents &amp; EV_WRITE) fd-&gt;revents |= fd-&gt;events &amp; 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 (&amp;tw, 0, timeout * 1e-3); 1526 ev_timer_init (&amp;tw, 0, timeout * 1e-3);
1510 ev_timer_start (loop, &amp;tw); 1527 ev_timer_start (loop, &amp;tw);
1511 1528
1512 // create on ev_io per pollfd 1529 // create one ev_io per pollfd
1513 for (int i = 0; i &lt; nfd; ++i) 1530 for (int i = 0; i &lt; 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 &amp; POLLIN ? EV_READ : 0) 1533 ((fds [i].events &amp; POLLIN ? EV_READ : 0)
1517 | (fds [i].events &amp; POLLOUT ? EV_WRITE : 0))); 1534 | (fds [i].events &amp; 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, &amp;tw); 1545 ev_timer_stop (loop, &amp;tw);
1530 1546
1531 for (int i = 0; i &lt; nfd; ++i) 1547 for (int i = 0; i &lt; 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 &amp; EV_READ ) fd-&gt;revents |= fd-&gt;events &amp; POLLIN;
1554 if (revents &amp; EV_WRITE) fd-&gt;revents |= fd-&gt;events &amp; 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>
1565in the prepare watcher and would dispose of the check watcher.</p>
1566<p>Method 3: If the module to be embedded supports explicit event
1567notification (adns does), you can also make use of the actual watcher
1568callbacks, 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-&gt;data;
1573 update_now (EV_A);
1574
1575 adns_processtimeouts (ads, &amp;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-&gt;data;
1582 update_now (EV_A);
1583
1584 if (revents &amp; EV_READ ) adns_processreadable (ads, w-&gt;fd, &amp;tv_now);
1585 if (revents &amp; EV_WRITE) adns_processwriteable (ads, w-&gt;fd, &amp;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
1592want to embed is too inflexible to support it. Instead, youc na override
1593their poll function. The drawback with this solution is that the main
1594loop is now no longer controllable by EV. The <code>Glib::EV</code> module does
1595this.</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 &lt; nfds; ++n)
1602 // create/start io watcher that sets the relevant bits in fds[n] and increment got_events
1603
1604 if (timeout &gt;= 0)
1605 // create/start timer
1606
1607 // poll
1608 ev_loop (EV_A_ 0);
1609
1610 // stop timer again
1611 if (timeout &gt;= 0)
1612 ev_timer_stop (EV_A_ &amp;to);
1613
1614 // stop io watchers again - their callbacks should have set
1615 for (n = 0; n &lt; 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
1740</pre> 1824</pre>
1741<p>This automatically includes <cite>ev.h</cite> and puts all of its definitions (many 1825<p>This automatically includes <cite>ev.h</cite> and puts all of its definitions (many
1742of them macros) into the global namespace. All C++ specific things are 1826of them macros) into the global namespace. All C++ specific things are
1743put into the <code>ev</code> namespace. It should support all the same embedding 1827put into the <code>ev</code> namespace. It should support all the same embedding
1744options as <cite>ev.h</cite>, most notably <code>EV_MULTIPLICITY</code>.</p> 1828options as <cite>ev.h</cite>, most notably <code>EV_MULTIPLICITY</code>.</p>
1745<p>Care has been taken to keep the overhead low. The only data member added 1829<p>Care has been taken to keep the overhead low. The only data member the C++
1746to the C-style watchers is the event loop the watcher is associated with 1830classes add (compared to plain C-style watchers) is the event loop pointer
1747(or no additional members at all if you disable <code>EV_MULTIPLICITY</code> when 1831that the watcher is associated with (or no additional members at all if
1748embedding libev).</p> 1832you disable <code>EV_MULTIPLICITY</code> when embedding libev).</p>
1749<p>Currently, functions and static and non-static member functions can be 1833<p>Currently, functions, and static and non-static member functions can be
1750used as callbacks. Other types should be easy to add as long as they only 1834used as callbacks. Other types should be easy to add as long as they only
1751need one additional pointer for context. If you need support for other 1835need one additional pointer for context. If you need support for other
1752types of functors please contact the author (preferably after implementing 1836types of functors please contact the author (preferably after implementing
1753it).</p> 1837it).</p>
1754<p>Here is a list of things available in the <code>ev</code> namespace:</p> 1838<p>Here is a list of things available in the <code>ev</code> namespace:</p>
1806 ev::io iow; 1890 ev::io iow;
1807 iow.set &lt;myclass, &amp;myclass::io_cb&gt; (&amp;obj); 1891 iow.set &lt;myclass, &amp;myclass::io_cb&gt; (&amp;obj);
1808 1892
1809</pre> 1893</pre>
1810 </dd> 1894 </dd>
1811 <dt>w-&gt;set (void (*function)(watcher &amp;w, int), void *data = 0)</dt> 1895 <dt>w-&gt;set&lt;function&gt; (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
1814callback. The optional <code>data</code> argument will be stored in the watcher's 1898callback. 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 &amp;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 &amp;w, int revents) { }
1904 iow.set &lt;io_cb&gt; ();
1905
1906</pre>
1817 </dd> 1907 </dd>
1818 <dt>w-&gt;set (struct ev_loop *)</dt> 1908 <dt>w-&gt;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
1821do this when the watcher is inactive (and not pending either).</p> 1911do this when the watcher is inactive (and not pending either).</p>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines