… | |
… | |
124 | this argument. |
124 | this argument. |
125 | |
125 | |
126 | =head2 TIME REPRESENTATION |
126 | =head2 TIME REPRESENTATION |
127 | |
127 | |
128 | Libev represents time as a single floating point number, representing |
128 | Libev represents time as a single floating point number, representing |
129 | the (fractional) number of seconds since the (POSIX) epoch (somewhere |
129 | the (fractional) number of seconds since the (POSIX) epoch (in practise |
130 | near the beginning of 1970, details are complicated, don't ask). This |
130 | somewhere near the beginning of 1970, details are complicated, don't |
131 | type is called C<ev_tstamp>, which is what you should use too. It usually |
131 | ask). This type is called C<ev_tstamp>, which is what you should use |
132 | aliases to the C<double> type in C. When you need to do any calculations |
132 | too. It usually aliases to the C<double> type in C. When you need to do |
133 | on it, you should treat it as some floating point value. Unlike the name |
133 | any calculations on it, you should treat it as some floating point value. |
|
|
134 | |
134 | component C<stamp> might indicate, it is also used for time differences |
135 | Unlike the name component C<stamp> might indicate, it is also used for |
135 | throughout libev. |
136 | time differences (e.g. delays) throughout libev. |
136 | |
137 | |
137 | =head1 ERROR HANDLING |
138 | =head1 ERROR HANDLING |
138 | |
139 | |
139 | Libev knows three classes of errors: operating system errors, usage errors |
140 | Libev knows three classes of errors: operating system errors, usage errors |
140 | and internal errors (bugs). |
141 | and internal errors (bugs). |
… | |
… | |
191 | as this indicates an incompatible change. Minor versions are usually |
192 | as this indicates an incompatible change. Minor versions are usually |
192 | compatible to older versions, so a larger minor version alone is usually |
193 | compatible to older versions, so a larger minor version alone is usually |
193 | not a problem. |
194 | not a problem. |
194 | |
195 | |
195 | Example: Make sure we haven't accidentally been linked against the wrong |
196 | Example: Make sure we haven't accidentally been linked against the wrong |
196 | version. |
197 | version (note, however, that this will not detect ABI mismatches :). |
197 | |
198 | |
198 | assert (("libev version mismatch", |
199 | assert (("libev version mismatch", |
199 | ev_version_major () == EV_VERSION_MAJOR |
200 | ev_version_major () == EV_VERSION_MAJOR |
200 | && ev_version_minor () >= EV_VERSION_MINOR)); |
201 | && ev_version_minor () >= EV_VERSION_MINOR)); |
201 | |
202 | |
… | |
… | |
345 | useful to try out specific backends to test their performance, or to work |
346 | useful to try out specific backends to test their performance, or to work |
346 | around bugs. |
347 | around bugs. |
347 | |
348 | |
348 | =item C<EVFLAG_FORKCHECK> |
349 | =item C<EVFLAG_FORKCHECK> |
349 | |
350 | |
350 | Instead of calling C<ev_default_fork> or C<ev_loop_fork> manually after |
351 | Instead of calling C<ev_loop_fork> manually after a fork, you can also |
351 | a fork, you can also make libev check for a fork in each iteration by |
352 | make libev check for a fork in each iteration by enabling this flag. |
352 | enabling this flag. |
|
|
353 | |
353 | |
354 | This works by calling C<getpid ()> on every iteration of the loop, |
354 | This works by calling C<getpid ()> on every iteration of the loop, |
355 | and thus this might slow down your event loop if you do a lot of loop |
355 | and thus this might slow down your event loop if you do a lot of loop |
356 | iterations and little real work, but is usually not noticeable (on my |
356 | iterations and little real work, but is usually not noticeable (on my |
357 | GNU/Linux system for example, C<getpid> is actually a simple 5-insn sequence |
357 | GNU/Linux system for example, C<getpid> is actually a simple 5-insn sequence |
… | |
… | |
611 | name, you can call it anytime, but it makes most sense after forking, in |
611 | name, you can call it anytime, but it makes most sense after forking, in |
612 | the child process (or both child and parent, but that again makes little |
612 | the child process (or both child and parent, but that again makes little |
613 | sense). You I<must> call it in the child before using any of the libev |
613 | sense). You I<must> call it in the child before using any of the libev |
614 | functions, and it will only take effect at the next C<ev_loop> iteration. |
614 | functions, and it will only take effect at the next C<ev_loop> iteration. |
615 | |
615 | |
|
|
616 | Again, you I<have> to call it on I<any> loop that you want to re-use after |
|
|
617 | a fork, I<even if you do not plan to use the loop in the parent>. This is |
|
|
618 | because some kernel interfaces *cough* I<kqueue> *cough* do funny things |
|
|
619 | during fork. |
|
|
620 | |
616 | On the other hand, you only need to call this function in the child |
621 | On the other hand, you only need to call this function in the child |
617 | process if and only if you want to use the event library in the child. If |
622 | process if and only if you want to use the event loop in the child. If you |
618 | you just fork+exec, you don't have to call it at all. |
623 | just fork+exec or create a new loop in the child, you don't have to call |
|
|
624 | it at all. |
619 | |
625 | |
620 | The function itself is quite fast and it's usually not a problem to call |
626 | The function itself is quite fast and it's usually not a problem to call |
621 | it just in case after a fork. To make this easy, the function will fit in |
627 | it just in case after a fork. To make this easy, the function will fit in |
622 | quite nicely into a call to C<pthread_atfork>: |
628 | quite nicely into a call to C<pthread_atfork>: |
623 | |
629 | |
… | |
… | |
625 | |
631 | |
626 | =item ev_loop_fork (loop) |
632 | =item ev_loop_fork (loop) |
627 | |
633 | |
628 | Like C<ev_default_fork>, but acts on an event loop created by |
634 | Like C<ev_default_fork>, but acts on an event loop created by |
629 | C<ev_loop_new>. Yes, you have to call this on every allocated event loop |
635 | C<ev_loop_new>. Yes, you have to call this on every allocated event loop |
630 | after fork that you want to re-use in the child, and how you do this is |
636 | after fork that you want to re-use in the child, and how you keep track of |
631 | entirely your own problem. |
637 | them is entirely your own problem. |
632 | |
638 | |
633 | =item int ev_is_default_loop (loop) |
639 | =item int ev_is_default_loop (loop) |
634 | |
640 | |
635 | Returns true when the given loop is, in fact, the default loop, and false |
641 | Returns true when the given loop is, in fact, the default loop, and false |
636 | otherwise. |
642 | otherwise. |
637 | |
643 | |
638 | =item unsigned int ev_loop_count (loop) |
644 | =item unsigned int ev_iteration (loop) |
639 | |
645 | |
640 | Returns the count of loop iterations for the loop, which is identical to |
646 | Returns the current iteration count for the loop, which is identical to |
641 | the number of times libev did poll for new events. It starts at C<0> and |
647 | the number of times libev did poll for new events. It starts at C<0> and |
642 | happily wraps around with enough iterations. |
648 | happily wraps around with enough iterations. |
643 | |
649 | |
644 | This value can sometimes be useful as a generation counter of sorts (it |
650 | This value can sometimes be useful as a generation counter of sorts (it |
645 | "ticks" the number of loop iterations), as it roughly corresponds with |
651 | "ticks" the number of loop iterations), as it roughly corresponds with |
646 | C<ev_prepare> and C<ev_check> calls. |
652 | C<ev_prepare> and C<ev_check> calls - and is incremented between the |
|
|
653 | prepare and check phases. |
647 | |
654 | |
648 | =item unsigned int ev_loop_depth (loop) |
655 | =item unsigned int ev_depth (loop) |
649 | |
656 | |
650 | Returns the number of times C<ev_loop> was entered minus the number of |
657 | Returns the number of times C<ev_loop> was entered minus the number of |
651 | times C<ev_loop> was exited, in other words, the recursion depth. |
658 | times C<ev_loop> was exited, in other words, the recursion depth. |
652 | |
659 | |
653 | Outside C<ev_loop>, this number is zero. In a callback, this number is |
660 | Outside C<ev_loop>, this number is zero. In a callback, this number is |
654 | C<1>, unless C<ev_loop> was invoked recursively (or from another thread), |
661 | C<1>, unless C<ev_loop> was invoked recursively (or from another thread), |
655 | in which case it is higher. |
662 | in which case it is higher. |
656 | |
663 | |
657 | Leaving C<ev_loop> abnormally (setjmp/longjmp, cancelling the thread |
664 | Leaving C<ev_loop> abnormally (setjmp/longjmp, cancelling the thread |
658 | etc.), doesn't count as exit. |
665 | etc.), doesn't count as "exit" - consider this as a hint to avoid such |
|
|
666 | ungentleman behaviour unless it's really convenient. |
659 | |
667 | |
660 | =item unsigned int ev_backend (loop) |
668 | =item unsigned int ev_backend (loop) |
661 | |
669 | |
662 | Returns one of the C<EVBACKEND_*> flags indicating the event backend in |
670 | Returns one of the C<EVBACKEND_*> flags indicating the event backend in |
663 | use. |
671 | use. |
… | |
… | |
1396 | { |
1404 | { |
1397 | // stop the I/O watcher, we received the event, but |
1405 | // stop the I/O watcher, we received the event, but |
1398 | // are not yet ready to handle it. |
1406 | // are not yet ready to handle it. |
1399 | ev_io_stop (EV_A_ w); |
1407 | ev_io_stop (EV_A_ w); |
1400 | |
1408 | |
1401 | // start the idle watcher to ahndle the actual event. |
1409 | // start the idle watcher to handle the actual event. |
1402 | // it will not be executed as long as other watchers |
1410 | // it will not be executed as long as other watchers |
1403 | // with the default priority are receiving events. |
1411 | // with the default priority are receiving events. |
1404 | ev_idle_start (EV_A_ &idle); |
1412 | ev_idle_start (EV_A_ &idle); |
1405 | } |
1413 | } |
1406 | |
1414 | |
… | |
… | |
1538 | somewhere, as that would have given you a big clue). |
1546 | somewhere, as that would have given you a big clue). |
1539 | |
1547 | |
1540 | =head3 The special problem of accept()ing when you can't |
1548 | =head3 The special problem of accept()ing when you can't |
1541 | |
1549 | |
1542 | Many implementations of the POSIX C<accept> function (for example, |
1550 | Many implementations of the POSIX C<accept> function (for example, |
1543 | found in port-2004 Linux) have the peculiar behaviour of not removing a |
1551 | found in post-2004 Linux) have the peculiar behaviour of not removing a |
1544 | connection from the pending queue in all error cases. |
1552 | connection from the pending queue in all error cases. |
1545 | |
1553 | |
1546 | For example, larger servers often run out of file descriptors (because |
1554 | For example, larger servers often run out of file descriptors (because |
1547 | of resource limits), causing C<accept> to fail with C<ENFILE> but not |
1555 | of resource limits), causing C<accept> to fail with C<ENFILE> but not |
1548 | rejecting the connection, leading to libev signalling readiness on |
1556 | rejecting the connection, leading to libev signalling readiness on |
… | |
… | |
1766 | callback (loop, timer, EV_TIMER); |
1774 | callback (loop, timer, EV_TIMER); |
1767 | |
1775 | |
1768 | And when there is some activity, simply store the current time in |
1776 | And when there is some activity, simply store the current time in |
1769 | C<last_activity>, no libev calls at all: |
1777 | C<last_activity>, no libev calls at all: |
1770 | |
1778 | |
1771 | last_actiivty = ev_now (loop); |
1779 | last_activity = ev_now (loop); |
1772 | |
1780 | |
1773 | This technique is slightly more complex, but in most cases where the |
1781 | This technique is slightly more complex, but in most cases where the |
1774 | time-out is unlikely to be triggered, much more efficient. |
1782 | time-out is unlikely to be triggered, much more efficient. |
1775 | |
1783 | |
1776 | Changing the timeout is trivial as well (if it isn't hard-coded in the |
1784 | Changing the timeout is trivial as well (if it isn't hard-coded in the |
… | |
… | |
3334 | ev::io iow; |
3342 | ev::io iow; |
3335 | iow.set <myclass, &myclass::io_cb> (&obj); |
3343 | iow.set <myclass, &myclass::io_cb> (&obj); |
3336 | |
3344 | |
3337 | =item w->set (object *) |
3345 | =item w->set (object *) |
3338 | |
3346 | |
3339 | This is an B<experimental> feature that might go away in a future version. |
|
|
3340 | |
|
|
3341 | This is a variation of a method callback - leaving out the method to call |
3347 | This is a variation of a method callback - leaving out the method to call |
3342 | will default the method to C<operator ()>, which makes it possible to use |
3348 | will default the method to C<operator ()>, which makes it possible to use |
3343 | functor objects without having to manually specify the C<operator ()> all |
3349 | functor objects without having to manually specify the C<operator ()> all |
3344 | the time. Incidentally, you can then also leave out the template argument |
3350 | the time. Incidentally, you can then also leave out the template argument |
3345 | list. |
3351 | list. |
… | |
… | |
3657 | define before including (or compiling) any of its files. The default in |
3663 | define before including (or compiling) any of its files. The default in |
3658 | the absence of autoconf is documented for every option. |
3664 | the absence of autoconf is documented for every option. |
3659 | |
3665 | |
3660 | Symbols marked with "(h)" do not change the ABI, and can have different |
3666 | Symbols marked with "(h)" do not change the ABI, and can have different |
3661 | values when compiling libev vs. including F<ev.h>, so it is permissible |
3667 | values when compiling libev vs. including F<ev.h>, so it is permissible |
3662 | to redefine them before including F<ev.h> without breakign compatibility |
3668 | to redefine them before including F<ev.h> without breaking compatibility |
3663 | to a compiled library. All other symbols change the ABI, which means all |
3669 | to a compiled library. All other symbols change the ABI, which means all |
3664 | users of libev and the libev code itself must be compiled with compatible |
3670 | users of libev and the libev code itself must be compiled with compatible |
3665 | settings. |
3671 | settings. |
3666 | |
3672 | |
3667 | =over 4 |
3673 | =over 4 |
… | |
… | |
4618 | involves iterating over all running async watchers or all signal numbers. |
4624 | involves iterating over all running async watchers or all signal numbers. |
4619 | |
4625 | |
4620 | =back |
4626 | =back |
4621 | |
4627 | |
4622 | |
4628 | |
4623 | =head1 PORTING FROM 3.X TO 4.X |
4629 | =head1 PORTING FROM LIBEV 3.X TO 4.X |
4624 | |
4630 | |
4625 | The major version 4 introduced some minor incompatible changes to the API. |
4631 | The major version 4 introduced some minor incompatible changes to the API. |
4626 | |
4632 | |
|
|
4633 | At the moment, the C<ev.h> header file tries to implement superficial |
|
|
4634 | compatibility, so most programs should still compile. Those might be |
|
|
4635 | removed in later versions of libev, so better update early than late. |
|
|
4636 | |
4627 | =over 4 |
4637 | =over 4 |
4628 | |
4638 | |
|
|
4639 | =item C<ev_loop_count> renamed to C<ev_iteration> |
|
|
4640 | |
|
|
4641 | =item C<ev_loop_depth> renamed to C<ev_depth> |
|
|
4642 | |
|
|
4643 | =item C<ev_loop_verify> renamed to C<ev_verify> |
|
|
4644 | |
|
|
4645 | Most functions working on C<struct ev_loop> objects don't have an |
|
|
4646 | C<ev_loop_> prefix, so it was removed. Note that C<ev_loop_fork> is |
|
|
4647 | still called C<ev_loop_fork> because it would otherwise clash with the |
|
|
4648 | C<ev_fork> typedef. |
|
|
4649 | |
4629 | =item C<EV_TIMEOUT> replaced by C<EV_TIMER> in C<revents> |
4650 | =item C<EV_TIMEOUT> renamed to C<EV_TIMER> in C<revents> |
4630 | |
4651 | |
4631 | This is a simple rename - all other watcher types use their name |
4652 | This is a simple rename - all other watcher types use their name |
4632 | as revents flag, and now C<ev_timer> does, too. |
4653 | as revents flag, and now C<ev_timer> does, too. |
4633 | |
4654 | |
4634 | Both C<EV_TIMER> and C<EV_TIMEOUT> symbols were present in 3.x versions |
4655 | Both C<EV_TIMER> and C<EV_TIMEOUT> symbols were present in 3.x versions |