… | |
… | |
3 | libev - a high performance full-featured event loop written in C |
3 | libev - a high performance full-featured event loop written in C |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | #include <ev.h> |
7 | #include <ev.h> |
|
|
8 | |
|
|
9 | =head1 EXAMPLE PROGRAM |
|
|
10 | |
|
|
11 | #include <ev.h> |
|
|
12 | |
|
|
13 | ev_io stdin_watcher; |
|
|
14 | ev_timer timeout_watcher; |
|
|
15 | |
|
|
16 | /* called when data readable on stdin */ |
|
|
17 | static void |
|
|
18 | stdin_cb (EV_P_ struct ev_io *w, int revents) |
|
|
19 | { |
|
|
20 | /* puts ("stdin ready"); */ |
|
|
21 | ev_io_stop (EV_A_ w); /* just a syntax example */ |
|
|
22 | ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ |
|
|
23 | } |
|
|
24 | |
|
|
25 | static void |
|
|
26 | timeout_cb (EV_P_ struct ev_timer *w, int revents) |
|
|
27 | { |
|
|
28 | /* puts ("timeout"); */ |
|
|
29 | ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ |
|
|
30 | } |
|
|
31 | |
|
|
32 | int |
|
|
33 | main (void) |
|
|
34 | { |
|
|
35 | struct ev_loop *loop = ev_default_loop (0); |
|
|
36 | |
|
|
37 | /* initialise an io watcher, then start it */ |
|
|
38 | ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); |
|
|
39 | ev_io_start (loop, &stdin_watcher); |
|
|
40 | |
|
|
41 | /* simple non-repeating 5.5 second timeout */ |
|
|
42 | ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); |
|
|
43 | ev_timer_start (loop, &timeout_watcher); |
|
|
44 | |
|
|
45 | /* loop till timeout or data ready */ |
|
|
46 | ev_loop (loop, 0); |
|
|
47 | |
|
|
48 | return 0; |
|
|
49 | } |
8 | |
50 | |
9 | =head1 DESCRIPTION |
51 | =head1 DESCRIPTION |
10 | |
52 | |
11 | Libev is an event loop: you register interest in certain events (such as a |
53 | Libev is an event loop: you register interest in certain events (such as a |
12 | file descriptor being readable or a timeout occuring), and it will manage |
54 | file descriptor being readable or a timeout occuring), and it will manage |
… | |
… | |
21 | details of the event, and then hand it over to libev by I<starting> the |
63 | details of the event, and then hand it over to libev by I<starting> the |
22 | watcher. |
64 | watcher. |
23 | |
65 | |
24 | =head1 FEATURES |
66 | =head1 FEATURES |
25 | |
67 | |
26 | Libev supports select, poll, the linux-specific epoll and the bsd-specific |
68 | Libev supports C<select>, C<poll>, the linux-specific C<epoll>, the |
27 | kqueue mechanisms for file descriptor events, relative timers, absolute |
69 | bsd-specific C<kqueue> and the solaris-specific event port mechanisms |
28 | timers with customised rescheduling, signal events, process status change |
70 | for file descriptor events (C<ev_io>), relative timers (C<ev_timer>), |
29 | events (related to SIGCHLD), and event watchers dealing with the event |
71 | absolute timers with customised rescheduling (C<ev_periodic>), synchronous |
30 | loop mechanism itself (idle, prepare and check watchers). It also is quite |
72 | signals (C<ev_signal>), process status change events (C<ev_child>), and |
|
|
73 | event watchers dealing with the event loop mechanism itself (C<ev_idle>, |
|
|
74 | C<ev_embed>, C<ev_prepare> and C<ev_check> watchers) as well as |
|
|
75 | file watchers (C<ev_stat>) and even limited support for fork events |
|
|
76 | (C<ev_fork>). |
|
|
77 | |
|
|
78 | It also is quite fast (see this |
31 | fast (see this L<benchmark|http://libev.schmorp.de/bench.html> comparing |
79 | L<benchmark|http://libev.schmorp.de/bench.html> comparing it to libevent |
32 | it to libevent for example). |
80 | for example). |
33 | |
81 | |
34 | =head1 CONVENTIONS |
82 | =head1 CONVENTIONS |
35 | |
83 | |
36 | Libev is very configurable. In this manual the default configuration |
84 | Libev is very configurable. In this manual the default configuration will |
37 | will be described, which supports multiple event loops. For more info |
85 | be described, which supports multiple event loops. For more info about |
38 | about various configuration options please have a look at the file |
86 | various configuration options please have a look at B<EMBED> section in |
39 | F<README.embed> in the libev distribution. If libev was configured without |
87 | this manual. If libev was configured without support for multiple event |
40 | support for multiple event loops, then all functions taking an initial |
88 | loops, then all functions taking an initial argument of name C<loop> |
41 | argument of name C<loop> (which is always of type C<struct ev_loop *>) |
89 | (which is always of type C<struct ev_loop *>) will not have this argument. |
42 | will not have this argument. |
|
|
43 | |
90 | |
44 | =head1 TIME REPRESENTATION |
91 | =head1 TIME REPRESENTATION |
45 | |
92 | |
46 | Libev represents time as a single floating point number, representing the |
93 | Libev represents time as a single floating point number, representing the |
47 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
94 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
48 | the beginning of 1970, details are complicated, don't ask). This type is |
95 | the beginning of 1970, details are complicated, don't ask). This type is |
49 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
96 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
50 | to the C<double> type in C, and when you need to do any calculations on |
97 | to the C<double> type in C, and when you need to do any calculations on |
51 | it, you should treat it as such. |
98 | it, you should treat it as such. |
52 | |
99 | |
53 | |
|
|
54 | =head1 GLOBAL FUNCTIONS |
100 | =head1 GLOBAL FUNCTIONS |
55 | |
101 | |
56 | These functions can be called anytime, even before initialising the |
102 | These functions can be called anytime, even before initialising the |
57 | library in any way. |
103 | library in any way. |
58 | |
104 | |
… | |
… | |
77 | Usually, it's a good idea to terminate if the major versions mismatch, |
123 | Usually, it's a good idea to terminate if the major versions mismatch, |
78 | as this indicates an incompatible change. Minor versions are usually |
124 | as this indicates an incompatible change. Minor versions are usually |
79 | compatible to older versions, so a larger minor version alone is usually |
125 | compatible to older versions, so a larger minor version alone is usually |
80 | not a problem. |
126 | not a problem. |
81 | |
127 | |
82 | Example: make sure we haven't accidentally been linked against the wrong |
128 | Example: Make sure we haven't accidentally been linked against the wrong |
83 | version: |
129 | version. |
84 | |
130 | |
85 | assert (("libev version mismatch", |
131 | assert (("libev version mismatch", |
86 | ev_version_major () == EV_VERSION_MAJOR |
132 | ev_version_major () == EV_VERSION_MAJOR |
87 | && ev_version_minor () >= EV_VERSION_MINOR)); |
133 | && ev_version_minor () >= EV_VERSION_MINOR)); |
88 | |
134 | |
… | |
… | |
116 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
162 | C<ev_embeddable_backends () & ev_supported_backends ()>, likewise for |
117 | recommended ones. |
163 | recommended ones. |
118 | |
164 | |
119 | See the description of C<ev_embed> watchers for more info. |
165 | See the description of C<ev_embed> watchers for more info. |
120 | |
166 | |
121 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
167 | =item ev_set_allocator (void *(*cb)(void *ptr, size_t size)) |
122 | |
168 | |
123 | Sets the allocation function to use (the prototype is similar to the |
169 | Sets the allocation function to use (the prototype and semantics are |
124 | realloc C function, the semantics are identical). It is used to allocate |
170 | identical to the realloc C function). It is used to allocate and free |
125 | and free memory (no surprises here). If it returns zero when memory |
171 | memory (no surprises here). If it returns zero when memory needs to be |
126 | needs to be allocated, the library might abort or take some potentially |
172 | allocated, the library might abort or take some potentially destructive |
127 | destructive action. The default is your system realloc function. |
173 | action. The default is your system realloc function. |
128 | |
174 | |
129 | You could override this function in high-availability programs to, say, |
175 | You could override this function in high-availability programs to, say, |
130 | free some memory if it cannot allocate memory, to use a special allocator, |
176 | free some memory if it cannot allocate memory, to use a special allocator, |
131 | or even to sleep a while and retry until some memory is available. |
177 | or even to sleep a while and retry until some memory is available. |
132 | |
178 | |
133 | Example: replace the libev allocator with one that waits a bit and then |
179 | Example: Replace the libev allocator with one that waits a bit and then |
134 | retries: better than mine). |
180 | retries). |
135 | |
181 | |
136 | static void * |
182 | static void * |
137 | persistent_realloc (void *ptr, long size) |
183 | persistent_realloc (void *ptr, size_t size) |
138 | { |
184 | { |
139 | for (;;) |
185 | for (;;) |
140 | { |
186 | { |
141 | void *newptr = realloc (ptr, size); |
187 | void *newptr = realloc (ptr, size); |
142 | |
188 | |
… | |
… | |
158 | callback is set, then libev will expect it to remedy the sitution, no |
204 | callback is set, then libev will expect it to remedy the sitution, no |
159 | matter what, when it returns. That is, libev will generally retry the |
205 | matter what, when it returns. That is, libev will generally retry the |
160 | requested operation, or, if the condition doesn't go away, do bad stuff |
206 | requested operation, or, if the condition doesn't go away, do bad stuff |
161 | (such as abort). |
207 | (such as abort). |
162 | |
208 | |
163 | Example: do the same thing as libev does internally: |
209 | Example: This is basically the same thing that libev does internally, too. |
164 | |
210 | |
165 | static void |
211 | static void |
166 | fatal_error (const char *msg) |
212 | fatal_error (const char *msg) |
167 | { |
213 | { |
168 | perror (msg); |
214 | perror (msg); |
… | |
… | |
314 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
360 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
315 | always distinct from the default loop. Unlike the default loop, it cannot |
361 | always distinct from the default loop. Unlike the default loop, it cannot |
316 | handle signal and child watchers, and attempts to do so will be greeted by |
362 | handle signal and child watchers, and attempts to do so will be greeted by |
317 | undefined behaviour (or a failed assertion if assertions are enabled). |
363 | undefined behaviour (or a failed assertion if assertions are enabled). |
318 | |
364 | |
319 | Example: try to create a event loop that uses epoll and nothing else. |
365 | Example: Try to create a event loop that uses epoll and nothing else. |
320 | |
366 | |
321 | struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
367 | struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
322 | if (!epoller) |
368 | if (!epoller) |
323 | fatal ("no epoll found here, maybe it hides under your chair"); |
369 | fatal ("no epoll found here, maybe it hides under your chair"); |
324 | |
370 | |
… | |
… | |
423 | Signals and child watchers are implemented as I/O watchers, and will |
469 | Signals and child watchers are implemented as I/O watchers, and will |
424 | be handled here by queueing them when their watcher gets executed. |
470 | be handled here by queueing them when their watcher gets executed. |
425 | - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
471 | - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
426 | were used, return, otherwise continue with step *. |
472 | were used, return, otherwise continue with step *. |
427 | |
473 | |
428 | Example: queue some jobs and then loop until no events are outsanding |
474 | Example: Queue some jobs and then loop until no events are outsanding |
429 | anymore. |
475 | anymore. |
430 | |
476 | |
431 | ... queue jobs here, make sure they register event watchers as long |
477 | ... queue jobs here, make sure they register event watchers as long |
432 | ... as they still have work to do (even an idle watcher will do..) |
478 | ... as they still have work to do (even an idle watcher will do..) |
433 | ev_loop (my_loop, 0); |
479 | ev_loop (my_loop, 0); |
… | |
… | |
453 | visible to the libev user and should not keep C<ev_loop> from exiting if |
499 | visible to the libev user and should not keep C<ev_loop> from exiting if |
454 | no event watchers registered by it are active. It is also an excellent |
500 | no event watchers registered by it are active. It is also an excellent |
455 | way to do this for generic recurring timers or from within third-party |
501 | way to do this for generic recurring timers or from within third-party |
456 | libraries. Just remember to I<unref after start> and I<ref before stop>. |
502 | libraries. Just remember to I<unref after start> and I<ref before stop>. |
457 | |
503 | |
458 | Example: create a signal watcher, but keep it from keeping C<ev_loop> |
504 | Example: Create a signal watcher, but keep it from keeping C<ev_loop> |
459 | running when nothing else is active. |
505 | running when nothing else is active. |
460 | |
506 | |
461 | struct dv_signal exitsig; |
507 | struct ev_signal exitsig; |
462 | ev_signal_init (&exitsig, sig_cb, SIGINT); |
508 | ev_signal_init (&exitsig, sig_cb, SIGINT); |
463 | ev_signal_start (myloop, &exitsig); |
509 | ev_signal_start (loop, &exitsig); |
464 | evf_unref (myloop); |
510 | evf_unref (loop); |
465 | |
511 | |
466 | Example: for some weird reason, unregister the above signal handler again. |
512 | Example: For some weird reason, unregister the above signal handler again. |
467 | |
513 | |
468 | ev_ref (myloop); |
514 | ev_ref (loop); |
469 | ev_signal_stop (myloop, &exitsig); |
515 | ev_signal_stop (loop, &exitsig); |
470 | |
516 | |
471 | =back |
517 | =back |
472 | |
518 | |
473 | |
519 | |
474 | =head1 ANATOMY OF A WATCHER |
520 | =head1 ANATOMY OF A WATCHER |
… | |
… | |
544 | The signal specified in the C<ev_signal> watcher has been received by a thread. |
590 | The signal specified in the C<ev_signal> watcher has been received by a thread. |
545 | |
591 | |
546 | =item C<EV_CHILD> |
592 | =item C<EV_CHILD> |
547 | |
593 | |
548 | The pid specified in the C<ev_child> watcher has received a status change. |
594 | The pid specified in the C<ev_child> watcher has received a status change. |
|
|
595 | |
|
|
596 | =item C<EV_STAT> |
|
|
597 | |
|
|
598 | The path specified in the C<ev_stat> watcher changed its attributes somehow. |
549 | |
599 | |
550 | =item C<EV_IDLE> |
600 | =item C<EV_IDLE> |
551 | |
601 | |
552 | The C<ev_idle> watcher has determined that you have nothing better to do. |
602 | The C<ev_idle> watcher has determined that you have nothing better to do. |
553 | |
603 | |
… | |
… | |
561 | received events. Callbacks of both watcher types can start and stop as |
611 | received events. Callbacks of both watcher types can start and stop as |
562 | many watchers as they want, and all of them will be taken into account |
612 | many watchers as they want, and all of them will be taken into account |
563 | (for example, a C<ev_prepare> watcher might start an idle watcher to keep |
613 | (for example, a C<ev_prepare> watcher might start an idle watcher to keep |
564 | C<ev_loop> from blocking). |
614 | C<ev_loop> from blocking). |
565 | |
615 | |
|
|
616 | =item C<EV_EMBED> |
|
|
617 | |
|
|
618 | The embedded event loop specified in the C<ev_embed> watcher needs attention. |
|
|
619 | |
|
|
620 | =item C<EV_FORK> |
|
|
621 | |
|
|
622 | The event loop has been resumed in the child process after fork (see |
|
|
623 | C<ev_fork>). |
|
|
624 | |
566 | =item C<EV_ERROR> |
625 | =item C<EV_ERROR> |
567 | |
626 | |
568 | An unspecified error has occured, the watcher has been stopped. This might |
627 | An unspecified error has occured, the watcher has been stopped. This might |
569 | happen because the watcher could not be properly started because libev |
628 | happen because the watcher could not be properly started because libev |
570 | ran out of memory, a file descriptor was found to be closed or any other |
629 | ran out of memory, a file descriptor was found to be closed or any other |
… | |
… | |
689 | |
748 | |
690 | |
749 | |
691 | =head1 WATCHER TYPES |
750 | =head1 WATCHER TYPES |
692 | |
751 | |
693 | This section describes each watcher in detail, but will not repeat |
752 | This section describes each watcher in detail, but will not repeat |
694 | information given in the last section. |
753 | information given in the last section. Any initialisation/set macros, |
|
|
754 | functions and members specific to the watcher type are explained. |
|
|
755 | |
|
|
756 | Members are additionally marked with either I<[read-only]>, meaning that, |
|
|
757 | while the watcher is active, you can look at the member and expect some |
|
|
758 | sensible content, but you must not modify it (you can modify it while the |
|
|
759 | watcher is stopped to your hearts content), or I<[read-write]>, which |
|
|
760 | means you can expect it to have some sensible content while the watcher |
|
|
761 | is active, but you can also modify it. Modifying it may not do something |
|
|
762 | sensible or take immediate effect (or do anything at all), but libev will |
|
|
763 | not crash or malfunction in any way. |
695 | |
764 | |
696 | |
765 | |
697 | =head2 C<ev_io> - is this file descriptor readable or writable? |
766 | =head2 C<ev_io> - is this file descriptor readable or writable? |
698 | |
767 | |
699 | I/O watchers check whether a file descriptor is readable or writable |
768 | I/O watchers check whether a file descriptor is readable or writable |
… | |
… | |
742 | |
811 | |
743 | Configures an C<ev_io> watcher. The C<fd> is the file descriptor to |
812 | Configures an C<ev_io> watcher. The C<fd> is the file descriptor to |
744 | rceeive events for and events is either C<EV_READ>, C<EV_WRITE> or |
813 | rceeive events for and events is either C<EV_READ>, C<EV_WRITE> or |
745 | C<EV_READ | EV_WRITE> to receive the given events. |
814 | C<EV_READ | EV_WRITE> to receive the given events. |
746 | |
815 | |
|
|
816 | =item int fd [read-only] |
|
|
817 | |
|
|
818 | The file descriptor being watched. |
|
|
819 | |
|
|
820 | =item int events [read-only] |
|
|
821 | |
|
|
822 | The events being watched. |
|
|
823 | |
747 | =back |
824 | =back |
748 | |
825 | |
749 | Example: call C<stdin_readable_cb> when STDIN_FILENO has become, well |
826 | Example: Call C<stdin_readable_cb> when STDIN_FILENO has become, well |
750 | readable, but only once. Since it is likely line-buffered, you could |
827 | readable, but only once. Since it is likely line-buffered, you could |
751 | attempt to read a whole line in the callback: |
828 | attempt to read a whole line in the callback. |
752 | |
829 | |
753 | static void |
830 | static void |
754 | stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
831 | stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
755 | { |
832 | { |
756 | ev_io_stop (loop, w); |
833 | ev_io_stop (loop, w); |
… | |
… | |
814 | |
891 | |
815 | If the timer is repeating, either start it if necessary (with the repeat |
892 | If the timer is repeating, either start it if necessary (with the repeat |
816 | value), or reset the running timer to the repeat value. |
893 | value), or reset the running timer to the repeat value. |
817 | |
894 | |
818 | This sounds a bit complicated, but here is a useful and typical |
895 | This sounds a bit complicated, but here is a useful and typical |
819 | example: Imagine you have a tcp connection and you want a so-called idle |
896 | example: Imagine you have a tcp connection and you want a so-called |
820 | timeout, that is, you want to be called when there have been, say, 60 |
897 | idle timeout, that is, you want to be called when there have been, |
821 | seconds of inactivity on the socket. The easiest way to do this is to |
898 | say, 60 seconds of inactivity on the socket. The easiest way to do |
822 | configure an C<ev_timer> with after=repeat=60 and calling ev_timer_again each |
899 | this is to configure an C<ev_timer> with C<after>=C<repeat>=C<60> and calling |
823 | time you successfully read or write some data. If you go into an idle |
900 | C<ev_timer_again> each time you successfully read or write some data. If |
824 | state where you do not expect data to travel on the socket, you can stop |
901 | you go into an idle state where you do not expect data to travel on the |
825 | the timer, and again will automatically restart it if need be. |
902 | socket, you can stop the timer, and again will automatically restart it if |
|
|
903 | need be. |
|
|
904 | |
|
|
905 | You can also ignore the C<after> value and C<ev_timer_start> altogether |
|
|
906 | and only ever use the C<repeat> value: |
|
|
907 | |
|
|
908 | ev_timer_init (timer, callback, 0., 5.); |
|
|
909 | ev_timer_again (loop, timer); |
|
|
910 | ... |
|
|
911 | timer->again = 17.; |
|
|
912 | ev_timer_again (loop, timer); |
|
|
913 | ... |
|
|
914 | timer->again = 10.; |
|
|
915 | ev_timer_again (loop, timer); |
|
|
916 | |
|
|
917 | This is more efficient then stopping/starting the timer eahc time you want |
|
|
918 | to modify its timeout value. |
|
|
919 | |
|
|
920 | =item ev_tstamp repeat [read-write] |
|
|
921 | |
|
|
922 | The current C<repeat> value. Will be used each time the watcher times out |
|
|
923 | or C<ev_timer_again> is called and determines the next timeout (if any), |
|
|
924 | which is also when any modifications are taken into account. |
826 | |
925 | |
827 | =back |
926 | =back |
828 | |
927 | |
829 | Example: create a timer that fires after 60 seconds. |
928 | Example: Create a timer that fires after 60 seconds. |
830 | |
929 | |
831 | static void |
930 | static void |
832 | one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
931 | one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
833 | { |
932 | { |
834 | .. one minute over, w is actually stopped right here |
933 | .. one minute over, w is actually stopped right here |
… | |
… | |
836 | |
935 | |
837 | struct ev_timer mytimer; |
936 | struct ev_timer mytimer; |
838 | ev_timer_init (&mytimer, one_minute_cb, 60., 0.); |
937 | ev_timer_init (&mytimer, one_minute_cb, 60., 0.); |
839 | ev_timer_start (loop, &mytimer); |
938 | ev_timer_start (loop, &mytimer); |
840 | |
939 | |
841 | Example: create a timeout timer that times out after 10 seconds of |
940 | Example: Create a timeout timer that times out after 10 seconds of |
842 | inactivity. |
941 | inactivity. |
843 | |
942 | |
844 | static void |
943 | static void |
845 | timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
944 | timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
846 | { |
945 | { |
… | |
… | |
957 | Simply stops and restarts the periodic watcher again. This is only useful |
1056 | Simply stops and restarts the periodic watcher again. This is only useful |
958 | when you changed some parameters or the reschedule callback would return |
1057 | when you changed some parameters or the reschedule callback would return |
959 | a different time than the last time it was called (e.g. in a crond like |
1058 | a different time than the last time it was called (e.g. in a crond like |
960 | program when the crontabs have changed). |
1059 | program when the crontabs have changed). |
961 | |
1060 | |
|
|
1061 | =item ev_tstamp interval [read-write] |
|
|
1062 | |
|
|
1063 | The current interval value. Can be modified any time, but changes only |
|
|
1064 | take effect when the periodic timer fires or C<ev_periodic_again> is being |
|
|
1065 | called. |
|
|
1066 | |
|
|
1067 | =item ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write] |
|
|
1068 | |
|
|
1069 | The current reschedule callback, or C<0>, if this functionality is |
|
|
1070 | switched off. Can be changed any time, but changes only take effect when |
|
|
1071 | the periodic timer fires or C<ev_periodic_again> is being called. |
|
|
1072 | |
962 | =back |
1073 | =back |
963 | |
1074 | |
964 | Example: call a callback every hour, or, more precisely, whenever the |
1075 | Example: Call a callback every hour, or, more precisely, whenever the |
965 | system clock is divisible by 3600. The callback invocation times have |
1076 | system clock is divisible by 3600. The callback invocation times have |
966 | potentially a lot of jittering, but good long-term stability. |
1077 | potentially a lot of jittering, but good long-term stability. |
967 | |
1078 | |
968 | static void |
1079 | static void |
969 | clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
1080 | clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
… | |
… | |
973 | |
1084 | |
974 | struct ev_periodic hourly_tick; |
1085 | struct ev_periodic hourly_tick; |
975 | ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); |
1086 | ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); |
976 | ev_periodic_start (loop, &hourly_tick); |
1087 | ev_periodic_start (loop, &hourly_tick); |
977 | |
1088 | |
978 | Example: the same as above, but use a reschedule callback to do it: |
1089 | Example: The same as above, but use a reschedule callback to do it: |
979 | |
1090 | |
980 | #include <math.h> |
1091 | #include <math.h> |
981 | |
1092 | |
982 | static ev_tstamp |
1093 | static ev_tstamp |
983 | my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
1094 | my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
… | |
… | |
985 | return fmod (now, 3600.) + 3600.; |
1096 | return fmod (now, 3600.) + 3600.; |
986 | } |
1097 | } |
987 | |
1098 | |
988 | ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
1099 | ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
989 | |
1100 | |
990 | Example: call a callback every hour, starting now: |
1101 | Example: Call a callback every hour, starting now: |
991 | |
1102 | |
992 | struct ev_periodic hourly_tick; |
1103 | struct ev_periodic hourly_tick; |
993 | ev_periodic_init (&hourly_tick, clock_cb, |
1104 | ev_periodic_init (&hourly_tick, clock_cb, |
994 | fmod (ev_now (loop), 3600.), 3600., 0); |
1105 | fmod (ev_now (loop), 3600.), 3600., 0); |
995 | ev_periodic_start (loop, &hourly_tick); |
1106 | ev_periodic_start (loop, &hourly_tick); |
… | |
… | |
1016 | =item ev_signal_set (ev_signal *, int signum) |
1127 | =item ev_signal_set (ev_signal *, int signum) |
1017 | |
1128 | |
1018 | Configures the watcher to trigger on the given signal number (usually one |
1129 | Configures the watcher to trigger on the given signal number (usually one |
1019 | of the C<SIGxxx> constants). |
1130 | of the C<SIGxxx> constants). |
1020 | |
1131 | |
|
|
1132 | =item int signum [read-only] |
|
|
1133 | |
|
|
1134 | The signal the watcher watches out for. |
|
|
1135 | |
1021 | =back |
1136 | =back |
1022 | |
1137 | |
1023 | |
1138 | |
1024 | =head2 C<ev_child> - watch out for process status changes |
1139 | =head2 C<ev_child> - watch out for process status changes |
1025 | |
1140 | |
… | |
… | |
1037 | at the C<rstatus> member of the C<ev_child> watcher structure to see |
1152 | at the C<rstatus> member of the C<ev_child> watcher structure to see |
1038 | the status word (use the macros from C<sys/wait.h> and see your systems |
1153 | the status word (use the macros from C<sys/wait.h> and see your systems |
1039 | C<waitpid> documentation). The C<rpid> member contains the pid of the |
1154 | C<waitpid> documentation). The C<rpid> member contains the pid of the |
1040 | process causing the status change. |
1155 | process causing the status change. |
1041 | |
1156 | |
|
|
1157 | =item int pid [read-only] |
|
|
1158 | |
|
|
1159 | The process id this watcher watches out for, or C<0>, meaning any process id. |
|
|
1160 | |
|
|
1161 | =item int rpid [read-write] |
|
|
1162 | |
|
|
1163 | The process id that detected a status change. |
|
|
1164 | |
|
|
1165 | =item int rstatus [read-write] |
|
|
1166 | |
|
|
1167 | The process exit/trace status caused by C<rpid> (see your systems |
|
|
1168 | C<waitpid> and C<sys/wait.h> documentation for details). |
|
|
1169 | |
1042 | =back |
1170 | =back |
1043 | |
1171 | |
1044 | Example: try to exit cleanly on SIGINT and SIGTERM. |
1172 | Example: Try to exit cleanly on SIGINT and SIGTERM. |
1045 | |
1173 | |
1046 | static void |
1174 | static void |
1047 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1175 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
1048 | { |
1176 | { |
1049 | ev_unloop (loop, EVUNLOOP_ALL); |
1177 | ev_unloop (loop, EVUNLOOP_ALL); |
1050 | } |
1178 | } |
1051 | |
1179 | |
1052 | struct ev_signal signal_watcher; |
1180 | struct ev_signal signal_watcher; |
1053 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1181 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
1054 | ev_signal_start (loop, &sigint_cb); |
1182 | ev_signal_start (loop, &sigint_cb); |
|
|
1183 | |
|
|
1184 | |
|
|
1185 | =head2 C<ev_stat> - did the file attributes just change? |
|
|
1186 | |
|
|
1187 | This watches a filesystem path for attribute changes. That is, it calls |
|
|
1188 | C<stat> regularly (or when the OS says it changed) and sees if it changed |
|
|
1189 | compared to the last time, invoking the callback if it did. |
|
|
1190 | |
|
|
1191 | The path does not need to exist: changing from "path exists" to "path does |
|
|
1192 | not exist" is a status change like any other. The condition "path does |
|
|
1193 | not exist" is signified by the C<st_nlink> field being zero (which is |
|
|
1194 | otherwise always forced to be at least one) and all the other fields of |
|
|
1195 | the stat buffer having unspecified contents. |
|
|
1196 | |
|
|
1197 | Since there is no standard to do this, the portable implementation simply |
|
|
1198 | calls C<stat (2)> regulalry on the path to see if it changed somehow. You |
|
|
1199 | can specify a recommended polling interval for this case. If you specify |
|
|
1200 | a polling interval of C<0> (highly recommended!) then a I<suitable, |
|
|
1201 | unspecified default> value will be used (which you can expect to be around |
|
|
1202 | five seconds, although this might change dynamically). Libev will also |
|
|
1203 | impose a minimum interval which is currently around C<0.1>, but thats |
|
|
1204 | usually overkill. |
|
|
1205 | |
|
|
1206 | This watcher type is not meant for massive numbers of stat watchers, |
|
|
1207 | as even with OS-supported change notifications, this can be |
|
|
1208 | resource-intensive. |
|
|
1209 | |
|
|
1210 | At the time of this writing, no specific OS backends are implemented, but |
|
|
1211 | if demand increases, at least a kqueue and inotify backend will be added. |
|
|
1212 | |
|
|
1213 | =over 4 |
|
|
1214 | |
|
|
1215 | =item ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval) |
|
|
1216 | |
|
|
1217 | =item ev_stat_set (ev_stat *, const char *path, ev_tstamp interval) |
|
|
1218 | |
|
|
1219 | Configures the watcher to wait for status changes of the given |
|
|
1220 | C<path>. The C<interval> is a hint on how quickly a change is expected to |
|
|
1221 | be detected and should normally be specified as C<0> to let libev choose |
|
|
1222 | a suitable value. The memory pointed to by C<path> must point to the same |
|
|
1223 | path for as long as the watcher is active. |
|
|
1224 | |
|
|
1225 | The callback will be receive C<EV_STAT> when a change was detected, |
|
|
1226 | relative to the attributes at the time the watcher was started (or the |
|
|
1227 | last change was detected). |
|
|
1228 | |
|
|
1229 | =item ev_stat_stat (ev_stat *) |
|
|
1230 | |
|
|
1231 | Updates the stat buffer immediately with new values. If you change the |
|
|
1232 | watched path in your callback, you could call this fucntion to avoid |
|
|
1233 | detecting this change (while introducing a race condition). Can also be |
|
|
1234 | useful simply to find out the new values. |
|
|
1235 | |
|
|
1236 | =item ev_statdata attr [read-only] |
|
|
1237 | |
|
|
1238 | The most-recently detected attributes of the file. Although the type is of |
|
|
1239 | C<ev_statdata>, this is usually the (or one of the) C<struct stat> types |
|
|
1240 | suitable for your system. If the C<st_nlink> member is C<0>, then there |
|
|
1241 | was some error while C<stat>ing the file. |
|
|
1242 | |
|
|
1243 | =item ev_statdata prev [read-only] |
|
|
1244 | |
|
|
1245 | The previous attributes of the file. The callback gets invoked whenever |
|
|
1246 | C<prev> != C<attr>. |
|
|
1247 | |
|
|
1248 | =item ev_tstamp interval [read-only] |
|
|
1249 | |
|
|
1250 | The specified interval. |
|
|
1251 | |
|
|
1252 | =item const char *path [read-only] |
|
|
1253 | |
|
|
1254 | The filesystem path that is being watched. |
|
|
1255 | |
|
|
1256 | =back |
|
|
1257 | |
|
|
1258 | Example: Watch C</etc/passwd> for attribute changes. |
|
|
1259 | |
|
|
1260 | static void |
|
|
1261 | passwd_cb (struct ev_loop *loop, ev_stat *w, int revents) |
|
|
1262 | { |
|
|
1263 | /* /etc/passwd changed in some way */ |
|
|
1264 | if (w->attr.st_nlink) |
|
|
1265 | { |
|
|
1266 | printf ("passwd current size %ld\n", (long)w->attr.st_size); |
|
|
1267 | printf ("passwd current atime %ld\n", (long)w->attr.st_mtime); |
|
|
1268 | printf ("passwd current mtime %ld\n", (long)w->attr.st_mtime); |
|
|
1269 | } |
|
|
1270 | else |
|
|
1271 | /* you shalt not abuse printf for puts */ |
|
|
1272 | puts ("wow, /etc/passwd is not there, expect problems. " |
|
|
1273 | "if this is windows, they already arrived\n"); |
|
|
1274 | } |
|
|
1275 | |
|
|
1276 | ... |
|
|
1277 | ev_stat passwd; |
|
|
1278 | |
|
|
1279 | ev_stat_init (&passwd, passwd_cb, "/etc/passwd"); |
|
|
1280 | ev_stat_start (loop, &passwd); |
1055 | |
1281 | |
1056 | |
1282 | |
1057 | =head2 C<ev_idle> - when you've got nothing better to do... |
1283 | =head2 C<ev_idle> - when you've got nothing better to do... |
1058 | |
1284 | |
1059 | Idle watchers trigger events when there are no other events are pending |
1285 | Idle watchers trigger events when there are no other events are pending |
… | |
… | |
1080 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
1306 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
1081 | believe me. |
1307 | believe me. |
1082 | |
1308 | |
1083 | =back |
1309 | =back |
1084 | |
1310 | |
1085 | Example: dynamically allocate an C<ev_idle>, start it, and in the |
1311 | Example: Dynamically allocate an C<ev_idle> watcher, start it, and in the |
1086 | callback, free it. Alos, use no error checking, as usual. |
1312 | callback, free it. Also, use no error checking, as usual. |
1087 | |
1313 | |
1088 | static void |
1314 | static void |
1089 | idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) |
1315 | idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) |
1090 | { |
1316 | { |
1091 | free (w); |
1317 | free (w); |
… | |
… | |
1102 | |
1328 | |
1103 | Prepare and check watchers are usually (but not always) used in tandem: |
1329 | Prepare and check watchers are usually (but not always) used in tandem: |
1104 | prepare watchers get invoked before the process blocks and check watchers |
1330 | prepare watchers get invoked before the process blocks and check watchers |
1105 | afterwards. |
1331 | afterwards. |
1106 | |
1332 | |
|
|
1333 | You I<must not> call C<ev_loop> or similar functions that enter |
|
|
1334 | the current event loop from either C<ev_prepare> or C<ev_check> |
|
|
1335 | watchers. Other loops than the current one are fine, however. The |
|
|
1336 | rationale behind this is that you do not need to check for recursion in |
|
|
1337 | those watchers, i.e. the sequence will always be C<ev_prepare>, blocking, |
|
|
1338 | C<ev_check> so if you have one watcher of each kind they will always be |
|
|
1339 | called in pairs bracketing the blocking call. |
|
|
1340 | |
1107 | Their main purpose is to integrate other event mechanisms into libev and |
1341 | Their main purpose is to integrate other event mechanisms into libev and |
1108 | their use is somewhat advanced. This could be used, for example, to track |
1342 | their use is somewhat advanced. This could be used, for example, to track |
1109 | variable changes, implement your own watchers, integrate net-snmp or a |
1343 | variable changes, implement your own watchers, integrate net-snmp or a |
1110 | coroutine library and lots more. |
1344 | coroutine library and lots more. They are also occasionally useful if |
|
|
1345 | you cache some data and want to flush it before blocking (for example, |
|
|
1346 | in X programs you might want to do an C<XFlush ()> in an C<ev_prepare> |
|
|
1347 | watcher). |
1111 | |
1348 | |
1112 | This is done by examining in each prepare call which file descriptors need |
1349 | This is done by examining in each prepare call which file descriptors need |
1113 | to be watched by the other library, registering C<ev_io> watchers for |
1350 | to be watched by the other library, registering C<ev_io> watchers for |
1114 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
1351 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
1115 | provide just this functionality). Then, in the check watcher you check for |
1352 | provide just this functionality). Then, in the check watcher you check for |
… | |
… | |
1137 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
1374 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
1138 | macros, but using them is utterly, utterly and completely pointless. |
1375 | macros, but using them is utterly, utterly and completely pointless. |
1139 | |
1376 | |
1140 | =back |
1377 | =back |
1141 | |
1378 | |
1142 | Example: *TODO*. |
1379 | Example: To include a library such as adns, you would add IO watchers |
|
|
1380 | and a timeout watcher in a prepare handler, as required by libadns, and |
|
|
1381 | in a check watcher, destroy them and call into libadns. What follows is |
|
|
1382 | pseudo-code only of course: |
|
|
1383 | |
|
|
1384 | static ev_io iow [nfd]; |
|
|
1385 | static ev_timer tw; |
|
|
1386 | |
|
|
1387 | static void |
|
|
1388 | io_cb (ev_loop *loop, ev_io *w, int revents) |
|
|
1389 | { |
|
|
1390 | // set the relevant poll flags |
|
|
1391 | // could also call adns_processreadable etc. here |
|
|
1392 | struct pollfd *fd = (struct pollfd *)w->data; |
|
|
1393 | if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; |
|
|
1394 | if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; |
|
|
1395 | } |
|
|
1396 | |
|
|
1397 | // create io watchers for each fd and a timer before blocking |
|
|
1398 | static void |
|
|
1399 | adns_prepare_cb (ev_loop *loop, ev_prepare *w, int revents) |
|
|
1400 | { |
|
|
1401 | int timeout = 3600000;truct pollfd fds [nfd]; |
|
|
1402 | // actual code will need to loop here and realloc etc. |
|
|
1403 | adns_beforepoll (ads, fds, &nfd, &timeout, timeval_from (ev_time ())); |
|
|
1404 | |
|
|
1405 | /* the callback is illegal, but won't be called as we stop during check */ |
|
|
1406 | ev_timer_init (&tw, 0, timeout * 1e-3); |
|
|
1407 | ev_timer_start (loop, &tw); |
|
|
1408 | |
|
|
1409 | // create on ev_io per pollfd |
|
|
1410 | for (int i = 0; i < nfd; ++i) |
|
|
1411 | { |
|
|
1412 | ev_io_init (iow + i, io_cb, fds [i].fd, |
|
|
1413 | ((fds [i].events & POLLIN ? EV_READ : 0) |
|
|
1414 | | (fds [i].events & POLLOUT ? EV_WRITE : 0))); |
|
|
1415 | |
|
|
1416 | fds [i].revents = 0; |
|
|
1417 | iow [i].data = fds + i; |
|
|
1418 | ev_io_start (loop, iow + i); |
|
|
1419 | } |
|
|
1420 | } |
|
|
1421 | |
|
|
1422 | // stop all watchers after blocking |
|
|
1423 | static void |
|
|
1424 | adns_check_cb (ev_loop *loop, ev_check *w, int revents) |
|
|
1425 | { |
|
|
1426 | ev_timer_stop (loop, &tw); |
|
|
1427 | |
|
|
1428 | for (int i = 0; i < nfd; ++i) |
|
|
1429 | ev_io_stop (loop, iow + i); |
|
|
1430 | |
|
|
1431 | adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); |
|
|
1432 | } |
1143 | |
1433 | |
1144 | |
1434 | |
1145 | =head2 C<ev_embed> - when one backend isn't enough... |
1435 | =head2 C<ev_embed> - when one backend isn't enough... |
1146 | |
1436 | |
1147 | This is a rather advanced watcher type that lets you embed one event loop |
1437 | This is a rather advanced watcher type that lets you embed one event loop |
… | |
… | |
1228 | |
1518 | |
1229 | Make a single, non-blocking sweep over the embedded loop. This works |
1519 | Make a single, non-blocking sweep over the embedded loop. This works |
1230 | similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most |
1520 | similarly to C<ev_loop (embedded_loop, EVLOOP_NONBLOCK)>, but in the most |
1231 | apropriate way for embedded loops. |
1521 | apropriate way for embedded loops. |
1232 | |
1522 | |
|
|
1523 | =item struct ev_loop *loop [read-only] |
|
|
1524 | |
|
|
1525 | The embedded event loop. |
|
|
1526 | |
|
|
1527 | =back |
|
|
1528 | |
|
|
1529 | |
|
|
1530 | =head2 C<ev_fork> - the audacity to resume the event loop after a fork |
|
|
1531 | |
|
|
1532 | Fork watchers are called when a C<fork ()> was detected (usually because |
|
|
1533 | whoever is a good citizen cared to tell libev about it by calling |
|
|
1534 | C<ev_default_fork> or C<ev_loop_fork>). The invocation is done before the |
|
|
1535 | event loop blocks next and before C<ev_check> watchers are being called, |
|
|
1536 | and only in the child after the fork. If whoever good citizen calling |
|
|
1537 | C<ev_default_fork> cheats and calls it in the wrong process, the fork |
|
|
1538 | handlers will be invoked, too, of course. |
|
|
1539 | |
|
|
1540 | =over 4 |
|
|
1541 | |
|
|
1542 | =item ev_fork_init (ev_signal *, callback) |
|
|
1543 | |
|
|
1544 | Initialises and configures the fork watcher - it has no parameters of any |
|
|
1545 | kind. There is a C<ev_fork_set> macro, but using it is utterly pointless, |
|
|
1546 | believe me. |
|
|
1547 | |
1233 | =back |
1548 | =back |
1234 | |
1549 | |
1235 | |
1550 | |
1236 | =head1 OTHER FUNCTIONS |
1551 | =head1 OTHER FUNCTIONS |
1237 | |
1552 | |
… | |
… | |
1399 | |
1714 | |
1400 | =item w->sweep () C<ev::embed> only |
1715 | =item w->sweep () C<ev::embed> only |
1401 | |
1716 | |
1402 | Invokes C<ev_embed_sweep>. |
1717 | Invokes C<ev_embed_sweep>. |
1403 | |
1718 | |
|
|
1719 | =item w->update () C<ev::stat> only |
|
|
1720 | |
|
|
1721 | Invokes C<ev_stat_stat>. |
|
|
1722 | |
1404 | =back |
1723 | =back |
1405 | |
1724 | |
1406 | =back |
1725 | =back |
1407 | |
1726 | |
1408 | Example: Define a class with an IO and idle watcher, start one of them in |
1727 | Example: Define a class with an IO and idle watcher, start one of them in |
… | |
… | |
1420 | : io (this, &myclass::io_cb), |
1739 | : io (this, &myclass::io_cb), |
1421 | idle (this, &myclass::idle_cb) |
1740 | idle (this, &myclass::idle_cb) |
1422 | { |
1741 | { |
1423 | io.start (fd, ev::READ); |
1742 | io.start (fd, ev::READ); |
1424 | } |
1743 | } |
|
|
1744 | |
|
|
1745 | |
|
|
1746 | =head1 MACRO MAGIC |
|
|
1747 | |
|
|
1748 | Libev can be compiled with a variety of options, the most fundemantal is |
|
|
1749 | C<EV_MULTIPLICITY>. This option determines wether (most) functions and |
|
|
1750 | callbacks have an initial C<struct ev_loop *> argument. |
|
|
1751 | |
|
|
1752 | To make it easier to write programs that cope with either variant, the |
|
|
1753 | following macros are defined: |
|
|
1754 | |
|
|
1755 | =over 4 |
|
|
1756 | |
|
|
1757 | =item C<EV_A>, C<EV_A_> |
|
|
1758 | |
|
|
1759 | This provides the loop I<argument> for functions, if one is required ("ev |
|
|
1760 | loop argument"). The C<EV_A> form is used when this is the sole argument, |
|
|
1761 | C<EV_A_> is used when other arguments are following. Example: |
|
|
1762 | |
|
|
1763 | ev_unref (EV_A); |
|
|
1764 | ev_timer_add (EV_A_ watcher); |
|
|
1765 | ev_loop (EV_A_ 0); |
|
|
1766 | |
|
|
1767 | It assumes the variable C<loop> of type C<struct ev_loop *> is in scope, |
|
|
1768 | which is often provided by the following macro. |
|
|
1769 | |
|
|
1770 | =item C<EV_P>, C<EV_P_> |
|
|
1771 | |
|
|
1772 | This provides the loop I<parameter> for functions, if one is required ("ev |
|
|
1773 | loop parameter"). The C<EV_P> form is used when this is the sole parameter, |
|
|
1774 | C<EV_P_> is used when other parameters are following. Example: |
|
|
1775 | |
|
|
1776 | // this is how ev_unref is being declared |
|
|
1777 | static void ev_unref (EV_P); |
|
|
1778 | |
|
|
1779 | // this is how you can declare your typical callback |
|
|
1780 | static void cb (EV_P_ ev_timer *w, int revents) |
|
|
1781 | |
|
|
1782 | It declares a parameter C<loop> of type C<struct ev_loop *>, quite |
|
|
1783 | suitable for use with C<EV_A>. |
|
|
1784 | |
|
|
1785 | =item C<EV_DEFAULT>, C<EV_DEFAULT_> |
|
|
1786 | |
|
|
1787 | Similar to the other two macros, this gives you the value of the default |
|
|
1788 | loop, if multiple loops are supported ("ev loop default"). |
|
|
1789 | |
|
|
1790 | =back |
|
|
1791 | |
|
|
1792 | Example: Declare and initialise a check watcher, working regardless of |
|
|
1793 | wether multiple loops are supported or not. |
|
|
1794 | |
|
|
1795 | static void |
|
|
1796 | check_cb (EV_P_ ev_timer *w, int revents) |
|
|
1797 | { |
|
|
1798 | ev_check_stop (EV_A_ w); |
|
|
1799 | } |
|
|
1800 | |
|
|
1801 | ev_check check; |
|
|
1802 | ev_check_init (&check, check_cb); |
|
|
1803 | ev_check_start (EV_DEFAULT_ &check); |
|
|
1804 | ev_loop (EV_DEFAULT_ 0); |
|
|
1805 | |
1425 | |
1806 | |
1426 | =head1 EMBEDDING |
1807 | =head1 EMBEDDING |
1427 | |
1808 | |
1428 | Libev can (and often is) directly embedded into host |
1809 | Libev can (and often is) directly embedded into host |
1429 | applications. Examples of applications that embed it include the Deliantra |
1810 | applications. Examples of applications that embed it include the Deliantra |
… | |
… | |
1636 | will have the C<struct ev_loop *> as first argument, and you can create |
2017 | will have the C<struct ev_loop *> as first argument, and you can create |
1637 | additional independent event loops. Otherwise there will be no support |
2018 | additional independent event loops. Otherwise there will be no support |
1638 | for multiple event loops and there is no first event loop pointer |
2019 | for multiple event loops and there is no first event loop pointer |
1639 | argument. Instead, all functions act on the single default loop. |
2020 | argument. Instead, all functions act on the single default loop. |
1640 | |
2021 | |
1641 | =item EV_PERIODICS |
2022 | =item EV_PERIODIC_ENABLE |
1642 | |
2023 | |
1643 | If undefined or defined to be C<1>, then periodic timers are supported, |
2024 | If undefined or defined to be C<1>, then periodic timers are supported. If |
1644 | otherwise not. This saves a few kb of code. |
2025 | defined to be C<0>, then they are not. Disabling them saves a few kB of |
|
|
2026 | code. |
|
|
2027 | |
|
|
2028 | =item EV_EMBED_ENABLE |
|
|
2029 | |
|
|
2030 | If undefined or defined to be C<1>, then embed watchers are supported. If |
|
|
2031 | defined to be C<0>, then they are not. |
|
|
2032 | |
|
|
2033 | =item EV_STAT_ENABLE |
|
|
2034 | |
|
|
2035 | If undefined or defined to be C<1>, then stat watchers are supported. If |
|
|
2036 | defined to be C<0>, then they are not. |
|
|
2037 | |
|
|
2038 | =item EV_FORK_ENABLE |
|
|
2039 | |
|
|
2040 | If undefined or defined to be C<1>, then fork watchers are supported. If |
|
|
2041 | defined to be C<0>, then they are not. |
|
|
2042 | |
|
|
2043 | =item EV_MINIMAL |
|
|
2044 | |
|
|
2045 | If you need to shave off some kilobytes of code at the expense of some |
|
|
2046 | speed, define this symbol to C<1>. Currently only used for gcc to override |
|
|
2047 | some inlining decisions, saves roughly 30% codesize of amd64. |
|
|
2048 | |
|
|
2049 | =item EV_PID_HASHSIZE |
|
|
2050 | |
|
|
2051 | C<ev_child> watchers use a small hash table to distribute workload by |
|
|
2052 | pid. The default size is C<16> (or C<1> with C<EV_MINIMAL>), usually more |
|
|
2053 | than enough. If you need to manage thousands of children you might want to |
|
|
2054 | increase this value. |
1645 | |
2055 | |
1646 | =item EV_COMMON |
2056 | =item EV_COMMON |
1647 | |
2057 | |
1648 | By default, all watchers have a C<void *data> member. By redefining |
2058 | By default, all watchers have a C<void *data> member. By redefining |
1649 | this macro to a something else you can include more and other types of |
2059 | this macro to a something else you can include more and other types of |
… | |
… | |
1692 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
2102 | And a F<ev_cpp.C> implementation file that contains libev proper and is compiled: |
1693 | |
2103 | |
1694 | #include "ev_cpp.h" |
2104 | #include "ev_cpp.h" |
1695 | #include "ev.c" |
2105 | #include "ev.c" |
1696 | |
2106 | |
|
|
2107 | |
|
|
2108 | =head1 COMPLEXITIES |
|
|
2109 | |
|
|
2110 | In this section the complexities of (many of) the algorithms used inside |
|
|
2111 | libev will be explained. For complexity discussions about backends see the |
|
|
2112 | documentation for C<ev_default_init>. |
|
|
2113 | |
|
|
2114 | =over 4 |
|
|
2115 | |
|
|
2116 | =item Starting and stopping timer/periodic watchers: O(log skipped_other_timers) |
|
|
2117 | |
|
|
2118 | =item Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers) |
|
|
2119 | |
|
|
2120 | =item Starting io/check/prepare/idle/signal/child watchers: O(1) |
|
|
2121 | |
|
|
2122 | =item Stopping check/prepare/idle watchers: O(1) |
|
|
2123 | |
|
|
2124 | =item Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16)) |
|
|
2125 | |
|
|
2126 | =item Finding the next timer per loop iteration: O(1) |
|
|
2127 | |
|
|
2128 | =item Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd) |
|
|
2129 | |
|
|
2130 | =item Activating one watcher: O(1) |
|
|
2131 | |
|
|
2132 | =back |
|
|
2133 | |
|
|
2134 | |
1697 | =head1 AUTHOR |
2135 | =head1 AUTHOR |
1698 | |
2136 | |
1699 | Marc Lehmann <libev@schmorp.de>. |
2137 | Marc Lehmann <libev@schmorp.de>. |
1700 | |
2138 | |