… | |
… | |
75 | While this document tries to be as complete as possible in documenting |
75 | While this document tries to be as complete as possible in documenting |
76 | libev, its usage and the rationale behind its design, it is not a tutorial |
76 | libev, its usage and the rationale behind its design, it is not a tutorial |
77 | on event-based programming, nor will it introduce event-based programming |
77 | on event-based programming, nor will it introduce event-based programming |
78 | with libev. |
78 | with libev. |
79 | |
79 | |
80 | Familarity with event based programming techniques in general is assumed |
80 | Familiarity with event based programming techniques in general is assumed |
81 | throughout this document. |
81 | throughout this document. |
82 | |
82 | |
83 | =head1 ABOUT LIBEV |
83 | =head1 ABOUT LIBEV |
84 | |
84 | |
85 | Libev is an event loop: you register interest in certain events (such as a |
85 | Libev is an event loop: you register interest in certain events (such as a |
… | |
… | |
192 | as this indicates an incompatible change. Minor versions are usually |
192 | as this indicates an incompatible change. Minor versions are usually |
193 | 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 |
194 | not a problem. |
194 | not a problem. |
195 | |
195 | |
196 | 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 |
197 | version. |
197 | version (note, however, that this will not detect ABI mismatches :). |
198 | |
198 | |
199 | assert (("libev version mismatch", |
199 | assert (("libev version mismatch", |
200 | ev_version_major () == EV_VERSION_MAJOR |
200 | ev_version_major () == EV_VERSION_MAJOR |
201 | && ev_version_minor () >= EV_VERSION_MINOR)); |
201 | && ev_version_minor () >= EV_VERSION_MINOR)); |
202 | |
202 | |
… | |
… | |
439 | of course I<doesn't>, and epoll just loves to report events for totally |
439 | of course I<doesn't>, and epoll just loves to report events for totally |
440 | I<different> file descriptors (even already closed ones, so one cannot |
440 | I<different> file descriptors (even already closed ones, so one cannot |
441 | even remove them from the set) than registered in the set (especially |
441 | even remove them from the set) than registered in the set (especially |
442 | on SMP systems). Libev tries to counter these spurious notifications by |
442 | on SMP systems). Libev tries to counter these spurious notifications by |
443 | employing an additional generation counter and comparing that against the |
443 | employing an additional generation counter and comparing that against the |
444 | events to filter out spurious ones, recreating the set when required. |
444 | events to filter out spurious ones, recreating the set when required. Last |
|
|
445 | not least, it also refuses to work with some file descriptors which work |
|
|
446 | perfectly fine with C<select> (files, many character devices...). |
445 | |
447 | |
446 | While stopping, setting and starting an I/O watcher in the same iteration |
448 | While stopping, setting and starting an I/O watcher in the same iteration |
447 | will result in some caching, there is still a system call per such |
449 | will result in some caching, there is still a system call per such |
448 | incident (because the same I<file descriptor> could point to a different |
450 | incident (because the same I<file descriptor> could point to a different |
449 | I<file description> now), so its best to avoid that. Also, C<dup ()>'ed |
451 | I<file description> now), so its best to avoid that. Also, C<dup ()>'ed |
… | |
… | |
705 | C<ev_resume> directly afterwards to resume timer processing. |
707 | C<ev_resume> directly afterwards to resume timer processing. |
706 | |
708 | |
707 | Effectively, all C<ev_timer> watchers will be delayed by the time spend |
709 | Effectively, all C<ev_timer> watchers will be delayed by the time spend |
708 | between C<ev_suspend> and C<ev_resume>, and all C<ev_periodic> watchers |
710 | between C<ev_suspend> and C<ev_resume>, and all C<ev_periodic> watchers |
709 | will be rescheduled (that is, they will lose any events that would have |
711 | will be rescheduled (that is, they will lose any events that would have |
710 | occured while suspended). |
712 | occurred while suspended). |
711 | |
713 | |
712 | After calling C<ev_suspend> you B<must not> call I<any> function on the |
714 | After calling C<ev_suspend> you B<must not> call I<any> function on the |
713 | given loop other than C<ev_resume>, and you B<must not> call C<ev_resume> |
715 | given loop other than C<ev_resume>, and you B<must not> call C<ev_resume> |
714 | without a previous call to C<ev_suspend>. |
716 | without a previous call to C<ev_suspend>. |
715 | |
717 | |
… | |
… | |
792 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
794 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
793 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
795 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
794 | |
796 | |
795 | This "unloop state" will be cleared when entering C<ev_loop> again. |
797 | This "unloop state" will be cleared when entering C<ev_loop> again. |
796 | |
798 | |
797 | It is safe to call C<ev_unloop> from otuside any C<ev_loop> calls. |
799 | It is safe to call C<ev_unloop> from outside any C<ev_loop> calls. |
798 | |
800 | |
799 | =item ev_ref (loop) |
801 | =item ev_ref (loop) |
800 | |
802 | |
801 | =item ev_unref (loop) |
803 | =item ev_unref (loop) |
802 | |
804 | |
… | |
… | |
872 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
874 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
873 | as this approaches the timing granularity of most systems. Note that if |
875 | as this approaches the timing granularity of most systems. Note that if |
874 | you do transactions with the outside world and you can't increase the |
876 | you do transactions with the outside world and you can't increase the |
875 | parallelity, then this setting will limit your transaction rate (if you |
877 | parallelity, then this setting will limit your transaction rate (if you |
876 | need to poll once per transaction and the I/O collect interval is 0.01, |
878 | need to poll once per transaction and the I/O collect interval is 0.01, |
877 | then you can't do more than 100 transations per second). |
879 | then you can't do more than 100 transactions per second). |
878 | |
880 | |
879 | Setting the I<timeout collect interval> can improve the opportunity for |
881 | Setting the I<timeout collect interval> can improve the opportunity for |
880 | saving power, as the program will "bundle" timer callback invocations that |
882 | saving power, as the program will "bundle" timer callback invocations that |
881 | are "near" in time together, by delaying some, thus reducing the number of |
883 | are "near" in time together, by delaying some, thus reducing the number of |
882 | times the process sleeps and wakes up again. Another useful technique to |
884 | times the process sleeps and wakes up again. Another useful technique to |
… | |
… | |
1380 | |
1382 | |
1381 | For example, to emulate how many other event libraries handle priorities, |
1383 | For example, to emulate how many other event libraries handle priorities, |
1382 | you can associate an C<ev_idle> watcher to each such watcher, and in |
1384 | you can associate an C<ev_idle> watcher to each such watcher, and in |
1383 | the normal watcher callback, you just start the idle watcher. The real |
1385 | the normal watcher callback, you just start the idle watcher. The real |
1384 | processing is done in the idle watcher callback. This causes libev to |
1386 | processing is done in the idle watcher callback. This causes libev to |
1385 | continously poll and process kernel event data for the watcher, but when |
1387 | continuously poll and process kernel event data for the watcher, but when |
1386 | the lock-out case is known to be rare (which in turn is rare :), this is |
1388 | the lock-out case is known to be rare (which in turn is rare :), this is |
1387 | workable. |
1389 | workable. |
1388 | |
1390 | |
1389 | Usually, however, the lock-out model implemented that way will perform |
1391 | Usually, however, the lock-out model implemented that way will perform |
1390 | miserably under the type of load it was designed to handle. In that case, |
1392 | miserably under the type of load it was designed to handle. In that case, |
… | |
… | |
1404 | { |
1406 | { |
1405 | // stop the I/O watcher, we received the event, but |
1407 | // stop the I/O watcher, we received the event, but |
1406 | // are not yet ready to handle it. |
1408 | // are not yet ready to handle it. |
1407 | ev_io_stop (EV_A_ w); |
1409 | ev_io_stop (EV_A_ w); |
1408 | |
1410 | |
1409 | // start the idle watcher to ahndle the actual event. |
1411 | // start the idle watcher to handle the actual event. |
1410 | // it will not be executed as long as other watchers |
1412 | // it will not be executed as long as other watchers |
1411 | // with the default priority are receiving events. |
1413 | // with the default priority are receiving events. |
1412 | ev_idle_start (EV_A_ &idle); |
1414 | ev_idle_start (EV_A_ &idle); |
1413 | } |
1415 | } |
1414 | |
1416 | |
… | |
… | |
1468 | |
1470 | |
1469 | If you cannot use non-blocking mode, then force the use of a |
1471 | If you cannot use non-blocking mode, then force the use of a |
1470 | known-to-be-good backend (at the time of this writing, this includes only |
1472 | known-to-be-good backend (at the time of this writing, this includes only |
1471 | C<EVBACKEND_SELECT> and C<EVBACKEND_POLL>). The same applies to file |
1473 | C<EVBACKEND_SELECT> and C<EVBACKEND_POLL>). The same applies to file |
1472 | descriptors for which non-blocking operation makes no sense (such as |
1474 | descriptors for which non-blocking operation makes no sense (such as |
1473 | files) - libev doesn't guarentee any specific behaviour in that case. |
1475 | files) - libev doesn't guarantee any specific behaviour in that case. |
1474 | |
1476 | |
1475 | Another thing you have to watch out for is that it is quite easy to |
1477 | Another thing you have to watch out for is that it is quite easy to |
1476 | receive "spurious" readiness notifications, that is your callback might |
1478 | receive "spurious" readiness notifications, that is your callback might |
1477 | be called with C<EV_READ> but a subsequent C<read>(2) will actually block |
1479 | be called with C<EV_READ> but a subsequent C<read>(2) will actually block |
1478 | because there is no data. Not only are some backends known to create a |
1480 | because there is no data. Not only are some backends known to create a |
… | |
… | |
1737 | ev_tstamp timeout = last_activity + 60.; |
1739 | ev_tstamp timeout = last_activity + 60.; |
1738 | |
1740 | |
1739 | // if last_activity + 60. is older than now, we did time out |
1741 | // if last_activity + 60. is older than now, we did time out |
1740 | if (timeout < now) |
1742 | if (timeout < now) |
1741 | { |
1743 | { |
1742 | // timeout occured, take action |
1744 | // timeout occurred, take action |
1743 | } |
1745 | } |
1744 | else |
1746 | else |
1745 | { |
1747 | { |
1746 | // callback was invoked, but there was some activity, re-arm |
1748 | // callback was invoked, but there was some activity, re-arm |
1747 | // the watcher to fire in last_activity + 60, which is |
1749 | // the watcher to fire in last_activity + 60, which is |
… | |
… | |
1774 | callback (loop, timer, EV_TIMER); |
1776 | callback (loop, timer, EV_TIMER); |
1775 | |
1777 | |
1776 | And when there is some activity, simply store the current time in |
1778 | And when there is some activity, simply store the current time in |
1777 | C<last_activity>, no libev calls at all: |
1779 | C<last_activity>, no libev calls at all: |
1778 | |
1780 | |
1779 | last_actiivty = ev_now (loop); |
1781 | last_activity = ev_now (loop); |
1780 | |
1782 | |
1781 | This technique is slightly more complex, but in most cases where the |
1783 | This technique is slightly more complex, but in most cases where the |
1782 | time-out is unlikely to be triggered, much more efficient. |
1784 | time-out is unlikely to be triggered, much more efficient. |
1783 | |
1785 | |
1784 | Changing the timeout is trivial as well (if it isn't hard-coded in the |
1786 | Changing the timeout is trivial as well (if it isn't hard-coded in the |
… | |
… | |
2123 | Example: Call a callback every hour, or, more precisely, whenever the |
2125 | Example: Call a callback every hour, or, more precisely, whenever the |
2124 | system time is divisible by 3600. The callback invocation times have |
2126 | system time is divisible by 3600. The callback invocation times have |
2125 | potentially a lot of jitter, but good long-term stability. |
2127 | potentially a lot of jitter, but good long-term stability. |
2126 | |
2128 | |
2127 | static void |
2129 | static void |
2128 | clock_cb (struct ev_loop *loop, ev_io *w, int revents) |
2130 | clock_cb (struct ev_loop *loop, ev_periodic *w, int revents) |
2129 | { |
2131 | { |
2130 | ... its now a full hour (UTC, or TAI or whatever your clock follows) |
2132 | ... its now a full hour (UTC, or TAI or whatever your clock follows) |
2131 | } |
2133 | } |
2132 | |
2134 | |
2133 | ev_periodic hourly_tick; |
2135 | ev_periodic hourly_tick; |
… | |
… | |
2965 | C<ev_default_fork> cheats and calls it in the wrong process, the fork |
2967 | C<ev_default_fork> cheats and calls it in the wrong process, the fork |
2966 | handlers will be invoked, too, of course. |
2968 | handlers will be invoked, too, of course. |
2967 | |
2969 | |
2968 | =head3 The special problem of life after fork - how is it possible? |
2970 | =head3 The special problem of life after fork - how is it possible? |
2969 | |
2971 | |
2970 | Most uses of C<fork()> consist of forking, then some simple calls to ste |
2972 | Most uses of C<fork()> consist of forking, then some simple calls to set |
2971 | up/change the process environment, followed by a call to C<exec()>. This |
2973 | up/change the process environment, followed by a call to C<exec()>. This |
2972 | sequence should be handled by libev without any problems. |
2974 | sequence should be handled by libev without any problems. |
2973 | |
2975 | |
2974 | This changes when the application actually wants to do event handling |
2976 | This changes when the application actually wants to do event handling |
2975 | in the child, or both parent in child, in effect "continuing" after the |
2977 | in the child, or both parent in child, in effect "continuing" after the |
… | |
… | |
3009 | believe me. |
3011 | believe me. |
3010 | |
3012 | |
3011 | =back |
3013 | =back |
3012 | |
3014 | |
3013 | |
3015 | |
3014 | =head2 C<ev_async> - how to wake up another event loop |
3016 | =head2 C<ev_async> - how to wake up an event loop |
3015 | |
3017 | |
3016 | In general, you cannot use an C<ev_loop> from multiple threads or other |
3018 | In general, you cannot use an C<ev_loop> from multiple threads or other |
3017 | asynchronous sources such as signal handlers (as opposed to multiple event |
3019 | asynchronous sources such as signal handlers (as opposed to multiple event |
3018 | loops - those are of course safe to use in different threads). |
3020 | loops - those are of course safe to use in different threads). |
3019 | |
3021 | |
3020 | Sometimes, however, you need to wake up another event loop you do not |
3022 | Sometimes, however, you need to wake up an event loop you do not control, |
3021 | control, for example because it belongs to another thread. This is what |
3023 | for example because it belongs to another thread. This is what C<ev_async> |
3022 | C<ev_async> watchers do: as long as the C<ev_async> watcher is active, you |
3024 | watchers do: as long as the C<ev_async> watcher is active, you can signal |
3023 | can signal it by calling C<ev_async_send>, which is thread- and signal |
3025 | it by calling C<ev_async_send>, which is thread- and signal safe. |
3024 | safe. |
|
|
3025 | |
3026 | |
3026 | This functionality is very similar to C<ev_signal> watchers, as signals, |
3027 | This functionality is very similar to C<ev_signal> watchers, as signals, |
3027 | too, are asynchronous in nature, and signals, too, will be compressed |
3028 | too, are asynchronous in nature, and signals, too, will be compressed |
3028 | (i.e. the number of callback invocations may be less than the number of |
3029 | (i.e. the number of callback invocations may be less than the number of |
3029 | C<ev_async_sent> calls). |
3030 | C<ev_async_sent> calls). |
… | |
… | |
3341 | myclass obj; |
3342 | myclass obj; |
3342 | ev::io iow; |
3343 | ev::io iow; |
3343 | iow.set <myclass, &myclass::io_cb> (&obj); |
3344 | iow.set <myclass, &myclass::io_cb> (&obj); |
3344 | |
3345 | |
3345 | =item w->set (object *) |
3346 | =item w->set (object *) |
3346 | |
|
|
3347 | This is an B<experimental> feature that might go away in a future version. |
|
|
3348 | |
3347 | |
3349 | This is a variation of a method callback - leaving out the method to call |
3348 | This is a variation of a method callback - leaving out the method to call |
3350 | will default the method to C<operator ()>, which makes it possible to use |
3349 | will default the method to C<operator ()>, which makes it possible to use |
3351 | functor objects without having to manually specify the C<operator ()> all |
3350 | functor objects without having to manually specify the C<operator ()> all |
3352 | the time. Incidentally, you can then also leave out the template argument |
3351 | the time. Incidentally, you can then also leave out the template argument |
… | |
… | |
3392 | Associates a different C<struct ev_loop> with this watcher. You can only |
3391 | Associates a different C<struct ev_loop> with this watcher. You can only |
3393 | do this when the watcher is inactive (and not pending either). |
3392 | do this when the watcher is inactive (and not pending either). |
3394 | |
3393 | |
3395 | =item w->set ([arguments]) |
3394 | =item w->set ([arguments]) |
3396 | |
3395 | |
3397 | Basically the same as C<ev_TYPE_set>, with the same arguments. Must be |
3396 | Basically the same as C<ev_TYPE_set>, with the same arguments. Either this |
3398 | called at least once. Unlike the C counterpart, an active watcher gets |
3397 | method or a suitable start method must be called at least once. Unlike the |
3399 | automatically stopped and restarted when reconfiguring it with this |
3398 | C counterpart, an active watcher gets automatically stopped and restarted |
3400 | method. |
3399 | when reconfiguring it with this method. |
3401 | |
3400 | |
3402 | =item w->start () |
3401 | =item w->start () |
3403 | |
3402 | |
3404 | Starts the watcher. Note that there is no C<loop> argument, as the |
3403 | Starts the watcher. Note that there is no C<loop> argument, as the |
3405 | constructor already stores the event loop. |
3404 | constructor already stores the event loop. |
3406 | |
3405 | |
|
|
3406 | =item w->start ([arguments]) |
|
|
3407 | |
|
|
3408 | Instead of calling C<set> and C<start> methods separately, it is often |
|
|
3409 | convenient to wrap them in one call. Uses the same type of arguments as |
|
|
3410 | the configure C<set> method of the watcher. |
|
|
3411 | |
3407 | =item w->stop () |
3412 | =item w->stop () |
3408 | |
3413 | |
3409 | Stops the watcher if it is active. Again, no C<loop> argument. |
3414 | Stops the watcher if it is active. Again, no C<loop> argument. |
3410 | |
3415 | |
3411 | =item w->again () (C<ev::timer>, C<ev::periodic> only) |
3416 | =item w->again () (C<ev::timer>, C<ev::periodic> only) |
… | |
… | |
3423 | |
3428 | |
3424 | =back |
3429 | =back |
3425 | |
3430 | |
3426 | =back |
3431 | =back |
3427 | |
3432 | |
3428 | Example: Define a class with an IO and idle watcher, start one of them in |
3433 | Example: Define a class with two I/O and idle watchers, start the I/O |
3429 | the constructor. |
3434 | watchers in the constructor. |
3430 | |
3435 | |
3431 | class myclass |
3436 | class myclass |
3432 | { |
3437 | { |
3433 | ev::io io ; void io_cb (ev::io &w, int revents); |
3438 | ev::io io ; void io_cb (ev::io &w, int revents); |
|
|
3439 | ev::io2 io2 ; void io2_cb (ev::io &w, int revents); |
3434 | ev::idle idle; void idle_cb (ev::idle &w, int revents); |
3440 | ev::idle idle; void idle_cb (ev::idle &w, int revents); |
3435 | |
3441 | |
3436 | myclass (int fd) |
3442 | myclass (int fd) |
3437 | { |
3443 | { |
3438 | io .set <myclass, &myclass::io_cb > (this); |
3444 | io .set <myclass, &myclass::io_cb > (this); |
|
|
3445 | io2 .set <myclass, &myclass::io2_cb > (this); |
3439 | idle.set <myclass, &myclass::idle_cb> (this); |
3446 | idle.set <myclass, &myclass::idle_cb> (this); |
3440 | |
3447 | |
3441 | io.start (fd, ev::READ); |
3448 | io.set (fd, ev::WRITE); // configure the watcher |
|
|
3449 | io.start (); // start it whenever convenient |
|
|
3450 | |
|
|
3451 | io2.start (fd, ev::READ); // set + start in one call |
3442 | } |
3452 | } |
3443 | }; |
3453 | }; |
3444 | |
3454 | |
3445 | |
3455 | |
3446 | =head1 OTHER LANGUAGE BINDINGS |
3456 | =head1 OTHER LANGUAGE BINDINGS |
… | |
… | |
3887 | EV_PREPARE_ENABLE, EV_CHECK_ENABLE, EV_FORK_ENABLE, EV_SIGNAL_ENABLE, |
3897 | EV_PREPARE_ENABLE, EV_CHECK_ENABLE, EV_FORK_ENABLE, EV_SIGNAL_ENABLE, |
3888 | EV_ASYNC_ENABLE, EV_CHILD_ENABLE. |
3898 | EV_ASYNC_ENABLE, EV_CHILD_ENABLE. |
3889 | |
3899 | |
3890 | If undefined or defined to be C<1> (and the platform supports it), then |
3900 | If undefined or defined to be C<1> (and the platform supports it), then |
3891 | the respective watcher type is supported. If defined to be C<0>, then it |
3901 | the respective watcher type is supported. If defined to be C<0>, then it |
3892 | is not. Disabling watcher types mainly saves codesize. |
3902 | is not. Disabling watcher types mainly saves code size. |
3893 | |
3903 | |
3894 | =item EV_FEATURES |
3904 | =item EV_FEATURES |
3895 | |
3905 | |
3896 | If you need to shave off some kilobytes of code at the expense of some |
3906 | If you need to shave off some kilobytes of code at the expense of some |
3897 | speed (but with the full API), you can define this symbol to request |
3907 | speed (but with the full API), you can define this symbol to request |
… | |
… | |
3917 | |
3927 | |
3918 | =item C<1> - faster/larger code |
3928 | =item C<1> - faster/larger code |
3919 | |
3929 | |
3920 | Use larger code to speed up some operations. |
3930 | Use larger code to speed up some operations. |
3921 | |
3931 | |
3922 | Currently this is used to override some inlining decisions (enlarging the roughly |
3932 | Currently this is used to override some inlining decisions (enlarging the |
3923 | 30% code size on amd64. |
3933 | code size by roughly 30% on amd64). |
3924 | |
3934 | |
3925 | When optimising for size, use of compiler flags such as C<-Os> with |
3935 | When optimising for size, use of compiler flags such as C<-Os> with |
3926 | gcc recommended, as well as C<-DNDEBUG>, as libev contains a number of |
3936 | gcc is recommended, as well as C<-DNDEBUG>, as libev contains a number of |
3927 | assertions. |
3937 | assertions. |
3928 | |
3938 | |
3929 | =item C<2> - faster/larger data structures |
3939 | =item C<2> - faster/larger data structures |
3930 | |
3940 | |
3931 | Replaces the small 2-heap for timer management by a faster 4-heap, larger |
3941 | Replaces the small 2-heap for timer management by a faster 4-heap, larger |
3932 | hash table sizes and so on. This will usually further increase codesize |
3942 | hash table sizes and so on. This will usually further increase code size |
3933 | and can additionally have an effect on the size of data structures at |
3943 | and can additionally have an effect on the size of data structures at |
3934 | runtime. |
3944 | runtime. |
3935 | |
3945 | |
3936 | =item C<4> - full API configuration |
3946 | =item C<4> - full API configuration |
3937 | |
3947 | |
… | |
… | |
3974 | I/O watcher then might come out at only 5Kb. |
3984 | I/O watcher then might come out at only 5Kb. |
3975 | |
3985 | |
3976 | =item EV_AVOID_STDIO |
3986 | =item EV_AVOID_STDIO |
3977 | |
3987 | |
3978 | If this is set to C<1> at compiletime, then libev will avoid using stdio |
3988 | If this is set to C<1> at compiletime, then libev will avoid using stdio |
3979 | functions (printf, scanf, perror etc.). This will increase the codesize |
3989 | functions (printf, scanf, perror etc.). This will increase the code size |
3980 | somewhat, but if your program doesn't otherwise depend on stdio and your |
3990 | somewhat, but if your program doesn't otherwise depend on stdio and your |
3981 | libc allows it, this avoids linking in the stdio library which is quite |
3991 | libc allows it, this avoids linking in the stdio library which is quite |
3982 | big. |
3992 | big. |
3983 | |
3993 | |
3984 | Note that error messages might become less precise when this option is |
3994 | Note that error messages might become less precise when this option is |
… | |
… | |
3988 | |
3998 | |
3989 | The highest supported signal number, +1 (or, the number of |
3999 | The highest supported signal number, +1 (or, the number of |
3990 | signals): Normally, libev tries to deduce the maximum number of signals |
4000 | signals): Normally, libev tries to deduce the maximum number of signals |
3991 | automatically, but sometimes this fails, in which case it can be |
4001 | automatically, but sometimes this fails, in which case it can be |
3992 | specified. Also, using a lower number than detected (C<32> should be |
4002 | specified. Also, using a lower number than detected (C<32> should be |
3993 | good for about any system in existance) can save some memory, as libev |
4003 | good for about any system in existence) can save some memory, as libev |
3994 | statically allocates some 12-24 bytes per signal number. |
4004 | statically allocates some 12-24 bytes per signal number. |
3995 | |
4005 | |
3996 | =item EV_PID_HASHSIZE |
4006 | =item EV_PID_HASHSIZE |
3997 | |
4007 | |
3998 | C<ev_child> watchers use a small hash table to distribute workload by |
4008 | C<ev_child> watchers use a small hash table to distribute workload by |
… | |
… | |
4044 | will be C<0>. |
4054 | will be C<0>. |
4045 | |
4055 | |
4046 | =item EV_COMMON |
4056 | =item EV_COMMON |
4047 | |
4057 | |
4048 | By default, all watchers have a C<void *data> member. By redefining |
4058 | By default, all watchers have a C<void *data> member. By redefining |
4049 | this macro to a something else you can include more and other types of |
4059 | this macro to something else you can include more and other types of |
4050 | members. You have to define it each time you include one of the files, |
4060 | members. You have to define it each time you include one of the files, |
4051 | though, and it must be identical each time. |
4061 | though, and it must be identical each time. |
4052 | |
4062 | |
4053 | For example, the perl EV module uses something like this: |
4063 | For example, the perl EV module uses something like this: |
4054 | |
4064 | |
… | |
… | |
4353 | maintainable. |
4363 | maintainable. |
4354 | |
4364 | |
4355 | And of course, some compiler warnings are just plain stupid, or simply |
4365 | And of course, some compiler warnings are just plain stupid, or simply |
4356 | wrong (because they don't actually warn about the condition their message |
4366 | wrong (because they don't actually warn about the condition their message |
4357 | seems to warn about). For example, certain older gcc versions had some |
4367 | seems to warn about). For example, certain older gcc versions had some |
4358 | warnings that resulted an extreme number of false positives. These have |
4368 | warnings that resulted in an extreme number of false positives. These have |
4359 | been fixed, but some people still insist on making code warn-free with |
4369 | been fixed, but some people still insist on making code warn-free with |
4360 | such buggy versions. |
4370 | such buggy versions. |
4361 | |
4371 | |
4362 | While libev is written to generate as few warnings as possible, |
4372 | While libev is written to generate as few warnings as possible, |
4363 | "warn-free" code is not a goal, and it is recommended not to build libev |
4373 | "warn-free" code is not a goal, and it is recommended not to build libev |
… | |
… | |
4399 | I suggest using suppression lists. |
4409 | I suggest using suppression lists. |
4400 | |
4410 | |
4401 | |
4411 | |
4402 | =head1 PORTABILITY NOTES |
4412 | =head1 PORTABILITY NOTES |
4403 | |
4413 | |
|
|
4414 | =head2 GNU/LINUX 32 BIT LIMITATIONS |
|
|
4415 | |
|
|
4416 | GNU/Linux is the only common platform that supports 64 bit file/large file |
|
|
4417 | interfaces but I<disables> them by default. |
|
|
4418 | |
|
|
4419 | That means that libev compiled in the default environment doesn't support |
|
|
4420 | files larger than 2GiB or so, which mainly affects C<ev_stat> watchers. |
|
|
4421 | |
|
|
4422 | Unfortunately, many programs try to work around this GNU/Linux issue |
|
|
4423 | by enabling the large file API, which makes them incompatible with the |
|
|
4424 | standard libev compiled for their system. |
|
|
4425 | |
|
|
4426 | Likewise, libev cannot enable the large file API itself as this would |
|
|
4427 | suddenly make it incompatible to the default compile time environment, |
|
|
4428 | i.e. all programs not using special compile switches. |
|
|
4429 | |
|
|
4430 | =head2 OS/X AND DARWIN BUGS |
|
|
4431 | |
|
|
4432 | The whole thing is a bug if you ask me - basically any system interface |
|
|
4433 | you touch is broken, whether it is locales, poll, kqueue or even the |
|
|
4434 | OpenGL drivers. |
|
|
4435 | |
|
|
4436 | =head3 C<kqueue> is buggy |
|
|
4437 | |
|
|
4438 | The kqueue syscall is broken in all known versions - most versions support |
|
|
4439 | only sockets, many support pipes. |
|
|
4440 | |
|
|
4441 | Libev tries to work around this by not using C<kqueue> by default on |
|
|
4442 | this rotten platform, but of course you can still ask for it when creating |
|
|
4443 | a loop. |
|
|
4444 | |
|
|
4445 | =head3 C<poll> is buggy |
|
|
4446 | |
|
|
4447 | Instead of fixing C<kqueue>, Apple replaced their (working) C<poll> |
|
|
4448 | implementation by something calling C<kqueue> internally around the 10.5.6 |
|
|
4449 | release, so now C<kqueue> I<and> C<poll> are broken. |
|
|
4450 | |
|
|
4451 | Libev tries to work around this by not using C<poll> by default on |
|
|
4452 | this rotten platform, but of course you can still ask for it when creating |
|
|
4453 | a loop. |
|
|
4454 | |
|
|
4455 | =head3 C<select> is buggy |
|
|
4456 | |
|
|
4457 | All that's left is C<select>, and of course Apple found a way to fuck this |
|
|
4458 | one up as well: On OS/X, C<select> actively limits the number of file |
|
|
4459 | descriptors you can pass in to 1024 - your program suddenly crashes when |
|
|
4460 | you use more. |
|
|
4461 | |
|
|
4462 | There is an undocumented "workaround" for this - defining |
|
|
4463 | C<_DARWIN_UNLIMITED_SELECT>, which libev tries to use, so select I<should> |
|
|
4464 | work on OS/X. |
|
|
4465 | |
|
|
4466 | =head2 SOLARIS PROBLEMS AND WORKAROUNDS |
|
|
4467 | |
|
|
4468 | =head3 C<errno> reentrancy |
|
|
4469 | |
|
|
4470 | The default compile environment on Solaris is unfortunately so |
|
|
4471 | thread-unsafe that you can't even use components/libraries compiled |
|
|
4472 | without C<-D_REENTRANT> (as long as they use C<errno>), which, of course, |
|
|
4473 | isn't defined by default. |
|
|
4474 | |
|
|
4475 | If you want to use libev in threaded environments you have to make sure |
|
|
4476 | it's compiled with C<_REENTRANT> defined. |
|
|
4477 | |
|
|
4478 | =head3 Event port backend |
|
|
4479 | |
|
|
4480 | The scalable event interface for Solaris is called "event ports". Unfortunately, |
|
|
4481 | this mechanism is very buggy. If you run into high CPU usage, your program |
|
|
4482 | freezes or you get a large number of spurious wakeups, make sure you have |
|
|
4483 | all the relevant and latest kernel patches applied. No, I don't know which |
|
|
4484 | ones, but there are multiple ones. |
|
|
4485 | |
|
|
4486 | If you can't get it to work, you can try running the program by setting |
|
|
4487 | the environment variable C<LIBEV_FLAGS=3> to only allow C<poll> and |
|
|
4488 | C<select> backends. |
|
|
4489 | |
|
|
4490 | =head2 AIX POLL BUG |
|
|
4491 | |
|
|
4492 | AIX unfortunately has a broken C<poll.h> header. Libev works around |
|
|
4493 | this by trying to avoid the poll backend altogether (i.e. it's not even |
|
|
4494 | compiled in), which normally isn't a big problem as C<select> works fine |
|
|
4495 | with large bitsets, and AIX is dead anyway. |
|
|
4496 | |
4404 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
4497 | =head2 WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS |
|
|
4498 | |
|
|
4499 | =head3 General issues |
4405 | |
4500 | |
4406 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
4501 | Win32 doesn't support any of the standards (e.g. POSIX) that libev |
4407 | requires, and its I/O model is fundamentally incompatible with the POSIX |
4502 | requires, and its I/O model is fundamentally incompatible with the POSIX |
4408 | model. Libev still offers limited functionality on this platform in |
4503 | model. Libev still offers limited functionality on this platform in |
4409 | the form of the C<EVBACKEND_SELECT> backend, and only supports socket |
4504 | the form of the C<EVBACKEND_SELECT> backend, and only supports socket |
4410 | descriptors. This only applies when using Win32 natively, not when using |
4505 | descriptors. This only applies when using Win32 natively, not when using |
4411 | e.g. cygwin. |
4506 | e.g. cygwin. Actually, it only applies to the microsofts own compilers, |
|
|
4507 | as every compielr comes with a slightly differently broken/incompatible |
|
|
4508 | environment. |
4412 | |
4509 | |
4413 | Lifting these limitations would basically require the full |
4510 | Lifting these limitations would basically require the full |
4414 | re-implementation of the I/O system. If you are into these kinds of |
4511 | re-implementation of the I/O system. If you are into this kind of thing, |
4415 | things, then note that glib does exactly that for you in a very portable |
4512 | then note that glib does exactly that for you in a very portable way (note |
4416 | way (note also that glib is the slowest event library known to man). |
4513 | also that glib is the slowest event library known to man). |
4417 | |
4514 | |
4418 | There is no supported compilation method available on windows except |
4515 | There is no supported compilation method available on windows except |
4419 | embedding it into other applications. |
4516 | embedding it into other applications. |
4420 | |
4517 | |
4421 | Sensible signal handling is officially unsupported by Microsoft - libev |
4518 | Sensible signal handling is officially unsupported by Microsoft - libev |
… | |
… | |
4449 | you do I<not> compile the F<ev.c> or any other embedded source files!): |
4546 | you do I<not> compile the F<ev.c> or any other embedded source files!): |
4450 | |
4547 | |
4451 | #include "evwrap.h" |
4548 | #include "evwrap.h" |
4452 | #include "ev.c" |
4549 | #include "ev.c" |
4453 | |
4550 | |
4454 | =over 4 |
|
|
4455 | |
|
|
4456 | =item The winsocket select function |
4551 | =head3 The winsocket C<select> function |
4457 | |
4552 | |
4458 | The winsocket C<select> function doesn't follow POSIX in that it |
4553 | The winsocket C<select> function doesn't follow POSIX in that it |
4459 | requires socket I<handles> and not socket I<file descriptors> (it is |
4554 | requires socket I<handles> and not socket I<file descriptors> (it is |
4460 | also extremely buggy). This makes select very inefficient, and also |
4555 | also extremely buggy). This makes select very inefficient, and also |
4461 | requires a mapping from file descriptors to socket handles (the Microsoft |
4556 | requires a mapping from file descriptors to socket handles (the Microsoft |
… | |
… | |
4470 | #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */ |
4565 | #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */ |
4471 | |
4566 | |
4472 | Note that winsockets handling of fd sets is O(n), so you can easily get a |
4567 | Note that winsockets handling of fd sets is O(n), so you can easily get a |
4473 | complexity in the O(n²) range when using win32. |
4568 | complexity in the O(n²) range when using win32. |
4474 | |
4569 | |
4475 | =item Limited number of file descriptors |
4570 | =head3 Limited number of file descriptors |
4476 | |
4571 | |
4477 | Windows has numerous arbitrary (and low) limits on things. |
4572 | Windows has numerous arbitrary (and low) limits on things. |
4478 | |
4573 | |
4479 | Early versions of winsocket's select only supported waiting for a maximum |
4574 | Early versions of winsocket's select only supported waiting for a maximum |
4480 | of C<64> handles (probably owning to the fact that all windows kernels |
4575 | of C<64> handles (probably owning to the fact that all windows kernels |
… | |
… | |
4495 | runtime libraries. This might get you to about C<512> or C<2048> sockets |
4590 | runtime libraries. This might get you to about C<512> or C<2048> sockets |
4496 | (depending on windows version and/or the phase of the moon). To get more, |
4591 | (depending on windows version and/or the phase of the moon). To get more, |
4497 | you need to wrap all I/O functions and provide your own fd management, but |
4592 | you need to wrap all I/O functions and provide your own fd management, but |
4498 | the cost of calling select (O(n²)) will likely make this unworkable. |
4593 | the cost of calling select (O(n²)) will likely make this unworkable. |
4499 | |
4594 | |
4500 | =back |
|
|
4501 | |
|
|
4502 | =head2 PORTABILITY REQUIREMENTS |
4595 | =head2 PORTABILITY REQUIREMENTS |
4503 | |
4596 | |
4504 | In addition to a working ISO-C implementation and of course the |
4597 | In addition to a working ISO-C implementation and of course the |
4505 | backend-specific APIs, libev relies on a few additional extensions: |
4598 | backend-specific APIs, libev relies on a few additional extensions: |
4506 | |
4599 | |
… | |
… | |
4544 | watchers. |
4637 | watchers. |
4545 | |
4638 | |
4546 | =item C<double> must hold a time value in seconds with enough accuracy |
4639 | =item C<double> must hold a time value in seconds with enough accuracy |
4547 | |
4640 | |
4548 | The type C<double> is used to represent timestamps. It is required to |
4641 | The type C<double> is used to represent timestamps. It is required to |
4549 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
4642 | have at least 51 bits of mantissa (and 9 bits of exponent), which is |
4550 | enough for at least into the year 4000. This requirement is fulfilled by |
4643 | good enough for at least into the year 4000 with millisecond accuracy |
|
|
4644 | (the design goal for libev). This requirement is overfulfilled by |
4551 | implementations implementing IEEE 754, which is basically all existing |
4645 | implementations using IEEE 754, which is basically all existing ones. With |
4552 | ones. With IEEE 754 doubles, you get microsecond accuracy until at least |
4646 | IEEE 754 doubles, you get microsecond accuracy until at least 2200. |
4553 | 2200. |
|
|
4554 | |
4647 | |
4555 | =back |
4648 | =back |
4556 | |
4649 | |
4557 | If you know of other additional requirements drop me a note. |
4650 | If you know of other additional requirements drop me a note. |
4558 | |
4651 | |
… | |
… | |
4653 | |
4746 | |
4654 | This is a simple rename - all other watcher types use their name |
4747 | This is a simple rename - all other watcher types use their name |
4655 | as revents flag, and now C<ev_timer> does, too. |
4748 | as revents flag, and now C<ev_timer> does, too. |
4656 | |
4749 | |
4657 | Both C<EV_TIMER> and C<EV_TIMEOUT> symbols were present in 3.x versions |
4750 | Both C<EV_TIMER> and C<EV_TIMEOUT> symbols were present in 3.x versions |
4658 | and continue to be present for the forseeable future, so this is mostly a |
4751 | and continue to be present for the foreseeable future, so this is mostly a |
4659 | documentation change. |
4752 | documentation change. |
4660 | |
4753 | |
4661 | =item C<EV_MINIMAL> mechanism replaced by C<EV_FEATURES> |
4754 | =item C<EV_MINIMAL> mechanism replaced by C<EV_FEATURES> |
4662 | |
4755 | |
4663 | The preprocessor symbol C<EV_MINIMAL> has been replaced by a different |
4756 | The preprocessor symbol C<EV_MINIMAL> has been replaced by a different |