… | |
… | |
359 | writing a server, you should C<accept ()> in a loop to accept as many |
359 | writing a server, you should C<accept ()> in a loop to accept as many |
360 | connections as possible during one iteration. You might also want to have |
360 | connections as possible during one iteration. You might also want to have |
361 | a look at C<ev_set_io_collect_interval ()> to increase the amount of |
361 | a look at C<ev_set_io_collect_interval ()> to increase the amount of |
362 | readiness notifications you get per iteration. |
362 | readiness notifications you get per iteration. |
363 | |
363 | |
|
|
364 | This backend maps C<EV_READ> to the C<readfds> set and C<EV_WRITE> to the |
|
|
365 | C<writefds> set (and to work around Microsoft Windows bugs, also onto the |
|
|
366 | C<exceptfds> set on that platform). |
|
|
367 | |
364 | =item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) |
368 | =item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) |
365 | |
369 | |
366 | And this is your standard poll(2) backend. It's more complicated |
370 | And this is your standard poll(2) backend. It's more complicated |
367 | than select, but handles sparse fds better and has no artificial |
371 | than select, but handles sparse fds better and has no artificial |
368 | limit on the number of fds you can use (except it will slow down |
372 | limit on the number of fds you can use (except it will slow down |
369 | considerably with a lot of inactive fds). It scales similarly to select, |
373 | considerably with a lot of inactive fds). It scales similarly to select, |
370 | i.e. O(total_fds). See the entry for C<EVBACKEND_SELECT>, above, for |
374 | i.e. O(total_fds). See the entry for C<EVBACKEND_SELECT>, above, for |
371 | performance tips. |
375 | performance tips. |
|
|
376 | |
|
|
377 | This backend maps C<EV_READ> to C<POLLIN | POLLERR | POLLHUP>, and |
|
|
378 | C<EV_WRITE> to C<POLLOUT | POLLERR | POLLHUP>. |
372 | |
379 | |
373 | =item C<EVBACKEND_EPOLL> (value 4, Linux) |
380 | =item C<EVBACKEND_EPOLL> (value 4, Linux) |
374 | |
381 | |
375 | For few fds, this backend is a bit little slower than poll and select, |
382 | For few fds, this backend is a bit little slower than poll and select, |
376 | but it scales phenomenally better. While poll and select usually scale |
383 | but it scales phenomenally better. While poll and select usually scale |
… | |
… | |
395 | keep at least one watcher active per fd at all times. |
402 | keep at least one watcher active per fd at all times. |
396 | |
403 | |
397 | While nominally embeddable in other event loops, this feature is broken in |
404 | While nominally embeddable in other event loops, this feature is broken in |
398 | all kernel versions tested so far. |
405 | all kernel versions tested so far. |
399 | |
406 | |
|
|
407 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
|
|
408 | C<EVBACKEND_POLL>. |
|
|
409 | |
400 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
410 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
401 | |
411 | |
402 | Kqueue deserves special mention, as at the time of this writing, it |
412 | Kqueue deserves special mention, as at the time of this writing, it |
403 | was broken on all BSDs except NetBSD (usually it doesn't work reliably |
413 | was broken on all BSDs except NetBSD (usually it doesn't work reliably |
404 | with anything but sockets and pipes, except on Darwin, where of course |
414 | with anything but sockets and pipes, except on Darwin, where of course |
… | |
… | |
425 | almost everywhere, you should only use it when you have a lot of sockets |
435 | almost everywhere, you should only use it when you have a lot of sockets |
426 | (for which it usually works), by embedding it into another event loop |
436 | (for which it usually works), by embedding it into another event loop |
427 | (e.g. C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>) and using it only for |
437 | (e.g. C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>) and using it only for |
428 | sockets. |
438 | sockets. |
429 | |
439 | |
|
|
440 | This backend maps C<EV_READ> into an C<EVFILT_READ> kevent with |
|
|
441 | C<NOTE_EOF>, and C<EV_WRITE> into an C<EVFILT_WRITE> kevent with |
|
|
442 | C<NOTE_EOF>. |
|
|
443 | |
430 | =item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8) |
444 | =item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8) |
431 | |
445 | |
432 | This is not implemented yet (and might never be, unless you send me an |
446 | This is not implemented yet (and might never be, unless you send me an |
433 | implementation). According to reports, C</dev/poll> only supports sockets |
447 | implementation). According to reports, C</dev/poll> only supports sockets |
434 | and is not embeddable, which would limit the usefulness of this backend |
448 | and is not embeddable, which would limit the usefulness of this backend |
… | |
… | |
449 | might perform better. |
463 | might perform better. |
450 | |
464 | |
451 | On the positive side, ignoring the spurious readiness notifications, this |
465 | On the positive side, ignoring the spurious readiness notifications, this |
452 | backend actually performed to specification in all tests and is fully |
466 | backend actually performed to specification in all tests and is fully |
453 | embeddable, which is a rare feat among the OS-specific backends. |
467 | embeddable, which is a rare feat among the OS-specific backends. |
|
|
468 | |
|
|
469 | This backend maps C<EV_READ> and C<EV_WRITE> in the same way as |
|
|
470 | C<EVBACKEND_POLL>. |
454 | |
471 | |
455 | =item C<EVBACKEND_ALL> |
472 | =item C<EVBACKEND_ALL> |
456 | |
473 | |
457 | Try all backends (even potentially broken ones that wouldn't be tried |
474 | Try all backends (even potentially broken ones that wouldn't be tried |
458 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
475 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
… | |
… | |
572 | Returns the current "event loop time", which is the time the event loop |
589 | Returns the current "event loop time", which is the time the event loop |
573 | received events and started processing them. This timestamp does not |
590 | received events and started processing them. This timestamp does not |
574 | change as long as callbacks are being processed, and this is also the base |
591 | change as long as callbacks are being processed, and this is also the base |
575 | time used for relative timers. You can treat it as the timestamp of the |
592 | time used for relative timers. You can treat it as the timestamp of the |
576 | event occurring (or more correctly, libev finding out about it). |
593 | event occurring (or more correctly, libev finding out about it). |
|
|
594 | |
|
|
595 | =item ev_now_update (loop) |
|
|
596 | |
|
|
597 | Establishes the current time by querying the kernel, updating the time |
|
|
598 | returned by C<ev_now ()> in the progress. This is a costly operation and |
|
|
599 | is usually done automatically within C<ev_loop ()>. |
|
|
600 | |
|
|
601 | This function is rarely useful, but when some event callback runs for a |
|
|
602 | very long time without entering the event loop, updating libev's idea of |
|
|
603 | the current time is a good idea. |
|
|
604 | |
|
|
605 | See also "The special problem of time updates" in the C<ev_timer> section. |
577 | |
606 | |
578 | =item ev_loop (loop, int flags) |
607 | =item ev_loop (loop, int flags) |
579 | |
608 | |
580 | Finally, this is it, the event handler. This function usually is called |
609 | Finally, this is it, the event handler. This function usually is called |
581 | after you initialised all your watchers and you want to start handling |
610 | after you initialised all your watchers and you want to start handling |
… | |
… | |
994 | { |
1023 | { |
995 | struct ev_io io; |
1024 | struct ev_io io; |
996 | int otherfd; |
1025 | int otherfd; |
997 | void *somedata; |
1026 | void *somedata; |
998 | struct whatever *mostinteresting; |
1027 | struct whatever *mostinteresting; |
999 | } |
1028 | }; |
|
|
1029 | |
|
|
1030 | ... |
|
|
1031 | struct my_io w; |
|
|
1032 | ev_io_init (&w.io, my_cb, fd, EV_READ); |
1000 | |
1033 | |
1001 | And since your callback will be called with a pointer to the watcher, you |
1034 | And since your callback will be called with a pointer to the watcher, you |
1002 | can cast it back to your own type: |
1035 | can cast it back to your own type: |
1003 | |
1036 | |
1004 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
1037 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
… | |
… | |
1008 | } |
1041 | } |
1009 | |
1042 | |
1010 | More interesting and less C-conformant ways of casting your callback type |
1043 | More interesting and less C-conformant ways of casting your callback type |
1011 | instead have been omitted. |
1044 | instead have been omitted. |
1012 | |
1045 | |
1013 | Another common scenario is having some data structure with multiple |
1046 | Another common scenario is to use some data structure with multiple |
1014 | watchers: |
1047 | embedded watchers: |
1015 | |
1048 | |
1016 | struct my_biggy |
1049 | struct my_biggy |
1017 | { |
1050 | { |
1018 | int some_data; |
1051 | int some_data; |
1019 | ev_timer t1; |
1052 | ev_timer t1; |
1020 | ev_timer t2; |
1053 | ev_timer t2; |
1021 | } |
1054 | } |
1022 | |
1055 | |
1023 | In this case getting the pointer to C<my_biggy> is a bit more complicated, |
1056 | In this case getting the pointer to C<my_biggy> is a bit more |
1024 | you need to use C<offsetof>: |
1057 | complicated: Either you store the address of your C<my_biggy> struct |
|
|
1058 | in the C<data> member of the watcher, or you need to use some pointer |
|
|
1059 | arithmetic using C<offsetof> inside your watchers: |
1025 | |
1060 | |
1026 | #include <stddef.h> |
1061 | #include <stddef.h> |
1027 | |
1062 | |
1028 | static void |
1063 | static void |
1029 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
1064 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
… | |
… | |
1134 | C<EVBACKEND_POLL>. |
1169 | C<EVBACKEND_POLL>. |
1135 | |
1170 | |
1136 | =head3 The special problem of SIGPIPE |
1171 | =head3 The special problem of SIGPIPE |
1137 | |
1172 | |
1138 | While not really specific to libev, it is easy to forget about SIGPIPE: |
1173 | While not really specific to libev, it is easy to forget about SIGPIPE: |
1139 | when reading from a pipe whose other end has been closed, your program |
1174 | when writing to a pipe whose other end has been closed, your program gets |
1140 | gets send a SIGPIPE, which, by default, aborts your program. For most |
1175 | send a SIGPIPE, which, by default, aborts your program. For most programs |
1141 | programs this is sensible behaviour, for daemons, this is usually |
1176 | this is sensible behaviour, for daemons, this is usually undesirable. |
1142 | undesirable. |
|
|
1143 | |
1177 | |
1144 | So when you encounter spurious, unexplained daemon exits, make sure you |
1178 | So when you encounter spurious, unexplained daemon exits, make sure you |
1145 | ignore SIGPIPE (and maybe make sure you log the exit status of your daemon |
1179 | ignore SIGPIPE (and maybe make sure you log the exit status of your daemon |
1146 | somewhere, as that would have given you a big clue). |
1180 | somewhere, as that would have given you a big clue). |
1147 | |
1181 | |
… | |
… | |
1198 | times out after an hour and you reset your system clock to January last |
1232 | times out after an hour and you reset your system clock to January last |
1199 | year, it will still time out after (roughly) and hour. "Roughly" because |
1233 | year, it will still time out after (roughly) and hour. "Roughly" because |
1200 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1234 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1201 | monotonic clock option helps a lot here). |
1235 | monotonic clock option helps a lot here). |
1202 | |
1236 | |
|
|
1237 | The callback is guaranteed to be invoked only after its timeout has passed, |
|
|
1238 | but if multiple timers become ready during the same loop iteration then |
|
|
1239 | order of execution is undefined. |
|
|
1240 | |
|
|
1241 | =head3 The special problem of time updates |
|
|
1242 | |
|
|
1243 | Establishing the current time is a costly operation (it usually takes at |
|
|
1244 | least two system calls): EV therefore updates its idea of the current |
|
|
1245 | time only before and after C<ev_loop> polls for new events, which causes |
|
|
1246 | a growing difference between C<ev_now ()> and C<ev_time ()> when handling |
|
|
1247 | lots of events. |
|
|
1248 | |
1203 | The relative timeouts are calculated relative to the C<ev_now ()> |
1249 | The relative timeouts are calculated relative to the C<ev_now ()> |
1204 | time. This is usually the right thing as this timestamp refers to the time |
1250 | time. This is usually the right thing as this timestamp refers to the time |
1205 | of the event triggering whatever timeout you are modifying/starting. If |
1251 | of the event triggering whatever timeout you are modifying/starting. If |
1206 | you suspect event processing to be delayed and you I<need> to base the timeout |
1252 | you suspect event processing to be delayed and you I<need> to base the |
1207 | on the current time, use something like this to adjust for this: |
1253 | timeout on the current time, use something like this to adjust for this: |
1208 | |
1254 | |
1209 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1255 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1210 | |
1256 | |
1211 | The callback is guaranteed to be invoked only after its timeout has passed, |
1257 | If the event loop is suspended for a long time, you can also force an |
1212 | but if multiple timers become ready during the same loop iteration then |
1258 | update of the time returned by C<ev_now ()> by calling C<ev_now_update |
1213 | order of execution is undefined. |
1259 | ()>. |
1214 | |
1260 | |
1215 | =head3 Watcher-Specific Functions and Data Members |
1261 | =head3 Watcher-Specific Functions and Data Members |
1216 | |
1262 | |
1217 | =over 4 |
1263 | =over 4 |
1218 | |
1264 | |
… | |
… | |
3194 | |
3240 | |
3195 | =head1 THREADS AND COROUTINES |
3241 | =head1 THREADS AND COROUTINES |
3196 | |
3242 | |
3197 | =head2 THREADS |
3243 | =head2 THREADS |
3198 | |
3244 | |
3199 | Libev itself is completely thread-safe, but it uses no locking. This |
3245 | Libev itself is thread-safe (unless the opposite is specifically |
|
|
3246 | documented for a function), but it uses no locking itself. This means that |
3200 | means that you can use as many loops as you want in parallel, as long as |
3247 | you can use as many loops as you want in parallel, as long as only one |
3201 | only one thread ever calls into one libev function with the same loop |
3248 | thread ever calls into one libev function with the same loop parameter: |
3202 | parameter. |
3249 | libev guarentees that different event loops share no data structures that |
|
|
3250 | need locking. |
3203 | |
3251 | |
3204 | Or put differently: calls with different loop parameters can be done in |
3252 | Or to put it differently: calls with different loop parameters can be done |
3205 | parallel from multiple threads, calls with the same loop parameter must be |
3253 | concurrently from multiple threads, calls with the same loop parameter |
3206 | done serially (but can be done from different threads, as long as only one |
3254 | must be done serially (but can be done from different threads, as long as |
3207 | thread ever is inside a call at any point in time, e.g. by using a mutex |
3255 | only one thread ever is inside a call at any point in time, e.g. by using |
3208 | per loop). |
3256 | a mutex per loop). |
|
|
3257 | |
|
|
3258 | Specifically to support threads (and signal handlers), libev implements |
|
|
3259 | so-called C<ev_async> watchers, which allow some limited form of |
|
|
3260 | concurrency on the same event loop. |
3209 | |
3261 | |
3210 | If you want to know which design (one loop, locking, or multiple loops |
3262 | If you want to know which design (one loop, locking, or multiple loops |
3211 | without or something else still) is best for your problem, then I cannot |
3263 | without or something else still) is best for your problem, then I cannot |
3212 | help you. I can give some generic advice however: |
3264 | help you. I can give some generic advice however: |
3213 | |
3265 | |
… | |
… | |
3231 | better than you currently do :-) |
3283 | better than you currently do :-) |
3232 | |
3284 | |
3233 | =item * often you need to talk to some other thread which blocks in the |
3285 | =item * often you need to talk to some other thread which blocks in the |
3234 | event loop - C<ev_async> watchers can be used to wake them up from other |
3286 | event loop - C<ev_async> watchers can be used to wake them up from other |
3235 | threads safely (or from signal contexts...). |
3287 | threads safely (or from signal contexts...). |
|
|
3288 | |
|
|
3289 | =item * some watcher types are only supported in the default loop - use |
|
|
3290 | C<ev_async> watchers to tell your other loops about any such events. |
3236 | |
3291 | |
3237 | =back |
3292 | =back |
3238 | |
3293 | |
3239 | =head2 COROUTINES |
3294 | =head2 COROUTINES |
3240 | |
3295 | |
… | |
… | |
3243 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3298 | coroutines (e.g. you can call C<ev_loop> on the same loop from two |
3244 | different coroutines and switch freely between both coroutines running the |
3299 | different coroutines and switch freely between both coroutines running the |
3245 | loop, as long as you don't confuse yourself). The only exception is that |
3300 | loop, as long as you don't confuse yourself). The only exception is that |
3246 | you must not do this from C<ev_periodic> reschedule callbacks. |
3301 | you must not do this from C<ev_periodic> reschedule callbacks. |
3247 | |
3302 | |
3248 | Care has been invested into making sure that libev does not keep local |
3303 | Care has been taken to ensure that libev does not keep local state inside |
3249 | state inside C<ev_loop>, and other calls do not usually allow coroutine |
3304 | C<ev_loop>, and other calls do not usually allow coroutine switches. |
3250 | switches. |
|
|
3251 | |
3305 | |
3252 | |
3306 | |
3253 | =head1 COMPLEXITIES |
3307 | =head1 COMPLEXITIES |
3254 | |
3308 | |
3255 | In this section the complexities of (many of) the algorithms used inside |
3309 | In this section the complexities of (many of) the algorithms used inside |