… | |
… | |
573 | received events and started processing them. This timestamp does not |
573 | 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 |
574 | 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 |
575 | 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). |
576 | event occurring (or more correctly, libev finding out about it). |
577 | |
577 | |
|
|
578 | =item ev_now_update (loop) |
|
|
579 | |
|
|
580 | Establishes the current time by querying the kernel, updating the time |
|
|
581 | returned by C<ev_now ()> in the progress. This is a costly operation and |
|
|
582 | is usually done automatically within C<ev_loop ()>. |
|
|
583 | |
|
|
584 | This function is rarely useful, but when some event callback runs for a |
|
|
585 | very long time without entering the event loop, updating libev's idea of |
|
|
586 | the current time is a good idea. |
|
|
587 | |
|
|
588 | See also "The special problem of time updates" in the C<ev_timer> section. |
|
|
589 | |
578 | =item ev_loop (loop, int flags) |
590 | =item ev_loop (loop, int flags) |
579 | |
591 | |
580 | Finally, this is it, the event handler. This function usually is called |
592 | 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 |
593 | after you initialised all your watchers and you want to start handling |
582 | events. |
594 | events. |
… | |
… | |
604 | |
616 | |
605 | Here are the gory details of what C<ev_loop> does: |
617 | Here are the gory details of what C<ev_loop> does: |
606 | |
618 | |
607 | - Before the first iteration, call any pending watchers. |
619 | - Before the first iteration, call any pending watchers. |
608 | * If EVFLAG_FORKCHECK was used, check for a fork. |
620 | * If EVFLAG_FORKCHECK was used, check for a fork. |
609 | - If a fork was detected, queue and call all fork watchers. |
621 | - If a fork was detected (by any means), queue and call all fork watchers. |
610 | - Queue and call all prepare watchers. |
622 | - Queue and call all prepare watchers. |
611 | - If we have been forked, recreate the kernel state. |
623 | - If we have been forked, detach and recreate the kernel state |
|
|
624 | as to not disturb the other process. |
612 | - Update the kernel state with all outstanding changes. |
625 | - Update the kernel state with all outstanding changes. |
613 | - Update the "event loop time". |
626 | - Update the "event loop time" (ev_now ()). |
614 | - Calculate for how long to sleep or block, if at all |
627 | - Calculate for how long to sleep or block, if at all |
615 | (active idle watchers, EVLOOP_NONBLOCK or not having |
628 | (active idle watchers, EVLOOP_NONBLOCK or not having |
616 | any active watchers at all will result in not sleeping). |
629 | any active watchers at all will result in not sleeping). |
617 | - Sleep if the I/O and timer collect interval say so. |
630 | - Sleep if the I/O and timer collect interval say so. |
618 | - Block the process, waiting for any events. |
631 | - Block the process, waiting for any events. |
619 | - Queue all outstanding I/O (fd) events. |
632 | - Queue all outstanding I/O (fd) events. |
620 | - Update the "event loop time" and do time jump handling. |
633 | - Update the "event loop time" (ev_now ()), and do time jump adjustments. |
621 | - Queue all outstanding timers. |
634 | - Queue all outstanding timers. |
622 | - Queue all outstanding periodics. |
635 | - Queue all outstanding periodics. |
623 | - If no events are pending now, queue all idle watchers. |
636 | - Unless any events are pending now, queue all idle watchers. |
624 | - Queue all check watchers. |
637 | - Queue all check watchers. |
625 | - Call all queued watchers in reverse order (i.e. check watchers first). |
638 | - Call all queued watchers in reverse order (i.e. check watchers first). |
626 | Signals and child watchers are implemented as I/O watchers, and will |
639 | Signals and child watchers are implemented as I/O watchers, and will |
627 | be handled here by queueing them when their watcher gets executed. |
640 | be handled here by queueing them when their watcher gets executed. |
628 | - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
641 | - If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
… | |
… | |
633 | anymore. |
646 | anymore. |
634 | |
647 | |
635 | ... queue jobs here, make sure they register event watchers as long |
648 | ... queue jobs here, make sure they register event watchers as long |
636 | ... as they still have work to do (even an idle watcher will do..) |
649 | ... as they still have work to do (even an idle watcher will do..) |
637 | ev_loop (my_loop, 0); |
650 | ev_loop (my_loop, 0); |
638 | ... jobs done. yeah! |
651 | ... jobs done or somebody called unloop. yeah! |
639 | |
652 | |
640 | =item ev_unloop (loop, how) |
653 | =item ev_unloop (loop, how) |
641 | |
654 | |
642 | Can be used to make a call to C<ev_loop> return early (but only after it |
655 | Can be used to make a call to C<ev_loop> return early (but only after it |
643 | has processed all outstanding events). The C<how> argument must be either |
656 | has processed all outstanding events). The C<how> argument must be either |
… | |
… | |
679 | =item ev_set_io_collect_interval (loop, ev_tstamp interval) |
692 | =item ev_set_io_collect_interval (loop, ev_tstamp interval) |
680 | |
693 | |
681 | =item ev_set_timeout_collect_interval (loop, ev_tstamp interval) |
694 | =item ev_set_timeout_collect_interval (loop, ev_tstamp interval) |
682 | |
695 | |
683 | These advanced functions influence the time that libev will spend waiting |
696 | These advanced functions influence the time that libev will spend waiting |
684 | for events. Both are by default C<0>, meaning that libev will try to |
697 | for events. Both time intervals are by default C<0>, meaning that libev |
685 | invoke timer/periodic callbacks and I/O callbacks with minimum latency. |
698 | will try to invoke timer/periodic callbacks and I/O callbacks with minimum |
|
|
699 | latency. |
686 | |
700 | |
687 | Setting these to a higher value (the C<interval> I<must> be >= C<0>) |
701 | Setting these to a higher value (the C<interval> I<must> be >= C<0>) |
688 | allows libev to delay invocation of I/O and timer/periodic callbacks to |
702 | allows libev to delay invocation of I/O and timer/periodic callbacks |
689 | increase efficiency of loop iterations. |
703 | to increase efficiency of loop iterations (or to increase power-saving |
|
|
704 | opportunities). |
690 | |
705 | |
691 | The background is that sometimes your program runs just fast enough to |
706 | The background is that sometimes your program runs just fast enough to |
692 | handle one (or very few) event(s) per loop iteration. While this makes |
707 | handle one (or very few) event(s) per loop iteration. While this makes |
693 | the program responsive, it also wastes a lot of CPU time to poll for new |
708 | the program responsive, it also wastes a lot of CPU time to poll for new |
694 | events, especially with backends like C<select ()> which have a high |
709 | events, especially with backends like C<select ()> which have a high |
… | |
… | |
709 | Many (busy) programs can usually benefit by setting the I/O collect |
724 | Many (busy) programs can usually benefit by setting the I/O collect |
710 | interval to a value near C<0.1> or so, which is often enough for |
725 | interval to a value near C<0.1> or so, which is often enough for |
711 | interactive servers (of course not for games), likewise for timeouts. It |
726 | interactive servers (of course not for games), likewise for timeouts. It |
712 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
727 | usually doesn't make much sense to set it to a lower value than C<0.01>, |
713 | as this approaches the timing granularity of most systems. |
728 | as this approaches the timing granularity of most systems. |
|
|
729 | |
|
|
730 | Setting the I<timeout collect interval> can improve the opportunity for |
|
|
731 | saving power, as the program will "bundle" timer callback invocations that |
|
|
732 | are "near" in time together, by delaying some, thus reducing the number of |
|
|
733 | times the process sleeps and wakes up again. Another useful technique to |
|
|
734 | reduce iterations/wake-ups is to use C<ev_periodic> watchers and make sure |
|
|
735 | they fire on, say, one-second boundaries only. |
714 | |
736 | |
715 | =item ev_loop_verify (loop) |
737 | =item ev_loop_verify (loop) |
716 | |
738 | |
717 | This function only does something when C<EV_VERIFY> support has been |
739 | This function only does something when C<EV_VERIFY> support has been |
718 | compiled in. It tries to go through all internal structures and checks |
740 | compiled in. It tries to go through all internal structures and checks |
… | |
… | |
984 | { |
1006 | { |
985 | struct ev_io io; |
1007 | struct ev_io io; |
986 | int otherfd; |
1008 | int otherfd; |
987 | void *somedata; |
1009 | void *somedata; |
988 | struct whatever *mostinteresting; |
1010 | struct whatever *mostinteresting; |
989 | } |
1011 | }; |
|
|
1012 | |
|
|
1013 | ... |
|
|
1014 | struct my_io w; |
|
|
1015 | ev_io_init (&w.io, my_cb, fd, EV_READ); |
990 | |
1016 | |
991 | And since your callback will be called with a pointer to the watcher, you |
1017 | And since your callback will be called with a pointer to the watcher, you |
992 | can cast it back to your own type: |
1018 | can cast it back to your own type: |
993 | |
1019 | |
994 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
1020 | static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) |
… | |
… | |
998 | } |
1024 | } |
999 | |
1025 | |
1000 | More interesting and less C-conformant ways of casting your callback type |
1026 | More interesting and less C-conformant ways of casting your callback type |
1001 | instead have been omitted. |
1027 | instead have been omitted. |
1002 | |
1028 | |
1003 | Another common scenario is having some data structure with multiple |
1029 | Another common scenario is to use some data structure with multiple |
1004 | watchers: |
1030 | embedded watchers: |
1005 | |
1031 | |
1006 | struct my_biggy |
1032 | struct my_biggy |
1007 | { |
1033 | { |
1008 | int some_data; |
1034 | int some_data; |
1009 | ev_timer t1; |
1035 | ev_timer t1; |
1010 | ev_timer t2; |
1036 | ev_timer t2; |
1011 | } |
1037 | } |
1012 | |
1038 | |
1013 | In this case getting the pointer to C<my_biggy> is a bit more complicated, |
1039 | In this case getting the pointer to C<my_biggy> is a bit more |
1014 | you need to use C<offsetof>: |
1040 | complicated: Either you store the address of your C<my_biggy> struct |
|
|
1041 | in the C<data> member of the watcher, or you need to use some pointer |
|
|
1042 | arithmetic using C<offsetof> inside your watchers: |
1015 | |
1043 | |
1016 | #include <stddef.h> |
1044 | #include <stddef.h> |
1017 | |
1045 | |
1018 | static void |
1046 | static void |
1019 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
1047 | t1_cb (EV_P_ struct ev_timer *w, int revents) |
… | |
… | |
1124 | C<EVBACKEND_POLL>. |
1152 | C<EVBACKEND_POLL>. |
1125 | |
1153 | |
1126 | =head3 The special problem of SIGPIPE |
1154 | =head3 The special problem of SIGPIPE |
1127 | |
1155 | |
1128 | While not really specific to libev, it is easy to forget about SIGPIPE: |
1156 | While not really specific to libev, it is easy to forget about SIGPIPE: |
1129 | when reading from a pipe whose other end has been closed, your program |
1157 | when writing to a pipe whose other end has been closed, your program gets |
1130 | gets send a SIGPIPE, which, by default, aborts your program. For most |
1158 | send a SIGPIPE, which, by default, aborts your program. For most programs |
1131 | programs this is sensible behaviour, for daemons, this is usually |
1159 | this is sensible behaviour, for daemons, this is usually undesirable. |
1132 | undesirable. |
|
|
1133 | |
1160 | |
1134 | So when you encounter spurious, unexplained daemon exits, make sure you |
1161 | So when you encounter spurious, unexplained daemon exits, make sure you |
1135 | ignore SIGPIPE (and maybe make sure you log the exit status of your daemon |
1162 | ignore SIGPIPE (and maybe make sure you log the exit status of your daemon |
1136 | somewhere, as that would have given you a big clue). |
1163 | somewhere, as that would have given you a big clue). |
1137 | |
1164 | |
… | |
… | |
1188 | times out after an hour and you reset your system clock to January last |
1215 | times out after an hour and you reset your system clock to January last |
1189 | year, it will still time out after (roughly) and hour. "Roughly" because |
1216 | year, it will still time out after (roughly) and hour. "Roughly" because |
1190 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1217 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
1191 | monotonic clock option helps a lot here). |
1218 | monotonic clock option helps a lot here). |
1192 | |
1219 | |
|
|
1220 | The callback is guaranteed to be invoked only after its timeout has passed, |
|
|
1221 | but if multiple timers become ready during the same loop iteration then |
|
|
1222 | order of execution is undefined. |
|
|
1223 | |
|
|
1224 | =head3 The special problem of time updates |
|
|
1225 | |
|
|
1226 | Establishing the current time is a costly operation (it usually takes at |
|
|
1227 | least two system calls): EV therefore updates its idea of the current |
|
|
1228 | time only before and after C<ev_loop> polls for new events, which causes |
|
|
1229 | a growing difference between C<ev_now ()> and C<ev_time ()> when handling |
|
|
1230 | lots of events. |
|
|
1231 | |
1193 | The relative timeouts are calculated relative to the C<ev_now ()> |
1232 | The relative timeouts are calculated relative to the C<ev_now ()> |
1194 | time. This is usually the right thing as this timestamp refers to the time |
1233 | time. This is usually the right thing as this timestamp refers to the time |
1195 | of the event triggering whatever timeout you are modifying/starting. If |
1234 | of the event triggering whatever timeout you are modifying/starting. If |
1196 | you suspect event processing to be delayed and you I<need> to base the timeout |
1235 | you suspect event processing to be delayed and you I<need> to base the |
1197 | on the current time, use something like this to adjust for this: |
1236 | timeout on the current time, use something like this to adjust for this: |
1198 | |
1237 | |
1199 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1238 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
1200 | |
1239 | |
1201 | The callback is guaranteed to be invoked only after its timeout has passed, |
1240 | If the event loop is suspended for a long time, you can also force an |
1202 | but if multiple timers become ready during the same loop iteration then |
1241 | update of the time returned by C<ev_now ()> by calling C<ev_now_update |
1203 | order of execution is undefined. |
1242 | ()>. |
1204 | |
1243 | |
1205 | =head3 Watcher-Specific Functions and Data Members |
1244 | =head3 Watcher-Specific Functions and Data Members |
1206 | |
1245 | |
1207 | =over 4 |
1246 | =over 4 |
1208 | |
1247 | |
… | |
… | |
1559 | handler, you can override it easily by installing your own handler for |
1598 | handler, you can override it easily by installing your own handler for |
1560 | C<SIGCHLD> after initialising the default loop, and making sure the |
1599 | C<SIGCHLD> after initialising the default loop, and making sure the |
1561 | default loop never gets destroyed. You are encouraged, however, to use an |
1600 | default loop never gets destroyed. You are encouraged, however, to use an |
1562 | event-based approach to child reaping and thus use libev's support for |
1601 | event-based approach to child reaping and thus use libev's support for |
1563 | that, so other libev users can use C<ev_child> watchers freely. |
1602 | that, so other libev users can use C<ev_child> watchers freely. |
|
|
1603 | |
|
|
1604 | =head3 Stopping the Child Watcher |
|
|
1605 | |
|
|
1606 | Currently, the child watcher never gets stopped, even when the |
|
|
1607 | child terminates, so normally one needs to stop the watcher in the |
|
|
1608 | callback. Future versions of libev might stop the watcher automatically |
|
|
1609 | when a child exit is detected. |
1564 | |
1610 | |
1565 | =head3 Watcher-Specific Functions and Data Members |
1611 | =head3 Watcher-Specific Functions and Data Members |
1566 | |
1612 | |
1567 | =over 4 |
1613 | =over 4 |
1568 | |
1614 | |
… | |
… | |
1662 | will be no polling. |
1708 | will be no polling. |
1663 | |
1709 | |
1664 | =head3 ABI Issues (Largefile Support) |
1710 | =head3 ABI Issues (Largefile Support) |
1665 | |
1711 | |
1666 | Libev by default (unless the user overrides this) uses the default |
1712 | Libev by default (unless the user overrides this) uses the default |
1667 | compilation environment, which means that on systems with optionally |
1713 | compilation environment, which means that on systems with large file |
1668 | disabled large file support, you get the 32 bit version of the stat |
1714 | support disabled by default, you get the 32 bit version of the stat |
1669 | structure. When using the library from programs that change the ABI to |
1715 | structure. When using the library from programs that change the ABI to |
1670 | use 64 bit file offsets the programs will fail. In that case you have to |
1716 | use 64 bit file offsets the programs will fail. In that case you have to |
1671 | compile libev with the same flags to get binary compatibility. This is |
1717 | compile libev with the same flags to get binary compatibility. This is |
1672 | obviously the case with any flags that change the ABI, but the problem is |
1718 | obviously the case with any flags that change the ABI, but the problem is |
1673 | most noticeably with ev_stat and large file support. |
1719 | most noticeably disabled with ev_stat and large file support. |
|
|
1720 | |
|
|
1721 | The solution for this is to lobby your distribution maker to make large |
|
|
1722 | file interfaces available by default (as e.g. FreeBSD does) and not |
|
|
1723 | optional. Libev cannot simply switch on large file support because it has |
|
|
1724 | to exchange stat structures with application programs compiled using the |
|
|
1725 | default compilation environment. |
1674 | |
1726 | |
1675 | =head3 Inotify |
1727 | =head3 Inotify |
1676 | |
1728 | |
1677 | When C<inotify (7)> support has been compiled into libev (generally only |
1729 | When C<inotify (7)> support has been compiled into libev (generally only |
1678 | available on Linux) and present at runtime, it will be used to speed up |
1730 | available on Linux) and present at runtime, it will be used to speed up |
… | |
… | |
2648 | L<http://rev.rubyforge.org/>. |
2700 | L<http://rev.rubyforge.org/>. |
2649 | |
2701 | |
2650 | =item D |
2702 | =item D |
2651 | |
2703 | |
2652 | Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to |
2704 | Leandro Lucarella has written a D language binding (F<ev.d>) for libev, to |
2653 | be found at L<http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. |
2705 | be found at L<http://proj.llucax.com.ar/wiki/evd>. |
2654 | |
2706 | |
2655 | =back |
2707 | =back |
2656 | |
2708 | |
2657 | |
2709 | |
2658 | =head1 MACRO MAGIC |
2710 | =head1 MACRO MAGIC |
… | |
… | |
3182 | parallel from multiple threads, calls with the same loop parameter must be |
3234 | parallel from multiple threads, calls with the same loop parameter must be |
3183 | done serially (but can be done from different threads, as long as only one |
3235 | done serially (but can be done from different threads, as long as only one |
3184 | thread ever is inside a call at any point in time, e.g. by using a mutex |
3236 | thread ever is inside a call at any point in time, e.g. by using a mutex |
3185 | per loop). |
3237 | per loop). |
3186 | |
3238 | |
3187 | If you want to know which design is best for your problem, then I cannot |
3239 | If you want to know which design (one loop, locking, or multiple loops |
3188 | help you but by giving some generic advice: |
3240 | without or something else still) is best for your problem, then I cannot |
|
|
3241 | help you. I can give some generic advice however: |
3189 | |
3242 | |
3190 | =over 4 |
3243 | =over 4 |
3191 | |
3244 | |
3192 | =item * most applications have a main thread: use the default libev loop |
3245 | =item * most applications have a main thread: use the default libev loop |
3193 | in that thread, or create a separate thread running only the default loop. |
3246 | in that thread, or create a separate thread running only the default loop. |
… | |
… | |
3326 | more than a hundred or so sockets, then likely it needs to use a totally |
3379 | more than a hundred or so sockets, then likely it needs to use a totally |
3327 | different implementation for windows, as libev offers the POSIX readiness |
3380 | different implementation for windows, as libev offers the POSIX readiness |
3328 | notification model, which cannot be implemented efficiently on windows |
3381 | notification model, which cannot be implemented efficiently on windows |
3329 | (Microsoft monopoly games). |
3382 | (Microsoft monopoly games). |
3330 | |
3383 | |
|
|
3384 | A typical way to use libev under windows is to embed it (see the embedding |
|
|
3385 | section for details) and use the following F<evwrap.h> header file instead |
|
|
3386 | of F<ev.h>: |
|
|
3387 | |
|
|
3388 | #define EV_STANDALONE /* keeps ev from requiring config.h */ |
|
|
3389 | #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */ |
|
|
3390 | |
|
|
3391 | #include "ev.h" |
|
|
3392 | |
|
|
3393 | And compile the following F<evwrap.c> file into your project (make sure |
|
|
3394 | you do I<not> compile the F<ev.c> or any other embedded soruce files!): |
|
|
3395 | |
|
|
3396 | #include "evwrap.h" |
|
|
3397 | #include "ev.c" |
|
|
3398 | |
3331 | =over 4 |
3399 | =over 4 |
3332 | |
3400 | |
3333 | =item The winsocket select function |
3401 | =item The winsocket select function |
3334 | |
3402 | |
3335 | The winsocket C<select> function doesn't follow POSIX in that it |
3403 | The winsocket C<select> function doesn't follow POSIX in that it |
3336 | requires socket I<handles> and not socket I<file descriptors> (it is |
3404 | requires socket I<handles> and not socket I<file descriptors> (it is |
3337 | also extremely buggy). This makes select very inefficient, and also |
3405 | also extremely buggy). This makes select very inefficient, and also |
3338 | requires a mapping from file descriptors to socket handles. See the |
3406 | requires a mapping from file descriptors to socket handles (the Microsoft |
|
|
3407 | C runtime provides the function C<_open_osfhandle> for this). See the |
3339 | discussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and |
3408 | discussion of the C<EV_SELECT_USE_FD_SET>, C<EV_SELECT_IS_WINSOCKET> and |
3340 | C<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info. |
3409 | C<EV_FD_TO_WIN32_HANDLE> preprocessor symbols for more info. |
3341 | |
3410 | |
3342 | The configuration for a "naked" win32 using the Microsoft runtime |
3411 | The configuration for a "naked" win32 using the Microsoft runtime |
3343 | libraries and raw winsocket select is: |
3412 | libraries and raw winsocket select is: |