… | |
… | |
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="Sat Dec 8 15:31:35 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 --> |
… | |
… | |
1492 | <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 |
1493 | 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> |
1494 | macros, but using them is utterly, utterly and completely pointless.</p> |
1494 | macros, but using them is utterly, utterly and completely pointless.</p> |
1495 | </dd> |
1495 | </dd> |
1496 | </dl> |
1496 | </dl> |
1497 | <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 |
1498 | 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, |
1499 | 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 |
1500 | 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> |
1501 | <pre> static ev_io iow [nfd]; |
1508 | <pre> static ev_io iow [nfd]; |
1502 | static ev_timer tw; |
1509 | static ev_timer tw; |
1503 | |
1510 | |
1504 | static void |
1511 | static void |
1505 | io_cb (ev_loop *loop, ev_io *w, int revents) |
1512 | io_cb (ev_loop *loop, ev_io *w, int revents) |
1506 | { |
1513 | { |
1507 | // set the relevant poll flags |
|
|
1508 | // could also call adns_processreadable etc. here |
|
|
1509 | struct pollfd *fd = (struct pollfd *)w->data; |
|
|
1510 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1511 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1512 | } |
1514 | } |
1513 | |
1515 | |
1514 | // create io watchers for each fd and a timer before blocking |
1516 | // create io watchers for each fd and a timer before blocking |
1515 | static void |
1517 | static void |
1516 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
1518 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
… | |
… | |
1522 | |
1524 | |
1523 | /* 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 */ |
1524 | ev_timer_init (&tw, 0, timeout * 1e-3); |
1526 | ev_timer_init (&tw, 0, timeout * 1e-3); |
1525 | ev_timer_start (loop, &tw); |
1527 | ev_timer_start (loop, &tw); |
1526 | |
1528 | |
1527 | // create on ev_io per pollfd |
1529 | // create one ev_io per pollfd |
1528 | for (int i = 0; i < nfd; ++i) |
1530 | for (int i = 0; i < nfd; ++i) |
1529 | { |
1531 | { |
1530 | ev_io_init (iow + i, io_cb, fds [i].fd, |
1532 | ev_io_init (iow + i, io_cb, fds [i].fd, |
1531 | ((fds [i].events & POLLIN ? EV_READ : 0) |
1533 | ((fds [i].events & POLLIN ? EV_READ : 0) |
1532 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
1534 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
1533 | |
1535 | |
1534 | fds [i].revents = 0; |
1536 | fds [i].revents = 0; |
1535 | iow [i].data = fds + i; |
|
|
1536 | ev_io_start (loop, iow + i); |
1537 | ev_io_start (loop, iow + i); |
1537 | } |
1538 | } |
1538 | } |
1539 | } |
1539 | |
1540 | |
1540 | // stop all watchers after blocking |
1541 | // stop all watchers after blocking |
… | |
… | |
1542 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
1543 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
1543 | { |
1544 | { |
1544 | ev_timer_stop (loop, &tw); |
1545 | ev_timer_stop (loop, &tw); |
1545 | |
1546 | |
1546 | 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 |
1547 | ev_io_stop (loop, iow + i); |
1557 | ev_io_stop (loop, iow + i); |
|
|
1558 | } |
1548 | |
1559 | |
1549 | 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; |
1550 | } |
1619 | } |
1551 | |
1620 | |
1552 | |
1621 | |
1553 | |
1622 | |
1554 | |
1623 | |