… | |
… | |
130 | .\} |
130 | .\} |
131 | .rm #[ #] #H #V #F C |
131 | .rm #[ #] #H #V #F C |
132 | .\" ======================================================================== |
132 | .\" ======================================================================== |
133 | .\" |
133 | .\" |
134 | .IX Title "LIBEV 3" |
134 | .IX Title "LIBEV 3" |
135 | .TH LIBEV 3 "2008-10-30" "libev-3.48" "libev - high performance full featured event loop" |
135 | .TH LIBEV 3 "2009-04-25" "libev-3.6" "libev - high performance full featured event loop" |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
137 | .\" way too many mistakes in technical documents. |
137 | .\" way too many mistakes in technical documents. |
138 | .if n .ad l |
138 | .if n .ad l |
139 | .nh |
139 | .nh |
140 | .SH "NAME" |
140 | .SH "NAME" |
… | |
… | |
148 | .IX Subsection "EXAMPLE PROGRAM" |
148 | .IX Subsection "EXAMPLE PROGRAM" |
149 | .Vb 2 |
149 | .Vb 2 |
150 | \& // a single header file is required |
150 | \& // a single header file is required |
151 | \& #include <ev.h> |
151 | \& #include <ev.h> |
152 | \& |
152 | \& |
|
|
153 | \& #include <stdio.h> // for puts |
|
|
154 | \& |
153 | \& // every watcher type has its own typedef\*(Aqd struct |
155 | \& // every watcher type has its own typedef\*(Aqd struct |
154 | \& // with the name ev_TYPE |
156 | \& // with the name ev_TYPE |
155 | \& ev_io stdin_watcher; |
157 | \& ev_io stdin_watcher; |
156 | \& ev_timer timeout_watcher; |
158 | \& ev_timer timeout_watcher; |
157 | \& |
159 | \& |
… | |
… | |
180 | \& |
182 | \& |
181 | \& int |
183 | \& int |
182 | \& main (void) |
184 | \& main (void) |
183 | \& { |
185 | \& { |
184 | \& // use the default event loop unless you have special needs |
186 | \& // use the default event loop unless you have special needs |
185 | \& ev_loop *loop = ev_default_loop (0); |
187 | \& struct ev_loop *loop = ev_default_loop (0); |
186 | \& |
188 | \& |
187 | \& // initialise an io watcher, then start it |
189 | \& // initialise an io watcher, then start it |
188 | \& // this one will watch for stdin to become readable |
190 | \& // this one will watch for stdin to become readable |
189 | \& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
191 | \& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
190 | \& ev_io_start (loop, &stdin_watcher); |
192 | \& ev_io_start (loop, &stdin_watcher); |
… | |
… | |
199 | \& |
201 | \& |
200 | \& // unloop was called, so exit |
202 | \& // unloop was called, so exit |
201 | \& return 0; |
203 | \& return 0; |
202 | \& } |
204 | \& } |
203 | .Ve |
205 | .Ve |
204 | .SH "DESCRIPTION" |
206 | .SH "ABOUT THIS DOCUMENT" |
205 | .IX Header "DESCRIPTION" |
207 | .IX Header "ABOUT THIS DOCUMENT" |
|
|
208 | This document documents the libev software package. |
|
|
209 | .PP |
206 | The newest version of this document is also available as an html-formatted |
210 | The newest version of this document is also available as an html-formatted |
207 | web page you might find easier to navigate when reading it for the first |
211 | web page you might find easier to navigate when reading it for the first |
208 | time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. |
212 | time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. |
209 | .PP |
213 | .PP |
|
|
214 | While this document tries to be as complete as possible in documenting |
|
|
215 | libev, its usage and the rationale behind its design, it is not a tutorial |
|
|
216 | on event-based programming, nor will it introduce event-based programming |
|
|
217 | with libev. |
|
|
218 | .PP |
|
|
219 | Familarity with event based programming techniques in general is assumed |
|
|
220 | throughout this document. |
|
|
221 | .SH "ABOUT LIBEV" |
|
|
222 | .IX Header "ABOUT LIBEV" |
210 | Libev is an event loop: you register interest in certain events (such as a |
223 | Libev is an event loop: you register interest in certain events (such as a |
211 | file descriptor being readable or a timeout occurring), and it will manage |
224 | file descriptor being readable or a timeout occurring), and it will manage |
212 | these event sources and provide your program with events. |
225 | these event sources and provide your program with events. |
213 | .PP |
226 | .PP |
214 | To do this, it must take more or less complete control over your process |
227 | To do this, it must take more or less complete control over your process |
… | |
… | |
244 | for multiple event loops, then all functions taking an initial argument of |
257 | for multiple event loops, then all functions taking an initial argument of |
245 | name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`ev_loop *\*(C'\fR) will not have |
258 | name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`ev_loop *\*(C'\fR) will not have |
246 | this argument. |
259 | this argument. |
247 | .Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" |
260 | .Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" |
248 | .IX Subsection "TIME REPRESENTATION" |
261 | .IX Subsection "TIME REPRESENTATION" |
249 | Libev represents time as a single floating point number, representing the |
262 | Libev represents time as a single floating point number, representing |
250 | (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near |
263 | the (fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere |
251 | the beginning of 1970, details are complicated, don't ask). This type is |
264 | near the beginning of 1970, details are complicated, don't ask). This |
252 | called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases |
265 | type is called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually |
253 | to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on |
266 | aliases to the \f(CW\*(C`double\*(C'\fR type in C. When you need to do any calculations |
254 | it, you should treat it as some floating point value. Unlike the name |
267 | on it, you should treat it as some floating point value. Unlike the name |
255 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
268 | component \f(CW\*(C`stamp\*(C'\fR might indicate, it is also used for time differences |
256 | throughout libev. |
269 | throughout libev. |
257 | .SH "ERROR HANDLING" |
270 | .SH "ERROR HANDLING" |
258 | .IX Header "ERROR HANDLING" |
271 | .IX Header "ERROR HANDLING" |
259 | Libev knows three classes of errors: operating system errors, usage errors |
272 | Libev knows three classes of errors: operating system errors, usage errors |
… | |
… | |
544 | starting a watcher (without re-setting it) also usually doesn't cause |
557 | starting a watcher (without re-setting it) also usually doesn't cause |
545 | extra overhead. A fork can both result in spurious notifications as well |
558 | extra overhead. A fork can both result in spurious notifications as well |
546 | as in libev having to destroy and recreate the epoll object, which can |
559 | as in libev having to destroy and recreate the epoll object, which can |
547 | take considerable time and thus should be avoided. |
560 | take considerable time and thus should be avoided. |
548 | .Sp |
561 | .Sp |
|
|
562 | All this means that, in practice, \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR can be as fast or |
|
|
563 | faster than epoll for maybe up to a hundred file descriptors, depending on |
|
|
564 | the usage. So sad. |
|
|
565 | .Sp |
549 | While nominally embeddable in other event loops, this feature is broken in |
566 | While nominally embeddable in other event loops, this feature is broken in |
550 | all kernel versions tested so far. |
567 | all kernel versions tested so far. |
551 | .Sp |
568 | .Sp |
552 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as |
569 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as |
553 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
570 | \&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. |
… | |
… | |
580 | .Sp |
597 | .Sp |
581 | While nominally embeddable in other event loops, this doesn't work |
598 | While nominally embeddable in other event loops, this doesn't work |
582 | everywhere, so you might need to test for this. And since it is broken |
599 | everywhere, so you might need to test for this. And since it is broken |
583 | almost everywhere, you should only use it when you have a lot of sockets |
600 | almost everywhere, you should only use it when you have a lot of sockets |
584 | (for which it usually works), by embedding it into another event loop |
601 | (for which it usually works), by embedding it into another event loop |
585 | (e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR) and, did I mention it, |
602 | (e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR (but \f(CW\*(C`poll\*(C'\fR is of course |
586 | using it only for sockets. |
603 | also broken on \s-1OS\s0 X)) and, did I mention it, using it only for sockets. |
587 | .Sp |
604 | .Sp |
588 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR into an \f(CW\*(C`EVFILT_READ\*(C'\fR kevent with |
605 | This backend maps \f(CW\*(C`EV_READ\*(C'\fR into an \f(CW\*(C`EVFILT_READ\*(C'\fR kevent with |
589 | \&\f(CW\*(C`NOTE_EOF\*(C'\fR, and \f(CW\*(C`EV_WRITE\*(C'\fR into an \f(CW\*(C`EVFILT_WRITE\*(C'\fR kevent with |
606 | \&\f(CW\*(C`NOTE_EOF\*(C'\fR, and \f(CW\*(C`EV_WRITE\*(C'\fR into an \f(CW\*(C`EVFILT_WRITE\*(C'\fR kevent with |
590 | \&\f(CW\*(C`NOTE_EOF\*(C'\fR. |
607 | \&\f(CW\*(C`NOTE_EOF\*(C'\fR. |
591 | .ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4 |
608 | .ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4 |
… | |
… | |
754 | This function is rarely useful, but when some event callback runs for a |
771 | This function is rarely useful, but when some event callback runs for a |
755 | very long time without entering the event loop, updating libev's idea of |
772 | very long time without entering the event loop, updating libev's idea of |
756 | the current time is a good idea. |
773 | the current time is a good idea. |
757 | .Sp |
774 | .Sp |
758 | See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer\*(C'\fR section. |
775 | See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer\*(C'\fR section. |
|
|
776 | .IP "ev_suspend (loop)" 4 |
|
|
777 | .IX Item "ev_suspend (loop)" |
|
|
778 | .PD 0 |
|
|
779 | .IP "ev_resume (loop)" 4 |
|
|
780 | .IX Item "ev_resume (loop)" |
|
|
781 | .PD |
|
|
782 | These two functions suspend and resume a loop, for use when the loop is |
|
|
783 | not used for a while and timeouts should not be processed. |
|
|
784 | .Sp |
|
|
785 | A typical use case would be an interactive program such as a game: When |
|
|
786 | the user presses \f(CW\*(C`^Z\*(C'\fR to suspend the game and resumes it an hour later it |
|
|
787 | would be best to handle timeouts as if no time had actually passed while |
|
|
788 | the program was suspended. This can be achieved by calling \f(CW\*(C`ev_suspend\*(C'\fR |
|
|
789 | in your \f(CW\*(C`SIGTSTP\*(C'\fR handler, sending yourself a \f(CW\*(C`SIGSTOP\*(C'\fR and calling |
|
|
790 | \&\f(CW\*(C`ev_resume\*(C'\fR directly afterwards to resume timer processing. |
|
|
791 | .Sp |
|
|
792 | Effectively, all \f(CW\*(C`ev_timer\*(C'\fR watchers will be delayed by the time spend |
|
|
793 | between \f(CW\*(C`ev_suspend\*(C'\fR and \f(CW\*(C`ev_resume\*(C'\fR, and all \f(CW\*(C`ev_periodic\*(C'\fR watchers |
|
|
794 | will be rescheduled (that is, they will lose any events that would have |
|
|
795 | occured while suspended). |
|
|
796 | .Sp |
|
|
797 | After calling \f(CW\*(C`ev_suspend\*(C'\fR you \fBmust not\fR call \fIany\fR function on the |
|
|
798 | given loop other than \f(CW\*(C`ev_resume\*(C'\fR, and you \fBmust not\fR call \f(CW\*(C`ev_resume\*(C'\fR |
|
|
799 | without a previous call to \f(CW\*(C`ev_suspend\*(C'\fR. |
|
|
800 | .Sp |
|
|
801 | Calling \f(CW\*(C`ev_suspend\*(C'\fR/\f(CW\*(C`ev_resume\*(C'\fR has the side effect of updating the |
|
|
802 | event loop time (see \f(CW\*(C`ev_now_update\*(C'\fR). |
759 | .IP "ev_loop (loop, int flags)" 4 |
803 | .IP "ev_loop (loop, int flags)" 4 |
760 | .IX Item "ev_loop (loop, int flags)" |
804 | .IX Item "ev_loop (loop, int flags)" |
761 | Finally, this is it, the event handler. This function usually is called |
805 | Finally, this is it, the event handler. This function usually is called |
762 | after you initialised all your watchers and you want to start handling |
806 | after you initialised all your watchers and you want to start handling |
763 | events. |
807 | events. |
… | |
… | |
850 | .Sp |
894 | .Sp |
851 | If you have a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR |
895 | If you have a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR |
852 | from returning, call \fIev_unref()\fR after starting, and \fIev_ref()\fR before |
896 | from returning, call \fIev_unref()\fR after starting, and \fIev_ref()\fR before |
853 | stopping it. |
897 | stopping it. |
854 | .Sp |
898 | .Sp |
855 | As an example, libev itself uses this for its internal signal pipe: It is |
899 | As an example, libev itself uses this for its internal signal pipe: It |
856 | not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting |
900 | is not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from |
857 | if no event watchers registered by it are active. It is also an excellent |
901 | exiting if no event watchers registered by it are active. It is also an |
858 | way to do this for generic recurring timers or from within third-party |
902 | excellent way to do this for generic recurring timers or from within |
859 | libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR |
903 | third-party libraries. Just remember to \fIunref after start\fR and \fIref |
860 | (but only if the watcher wasn't active before, or was active before, |
904 | before stop\fR (but only if the watcher wasn't active before, or was active |
861 | respectively). |
905 | before, respectively. Note also that libev might stop watchers itself |
|
|
906 | (e.g. non-repeating timers) in which case you have to \f(CW\*(C`ev_ref\*(C'\fR |
|
|
907 | in the callback). |
862 | .Sp |
908 | .Sp |
863 | Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR |
909 | Example: Create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR |
864 | running when nothing else is active. |
910 | running when nothing else is active. |
865 | .Sp |
911 | .Sp |
866 | .Vb 4 |
912 | .Vb 4 |
… | |
… | |
1054 | \&\f(CW\*(C`ev_fork\*(C'\fR). |
1100 | \&\f(CW\*(C`ev_fork\*(C'\fR). |
1055 | .ie n .IP """EV_ASYNC""" 4 |
1101 | .ie n .IP """EV_ASYNC""" 4 |
1056 | .el .IP "\f(CWEV_ASYNC\fR" 4 |
1102 | .el .IP "\f(CWEV_ASYNC\fR" 4 |
1057 | .IX Item "EV_ASYNC" |
1103 | .IX Item "EV_ASYNC" |
1058 | The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR). |
1104 | The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR). |
|
|
1105 | .ie n .IP """EV_CUSTOM""" 4 |
|
|
1106 | .el .IP "\f(CWEV_CUSTOM\fR" 4 |
|
|
1107 | .IX Item "EV_CUSTOM" |
|
|
1108 | Not ever sent (or otherwise used) by libev itself, but can be freely used |
|
|
1109 | by libev users to signal watchers (e.g. via \f(CW\*(C`ev_feed_event\*(C'\fR). |
1059 | .ie n .IP """EV_ERROR""" 4 |
1110 | .ie n .IP """EV_ERROR""" 4 |
1060 | .el .IP "\f(CWEV_ERROR\fR" 4 |
1111 | .el .IP "\f(CWEV_ERROR\fR" 4 |
1061 | .IX Item "EV_ERROR" |
1112 | .IX Item "EV_ERROR" |
1062 | An unspecified error has occurred, the watcher has been stopped. This might |
1113 | An unspecified error has occurred, the watcher has been stopped. This might |
1063 | happen because the watcher could not be properly started because libev |
1114 | happen because the watcher could not be properly started because libev |
… | |
… | |
1178 | integer between \f(CW\*(C`EV_MAXPRI\*(C'\fR (default: \f(CW2\fR) and \f(CW\*(C`EV_MINPRI\*(C'\fR |
1229 | integer between \f(CW\*(C`EV_MAXPRI\*(C'\fR (default: \f(CW2\fR) and \f(CW\*(C`EV_MINPRI\*(C'\fR |
1179 | (default: \f(CW\*(C`\-2\*(C'\fR). Pending watchers with higher priority will be invoked |
1230 | (default: \f(CW\*(C`\-2\*(C'\fR). Pending watchers with higher priority will be invoked |
1180 | before watchers with lower priority, but priority will not keep watchers |
1231 | before watchers with lower priority, but priority will not keep watchers |
1181 | from being executed (except for \f(CW\*(C`ev_idle\*(C'\fR watchers). |
1232 | from being executed (except for \f(CW\*(C`ev_idle\*(C'\fR watchers). |
1182 | .Sp |
1233 | .Sp |
1183 | This means that priorities are \fIonly\fR used for ordering callback |
|
|
1184 | invocation after new events have been received. This is useful, for |
|
|
1185 | example, to reduce latency after idling, or more often, to bind two |
|
|
1186 | watchers on the same event and make sure one is called first. |
|
|
1187 | .Sp |
|
|
1188 | If you need to suppress invocation when higher priority events are pending |
1234 | If you need to suppress invocation when higher priority events are pending |
1189 | you need to look at \f(CW\*(C`ev_idle\*(C'\fR watchers, which provide this functionality. |
1235 | you need to look at \f(CW\*(C`ev_idle\*(C'\fR watchers, which provide this functionality. |
1190 | .Sp |
1236 | .Sp |
1191 | You \fImust not\fR change the priority of a watcher as long as it is active or |
1237 | You \fImust not\fR change the priority of a watcher as long as it is active or |
1192 | pending. |
1238 | pending. |
1193 | .Sp |
|
|
1194 | The default priority used by watchers when no priority has been set is |
|
|
1195 | always \f(CW0\fR, which is supposed to not be too high and not be too low :). |
|
|
1196 | .Sp |
1239 | .Sp |
1197 | Setting a priority outside the range of \f(CW\*(C`EV_MINPRI\*(C'\fR to \f(CW\*(C`EV_MAXPRI\*(C'\fR is |
1240 | Setting a priority outside the range of \f(CW\*(C`EV_MINPRI\*(C'\fR to \f(CW\*(C`EV_MAXPRI\*(C'\fR is |
1198 | fine, as long as you do not mind that the priority value you query might |
1241 | fine, as long as you do not mind that the priority value you query might |
1199 | or might not have been clamped to the valid range. |
1242 | or might not have been clamped to the valid range. |
|
|
1243 | .Sp |
|
|
1244 | The default priority used by watchers when no priority has been set is |
|
|
1245 | always \f(CW0\fR, which is supposed to not be too high and not be too low :). |
|
|
1246 | .Sp |
|
|
1247 | See \*(L"\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0\*(R", below, for a more thorough treatment of |
|
|
1248 | priorities. |
1200 | .IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 |
1249 | .IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 |
1201 | .IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" |
1250 | .IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" |
1202 | Invoke the \f(CW\*(C`watcher\*(C'\fR with the given \f(CW\*(C`loop\*(C'\fR and \f(CW\*(C`revents\*(C'\fR. Neither |
1251 | Invoke the \f(CW\*(C`watcher\*(C'\fR with the given \f(CW\*(C`loop\*(C'\fR and \f(CW\*(C`revents\*(C'\fR. Neither |
1203 | \&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback |
1252 | \&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback |
1204 | can deal with that fact, as both are simply passed through to the |
1253 | can deal with that fact, as both are simply passed through to the |
… | |
… | |
1281 | \& { |
1330 | \& { |
1282 | \& struct my_biggy big = (struct my_biggy * |
1331 | \& struct my_biggy big = (struct my_biggy * |
1283 | \& (((char *)w) \- offsetof (struct my_biggy, t2)); |
1332 | \& (((char *)w) \- offsetof (struct my_biggy, t2)); |
1284 | \& } |
1333 | \& } |
1285 | .Ve |
1334 | .Ve |
|
|
1335 | .Sh "\s-1WATCHER\s0 \s-1PRIORITY\s0 \s-1MODELS\s0" |
|
|
1336 | .IX Subsection "WATCHER PRIORITY MODELS" |
|
|
1337 | Many event loops support \fIwatcher priorities\fR, which are usually small |
|
|
1338 | integers that influence the ordering of event callback invocation |
|
|
1339 | between watchers in some way, all else being equal. |
|
|
1340 | .PP |
|
|
1341 | In libev, Watcher priorities can be set using \f(CW\*(C`ev_set_priority\*(C'\fR. See its |
|
|
1342 | description for the more technical details such as the actual priority |
|
|
1343 | range. |
|
|
1344 | .PP |
|
|
1345 | There are two common ways how these these priorities are being interpreted |
|
|
1346 | by event loops: |
|
|
1347 | .PP |
|
|
1348 | In the more common lock-out model, higher priorities \*(L"lock out\*(R" invocation |
|
|
1349 | of lower priority watchers, which means as long as higher priority |
|
|
1350 | watchers receive events, lower priority watchers are not being invoked. |
|
|
1351 | .PP |
|
|
1352 | The less common only-for-ordering model uses priorities solely to order |
|
|
1353 | callback invocation within a single event loop iteration: Higher priority |
|
|
1354 | watchers are invoked before lower priority ones, but they all get invoked |
|
|
1355 | before polling for new events. |
|
|
1356 | .PP |
|
|
1357 | Libev uses the second (only-for-ordering) model for all its watchers |
|
|
1358 | except for idle watchers (which use the lock-out model). |
|
|
1359 | .PP |
|
|
1360 | The rationale behind this is that implementing the lock-out model for |
|
|
1361 | watchers is not well supported by most kernel interfaces, and most event |
|
|
1362 | libraries will just poll for the same events again and again as long as |
|
|
1363 | their callbacks have not been executed, which is very inefficient in the |
|
|
1364 | common case of one high-priority watcher locking out a mass of lower |
|
|
1365 | priority ones. |
|
|
1366 | .PP |
|
|
1367 | Static (ordering) priorities are most useful when you have two or more |
|
|
1368 | watchers handling the same resource: a typical usage example is having an |
|
|
1369 | \&\f(CW\*(C`ev_io\*(C'\fR watcher to receive data, and an associated \f(CW\*(C`ev_timer\*(C'\fR to handle |
|
|
1370 | timeouts. Under load, data might be received while the program handles |
|
|
1371 | other jobs, but since timers normally get invoked first, the timeout |
|
|
1372 | handler will be executed before checking for data. In that case, giving |
|
|
1373 | the timer a lower priority than the I/O watcher ensures that I/O will be |
|
|
1374 | handled first even under adverse conditions (which is usually, but not |
|
|
1375 | always, what you want). |
|
|
1376 | .PP |
|
|
1377 | Since idle watchers use the \*(L"lock-out\*(R" model, meaning that idle watchers |
|
|
1378 | will only be executed when no same or higher priority watchers have |
|
|
1379 | received events, they can be used to implement the \*(L"lock-out\*(R" model when |
|
|
1380 | required. |
|
|
1381 | .PP |
|
|
1382 | For example, to emulate how many other event libraries handle priorities, |
|
|
1383 | you can associate an \f(CW\*(C`ev_idle\*(C'\fR watcher to each such watcher, and in |
|
|
1384 | the normal watcher callback, you just start the idle watcher. The real |
|
|
1385 | processing is done in the idle watcher callback. This causes libev to |
|
|
1386 | continously poll and process kernel event data for the watcher, but when |
|
|
1387 | the lock-out case is known to be rare (which in turn is rare :), this is |
|
|
1388 | workable. |
|
|
1389 | .PP |
|
|
1390 | Usually, however, the lock-out model implemented that way will perform |
|
|
1391 | miserably under the type of load it was designed to handle. In that case, |
|
|
1392 | it might be preferable to stop the real watcher before starting the |
|
|
1393 | idle watcher, so the kernel will not have to process the event in case |
|
|
1394 | the actual processing will be delayed for considerable time. |
|
|
1395 | .PP |
|
|
1396 | Here is an example of an I/O watcher that should run at a strictly lower |
|
|
1397 | priority than the default, and which should only process data when no |
|
|
1398 | other events are pending: |
|
|
1399 | .PP |
|
|
1400 | .Vb 2 |
|
|
1401 | \& ev_idle idle; // actual processing watcher |
|
|
1402 | \& ev_io io; // actual event watcher |
|
|
1403 | \& |
|
|
1404 | \& static void |
|
|
1405 | \& io_cb (EV_P_ ev_io *w, int revents) |
|
|
1406 | \& { |
|
|
1407 | \& // stop the I/O watcher, we received the event, but |
|
|
1408 | \& // are not yet ready to handle it. |
|
|
1409 | \& ev_io_stop (EV_A_ w); |
|
|
1410 | \& |
|
|
1411 | \& // start the idle watcher to ahndle the actual event. |
|
|
1412 | \& // it will not be executed as long as other watchers |
|
|
1413 | \& // with the default priority are receiving events. |
|
|
1414 | \& ev_idle_start (EV_A_ &idle); |
|
|
1415 | \& } |
|
|
1416 | \& |
|
|
1417 | \& static void |
|
|
1418 | \& idle\-cb (EV_P_ ev_idle *w, int revents) |
|
|
1419 | \& { |
|
|
1420 | \& // actual processing |
|
|
1421 | \& read (STDIN_FILENO, ...); |
|
|
1422 | \& |
|
|
1423 | \& // have to start the I/O watcher again, as |
|
|
1424 | \& // we have handled the event |
|
|
1425 | \& ev_io_start (EV_P_ &io); |
|
|
1426 | \& } |
|
|
1427 | \& |
|
|
1428 | \& // initialisation |
|
|
1429 | \& ev_idle_init (&idle, idle_cb); |
|
|
1430 | \& ev_io_init (&io, io_cb, STDIN_FILENO, EV_READ); |
|
|
1431 | \& ev_io_start (EV_DEFAULT_ &io); |
|
|
1432 | .Ve |
|
|
1433 | .PP |
|
|
1434 | In the \*(L"real\*(R" world, it might also be beneficial to start a timer, so that |
|
|
1435 | low-priority connections can not be locked out forever under load. This |
|
|
1436 | enables your program to keep a lower latency for important connections |
|
|
1437 | during short periods of high load, while not completely locking out less |
|
|
1438 | important ones. |
1286 | .SH "WATCHER TYPES" |
1439 | .SH "WATCHER TYPES" |
1287 | .IX Header "WATCHER TYPES" |
1440 | .IX Header "WATCHER TYPES" |
1288 | This section describes each watcher in detail, but will not repeat |
1441 | This section describes each watcher in detail, but will not repeat |
1289 | information given in the last section. Any initialisation/set macros, |
1442 | information given in the last section. Any initialisation/set macros, |
1290 | functions and members specific to the watcher type are explained. |
1443 | functions and members specific to the watcher type are explained. |
… | |
… | |
1313 | descriptors to non-blocking mode is also usually a good idea (but not |
1466 | descriptors to non-blocking mode is also usually a good idea (but not |
1314 | required if you know what you are doing). |
1467 | required if you know what you are doing). |
1315 | .PP |
1468 | .PP |
1316 | If you cannot use non-blocking mode, then force the use of a |
1469 | If you cannot use non-blocking mode, then force the use of a |
1317 | known-to-be-good backend (at the time of this writing, this includes only |
1470 | known-to-be-good backend (at the time of this writing, this includes only |
1318 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). |
1471 | \&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR). The same applies to file |
|
|
1472 | descriptors for which non-blocking operation makes no sense (such as |
|
|
1473 | files) \- libev doesn't guarentee any specific behaviour in that case. |
1319 | .PP |
1474 | .PP |
1320 | Another thing you have to watch out for is that it is quite easy to |
1475 | Another thing you have to watch out for is that it is quite easy to |
1321 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1476 | receive \*(L"spurious\*(R" readiness notifications, that is your callback might |
1322 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1477 | be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block |
1323 | because there is no data. Not only are some backends known to create a |
1478 | because there is no data. Not only are some backends known to create a |
… | |
… | |
1445 | year, it will still time out after (roughly) one hour. \*(L"Roughly\*(R" because |
1600 | year, it will still time out after (roughly) one hour. \*(L"Roughly\*(R" because |
1446 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1601 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1447 | monotonic clock option helps a lot here). |
1602 | monotonic clock option helps a lot here). |
1448 | .PP |
1603 | .PP |
1449 | The callback is guaranteed to be invoked only \fIafter\fR its timeout has |
1604 | The callback is guaranteed to be invoked only \fIafter\fR its timeout has |
1450 | passed, but if multiple timers become ready during the same loop iteration |
1605 | passed (not \fIat\fR, so on systems with very low-resolution clocks this |
1451 | then order of execution is undefined. |
1606 | might introduce a small delay). If multiple timers become ready during the |
|
|
1607 | same loop iteration then the ones with earlier time-out values are invoked |
|
|
1608 | before ones with later time-out values (but this is no longer true when a |
|
|
1609 | callback calls \f(CW\*(C`ev_loop\*(C'\fR recursively). |
1452 | .PP |
1610 | .PP |
1453 | \fIBe smart about timeouts\fR |
1611 | \fIBe smart about timeouts\fR |
1454 | .IX Subsection "Be smart about timeouts" |
1612 | .IX Subsection "Be smart about timeouts" |
1455 | .PP |
1613 | .PP |
1456 | Many real-world problems involve some kind of timeout, usually for error |
1614 | Many real-world problems involve some kind of timeout, usually for error |
… | |
… | |
1557 | \& else |
1715 | \& else |
1558 | \& { |
1716 | \& { |
1559 | \& // callback was invoked, but there was some activity, re\-arm |
1717 | \& // callback was invoked, but there was some activity, re\-arm |
1560 | \& // the watcher to fire in last_activity + 60, which is |
1718 | \& // the watcher to fire in last_activity + 60, which is |
1561 | \& // guaranteed to be in the future, so "again" is positive: |
1719 | \& // guaranteed to be in the future, so "again" is positive: |
1562 | \& w\->again = timeout \- now; |
1720 | \& w\->repeat = timeout \- now; |
1563 | \& ev_timer_again (EV_A_ w); |
1721 | \& ev_timer_again (EV_A_ w); |
1564 | \& } |
1722 | \& } |
1565 | \& } |
1723 | \& } |
1566 | .Ve |
1724 | .Ve |
1567 | .Sp |
1725 | .Sp |
… | |
… | |
1737 | .el .Sh "\f(CWev_periodic\fP \- to cron or not to cron?" |
1895 | .el .Sh "\f(CWev_periodic\fP \- to cron or not to cron?" |
1738 | .IX Subsection "ev_periodic - to cron or not to cron?" |
1896 | .IX Subsection "ev_periodic - to cron or not to cron?" |
1739 | Periodic watchers are also timers of a kind, but they are very versatile |
1897 | Periodic watchers are also timers of a kind, but they are very versatile |
1740 | (and unfortunately a bit complex). |
1898 | (and unfortunately a bit complex). |
1741 | .PP |
1899 | .PP |
1742 | Unlike \f(CW\*(C`ev_timer\*(C'\fR's, they are not based on real time (or relative time) |
1900 | Unlike \f(CW\*(C`ev_timer\*(C'\fR, periodic watchers are not based on real time (or |
1743 | but on wall clock time (absolute time). You can tell a periodic watcher |
1901 | relative time, the physical time that passes) but on wall clock time |
1744 | to trigger after some specific point in time. For example, if you tell a |
1902 | (absolute time, the thing you can read on your calender or clock). The |
1745 | periodic watcher to trigger in 10 seconds (by specifying e.g. \f(CW\*(C`ev_now () |
1903 | difference is that wall clock time can run faster or slower than real |
1746 | + 10.\*(C'\fR, that is, an absolute time not a delay) and then reset your system |
1904 | time, and time jumps are not uncommon (e.g. when you adjust your |
1747 | clock to January of the previous year, then it will take more than year |
1905 | wrist-watch). |
1748 | to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger |
|
|
1749 | roughly 10 seconds later as it uses a relative timeout). |
|
|
1750 | .PP |
1906 | .PP |
|
|
1907 | You can tell a periodic watcher to trigger after some specific point |
|
|
1908 | in time: for example, if you tell a periodic watcher to trigger \*(L"in 10 |
|
|
1909 | seconds\*(R" (by specifying e.g. \f(CW\*(C`ev_now () + 10.\*(C'\fR, that is, an absolute time |
|
|
1910 | not a delay) and then reset your system clock to January of the previous |
|
|
1911 | year, then it will take a year or more to trigger the event (unlike an |
|
|
1912 | \&\f(CW\*(C`ev_timer\*(C'\fR, which would still trigger roughly 10 seconds after starting |
|
|
1913 | it, as it uses a relative timeout). |
|
|
1914 | .PP |
1751 | \&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, |
1915 | \&\f(CW\*(C`ev_periodic\*(C'\fR watchers can also be used to implement vastly more complex |
1752 | such as triggering an event on each \*(L"midnight, local time\*(R", or other |
1916 | timers, such as triggering an event on each \*(L"midnight, local time\*(R", or |
1753 | complicated rules. |
1917 | other complicated rules. This cannot be done with \f(CW\*(C`ev_timer\*(C'\fR watchers, as |
|
|
1918 | those cannot react to time jumps. |
1754 | .PP |
1919 | .PP |
1755 | As with timers, the callback is guaranteed to be invoked only when the |
1920 | As with timers, the callback is guaranteed to be invoked only when the |
1756 | time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready |
1921 | point in time where it is supposed to trigger has passed. If multiple |
1757 | during the same loop iteration, then order of execution is undefined. |
1922 | timers become ready during the same loop iteration then the ones with |
|
|
1923 | earlier time-out values are invoked before ones with later time-out values |
|
|
1924 | (but this is no longer true when a callback calls \f(CW\*(C`ev_loop\*(C'\fR recursively). |
1758 | .PP |
1925 | .PP |
1759 | \fIWatcher-Specific Functions and Data Members\fR |
1926 | \fIWatcher-Specific Functions and Data Members\fR |
1760 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1927 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1761 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 |
1928 | .IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" 4 |
1762 | .IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" |
1929 | .IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" |
1763 | .PD 0 |
1930 | .PD 0 |
1764 | .IP "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 4 |
1931 | .IP "ev_periodic_set (ev_periodic *, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" 4 |
1765 | .IX Item "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" |
1932 | .IX Item "ev_periodic_set (ev_periodic *, ev_tstamp offset, ev_tstamp interval, reschedule_cb)" |
1766 | .PD |
1933 | .PD |
1767 | Lots of arguments, lets sort it out... There are basically three modes of |
1934 | Lots of arguments, let's sort it out... There are basically three modes of |
1768 | operation, and we will explain them from simplest to most complex: |
1935 | operation, and we will explain them from simplest to most complex: |
1769 | .RS 4 |
1936 | .RS 4 |
1770 | .IP "\(bu" 4 |
1937 | .IP "\(bu" 4 |
1771 | absolute timer (at = time, interval = reschedule_cb = 0) |
1938 | absolute timer (offset = absolute time, interval = 0, reschedule_cb = 0) |
1772 | .Sp |
1939 | .Sp |
1773 | In this configuration the watcher triggers an event after the wall clock |
1940 | In this configuration the watcher triggers an event after the wall clock |
1774 | time \f(CW\*(C`at\*(C'\fR has passed. It will not repeat and will not adjust when a time |
1941 | time \f(CW\*(C`offset\*(C'\fR has passed. It will not repeat and will not adjust when a |
1775 | jump occurs, that is, if it is to be run at January 1st 2011 then it will |
1942 | time jump occurs, that is, if it is to be run at January 1st 2011 then it |
1776 | only run when the system clock reaches or surpasses this time. |
1943 | will be stopped and invoked when the system clock reaches or surpasses |
|
|
1944 | this point in time. |
1777 | .IP "\(bu" 4 |
1945 | .IP "\(bu" 4 |
1778 | repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) |
1946 | repeating interval timer (offset = offset within interval, interval > 0, reschedule_cb = 0) |
1779 | .Sp |
1947 | .Sp |
1780 | In this mode the watcher will always be scheduled to time out at the next |
1948 | In this mode the watcher will always be scheduled to time out at the next |
1781 | \&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) |
1949 | \&\f(CW\*(C`offset + N * interval\*(C'\fR time (for some integer N, which can also be |
1782 | and then repeat, regardless of any time jumps. |
1950 | negative) and then repeat, regardless of any time jumps. The \f(CW\*(C`offset\*(C'\fR |
|
|
1951 | argument is merely an offset into the \f(CW\*(C`interval\*(C'\fR periods. |
1783 | .Sp |
1952 | .Sp |
1784 | This can be used to create timers that do not drift with respect to the |
1953 | This can be used to create timers that do not drift with respect to the |
1785 | system clock, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each |
1954 | system clock, for example, here is an \f(CW\*(C`ev_periodic\*(C'\fR that triggers each |
1786 | hour, on the hour: |
1955 | hour, on the hour (with respect to \s-1UTC\s0): |
1787 | .Sp |
1956 | .Sp |
1788 | .Vb 1 |
1957 | .Vb 1 |
1789 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1958 | \& ev_periodic_set (&periodic, 0., 3600., 0); |
1790 | .Ve |
1959 | .Ve |
1791 | .Sp |
1960 | .Sp |
… | |
… | |
1794 | full hour (\s-1UTC\s0), or more correctly, when the system time is evenly divisible |
1963 | full hour (\s-1UTC\s0), or more correctly, when the system time is evenly divisible |
1795 | by 3600. |
1964 | by 3600. |
1796 | .Sp |
1965 | .Sp |
1797 | Another way to think about it (for the mathematically inclined) is that |
1966 | Another way to think about it (for the mathematically inclined) is that |
1798 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1967 | \&\f(CW\*(C`ev_periodic\*(C'\fR will try to run the callback in this mode at the next possible |
1799 | time where \f(CW\*(C`time = at (mod interval)\*(C'\fR, regardless of any time jumps. |
1968 | time where \f(CW\*(C`time = offset (mod interval)\*(C'\fR, regardless of any time jumps. |
1800 | .Sp |
1969 | .Sp |
1801 | For numerical stability it is preferable that the \f(CW\*(C`at\*(C'\fR value is near |
1970 | For numerical stability it is preferable that the \f(CW\*(C`offset\*(C'\fR value is near |
1802 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1971 | \&\f(CW\*(C`ev_now ()\*(C'\fR (the current time), but there is no range requirement for |
1803 | this value, and in fact is often specified as zero. |
1972 | this value, and in fact is often specified as zero. |
1804 | .Sp |
1973 | .Sp |
1805 | Note also that there is an upper limit to how often a timer can fire (\s-1CPU\s0 |
1974 | Note also that there is an upper limit to how often a timer can fire (\s-1CPU\s0 |
1806 | speed for example), so if \f(CW\*(C`interval\*(C'\fR is very small then timing stability |
1975 | speed for example), so if \f(CW\*(C`interval\*(C'\fR is very small then timing stability |
1807 | will of course deteriorate. Libev itself tries to be exact to be about one |
1976 | will of course deteriorate. Libev itself tries to be exact to be about one |
1808 | millisecond (if the \s-1OS\s0 supports it and the machine is fast enough). |
1977 | millisecond (if the \s-1OS\s0 supports it and the machine is fast enough). |
1809 | .IP "\(bu" 4 |
1978 | .IP "\(bu" 4 |
1810 | manual reschedule mode (at and interval ignored, reschedule_cb = callback) |
1979 | manual reschedule mode (offset ignored, interval ignored, reschedule_cb = callback) |
1811 | .Sp |
1980 | .Sp |
1812 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`at\*(C'\fR are both being |
1981 | In this mode the values for \f(CW\*(C`interval\*(C'\fR and \f(CW\*(C`offset\*(C'\fR are both being |
1813 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1982 | ignored. Instead, each time the periodic watcher gets scheduled, the |
1814 | reschedule callback will be called with the watcher as first, and the |
1983 | reschedule callback will be called with the watcher as first, and the |
1815 | current time as second argument. |
1984 | current time as second argument. |
1816 | .Sp |
1985 | .Sp |
1817 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, |
1986 | \&\s-1NOTE:\s0 \fIThis callback \s-1MUST\s0 \s-1NOT\s0 stop or destroy any periodic watcher, ever, |
1818 | ever, or make \s-1ANY\s0 event loop modifications whatsoever\fR. |
1987 | or make \s-1ANY\s0 other event loop modifications whatsoever, unless explicitly |
|
|
1988 | allowed by documentation here\fR. |
1819 | .Sp |
1989 | .Sp |
1820 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
1990 | If you need to stop it, return \f(CW\*(C`now + 1e30\*(C'\fR (or so, fudge fudge) and stop |
1821 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
1991 | it afterwards (e.g. by starting an \f(CW\*(C`ev_prepare\*(C'\fR watcher, which is the |
1822 | only event loop modification you are allowed to do). |
1992 | only event loop modification you are allowed to do). |
1823 | .Sp |
1993 | .Sp |
… | |
… | |
1854 | when you changed some parameters or the reschedule callback would return |
2024 | when you changed some parameters or the reschedule callback would return |
1855 | a different time than the last time it was called (e.g. in a crond like |
2025 | a different time than the last time it was called (e.g. in a crond like |
1856 | program when the crontabs have changed). |
2026 | program when the crontabs have changed). |
1857 | .IP "ev_tstamp ev_periodic_at (ev_periodic *)" 4 |
2027 | .IP "ev_tstamp ev_periodic_at (ev_periodic *)" 4 |
1858 | .IX Item "ev_tstamp ev_periodic_at (ev_periodic *)" |
2028 | .IX Item "ev_tstamp ev_periodic_at (ev_periodic *)" |
1859 | When active, returns the absolute time that the watcher is supposed to |
2029 | When active, returns the absolute time that the watcher is supposed |
1860 | trigger next. |
2030 | to trigger next. This is not the same as the \f(CW\*(C`offset\*(C'\fR argument to |
|
|
2031 | \&\f(CW\*(C`ev_periodic_set\*(C'\fR, but indeed works even in interval and manual |
|
|
2032 | rescheduling modes. |
1861 | .IP "ev_tstamp offset [read\-write]" 4 |
2033 | .IP "ev_tstamp offset [read\-write]" 4 |
1862 | .IX Item "ev_tstamp offset [read-write]" |
2034 | .IX Item "ev_tstamp offset [read-write]" |
1863 | When repeating, this contains the offset value, otherwise this is the |
2035 | When repeating, this contains the offset value, otherwise this is the |
1864 | absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). |
2036 | absolute point in time (the \f(CW\*(C`offset\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR, |
|
|
2037 | although libev might modify this value for better numerical stability). |
1865 | .Sp |
2038 | .Sp |
1866 | Can be modified any time, but changes only take effect when the periodic |
2039 | Can be modified any time, but changes only take effect when the periodic |
1867 | timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
2040 | timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
1868 | .IP "ev_tstamp interval [read\-write]" 4 |
2041 | .IP "ev_tstamp interval [read\-write]" 4 |
1869 | .IX Item "ev_tstamp interval [read-write]" |
2042 | .IX Item "ev_tstamp interval [read-write]" |
… | |
… | |
2081 | \&\f(CW\*(C`stat\*(C'\fR on that path in regular intervals (or when the \s-1OS\s0 says it changed) |
2254 | \&\f(CW\*(C`stat\*(C'\fR on that path in regular intervals (or when the \s-1OS\s0 says it changed) |
2082 | and sees if it changed compared to the last time, invoking the callback if |
2255 | and sees if it changed compared to the last time, invoking the callback if |
2083 | it did. |
2256 | it did. |
2084 | .PP |
2257 | .PP |
2085 | The path does not need to exist: changing from \*(L"path exists\*(R" to \*(L"path does |
2258 | The path does not need to exist: changing from \*(L"path exists\*(R" to \*(L"path does |
2086 | not exist\*(R" is a status change like any other. The condition \*(L"path does |
2259 | not exist\*(R" is a status change like any other. The condition \*(L"path does not |
2087 | not exist\*(R" is signified by the \f(CW\*(C`st_nlink\*(C'\fR field being zero (which is |
2260 | exist\*(R" (or more correctly \*(L"path cannot be stat'ed\*(R") is signified by the |
2088 | otherwise always forced to be at least one) and all the other fields of |
2261 | \&\f(CW\*(C`st_nlink\*(C'\fR field being zero (which is otherwise always forced to be at |
2089 | the stat buffer having unspecified contents. |
2262 | least one) and all the other fields of the stat buffer having unspecified |
|
|
2263 | contents. |
2090 | .PP |
2264 | .PP |
2091 | The path \fImust not\fR end in a slash or contain special components such as |
2265 | The path \fImust not\fR end in a slash or contain special components such as |
2092 | \&\f(CW\*(C`.\*(C'\fR or \f(CW\*(C`..\*(C'\fR. The path \fIshould\fR be absolute: If it is relative and |
2266 | \&\f(CW\*(C`.\*(C'\fR or \f(CW\*(C`..\*(C'\fR. The path \fIshould\fR be absolute: If it is relative and |
2093 | your working directory changes, then the behaviour is undefined. |
2267 | your working directory changes, then the behaviour is undefined. |
2094 | .PP |
2268 | .PP |
… | |
… | |
2104 | This watcher type is not meant for massive numbers of stat watchers, |
2278 | This watcher type is not meant for massive numbers of stat watchers, |
2105 | as even with OS-supported change notifications, this can be |
2279 | as even with OS-supported change notifications, this can be |
2106 | resource-intensive. |
2280 | resource-intensive. |
2107 | .PP |
2281 | .PP |
2108 | At the time of this writing, the only OS-specific interface implemented |
2282 | At the time of this writing, the only OS-specific interface implemented |
2109 | is the Linux inotify interface (implementing kqueue support is left as |
2283 | is the Linux inotify interface (implementing kqueue support is left as an |
2110 | an exercise for the reader. Note, however, that the author sees no way |
2284 | exercise for the reader. Note, however, that the author sees no way of |
2111 | of implementing \f(CW\*(C`ev_stat\*(C'\fR semantics with kqueue). |
2285 | implementing \f(CW\*(C`ev_stat\*(C'\fR semantics with kqueue, except as a hint). |
2112 | .PP |
2286 | .PP |
2113 | \fI\s-1ABI\s0 Issues (Largefile Support)\fR |
2287 | \fI\s-1ABI\s0 Issues (Largefile Support)\fR |
2114 | .IX Subsection "ABI Issues (Largefile Support)" |
2288 | .IX Subsection "ABI Issues (Largefile Support)" |
2115 | .PP |
2289 | .PP |
2116 | Libev by default (unless the user overrides this) uses the default |
2290 | Libev by default (unless the user overrides this) uses the default |
… | |
… | |
2129 | default compilation environment. |
2303 | default compilation environment. |
2130 | .PP |
2304 | .PP |
2131 | \fIInotify and Kqueue\fR |
2305 | \fIInotify and Kqueue\fR |
2132 | .IX Subsection "Inotify and Kqueue" |
2306 | .IX Subsection "Inotify and Kqueue" |
2133 | .PP |
2307 | .PP |
2134 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally |
2308 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev and present at |
2135 | only available with Linux 2.6.25 or above due to bugs in earlier |
2309 | runtime, it will be used to speed up change detection where possible. The |
2136 | implementations) and present at runtime, it will be used to speed up |
2310 | inotify descriptor will be created lazily when the first \f(CW\*(C`ev_stat\*(C'\fR |
2137 | change detection where possible. The inotify descriptor will be created |
2311 | watcher is being started. |
2138 | lazily when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. |
|
|
2139 | .PP |
2312 | .PP |
2140 | Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
2313 | Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
2141 | except that changes might be detected earlier, and in some cases, to avoid |
2314 | except that changes might be detected earlier, and in some cases, to avoid |
2142 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support |
2315 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support |
2143 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling, |
2316 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling, |
2144 | but as long as the path exists, libev usually gets away without polling. |
2317 | but as long as kernel 2.6.25 or newer is used (2.6.24 and older have too |
|
|
2318 | many bugs), the path exists (i.e. stat succeeds), and the path resides on |
|
|
2319 | a local filesystem (libev currently assumes only ext2/3, jfs, reiserfs and |
|
|
2320 | xfs are fully working) libev usually gets away without polling. |
2145 | .PP |
2321 | .PP |
2146 | There is no support for kqueue, as apparently it cannot be used to |
2322 | There is no support for kqueue, as apparently it cannot be used to |
2147 | implement this functionality, due to the requirement of having a file |
2323 | implement this functionality, due to the requirement of having a file |
2148 | descriptor open on the object at all times, and detecting renames, unlinks |
2324 | descriptor open on the object at all times, and detecting renames, unlinks |
2149 | etc. is difficult. |
2325 | etc. is difficult. |
|
|
2326 | .PP |
|
|
2327 | \fI\f(CI\*(C`stat ()\*(C'\fI is a synchronous operation\fR |
|
|
2328 | .IX Subsection "stat () is a synchronous operation" |
|
|
2329 | .PP |
|
|
2330 | Libev doesn't normally do any kind of I/O itself, and so is not blocking |
|
|
2331 | the process. The exception are \f(CW\*(C`ev_stat\*(C'\fR watchers \- those call \f(CW\*(C`stat |
|
|
2332 | ()\*(C'\fR, which is a synchronous operation. |
|
|
2333 | .PP |
|
|
2334 | For local paths, this usually doesn't matter: unless the system is very |
|
|
2335 | busy or the intervals between stat's are large, a stat call will be fast, |
|
|
2336 | as the path data is usually in memory already (except when starting the |
|
|
2337 | watcher). |
|
|
2338 | .PP |
|
|
2339 | For networked file systems, calling \f(CW\*(C`stat ()\*(C'\fR can block an indefinite |
|
|
2340 | time due to network issues, and even under good conditions, a stat call |
|
|
2341 | often takes multiple milliseconds. |
|
|
2342 | .PP |
|
|
2343 | Therefore, it is best to avoid using \f(CW\*(C`ev_stat\*(C'\fR watchers on networked |
|
|
2344 | paths, although this is fully supported by libev. |
2150 | .PP |
2345 | .PP |
2151 | \fIThe special problem of stat time resolution\fR |
2346 | \fIThe special problem of stat time resolution\fR |
2152 | .IX Subsection "The special problem of stat time resolution" |
2347 | .IX Subsection "The special problem of stat time resolution" |
2153 | .PP |
2348 | .PP |
2154 | The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, |
2349 | The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, |
… | |
… | |
2299 | \&\*(L"pseudo-background processing\*(R", or delay processing stuff to after the |
2494 | \&\*(L"pseudo-background processing\*(R", or delay processing stuff to after the |
2300 | event loop has handled all outstanding events. |
2495 | event loop has handled all outstanding events. |
2301 | .PP |
2496 | .PP |
2302 | \fIWatcher-Specific Functions and Data Members\fR |
2497 | \fIWatcher-Specific Functions and Data Members\fR |
2303 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2498 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2304 | .IP "ev_idle_init (ev_signal *, callback)" 4 |
2499 | .IP "ev_idle_init (ev_idle *, callback)" 4 |
2305 | .IX Item "ev_idle_init (ev_signal *, callback)" |
2500 | .IX Item "ev_idle_init (ev_idle *, callback)" |
2306 | Initialises and configures the idle watcher \- it has no parameters of any |
2501 | Initialises and configures the idle watcher \- it has no parameters of any |
2307 | kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, |
2502 | kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, |
2308 | believe me. |
2503 | believe me. |
2309 | .PP |
2504 | .PP |
2310 | \fIExamples\fR |
2505 | \fIExamples\fR |
… | |
… | |
2552 | some fds have to be watched and handled very quickly (with low latency), |
2747 | some fds have to be watched and handled very quickly (with low latency), |
2553 | and even priorities and idle watchers might have too much overhead. In |
2748 | and even priorities and idle watchers might have too much overhead. In |
2554 | this case you would put all the high priority stuff in one loop and all |
2749 | this case you would put all the high priority stuff in one loop and all |
2555 | the rest in a second one, and embed the second one in the first. |
2750 | the rest in a second one, and embed the second one in the first. |
2556 | .PP |
2751 | .PP |
2557 | As long as the watcher is active, the callback will be invoked every time |
2752 | As long as the watcher is active, the callback will be invoked every |
2558 | there might be events pending in the embedded loop. The callback must then |
2753 | time there might be events pending in the embedded loop. The callback |
2559 | call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke |
2754 | must then call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single |
2560 | their callbacks (you could also start an idle watcher to give the embedded |
2755 | sweep and invoke their callbacks (the callback doesn't need to invoke the |
2561 | loop strictly lower priority for example). You can also set the callback |
2756 | \&\f(CW\*(C`ev_embed_sweep\*(C'\fR function directly, it could also start an idle watcher |
2562 | to \f(CW0\fR, in which case the embed watcher will automatically execute the |
2757 | to give the embedded loop strictly lower priority for example). |
2563 | embedded loop sweep. |
|
|
2564 | .PP |
2758 | .PP |
2565 | As long as the watcher is started it will automatically handle events. The |
2759 | You can also set the callback to \f(CW0\fR, in which case the embed watcher |
2566 | callback will be invoked whenever some events have been handled. You can |
2760 | will automatically execute the embedded loop sweep whenever necessary. |
2567 | set the callback to \f(CW0\fR to avoid having to specify one if you are not |
|
|
2568 | interested in that. |
|
|
2569 | .PP |
2761 | .PP |
2570 | Also, there have not currently been made special provisions for forking: |
2762 | Fork detection will be handled transparently while the \f(CW\*(C`ev_embed\*(C'\fR watcher |
2571 | when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops, |
2763 | is active, i.e., the embedded loop will automatically be forked when the |
2572 | but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers |
2764 | embedding loop forks. In other cases, the user is responsible for calling |
2573 | yourself \- but you can use a fork watcher to handle this automatically, |
2765 | \&\f(CW\*(C`ev_loop_fork\*(C'\fR on the embedded loop. |
2574 | and future versions of libev might do just that. |
|
|
2575 | .PP |
2766 | .PP |
2576 | Unfortunately, not all backends are embeddable: only the ones returned by |
2767 | Unfortunately, not all backends are embeddable: only the ones returned by |
2577 | \&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any |
2768 | \&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any |
2578 | portable one. |
2769 | portable one. |
2579 | .PP |
2770 | .PP |
… | |
… | |
2674 | event loop blocks next and before \f(CW\*(C`ev_check\*(C'\fR watchers are being called, |
2865 | event loop blocks next and before \f(CW\*(C`ev_check\*(C'\fR watchers are being called, |
2675 | and only in the child after the fork. If whoever good citizen calling |
2866 | and only in the child after the fork. If whoever good citizen calling |
2676 | \&\f(CW\*(C`ev_default_fork\*(C'\fR cheats and calls it in the wrong process, the fork |
2867 | \&\f(CW\*(C`ev_default_fork\*(C'\fR cheats and calls it in the wrong process, the fork |
2677 | handlers will be invoked, too, of course. |
2868 | handlers will be invoked, too, of course. |
2678 | .PP |
2869 | .PP |
|
|
2870 | \fIThe special problem of life after fork \- how is it possible?\fR |
|
|
2871 | .IX Subsection "The special problem of life after fork - how is it possible?" |
|
|
2872 | .PP |
|
|
2873 | Most uses of \f(CW\*(C`fork()\*(C'\fR consist of forking, then some simple calls to ste |
|
|
2874 | up/change the process environment, followed by a call to \f(CW\*(C`exec()\*(C'\fR. This |
|
|
2875 | sequence should be handled by libev without any problems. |
|
|
2876 | .PP |
|
|
2877 | This changes when the application actually wants to do event handling |
|
|
2878 | in the child, or both parent in child, in effect \*(L"continuing\*(R" after the |
|
|
2879 | fork. |
|
|
2880 | .PP |
|
|
2881 | The default mode of operation (for libev, with application help to detect |
|
|
2882 | forks) is to duplicate all the state in the child, as would be expected |
|
|
2883 | when \fIeither\fR the parent \fIor\fR the child process continues. |
|
|
2884 | .PP |
|
|
2885 | When both processes want to continue using libev, then this is usually the |
|
|
2886 | wrong result. In that case, usually one process (typically the parent) is |
|
|
2887 | supposed to continue with all watchers in place as before, while the other |
|
|
2888 | process typically wants to start fresh, i.e. without any active watchers. |
|
|
2889 | .PP |
|
|
2890 | The cleanest and most efficient way to achieve that with libev is to |
|
|
2891 | simply create a new event loop, which of course will be \*(L"empty\*(R", and |
|
|
2892 | use that for new watchers. This has the advantage of not touching more |
|
|
2893 | memory than necessary, and thus avoiding the copy-on-write, and the |
|
|
2894 | disadvantage of having to use multiple event loops (which do not support |
|
|
2895 | signal watchers). |
|
|
2896 | .PP |
|
|
2897 | When this is not possible, or you want to use the default loop for |
|
|
2898 | other reasons, then in the process that wants to start \*(L"fresh\*(R", call |
|
|
2899 | \&\f(CW\*(C`ev_default_destroy ()\*(C'\fR followed by \f(CW\*(C`ev_default_loop (...)\*(C'\fR. Destroying |
|
|
2900 | the default loop will \*(L"orphan\*(R" (not stop) all registered watchers, so you |
|
|
2901 | have to be careful not to execute code that modifies those watchers. Note |
|
|
2902 | also that in that case, you have to re-register any signal watchers. |
|
|
2903 | .PP |
2679 | \fIWatcher-Specific Functions and Data Members\fR |
2904 | \fIWatcher-Specific Functions and Data Members\fR |
2680 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2905 | .IX Subsection "Watcher-Specific Functions and Data Members" |
2681 | .IP "ev_fork_init (ev_signal *, callback)" 4 |
2906 | .IP "ev_fork_init (ev_signal *, callback)" 4 |
2682 | .IX Item "ev_fork_init (ev_signal *, callback)" |
2907 | .IX Item "ev_fork_init (ev_signal *, callback)" |
2683 | Initialises and configures the fork watcher \- it has no parameters of any |
2908 | Initialises and configures the fork watcher \- it has no parameters of any |
… | |
… | |
2801 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
3026 | an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike |
2802 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or |
3027 | \&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or |
2803 | similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
3028 | similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding |
2804 | section below on what exactly this means). |
3029 | section below on what exactly this means). |
2805 | .Sp |
3030 | .Sp |
|
|
3031 | Note that, as with other watchers in libev, multiple events might get |
|
|
3032 | compressed into a single callback invocation (another way to look at this |
|
|
3033 | is that \f(CW\*(C`ev_async\*(C'\fR watchers are level-triggered, set on \f(CW\*(C`ev_async_send\*(C'\fR, |
|
|
3034 | reset when the event loop detects that). |
|
|
3035 | .Sp |
2806 | This call incurs the overhead of a system call only once per loop iteration, |
3036 | This call incurs the overhead of a system call only once per event loop |
2807 | so while the overhead might be noticeable, it doesn't apply to repeated |
3037 | iteration, so while the overhead might be noticeable, it doesn't apply to |
2808 | calls to \f(CW\*(C`ev_async_send\*(C'\fR. |
3038 | repeated calls to \f(CW\*(C`ev_async_send\*(C'\fR for the same event loop. |
2809 | .IP "bool = ev_async_pending (ev_async *)" 4 |
3039 | .IP "bool = ev_async_pending (ev_async *)" 4 |
2810 | .IX Item "bool = ev_async_pending (ev_async *)" |
3040 | .IX Item "bool = ev_async_pending (ev_async *)" |
2811 | Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the |
3041 | Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the |
2812 | watcher but the event has not yet been processed (or even noted) by the |
3042 | watcher but the event has not yet been processed (or even noted) by the |
2813 | event loop. |
3043 | event loop. |
… | |
… | |
2815 | \&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When |
3045 | \&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When |
2816 | the loop iterates next and checks for the watcher to have become active, |
3046 | the loop iterates next and checks for the watcher to have become active, |
2817 | it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very |
3047 | it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very |
2818 | quickly check whether invoking the loop might be a good idea. |
3048 | quickly check whether invoking the loop might be a good idea. |
2819 | .Sp |
3049 | .Sp |
2820 | Not that this does \fInot\fR check whether the watcher itself is pending, only |
3050 | Not that this does \fInot\fR check whether the watcher itself is pending, |
2821 | whether it has been requested to make this watcher pending. |
3051 | only whether it has been requested to make this watcher pending: there |
|
|
3052 | is a time window between the event loop checking and resetting the async |
|
|
3053 | notification, and the callback being invoked. |
2822 | .SH "OTHER FUNCTIONS" |
3054 | .SH "OTHER FUNCTIONS" |
2823 | .IX Header "OTHER FUNCTIONS" |
3055 | .IX Header "OTHER FUNCTIONS" |
2824 | There are some other functions of possible interest. Described. Here. Now. |
3056 | There are some other functions of possible interest. Described. Here. Now. |
2825 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
3057 | .IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 |
2826 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
3058 | .IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" |
… | |
… | |
2988 | \& |
3220 | \& |
2989 | \& myclass obj; |
3221 | \& myclass obj; |
2990 | \& ev::io iow; |
3222 | \& ev::io iow; |
2991 | \& iow.set <myclass, &myclass::io_cb> (&obj); |
3223 | \& iow.set <myclass, &myclass::io_cb> (&obj); |
2992 | .Ve |
3224 | .Ve |
|
|
3225 | .IP "w\->set (object *)" 4 |
|
|
3226 | .IX Item "w->set (object *)" |
|
|
3227 | This is an \fBexperimental\fR feature that might go away in a future version. |
|
|
3228 | .Sp |
|
|
3229 | This is a variation of a method callback \- leaving out the method to call |
|
|
3230 | will default the method to \f(CW\*(C`operator ()\*(C'\fR, which makes it possible to use |
|
|
3231 | functor objects without having to manually specify the \f(CW\*(C`operator ()\*(C'\fR all |
|
|
3232 | the time. Incidentally, you can then also leave out the template argument |
|
|
3233 | list. |
|
|
3234 | .Sp |
|
|
3235 | The \f(CW\*(C`operator ()\*(C'\fR method prototype must be \f(CW\*(C`void operator ()(watcher &w, |
|
|
3236 | int revents)\*(C'\fR. |
|
|
3237 | .Sp |
|
|
3238 | See the method\-\f(CW\*(C`set\*(C'\fR above for more details. |
|
|
3239 | .Sp |
|
|
3240 | Example: use a functor object as callback. |
|
|
3241 | .Sp |
|
|
3242 | .Vb 7 |
|
|
3243 | \& struct myfunctor |
|
|
3244 | \& { |
|
|
3245 | \& void operator() (ev::io &w, int revents) |
|
|
3246 | \& { |
|
|
3247 | \& ... |
|
|
3248 | \& } |
|
|
3249 | \& } |
|
|
3250 | \& |
|
|
3251 | \& myfunctor f; |
|
|
3252 | \& |
|
|
3253 | \& ev::io w; |
|
|
3254 | \& w.set (&f); |
|
|
3255 | .Ve |
2993 | .IP "w\->set<function> (void *data = 0)" 4 |
3256 | .IP "w\->set<function> (void *data = 0)" 4 |
2994 | .IX Item "w->set<function> (void *data = 0)" |
3257 | .IX Item "w->set<function> (void *data = 0)" |
2995 | Also sets a callback, but uses a static method or plain function as |
3258 | Also sets a callback, but uses a static method or plain function as |
2996 | callback. The optional \f(CW\*(C`data\*(C'\fR argument will be stored in the watcher's |
3259 | callback. The optional \f(CW\*(C`data\*(C'\fR argument will be stored in the watcher's |
2997 | \&\f(CW\*(C`data\*(C'\fR member and is free for you to use. |
3260 | \&\f(CW\*(C`data\*(C'\fR member and is free for you to use. |
… | |
… | |
3076 | It can be found and installed via \s-1CPAN\s0, its homepage is at |
3339 | It can be found and installed via \s-1CPAN\s0, its homepage is at |
3077 | <http://software.schmorp.de/pkg/EV>. |
3340 | <http://software.schmorp.de/pkg/EV>. |
3078 | .IP "Python" 4 |
3341 | .IP "Python" 4 |
3079 | .IX Item "Python" |
3342 | .IX Item "Python" |
3080 | Python bindings can be found at <http://code.google.com/p/pyev/>. It |
3343 | Python bindings can be found at <http://code.google.com/p/pyev/>. It |
3081 | seems to be quite complete and well-documented. Note, however, that the |
3344 | seems to be quite complete and well-documented. |
3082 | patch they require for libev is outright dangerous as it breaks the \s-1ABI\s0 |
|
|
3083 | for everybody else, and therefore, should never be applied in an installed |
|
|
3084 | libev (if python requires an incompatible \s-1ABI\s0 then it needs to embed |
|
|
3085 | libev). |
|
|
3086 | .IP "Ruby" 4 |
3345 | .IP "Ruby" 4 |
3087 | .IX Item "Ruby" |
3346 | .IX Item "Ruby" |
3088 | Tony Arcieri has written a ruby extension that offers access to a subset |
3347 | Tony Arcieri has written a ruby extension that offers access to a subset |
3089 | of the libev \s-1API\s0 and adds file handle abstractions, asynchronous \s-1DNS\s0 and |
3348 | of the libev \s-1API\s0 and adds file handle abstractions, asynchronous \s-1DNS\s0 and |
3090 | more on top of it. It can be found via gem servers. Its homepage is at |
3349 | more on top of it. It can be found via gem servers. Its homepage is at |
3091 | <http://rev.rubyforge.org/>. |
3350 | <http://rev.rubyforge.org/>. |
|
|
3351 | .Sp |
|
|
3352 | Roger Pack reports that using the link order \f(CW\*(C`\-lws2_32 \-lmsvcrt\-ruby\-190\*(C'\fR |
|
|
3353 | makes rev work even on mingw. |
|
|
3354 | .IP "Haskell" 4 |
|
|
3355 | .IX Item "Haskell" |
|
|
3356 | A haskell binding to libev is available at |
|
|
3357 | <http://hackage.haskell.org/cgi\-bin/hackage\-scripts/package/hlibev>. |
3092 | .IP "D" 4 |
3358 | .IP "D" 4 |
3093 | .IX Item "D" |
3359 | .IX Item "D" |
3094 | Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to |
3360 | Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to |
3095 | be found at <http://proj.llucax.com.ar/wiki/evd>. |
3361 | be found at <http://proj.llucax.com.ar/wiki/evd>. |
3096 | .IP "Ocaml" 4 |
3362 | .IP "Ocaml" 4 |
… | |
… | |
3279 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
3545 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
3280 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
3546 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
3281 | implementations for some libevent functions (such as logging, which is not |
3547 | implementations for some libevent functions (such as logging, which is not |
3282 | supported). It will also not define any of the structs usually found in |
3548 | supported). It will also not define any of the structs usually found in |
3283 | \&\fIevent.h\fR that are not directly supported by the libev core alone. |
3549 | \&\fIevent.h\fR that are not directly supported by the libev core alone. |
|
|
3550 | .Sp |
|
|
3551 | In stanbdalone mode, libev will still try to automatically deduce the |
|
|
3552 | configuration, but has to be more conservative. |
3284 | .IP "\s-1EV_USE_MONOTONIC\s0" 4 |
3553 | .IP "\s-1EV_USE_MONOTONIC\s0" 4 |
3285 | .IX Item "EV_USE_MONOTONIC" |
3554 | .IX Item "EV_USE_MONOTONIC" |
3286 | If defined to be \f(CW1\fR, libev will try to detect the availability of the |
3555 | If defined to be \f(CW1\fR, libev will try to detect the availability of the |
3287 | monotonic clock option at both compile time and runtime. Otherwise no use |
3556 | monotonic clock option at both compile time and runtime. Otherwise no |
3288 | of the monotonic clock option will be attempted. If you enable this, you |
3557 | use of the monotonic clock option will be attempted. If you enable this, |
3289 | usually have to link against librt or something similar. Enabling it when |
3558 | you usually have to link against librt or something similar. Enabling it |
3290 | the functionality isn't available is safe, though, although you have |
3559 | when the functionality isn't available is safe, though, although you have |
3291 | to make sure you link against any libraries where the \f(CW\*(C`clock_gettime\*(C'\fR |
3560 | to make sure you link against any libraries where the \f(CW\*(C`clock_gettime\*(C'\fR |
3292 | function is hiding in (often \fI\-lrt\fR). |
3561 | function is hiding in (often \fI\-lrt\fR). See also \f(CW\*(C`EV_USE_CLOCK_SYSCALL\*(C'\fR. |
3293 | .IP "\s-1EV_USE_REALTIME\s0" 4 |
3562 | .IP "\s-1EV_USE_REALTIME\s0" 4 |
3294 | .IX Item "EV_USE_REALTIME" |
3563 | .IX Item "EV_USE_REALTIME" |
3295 | If defined to be \f(CW1\fR, libev will try to detect the availability of the |
3564 | If defined to be \f(CW1\fR, libev will try to detect the availability of the |
3296 | real-time clock option at compile time (and assume its availability at |
3565 | real-time clock option at compile time (and assume its availability |
3297 | runtime if successful). Otherwise no use of the real-time clock option will |
3566 | at runtime if successful). Otherwise no use of the real-time clock |
3298 | be attempted. This effectively replaces \f(CW\*(C`gettimeofday\*(C'\fR by \f(CW\*(C`clock_get |
3567 | option will be attempted. This effectively replaces \f(CW\*(C`gettimeofday\*(C'\fR |
3299 | (CLOCK_REALTIME, ...)\*(C'\fR and will not normally affect correctness. See the |
3568 | by \f(CW\*(C`clock_get (CLOCK_REALTIME, ...)\*(C'\fR and will not normally affect |
3300 | note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. |
3569 | correctness. See the note about libraries in the description of |
|
|
3570 | \&\f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. Defaults to the opposite value of |
|
|
3571 | \&\f(CW\*(C`EV_USE_CLOCK_SYSCALL\*(C'\fR. |
|
|
3572 | .IP "\s-1EV_USE_CLOCK_SYSCALL\s0" 4 |
|
|
3573 | .IX Item "EV_USE_CLOCK_SYSCALL" |
|
|
3574 | If defined to be \f(CW1\fR, libev will try to use a direct syscall instead |
|
|
3575 | of calling the system-provided \f(CW\*(C`clock_gettime\*(C'\fR function. This option |
|
|
3576 | exists because on GNU/Linux, \f(CW\*(C`clock_gettime\*(C'\fR is in \f(CW\*(C`librt\*(C'\fR, but \f(CW\*(C`librt\*(C'\fR |
|
|
3577 | unconditionally pulls in \f(CW\*(C`libpthread\*(C'\fR, slowing down single-threaded |
|
|
3578 | programs needlessly. Using a direct syscall is slightly slower (in |
|
|
3579 | theory), because no optimised vdso implementation can be used, but avoids |
|
|
3580 | the pthread dependency. Defaults to \f(CW1\fR on GNU/Linux with glibc 2.x or |
|
|
3581 | higher, as it simplifies linking (no need for \f(CW\*(C`\-lrt\*(C'\fR). |
3301 | .IP "\s-1EV_USE_NANOSLEEP\s0" 4 |
3582 | .IP "\s-1EV_USE_NANOSLEEP\s0" 4 |
3302 | .IX Item "EV_USE_NANOSLEEP" |
3583 | .IX Item "EV_USE_NANOSLEEP" |
3303 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
3584 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
3304 | and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. |
3585 | and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. |
3305 | .IP "\s-1EV_USE_EVENTFD\s0" 4 |
3586 | .IP "\s-1EV_USE_EVENTFD\s0" 4 |
… | |
… | |
3317 | will not be compiled in. |
3598 | will not be compiled in. |
3318 | .IP "\s-1EV_SELECT_USE_FD_SET\s0" 4 |
3599 | .IP "\s-1EV_SELECT_USE_FD_SET\s0" 4 |
3319 | .IX Item "EV_SELECT_USE_FD_SET" |
3600 | .IX Item "EV_SELECT_USE_FD_SET" |
3320 | If defined to \f(CW1\fR, then the select backend will use the system \f(CW\*(C`fd_set\*(C'\fR |
3601 | If defined to \f(CW1\fR, then the select backend will use the system \f(CW\*(C`fd_set\*(C'\fR |
3321 | structure. This is useful if libev doesn't compile due to a missing |
3602 | structure. This is useful if libev doesn't compile due to a missing |
3322 | \&\f(CW\*(C`NFDBITS\*(C'\fR or \f(CW\*(C`fd_mask\*(C'\fR definition or it mis-guesses the bitset layout on |
3603 | \&\f(CW\*(C`NFDBITS\*(C'\fR or \f(CW\*(C`fd_mask\*(C'\fR definition or it mis-guesses the bitset layout |
3323 | exotic systems. This usually limits the range of file descriptors to some |
3604 | on exotic systems. This usually limits the range of file descriptors to |
3324 | low limit such as 1024 or might have other limitations (winsocket only |
3605 | some low limit such as 1024 or might have other limitations (winsocket |
3325 | allows 64 sockets). The \f(CW\*(C`FD_SETSIZE\*(C'\fR macro, set before compilation, might |
3606 | only allows 64 sockets). The \f(CW\*(C`FD_SETSIZE\*(C'\fR macro, set before compilation, |
3326 | influence the size of the \f(CW\*(C`fd_set\*(C'\fR used. |
3607 | configures the maximum size of the \f(CW\*(C`fd_set\*(C'\fR. |
3327 | .IP "\s-1EV_SELECT_IS_WINSOCKET\s0" 4 |
3608 | .IP "\s-1EV_SELECT_IS_WINSOCKET\s0" 4 |
3328 | .IX Item "EV_SELECT_IS_WINSOCKET" |
3609 | .IX Item "EV_SELECT_IS_WINSOCKET" |
3329 | When defined to \f(CW1\fR, the select backend will assume that |
3610 | When defined to \f(CW1\fR, the select backend will assume that |
3330 | select/socket/connect etc. don't understand file descriptors but |
3611 | select/socket/connect etc. don't understand file descriptors but |
3331 | wants osf handles on win32 (this is the case when the select to |
3612 | wants osf handles on win32 (this is the case when the select to |
… | |
… | |
3750 | way (note also that glib is the slowest event library known to man). |
4031 | way (note also that glib is the slowest event library known to man). |
3751 | .PP |
4032 | .PP |
3752 | There is no supported compilation method available on windows except |
4033 | There is no supported compilation method available on windows except |
3753 | embedding it into other applications. |
4034 | embedding it into other applications. |
3754 | .PP |
4035 | .PP |
|
|
4036 | Sensible signal handling is officially unsupported by Microsoft \- libev |
|
|
4037 | tries its best, but under most conditions, signals will simply not work. |
|
|
4038 | .PP |
3755 | Not a libev limitation but worth mentioning: windows apparently doesn't |
4039 | Not a libev limitation but worth mentioning: windows apparently doesn't |
3756 | accept large writes: instead of resulting in a partial write, windows will |
4040 | accept large writes: instead of resulting in a partial write, windows will |
3757 | either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large, |
4041 | either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large, |
3758 | so make sure you only write small amounts into your sockets (less than a |
4042 | so make sure you only write small amounts into your sockets (less than a |
3759 | megabyte seems safe, but this apparently depends on the amount of memory |
4043 | megabyte seems safe, but this apparently depends on the amount of memory |
… | |
… | |
3763 | the abysmal performance of winsockets, using a large number of sockets |
4047 | the abysmal performance of winsockets, using a large number of sockets |
3764 | is not recommended (and not reasonable). If your program needs to use |
4048 | is not recommended (and not reasonable). If your program needs to use |
3765 | more than a hundred or so sockets, then likely it needs to use a totally |
4049 | more than a hundred or so sockets, then likely it needs to use a totally |
3766 | different implementation for windows, as libev offers the \s-1POSIX\s0 readiness |
4050 | different implementation for windows, as libev offers the \s-1POSIX\s0 readiness |
3767 | notification model, which cannot be implemented efficiently on windows |
4051 | notification model, which cannot be implemented efficiently on windows |
3768 | (Microsoft monopoly games). |
4052 | (due to Microsoft monopoly games). |
3769 | .PP |
4053 | .PP |
3770 | A typical way to use libev under windows is to embed it (see the embedding |
4054 | A typical way to use libev under windows is to embed it (see the embedding |
3771 | section for details) and use the following \fIevwrap.h\fR header file instead |
4055 | section for details) and use the following \fIevwrap.h\fR header file instead |
3772 | of \fIev.h\fR: |
4056 | of \fIev.h\fR: |
3773 | .PP |
4057 | .PP |
… | |
… | |
3811 | .Sp |
4095 | .Sp |
3812 | Early versions of winsocket's select only supported waiting for a maximum |
4096 | Early versions of winsocket's select only supported waiting for a maximum |
3813 | of \f(CW64\fR handles (probably owning to the fact that all windows kernels |
4097 | of \f(CW64\fR handles (probably owning to the fact that all windows kernels |
3814 | can only wait for \f(CW64\fR things at the same time internally; Microsoft |
4098 | can only wait for \f(CW64\fR things at the same time internally; Microsoft |
3815 | recommends spawning a chain of threads and wait for 63 handles and the |
4099 | recommends spawning a chain of threads and wait for 63 handles and the |
3816 | previous thread in each. Great). |
4100 | previous thread in each. Sounds great!). |
3817 | .Sp |
4101 | .Sp |
3818 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
4102 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
3819 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
4103 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
3820 | call (which might be in libev or elsewhere, for example, perl does its own |
4104 | call (which might be in libev or elsewhere, for example, perl and many |
3821 | select emulation on windows). |
4105 | other interpreters do their own select emulation on windows). |
3822 | .Sp |
4106 | .Sp |
3823 | Another limit is the number of file descriptors in the Microsoft runtime |
4107 | Another limit is the number of file descriptors in the Microsoft runtime |
3824 | libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR fetish |
4108 | libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR |
3825 | or something like this inside Microsoft). You can increase this by calling |
4109 | fetish or something like this inside Microsoft). You can increase this |
3826 | \&\f(CW\*(C`_setmaxstdio\*(C'\fR, which can increase this limit to \f(CW2048\fR (another |
4110 | by calling \f(CW\*(C`_setmaxstdio\*(C'\fR, which can increase this limit to \f(CW2048\fR |
3827 | arbitrary limit), but is broken in many versions of the Microsoft runtime |
4111 | (another arbitrary limit), but is broken in many versions of the Microsoft |
3828 | libraries. |
|
|
3829 | .Sp |
|
|
3830 | This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on |
4112 | runtime libraries. This might get you to about \f(CW512\fR or \f(CW2048\fR sockets |
3831 | windows version and/or the phase of the moon). To get more, you need to |
4113 | (depending on windows version and/or the phase of the moon). To get more, |
3832 | wrap all I/O functions and provide your own fd management, but the cost of |
4114 | you need to wrap all I/O functions and provide your own fd management, but |
3833 | calling select (O(nA\*^X)) will likely make this unworkable. |
4115 | the cost of calling select (O(nA\*^X)) will likely make this unworkable. |
3834 | .Sh "\s-1PORTABILITY\s0 \s-1REQUIREMENTS\s0" |
4116 | .Sh "\s-1PORTABILITY\s0 \s-1REQUIREMENTS\s0" |
3835 | .IX Subsection "PORTABILITY REQUIREMENTS" |
4117 | .IX Subsection "PORTABILITY REQUIREMENTS" |
3836 | In addition to a working ISO-C implementation and of course the |
4118 | In addition to a working ISO-C implementation and of course the |
3837 | backend-specific APIs, libev relies on a few additional extensions: |
4119 | backend-specific APIs, libev relies on a few additional extensions: |
3838 | .ie n .IP """void (*)(ev_watcher_type *, int revents)""\fR must have compatible calling conventions regardless of \f(CW""ev_watcher_type *""." 4 |
4120 | .ie n .IP """void (*)(ev_watcher_type *, int revents)""\fR must have compatible calling conventions regardless of \f(CW""ev_watcher_type *""." 4 |
… | |
… | |
3941 | .IX Item "Processing signals: O(max_signal_number)" |
4223 | .IX Item "Processing signals: O(max_signal_number)" |
3942 | .PD |
4224 | .PD |
3943 | Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR |
4225 | Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR |
3944 | calls in the current loop iteration. Checking for async and signal events |
4226 | calls in the current loop iteration. Checking for async and signal events |
3945 | involves iterating over all running async watchers or all signal numbers. |
4227 | involves iterating over all running async watchers or all signal numbers. |
|
|
4228 | .SH "GLOSSARY" |
|
|
4229 | .IX Header "GLOSSARY" |
|
|
4230 | .IP "active" 4 |
|
|
4231 | .IX Item "active" |
|
|
4232 | A watcher is active as long as it has been started (has been attached to |
|
|
4233 | an event loop) but not yet stopped (disassociated from the event loop). |
|
|
4234 | .IP "application" 4 |
|
|
4235 | .IX Item "application" |
|
|
4236 | In this document, an application is whatever is using libev. |
|
|
4237 | .IP "callback" 4 |
|
|
4238 | .IX Item "callback" |
|
|
4239 | The address of a function that is called when some event has been |
|
|
4240 | detected. Callbacks are being passed the event loop, the watcher that |
|
|
4241 | received the event, and the actual event bitset. |
|
|
4242 | .IP "callback invocation" 4 |
|
|
4243 | .IX Item "callback invocation" |
|
|
4244 | The act of calling the callback associated with a watcher. |
|
|
4245 | .IP "event" 4 |
|
|
4246 | .IX Item "event" |
|
|
4247 | A change of state of some external event, such as data now being available |
|
|
4248 | for reading on a file descriptor, time having passed or simply not having |
|
|
4249 | any other events happening anymore. |
|
|
4250 | .Sp |
|
|
4251 | In libev, events are represented as single bits (such as \f(CW\*(C`EV_READ\*(C'\fR or |
|
|
4252 | \&\f(CW\*(C`EV_TIMEOUT\*(C'\fR). |
|
|
4253 | .IP "event library" 4 |
|
|
4254 | .IX Item "event library" |
|
|
4255 | A software package implementing an event model and loop. |
|
|
4256 | .IP "event loop" 4 |
|
|
4257 | .IX Item "event loop" |
|
|
4258 | An entity that handles and processes external events and converts them |
|
|
4259 | into callback invocations. |
|
|
4260 | .IP "event model" 4 |
|
|
4261 | .IX Item "event model" |
|
|
4262 | The model used to describe how an event loop handles and processes |
|
|
4263 | watchers and events. |
|
|
4264 | .IP "pending" 4 |
|
|
4265 | .IX Item "pending" |
|
|
4266 | A watcher is pending as soon as the corresponding event has been detected, |
|
|
4267 | and stops being pending as soon as the watcher will be invoked or its |
|
|
4268 | pending status is explicitly cleared by the application. |
|
|
4269 | .Sp |
|
|
4270 | A watcher can be pending, but not active. Stopping a watcher also clears |
|
|
4271 | its pending status. |
|
|
4272 | .IP "real time" 4 |
|
|
4273 | .IX Item "real time" |
|
|
4274 | The physical time that is observed. It is apparently strictly monotonic :) |
|
|
4275 | .IP "wall-clock time" 4 |
|
|
4276 | .IX Item "wall-clock time" |
|
|
4277 | The time and date as shown on clocks. Unlike real time, it can actually |
|
|
4278 | be wrong and jump forwards and backwards, e.g. when the you adjust your |
|
|
4279 | clock. |
|
|
4280 | .IP "watcher" 4 |
|
|
4281 | .IX Item "watcher" |
|
|
4282 | A data structure that describes interest in certain events. Watchers need |
|
|
4283 | to be started (attached to an event loop) before they can receive events. |
|
|
4284 | .IP "watcher invocation" 4 |
|
|
4285 | .IX Item "watcher invocation" |
|
|
4286 | The act of calling the callback associated with a watcher. |
3946 | .SH "AUTHOR" |
4287 | .SH "AUTHOR" |
3947 | .IX Header "AUTHOR" |
4288 | .IX Header "AUTHOR" |
3948 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. |
4289 | Marc Lehmann <libev@schmorp.de>, with repeated corrections by Mikael Magnusson. |