… | |
… | |
39 | F<README.embed> in the libev distribution. If libev was configured without |
39 | F<README.embed> in the libev distribution. If libev was configured without |
40 | support for multiple event loops, then all functions taking an initial |
40 | support for multiple event loops, then all functions taking an initial |
41 | argument of name C<loop> (which is always of type C<struct ev_loop *>) |
41 | argument of name C<loop> (which is always of type C<struct ev_loop *>) |
42 | will not have this argument. |
42 | will not have this argument. |
43 | |
43 | |
44 | =head1 TIME AND OTHER GLOBAL FUNCTIONS |
44 | =head1 TIME REPRESENTATION |
45 | |
45 | |
46 | Libev represents time as a single floating point number, representing the |
46 | Libev represents time as a single floating point number, representing the |
47 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
47 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
48 | the beginning of 1970, details are complicated, don't ask). This type is |
48 | 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 |
49 | called C<ev_tstamp>, which is what you should use too. It usually aliases |
50 | to the double type in C. |
50 | to the C<double> type in C, and when you need to do any calculations on |
|
|
51 | it, you should treat it as such. |
|
|
52 | |
|
|
53 | |
|
|
54 | =head1 GLOBAL FUNCTIONS |
|
|
55 | |
|
|
56 | These functions can be called anytime, even before initialising the |
|
|
57 | library in any way. |
51 | |
58 | |
52 | =over 4 |
59 | =over 4 |
53 | |
60 | |
54 | =item ev_tstamp ev_time () |
61 | =item ev_tstamp ev_time () |
55 | |
62 | |
56 | Returns the current time as libev would use it. |
63 | Returns the current time as libev would use it. Please note that the |
|
|
64 | C<ev_now> function is usually faster and also often returns the timestamp |
|
|
65 | you actually want to know. |
57 | |
66 | |
58 | =item int ev_version_major () |
67 | =item int ev_version_major () |
59 | |
68 | |
60 | =item int ev_version_minor () |
69 | =item int ev_version_minor () |
61 | |
70 | |
… | |
… | |
68 | Usually, it's a good idea to terminate if the major versions mismatch, |
77 | Usually, it's a good idea to terminate if the major versions mismatch, |
69 | as this indicates an incompatible change. Minor versions are usually |
78 | as this indicates an incompatible change. Minor versions are usually |
70 | compatible to older versions, so a larger minor version alone is usually |
79 | compatible to older versions, so a larger minor version alone is usually |
71 | not a problem. |
80 | not a problem. |
72 | |
81 | |
|
|
82 | Example: make sure we haven't accidentally been linked against the wrong |
|
|
83 | version: |
|
|
84 | |
|
|
85 | assert (("libev version mismatch", |
|
|
86 | ev_version_major () == EV_VERSION_MAJOR |
|
|
87 | && ev_version_minor () >= EV_VERSION_MINOR)); |
|
|
88 | |
|
|
89 | =item unsigned int ev_supported_backends () |
|
|
90 | |
|
|
91 | Return the set of all backends (i.e. their corresponding C<EV_BACKEND_*> |
|
|
92 | value) compiled into this binary of libev (independent of their |
|
|
93 | availability on the system you are running on). See C<ev_default_loop> for |
|
|
94 | a description of the set values. |
|
|
95 | |
|
|
96 | Example: make sure we have the epoll method, because yeah this is cool and |
|
|
97 | a must have and can we have a torrent of it please!!!11 |
|
|
98 | |
|
|
99 | assert (("sorry, no epoll, no sex", |
|
|
100 | ev_supported_backends () & EVBACKEND_EPOLL)); |
|
|
101 | |
|
|
102 | =item unsigned int ev_recommended_backends () |
|
|
103 | |
|
|
104 | Return the set of all backends compiled into this binary of libev and also |
|
|
105 | recommended for this platform. This set is often smaller than the one |
|
|
106 | returned by C<ev_supported_backends>, as for example kqueue is broken on |
|
|
107 | most BSDs and will not be autodetected unless you explicitly request it |
|
|
108 | (assuming you know what you are doing). This is the set of backends that |
|
|
109 | libev will probe for if you specify no backends explicitly. |
|
|
110 | |
73 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
111 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
74 | |
112 | |
75 | Sets the allocation function to use (the prototype is similar to the |
113 | Sets the allocation function to use (the prototype is similar to the |
76 | realloc C function, the semantics are identical). It is used to allocate |
114 | realloc C function, the semantics are identical). It is used to allocate |
77 | and free memory (no surprises here). If it returns zero when memory |
115 | and free memory (no surprises here). If it returns zero when memory |
… | |
… | |
79 | destructive action. The default is your system realloc function. |
117 | destructive action. The default is your system realloc function. |
80 | |
118 | |
81 | You could override this function in high-availability programs to, say, |
119 | You could override this function in high-availability programs to, say, |
82 | free some memory if it cannot allocate memory, to use a special allocator, |
120 | free some memory if it cannot allocate memory, to use a special allocator, |
83 | or even to sleep a while and retry until some memory is available. |
121 | or even to sleep a while and retry until some memory is available. |
|
|
122 | |
|
|
123 | Example: replace the libev allocator with one that waits a bit and then |
|
|
124 | retries: better than mine). |
|
|
125 | |
|
|
126 | static void * |
|
|
127 | persistent_realloc (void *ptr, long size) |
|
|
128 | { |
|
|
129 | for (;;) |
|
|
130 | { |
|
|
131 | void *newptr = realloc (ptr, size); |
|
|
132 | |
|
|
133 | if (newptr) |
|
|
134 | return newptr; |
|
|
135 | |
|
|
136 | sleep (60); |
|
|
137 | } |
|
|
138 | } |
|
|
139 | |
|
|
140 | ... |
|
|
141 | ev_set_allocator (persistent_realloc); |
84 | |
142 | |
85 | =item ev_set_syserr_cb (void (*cb)(const char *msg)); |
143 | =item ev_set_syserr_cb (void (*cb)(const char *msg)); |
86 | |
144 | |
87 | Set the callback function to call on a retryable syscall error (such |
145 | Set the callback function to call on a retryable syscall error (such |
88 | as failed select, poll, epoll_wait). The message is a printable string |
146 | as failed select, poll, epoll_wait). The message is a printable string |
… | |
… | |
90 | callback is set, then libev will expect it to remedy the sitution, no |
148 | callback is set, then libev will expect it to remedy the sitution, no |
91 | matter what, when it returns. That is, libev will generally retry the |
149 | matter what, when it returns. That is, libev will generally retry the |
92 | requested operation, or, if the condition doesn't go away, do bad stuff |
150 | requested operation, or, if the condition doesn't go away, do bad stuff |
93 | (such as abort). |
151 | (such as abort). |
94 | |
152 | |
|
|
153 | Example: do the same thing as libev does internally: |
|
|
154 | |
|
|
155 | static void |
|
|
156 | fatal_error (const char *msg) |
|
|
157 | { |
|
|
158 | perror (msg); |
|
|
159 | abort (); |
|
|
160 | } |
|
|
161 | |
|
|
162 | ... |
|
|
163 | ev_set_syserr_cb (fatal_error); |
|
|
164 | |
95 | =back |
165 | =back |
96 | |
166 | |
97 | =head1 FUNCTIONS CONTROLLING THE EVENT LOOP |
167 | =head1 FUNCTIONS CONTROLLING THE EVENT LOOP |
98 | |
168 | |
99 | An event loop is described by a C<struct ev_loop *>. The library knows two |
169 | An event loop is described by a C<struct ev_loop *>. The library knows two |
100 | types of such loops, the I<default> loop, which supports signals and child |
170 | types of such loops, the I<default> loop, which supports signals and child |
101 | events, and dynamically created loops which do not. |
171 | events, and dynamically created loops which do not. |
102 | |
172 | |
103 | If you use threads, a common model is to run the default event loop |
173 | If you use threads, a common model is to run the default event loop |
104 | in your main thread (or in a separate thrad) and for each thread you |
174 | in your main thread (or in a separate thread) and for each thread you |
105 | create, you also create another event loop. Libev itself does no locking |
175 | create, you also create another event loop. Libev itself does no locking |
106 | whatsoever, so if you mix calls to the same event loop in different |
176 | whatsoever, so if you mix calls to the same event loop in different |
107 | threads, make sure you lock (this is usually a bad idea, though, even if |
177 | threads, make sure you lock (this is usually a bad idea, though, even if |
108 | done correctly, because it's hideous and inefficient). |
178 | done correctly, because it's hideous and inefficient). |
109 | |
179 | |
… | |
… | |
112 | =item struct ev_loop *ev_default_loop (unsigned int flags) |
182 | =item struct ev_loop *ev_default_loop (unsigned int flags) |
113 | |
183 | |
114 | This will initialise the default event loop if it hasn't been initialised |
184 | This will initialise the default event loop if it hasn't been initialised |
115 | yet and return it. If the default loop could not be initialised, returns |
185 | yet and return it. If the default loop could not be initialised, returns |
116 | false. If it already was initialised it simply returns it (and ignores the |
186 | false. If it already was initialised it simply returns it (and ignores the |
117 | flags). |
187 | flags. If that is troubling you, check C<ev_backend ()> afterwards). |
118 | |
188 | |
119 | If you don't know what event loop to use, use the one returned from this |
189 | If you don't know what event loop to use, use the one returned from this |
120 | function. |
190 | function. |
121 | |
191 | |
122 | The flags argument can be used to specify special behaviour or specific |
192 | The flags argument can be used to specify special behaviour or specific |
123 | backends to use, and is usually specified as 0 (or EVFLAG_AUTO). |
193 | backends to use, and is usually specified as C<0> (or C<EVFLAG_AUTO>). |
124 | |
194 | |
125 | It supports the following flags: |
195 | The following flags are supported: |
126 | |
196 | |
127 | =over 4 |
197 | =over 4 |
128 | |
198 | |
129 | =item C<EVFLAG_AUTO> |
199 | =item C<EVFLAG_AUTO> |
130 | |
200 | |
… | |
… | |
138 | C<LIBEV_FLAGS>. Otherwise (the default), this environment variable will |
208 | C<LIBEV_FLAGS>. Otherwise (the default), this environment variable will |
139 | override the flags completely if it is found in the environment. This is |
209 | override the flags completely if it is found in the environment. This is |
140 | useful to try out specific backends to test their performance, or to work |
210 | useful to try out specific backends to test their performance, or to work |
141 | around bugs. |
211 | around bugs. |
142 | |
212 | |
143 | =item C<EVMETHOD_SELECT> (portable select backend) |
213 | =item C<EVBACKEND_SELECT> (value 1, portable select backend) |
144 | |
214 | |
|
|
215 | This is your standard select(2) backend. Not I<completely> standard, as |
|
|
216 | libev tries to roll its own fd_set with no limits on the number of fds, |
|
|
217 | but if that fails, expect a fairly low limit on the number of fds when |
|
|
218 | using this backend. It doesn't scale too well (O(highest_fd)), but its usually |
|
|
219 | the fastest backend for a low number of fds. |
|
|
220 | |
145 | =item C<EVMETHOD_POLL> (poll backend, available everywhere except on windows) |
221 | =item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) |
146 | |
222 | |
147 | =item C<EVMETHOD_EPOLL> (linux only) |
223 | And this is your standard poll(2) backend. It's more complicated than |
|
|
224 | select, but handles sparse fds better and has no artificial limit on the |
|
|
225 | number of fds you can use (except it will slow down considerably with a |
|
|
226 | lot of inactive fds). It scales similarly to select, i.e. O(total_fds). |
148 | |
227 | |
149 | =item C<EVMETHOD_KQUEUE> (some bsds only) |
228 | =item C<EVBACKEND_EPOLL> (value 4, Linux) |
150 | |
229 | |
151 | =item C<EVMETHOD_DEVPOLL> (solaris 8 only) |
230 | For few fds, this backend is a bit little slower than poll and select, |
|
|
231 | but it scales phenomenally better. While poll and select usually scale like |
|
|
232 | O(total_fds) where n is the total number of fds (or the highest fd), epoll scales |
|
|
233 | either O(1) or O(active_fds). |
152 | |
234 | |
153 | =item C<EVMETHOD_PORT> (solaris 10 only) |
235 | While stopping and starting an I/O watcher in the same iteration will |
|
|
236 | result in some caching, there is still a syscall per such incident |
|
|
237 | (because the fd could point to a different file description now), so its |
|
|
238 | best to avoid that. Also, dup()ed file descriptors might not work very |
|
|
239 | well if you register events for both fds. |
|
|
240 | |
|
|
241 | Please note that epoll sometimes generates spurious notifications, so you |
|
|
242 | need to use non-blocking I/O or other means to avoid blocking when no data |
|
|
243 | (or space) is available. |
|
|
244 | |
|
|
245 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
|
|
246 | |
|
|
247 | Kqueue deserves special mention, as at the time of this writing, it |
|
|
248 | was broken on all BSDs except NetBSD (usually it doesn't work with |
|
|
249 | anything but sockets and pipes, except on Darwin, where of course its |
|
|
250 | completely useless). For this reason its not being "autodetected" |
|
|
251 | unless you explicitly specify it explicitly in the flags (i.e. using |
|
|
252 | C<EVBACKEND_KQUEUE>). |
|
|
253 | |
|
|
254 | It scales in the same way as the epoll backend, but the interface to the |
|
|
255 | kernel is more efficient (which says nothing about its actual speed, of |
|
|
256 | course). While starting and stopping an I/O watcher does not cause an |
|
|
257 | extra syscall as with epoll, it still adds up to four event changes per |
|
|
258 | incident, so its best to avoid that. |
|
|
259 | |
|
|
260 | =item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8) |
|
|
261 | |
|
|
262 | This is not implemented yet (and might never be). |
|
|
263 | |
|
|
264 | =item C<EVBACKEND_PORT> (value 32, Solaris 10) |
|
|
265 | |
|
|
266 | This uses the Solaris 10 port mechanism. As with everything on Solaris, |
|
|
267 | it's really slow, but it still scales very well (O(active_fds)). |
|
|
268 | |
|
|
269 | Please note that solaris ports can result in a lot of spurious |
|
|
270 | notifications, so you need to use non-blocking I/O or other means to avoid |
|
|
271 | blocking when no data (or space) is available. |
|
|
272 | |
|
|
273 | =item C<EVBACKEND_ALL> |
|
|
274 | |
|
|
275 | Try all backends (even potentially broken ones that wouldn't be tried |
|
|
276 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
|
|
277 | C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. |
|
|
278 | |
|
|
279 | =back |
154 | |
280 | |
155 | If one or more of these are ored into the flags value, then only these |
281 | If one or more of these are ored into the flags value, then only these |
156 | backends will be tried (in the reverse order as given here). If one are |
282 | backends will be tried (in the reverse order as given here). If none are |
157 | specified, any backend will do. |
283 | specified, most compiled-in backend will be tried, usually in reverse |
|
|
284 | order of their flag values :) |
158 | |
285 | |
159 | =back |
286 | The most typical usage is like this: |
|
|
287 | |
|
|
288 | if (!ev_default_loop (0)) |
|
|
289 | fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); |
|
|
290 | |
|
|
291 | Restrict libev to the select and poll backends, and do not allow |
|
|
292 | environment settings to be taken into account: |
|
|
293 | |
|
|
294 | ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); |
|
|
295 | |
|
|
296 | Use whatever libev has to offer, but make sure that kqueue is used if |
|
|
297 | available (warning, breaks stuff, best use only with your own private |
|
|
298 | event loop and only if you know the OS supports your types of fds): |
|
|
299 | |
|
|
300 | ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); |
160 | |
301 | |
161 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
302 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
162 | |
303 | |
163 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
304 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
164 | always distinct from the default loop. Unlike the default loop, it cannot |
305 | always distinct from the default loop. Unlike the default loop, it cannot |
165 | handle signal and child watchers, and attempts to do so will be greeted by |
306 | handle signal and child watchers, and attempts to do so will be greeted by |
166 | undefined behaviour (or a failed assertion if assertions are enabled). |
307 | undefined behaviour (or a failed assertion if assertions are enabled). |
167 | |
308 | |
|
|
309 | Example: try to create a event loop that uses epoll and nothing else. |
|
|
310 | |
|
|
311 | struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); |
|
|
312 | if (!epoller) |
|
|
313 | fatal ("no epoll found here, maybe it hides under your chair"); |
|
|
314 | |
168 | =item ev_default_destroy () |
315 | =item ev_default_destroy () |
169 | |
316 | |
170 | Destroys the default loop again (frees all memory and kernel state |
317 | Destroys the default loop again (frees all memory and kernel state |
171 | etc.). This stops all registered event watchers (by not touching them in |
318 | etc.). This stops all registered event watchers (by not touching them in |
172 | any way whatsoever, although you cannot rely on this :). |
319 | any way whatsoever, although you cannot rely on this :). |
… | |
… | |
181 | This function reinitialises the kernel state for backends that have |
328 | This function reinitialises the kernel state for backends that have |
182 | one. Despite the name, you can call it anytime, but it makes most sense |
329 | one. Despite the name, you can call it anytime, but it makes most sense |
183 | after forking, in either the parent or child process (or both, but that |
330 | after forking, in either the parent or child process (or both, but that |
184 | again makes little sense). |
331 | again makes little sense). |
185 | |
332 | |
186 | You I<must> call this function after forking if and only if you want to |
333 | You I<must> call this function in the child process after forking if and |
187 | use the event library in both processes. If you just fork+exec, you don't |
334 | only if you want to use the event library in both processes. If you just |
188 | have to call it. |
335 | fork+exec, you don't have to call it. |
189 | |
336 | |
190 | The function itself is quite fast and it's usually not a problem to call |
337 | The function itself is quite fast and it's usually not a problem to call |
191 | it just in case after a fork. To make this easy, the function will fit in |
338 | it just in case after a fork. To make this easy, the function will fit in |
192 | quite nicely into a call to C<pthread_atfork>: |
339 | quite nicely into a call to C<pthread_atfork>: |
193 | |
340 | |
194 | pthread_atfork (0, 0, ev_default_fork); |
341 | pthread_atfork (0, 0, ev_default_fork); |
195 | |
342 | |
|
|
343 | At the moment, C<EVBACKEND_SELECT> and C<EVBACKEND_POLL> are safe to use |
|
|
344 | without calling this function, so if you force one of those backends you |
|
|
345 | do not need to care. |
|
|
346 | |
196 | =item ev_loop_fork (loop) |
347 | =item ev_loop_fork (loop) |
197 | |
348 | |
198 | Like C<ev_default_fork>, but acts on an event loop created by |
349 | Like C<ev_default_fork>, but acts on an event loop created by |
199 | C<ev_loop_new>. Yes, you have to call this on every allocated event loop |
350 | C<ev_loop_new>. Yes, you have to call this on every allocated event loop |
200 | after fork, and how you do this is entirely your own problem. |
351 | after fork, and how you do this is entirely your own problem. |
201 | |
352 | |
202 | =item unsigned int ev_method (loop) |
353 | =item unsigned int ev_backend (loop) |
203 | |
354 | |
204 | Returns one of the C<EVMETHOD_*> flags indicating the event backend in |
355 | Returns one of the C<EVBACKEND_*> flags indicating the event backend in |
205 | use. |
356 | use. |
206 | |
357 | |
207 | =item ev_tstamp ev_now (loop) |
358 | =item ev_tstamp ev_now (loop) |
208 | |
359 | |
209 | Returns the current "event loop time", which is the time the event loop |
360 | Returns the current "event loop time", which is the time the event loop |
210 | got events and started processing them. This timestamp does not change |
361 | received events and started processing them. This timestamp does not |
211 | as long as callbacks are being processed, and this is also the base time |
362 | change as long as callbacks are being processed, and this is also the base |
212 | used for relative timers. You can treat it as the timestamp of the event |
363 | time used for relative timers. You can treat it as the timestamp of the |
213 | occuring (or more correctly, the mainloop finding out about it). |
364 | event occuring (or more correctly, libev finding out about it). |
214 | |
365 | |
215 | =item ev_loop (loop, int flags) |
366 | =item ev_loop (loop, int flags) |
216 | |
367 | |
217 | Finally, this is it, the event handler. This function usually is called |
368 | Finally, this is it, the event handler. This function usually is called |
218 | after you initialised all your watchers and you want to start handling |
369 | after you initialised all your watchers and you want to start handling |
219 | events. |
370 | events. |
220 | |
371 | |
221 | If the flags argument is specified as 0, it will not return until either |
372 | If the flags argument is specified as C<0>, it will not return until |
222 | no event watchers are active anymore or C<ev_unloop> was called. |
373 | either no event watchers are active anymore or C<ev_unloop> was called. |
|
|
374 | |
|
|
375 | Please note that an explicit C<ev_unloop> is usually better than |
|
|
376 | relying on all watchers to be stopped when deciding when a program has |
|
|
377 | finished (especially in interactive programs), but having a program that |
|
|
378 | automatically loops as long as it has to and no longer by virtue of |
|
|
379 | relying on its watchers stopping correctly is a thing of beauty. |
223 | |
380 | |
224 | A flags value of C<EVLOOP_NONBLOCK> will look for new events, will handle |
381 | A flags value of C<EVLOOP_NONBLOCK> will look for new events, will handle |
225 | those events and any outstanding ones, but will not block your process in |
382 | those events and any outstanding ones, but will not block your process in |
226 | case there are no events and will return after one iteration of the loop. |
383 | case there are no events and will return after one iteration of the loop. |
227 | |
384 | |
228 | A flags value of C<EVLOOP_ONESHOT> will look for new events (waiting if |
385 | A flags value of C<EVLOOP_ONESHOT> will look for new events (waiting if |
229 | neccessary) and will handle those and any outstanding ones. It will block |
386 | neccessary) and will handle those and any outstanding ones. It will block |
230 | your process until at least one new event arrives, and will return after |
387 | your process until at least one new event arrives, and will return after |
231 | one iteration of the loop. |
388 | one iteration of the loop. This is useful if you are waiting for some |
|
|
389 | external event in conjunction with something not expressible using other |
|
|
390 | libev watchers. However, a pair of C<ev_prepare>/C<ev_check> watchers is |
|
|
391 | usually a better approach for this kind of thing. |
232 | |
392 | |
233 | This flags value could be used to implement alternative looping |
393 | Here are the gory details of what C<ev_loop> does: |
234 | constructs, but the C<prepare> and C<check> watchers provide a better and |
394 | |
235 | more generic mechanism. |
395 | * If there are no active watchers (reference count is zero), return. |
|
|
396 | - Queue prepare watchers and then call all outstanding watchers. |
|
|
397 | - If we have been forked, recreate the kernel state. |
|
|
398 | - Update the kernel state with all outstanding changes. |
|
|
399 | - Update the "event loop time". |
|
|
400 | - Calculate for how long to block. |
|
|
401 | - Block the process, waiting for any events. |
|
|
402 | - Queue all outstanding I/O (fd) events. |
|
|
403 | - Update the "event loop time" and do time jump handling. |
|
|
404 | - Queue all outstanding timers. |
|
|
405 | - Queue all outstanding periodics. |
|
|
406 | - If no events are pending now, queue all idle watchers. |
|
|
407 | - Queue all check watchers. |
|
|
408 | - Call all queued watchers in reverse order (i.e. check watchers first). |
|
|
409 | Signals and child watchers are implemented as I/O watchers, and will |
|
|
410 | be handled here by queueing them when their watcher gets executed. |
|
|
411 | - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
|
|
412 | were used, return, otherwise continue with step *. |
|
|
413 | |
|
|
414 | Example: queue some jobs and then loop until no events are outsanding |
|
|
415 | anymore. |
|
|
416 | |
|
|
417 | ... queue jobs here, make sure they register event watchers as long |
|
|
418 | ... as they still have work to do (even an idle watcher will do..) |
|
|
419 | ev_loop (my_loop, 0); |
|
|
420 | ... jobs done. yeah! |
236 | |
421 | |
237 | =item ev_unloop (loop, how) |
422 | =item ev_unloop (loop, how) |
238 | |
423 | |
239 | Can be used to make a call to C<ev_loop> return early (but only after it |
424 | Can be used to make a call to C<ev_loop> return early (but only after it |
240 | has processed all outstanding events). The C<how> argument must be either |
425 | has processed all outstanding events). The C<how> argument must be either |
241 | C<EVUNLOOP_ONCE>, which will make the innermost C<ev_loop> call return, or |
426 | C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or |
242 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
427 | C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. |
243 | |
428 | |
244 | =item ev_ref (loop) |
429 | =item ev_ref (loop) |
245 | |
430 | |
246 | =item ev_unref (loop) |
431 | =item ev_unref (loop) |
… | |
… | |
254 | visible to the libev user and should not keep C<ev_loop> from exiting if |
439 | visible to the libev user and should not keep C<ev_loop> from exiting if |
255 | no event watchers registered by it are active. It is also an excellent |
440 | no event watchers registered by it are active. It is also an excellent |
256 | way to do this for generic recurring timers or from within third-party |
441 | way to do this for generic recurring timers or from within third-party |
257 | libraries. Just remember to I<unref after start> and I<ref before stop>. |
442 | libraries. Just remember to I<unref after start> and I<ref before stop>. |
258 | |
443 | |
|
|
444 | Example: create a signal watcher, but keep it from keeping C<ev_loop> |
|
|
445 | running when nothing else is active. |
|
|
446 | |
|
|
447 | struct dv_signal exitsig; |
|
|
448 | ev_signal_init (&exitsig, sig_cb, SIGINT); |
|
|
449 | ev_signal_start (myloop, &exitsig); |
|
|
450 | evf_unref (myloop); |
|
|
451 | |
|
|
452 | Example: for some weird reason, unregister the above signal handler again. |
|
|
453 | |
|
|
454 | ev_ref (myloop); |
|
|
455 | ev_signal_stop (myloop, &exitsig); |
|
|
456 | |
259 | =back |
457 | =back |
260 | |
458 | |
261 | =head1 ANATOMY OF A WATCHER |
459 | =head1 ANATOMY OF A WATCHER |
262 | |
460 | |
263 | A watcher is a structure that you create and register to record your |
461 | A watcher is a structure that you create and register to record your |
… | |
… | |
297 | *) >>), and you can stop watching for events at any time by calling the |
495 | *) >>), and you can stop watching for events at any time by calling the |
298 | corresponding stop function (C<< ev_<type>_stop (loop, watcher *) >>. |
496 | corresponding stop function (C<< ev_<type>_stop (loop, watcher *) >>. |
299 | |
497 | |
300 | As long as your watcher is active (has been started but not stopped) you |
498 | As long as your watcher is active (has been started but not stopped) you |
301 | must not touch the values stored in it. Most specifically you must never |
499 | must not touch the values stored in it. Most specifically you must never |
302 | reinitialise it or call its set method. |
500 | reinitialise it or call its set macro. |
303 | |
501 | |
304 | You can check whether an event is active by calling the C<ev_is_active |
502 | You can check whether an event is active by calling the C<ev_is_active |
305 | (watcher *)> macro. To see whether an event is outstanding (but the |
503 | (watcher *)> macro. To see whether an event is outstanding (but the |
306 | callback for it has not been called yet) you can use the C<ev_is_pending |
504 | callback for it has not been called yet) you can use the C<ev_is_pending |
307 | (watcher *)> macro. |
505 | (watcher *)> macro. |
… | |
… | |
404 | =head1 WATCHER TYPES |
602 | =head1 WATCHER TYPES |
405 | |
603 | |
406 | This section describes each watcher in detail, but will not repeat |
604 | This section describes each watcher in detail, but will not repeat |
407 | information given in the last section. |
605 | information given in the last section. |
408 | |
606 | |
|
|
607 | |
409 | =head2 C<ev_io> - is this file descriptor readable or writable |
608 | =head2 C<ev_io> - is this file descriptor readable or writable |
410 | |
609 | |
411 | I/O watchers check whether a file descriptor is readable or writable |
610 | I/O watchers check whether a file descriptor is readable or writable |
412 | in each iteration of the event loop (This behaviour is called |
611 | in each iteration of the event loop (This behaviour is called |
413 | level-triggering because you keep receiving events as long as the |
612 | level-triggering because you keep receiving events as long as the |
414 | condition persists. Remember you can stop the watcher if you don't want to |
613 | condition persists. Remember you can stop the watcher if you don't want to |
415 | act on the event and neither want to receive future events). |
614 | act on the event and neither want to receive future events). |
416 | |
615 | |
417 | In general you can register as many read and/or write event watchers oer |
616 | In general you can register as many read and/or write event watchers per |
418 | fd as you want (as long as you don't confuse yourself). Setting all file |
617 | fd as you want (as long as you don't confuse yourself). Setting all file |
419 | descriptors to non-blocking mode is also usually a good idea (but not |
618 | descriptors to non-blocking mode is also usually a good idea (but not |
420 | required if you know what you are doing). |
619 | required if you know what you are doing). |
421 | |
620 | |
422 | You have to be careful with dup'ed file descriptors, though. Some backends |
621 | You have to be careful with dup'ed file descriptors, though. Some backends |
423 | (the linux epoll backend is a notable example) cannot handle dup'ed file |
622 | (the linux epoll backend is a notable example) cannot handle dup'ed file |
424 | descriptors correctly if you register interest in two or more fds pointing |
623 | descriptors correctly if you register interest in two or more fds pointing |
425 | to the same file/socket etc. description. |
624 | to the same underlying file/socket etc. description (that is, they share |
|
|
625 | the same underlying "file open"). |
426 | |
626 | |
427 | If you must do this, then force the use of a known-to-be-good backend |
627 | If you must do this, then force the use of a known-to-be-good backend |
428 | (at the time of this writing, this includes only EVMETHOD_SELECT and |
628 | (at the time of this writing, this includes only C<EVBACKEND_SELECT> and |
429 | EVMETHOD_POLL). |
629 | C<EVBACKEND_POLL>). |
430 | |
630 | |
431 | =over 4 |
631 | =over 4 |
432 | |
632 | |
433 | =item ev_io_init (ev_io *, callback, int fd, int events) |
633 | =item ev_io_init (ev_io *, callback, int fd, int events) |
434 | |
634 | |
… | |
… | |
436 | |
636 | |
437 | Configures an C<ev_io> watcher. The fd is the file descriptor to rceeive |
637 | Configures an C<ev_io> watcher. The fd is the file descriptor to rceeive |
438 | events for and events is either C<EV_READ>, C<EV_WRITE> or C<EV_READ | |
638 | events for and events is either C<EV_READ>, C<EV_WRITE> or C<EV_READ | |
439 | EV_WRITE> to receive the given events. |
639 | EV_WRITE> to receive the given events. |
440 | |
640 | |
|
|
641 | Please note that most of the more scalable backend mechanisms (for example |
|
|
642 | epoll and solaris ports) can result in spurious readyness notifications |
|
|
643 | for file descriptors, so you practically need to use non-blocking I/O (and |
|
|
644 | treat callback invocation as hint only), or retest separately with a safe |
|
|
645 | interface before doing I/O (XLib can do this), or force the use of either |
|
|
646 | C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>, which don't suffer from this |
|
|
647 | problem. Also note that it is quite easy to have your callback invoked |
|
|
648 | when the readyness condition is no longer valid even when employing |
|
|
649 | typical ways of handling events, so its a good idea to use non-blocking |
|
|
650 | I/O unconditionally. |
|
|
651 | |
441 | =back |
652 | =back |
|
|
653 | |
|
|
654 | Example: call C<stdin_readable_cb> when STDIN_FILENO has become, well |
|
|
655 | readable, but only once. Since it is likely line-buffered, you could |
|
|
656 | attempt to read a whole line in the callback: |
|
|
657 | |
|
|
658 | static void |
|
|
659 | stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
|
|
660 | { |
|
|
661 | ev_io_stop (loop, w); |
|
|
662 | .. read from stdin here (or from w->fd) and haqndle any I/O errors |
|
|
663 | } |
|
|
664 | |
|
|
665 | ... |
|
|
666 | struct ev_loop *loop = ev_default_init (0); |
|
|
667 | struct ev_io stdin_readable; |
|
|
668 | ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ); |
|
|
669 | ev_io_start (loop, &stdin_readable); |
|
|
670 | ev_loop (loop, 0); |
|
|
671 | |
442 | |
672 | |
443 | =head2 C<ev_timer> - relative and optionally recurring timeouts |
673 | =head2 C<ev_timer> - relative and optionally recurring timeouts |
444 | |
674 | |
445 | Timer watchers are simple relative timers that generate an event after a |
675 | Timer watchers are simple relative timers that generate an event after a |
446 | given time, and optionally repeating in regular intervals after that. |
676 | given time, and optionally repeating in regular intervals after that. |
447 | |
677 | |
448 | The timers are based on real time, that is, if you register an event that |
678 | The timers are based on real time, that is, if you register an event that |
449 | times out after an hour and youreset your system clock to last years |
679 | times out after an hour and you reset your system clock to last years |
450 | time, it will still time out after (roughly) and hour. "Roughly" because |
680 | time, it will still time out after (roughly) and hour. "Roughly" because |
451 | detecting time jumps is hard, and soem inaccuracies are unavoidable (the |
681 | detecting time jumps is hard, and some inaccuracies are unavoidable (the |
452 | monotonic clock option helps a lot here). |
682 | monotonic clock option helps a lot here). |
453 | |
683 | |
454 | The relative timeouts are calculated relative to the C<ev_now ()> |
684 | The relative timeouts are calculated relative to the C<ev_now ()> |
455 | time. This is usually the right thing as this timestamp refers to the time |
685 | time. This is usually the right thing as this timestamp refers to the time |
456 | of the event triggering whatever timeout you are modifying/starting. If |
686 | of the event triggering whatever timeout you are modifying/starting. If |
457 | you suspect event processing to be delayed and you *need* to base the timeout |
687 | you suspect event processing to be delayed and you I<need> to base the timeout |
458 | ion the current time, use something like this to adjust for this: |
688 | on the current time, use something like this to adjust for this: |
459 | |
689 | |
460 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
690 | ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); |
|
|
691 | |
|
|
692 | The callback is guarenteed to be invoked only when its timeout has passed, |
|
|
693 | but if multiple timers become ready during the same loop iteration then |
|
|
694 | order of execution is undefined. |
461 | |
695 | |
462 | =over 4 |
696 | =over 4 |
463 | |
697 | |
464 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
698 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
465 | |
699 | |
… | |
… | |
471 | later, again, and again, until stopped manually. |
705 | later, again, and again, until stopped manually. |
472 | |
706 | |
473 | The timer itself will do a best-effort at avoiding drift, that is, if you |
707 | The timer itself will do a best-effort at avoiding drift, that is, if you |
474 | configure a timer to trigger every 10 seconds, then it will trigger at |
708 | configure a timer to trigger every 10 seconds, then it will trigger at |
475 | exactly 10 second intervals. If, however, your program cannot keep up with |
709 | exactly 10 second intervals. If, however, your program cannot keep up with |
476 | the timer (ecause it takes longer than those 10 seconds to do stuff) the |
710 | the timer (because it takes longer than those 10 seconds to do stuff) the |
477 | timer will not fire more than once per event loop iteration. |
711 | timer will not fire more than once per event loop iteration. |
478 | |
712 | |
479 | =item ev_timer_again (loop) |
713 | =item ev_timer_again (loop) |
480 | |
714 | |
481 | This will act as if the timer timed out and restart it again if it is |
715 | This will act as if the timer timed out and restart it again if it is |
… | |
… | |
495 | state where you do not expect data to travel on the socket, you can stop |
729 | state where you do not expect data to travel on the socket, you can stop |
496 | the timer, and again will automatically restart it if need be. |
730 | the timer, and again will automatically restart it if need be. |
497 | |
731 | |
498 | =back |
732 | =back |
499 | |
733 | |
|
|
734 | Example: create a timer that fires after 60 seconds. |
|
|
735 | |
|
|
736 | static void |
|
|
737 | one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
|
|
738 | { |
|
|
739 | .. one minute over, w is actually stopped right here |
|
|
740 | } |
|
|
741 | |
|
|
742 | struct ev_timer mytimer; |
|
|
743 | ev_timer_init (&mytimer, one_minute_cb, 60., 0.); |
|
|
744 | ev_timer_start (loop, &mytimer); |
|
|
745 | |
|
|
746 | Example: create a timeout timer that times out after 10 seconds of |
|
|
747 | inactivity. |
|
|
748 | |
|
|
749 | static void |
|
|
750 | timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents) |
|
|
751 | { |
|
|
752 | .. ten seconds without any activity |
|
|
753 | } |
|
|
754 | |
|
|
755 | struct ev_timer mytimer; |
|
|
756 | ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */ |
|
|
757 | ev_timer_again (&mytimer); /* start timer */ |
|
|
758 | ev_loop (loop, 0); |
|
|
759 | |
|
|
760 | // and in some piece of code that gets executed on any "activity": |
|
|
761 | // reset the timeout to start ticking again at 10 seconds |
|
|
762 | ev_timer_again (&mytimer); |
|
|
763 | |
|
|
764 | |
500 | =head2 C<ev_periodic> - to cron or not to cron |
765 | =head2 C<ev_periodic> - to cron or not to cron |
501 | |
766 | |
502 | Periodic watchers are also timers of a kind, but they are very versatile |
767 | Periodic watchers are also timers of a kind, but they are very versatile |
503 | (and unfortunately a bit complex). |
768 | (and unfortunately a bit complex). |
504 | |
769 | |
… | |
… | |
512 | again). |
777 | again). |
513 | |
778 | |
514 | They can also be used to implement vastly more complex timers, such as |
779 | They can also be used to implement vastly more complex timers, such as |
515 | triggering an event on eahc midnight, local time. |
780 | triggering an event on eahc midnight, local time. |
516 | |
781 | |
|
|
782 | As with timers, the callback is guarenteed to be invoked only when the |
|
|
783 | time (C<at>) has been passed, but if multiple periodic timers become ready |
|
|
784 | during the same loop iteration then order of execution is undefined. |
|
|
785 | |
517 | =over 4 |
786 | =over 4 |
518 | |
787 | |
519 | =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) |
788 | =item ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb) |
520 | |
789 | |
521 | =item ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb) |
790 | =item ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb) |
522 | |
791 | |
523 | Lots of arguments, lets sort it out... There are basically three modes of |
792 | Lots of arguments, lets sort it out... There are basically three modes of |
524 | operation, and we will explain them from simplest to complex: |
793 | operation, and we will explain them from simplest to complex: |
525 | |
|
|
526 | |
794 | |
527 | =over 4 |
795 | =over 4 |
528 | |
796 | |
529 | =item * absolute timer (interval = reschedule_cb = 0) |
797 | =item * absolute timer (interval = reschedule_cb = 0) |
530 | |
798 | |
… | |
… | |
558 | In this mode the values for C<interval> and C<at> are both being |
826 | In this mode the values for C<interval> and C<at> are both being |
559 | ignored. Instead, each time the periodic watcher gets scheduled, the |
827 | ignored. Instead, each time the periodic watcher gets scheduled, the |
560 | reschedule callback will be called with the watcher as first, and the |
828 | reschedule callback will be called with the watcher as first, and the |
561 | current time as second argument. |
829 | current time as second argument. |
562 | |
830 | |
563 | NOTE: I<This callback MUST NOT stop or destroy the periodic or any other |
831 | NOTE: I<This callback MUST NOT stop or destroy any periodic watcher, |
564 | periodic watcher, ever, or make any event loop modifications>. If you need |
832 | ever, or make any event loop modifications>. If you need to stop it, |
565 | to stop it, return C<now + 1e30> (or so, fudge fudge) and stop it afterwards. |
833 | return C<now + 1e30> (or so, fudge fudge) and stop it afterwards (e.g. by |
566 | |
834 | starting a prepare watcher). |
567 | Also, I<< this callback must always return a time that is later than the |
|
|
568 | passed C<now> value >>. Not even C<now> itself will be ok. |
|
|
569 | |
835 | |
570 | Its prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic *w, |
836 | Its prototype is C<ev_tstamp (*reschedule_cb)(struct ev_periodic *w, |
571 | ev_tstamp now)>, e.g.: |
837 | ev_tstamp now)>, e.g.: |
572 | |
838 | |
573 | static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
839 | static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now) |
… | |
… | |
578 | It must return the next time to trigger, based on the passed time value |
844 | It must return the next time to trigger, based on the passed time value |
579 | (that is, the lowest time value larger than to the second argument). It |
845 | (that is, the lowest time value larger than to the second argument). It |
580 | will usually be called just before the callback will be triggered, but |
846 | will usually be called just before the callback will be triggered, but |
581 | might be called at other times, too. |
847 | might be called at other times, too. |
582 | |
848 | |
|
|
849 | NOTE: I<< This callback must always return a time that is later than the |
|
|
850 | passed C<now> value >>. Not even C<now> itself will do, it I<must> be larger. |
|
|
851 | |
583 | This can be used to create very complex timers, such as a timer that |
852 | This can be used to create very complex timers, such as a timer that |
584 | triggers on each midnight, local time. To do this, you would calculate the |
853 | triggers on each midnight, local time. To do this, you would calculate the |
585 | next midnight after C<now> and return the timestamp value for this. How you do this |
854 | next midnight after C<now> and return the timestamp value for this. How |
586 | is, again, up to you (but it is not trivial). |
855 | you do this is, again, up to you (but it is not trivial, which is the main |
|
|
856 | reason I omitted it as an example). |
587 | |
857 | |
588 | =back |
858 | =back |
589 | |
859 | |
590 | =item ev_periodic_again (loop, ev_periodic *) |
860 | =item ev_periodic_again (loop, ev_periodic *) |
591 | |
861 | |
… | |
… | |
593 | when you changed some parameters or the reschedule callback would return |
863 | when you changed some parameters or the reschedule callback would return |
594 | a different time than the last time it was called (e.g. in a crond like |
864 | a different time than the last time it was called (e.g. in a crond like |
595 | program when the crontabs have changed). |
865 | program when the crontabs have changed). |
596 | |
866 | |
597 | =back |
867 | =back |
|
|
868 | |
|
|
869 | Example: call a callback every hour, or, more precisely, whenever the |
|
|
870 | system clock is divisible by 3600. The callback invocation times have |
|
|
871 | potentially a lot of jittering, but good long-term stability. |
|
|
872 | |
|
|
873 | static void |
|
|
874 | clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) |
|
|
875 | { |
|
|
876 | ... its now a full hour (UTC, or TAI or whatever your clock follows) |
|
|
877 | } |
|
|
878 | |
|
|
879 | struct ev_periodic hourly_tick; |
|
|
880 | ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0); |
|
|
881 | ev_periodic_start (loop, &hourly_tick); |
|
|
882 | |
|
|
883 | Example: the same as above, but use a reschedule callback to do it: |
|
|
884 | |
|
|
885 | #include <math.h> |
|
|
886 | |
|
|
887 | static ev_tstamp |
|
|
888 | my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) |
|
|
889 | { |
|
|
890 | return fmod (now, 3600.) + 3600.; |
|
|
891 | } |
|
|
892 | |
|
|
893 | ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); |
|
|
894 | |
|
|
895 | Example: call a callback every hour, starting now: |
|
|
896 | |
|
|
897 | struct ev_periodic hourly_tick; |
|
|
898 | ev_periodic_init (&hourly_tick, clock_cb, |
|
|
899 | fmod (ev_now (loop), 3600.), 3600., 0); |
|
|
900 | ev_periodic_start (loop, &hourly_tick); |
|
|
901 | |
598 | |
902 | |
599 | =head2 C<ev_signal> - signal me when a signal gets signalled |
903 | =head2 C<ev_signal> - signal me when a signal gets signalled |
600 | |
904 | |
601 | Signal watchers will trigger an event when the process receives a specific |
905 | Signal watchers will trigger an event when the process receives a specific |
602 | signal one or more times. Even though signals are very asynchronous, libev |
906 | signal one or more times. Even though signals are very asynchronous, libev |
… | |
… | |
638 | the status word (use the macros from C<sys/wait.h> and see your systems |
942 | the status word (use the macros from C<sys/wait.h> and see your systems |
639 | C<waitpid> documentation). The C<rpid> member contains the pid of the |
943 | C<waitpid> documentation). The C<rpid> member contains the pid of the |
640 | process causing the status change. |
944 | process causing the status change. |
641 | |
945 | |
642 | =back |
946 | =back |
|
|
947 | |
|
|
948 | Example: try to exit cleanly on SIGINT and SIGTERM. |
|
|
949 | |
|
|
950 | static void |
|
|
951 | sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) |
|
|
952 | { |
|
|
953 | ev_unloop (loop, EVUNLOOP_ALL); |
|
|
954 | } |
|
|
955 | |
|
|
956 | struct ev_signal signal_watcher; |
|
|
957 | ev_signal_init (&signal_watcher, sigint_cb, SIGINT); |
|
|
958 | ev_signal_start (loop, &sigint_cb); |
|
|
959 | |
643 | |
960 | |
644 | =head2 C<ev_idle> - when you've got nothing better to do |
961 | =head2 C<ev_idle> - when you've got nothing better to do |
645 | |
962 | |
646 | Idle watchers trigger events when there are no other events are pending |
963 | Idle watchers trigger events when there are no other events are pending |
647 | (prepare, check and other idle watchers do not count). That is, as long |
964 | (prepare, check and other idle watchers do not count). That is, as long |
… | |
… | |
667 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
984 | kind. There is a C<ev_idle_set> macro, but using it is utterly pointless, |
668 | believe me. |
985 | believe me. |
669 | |
986 | |
670 | =back |
987 | =back |
671 | |
988 | |
|
|
989 | Example: dynamically allocate an C<ev_idle>, start it, and in the |
|
|
990 | callback, free it. Alos, use no error checking, as usual. |
|
|
991 | |
|
|
992 | static void |
|
|
993 | idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents) |
|
|
994 | { |
|
|
995 | free (w); |
|
|
996 | // now do something you wanted to do when the program has |
|
|
997 | // no longer asnything immediate to do. |
|
|
998 | } |
|
|
999 | |
|
|
1000 | struct ev_idle *idle_watcher = malloc (sizeof (struct ev_idle)); |
|
|
1001 | ev_idle_init (idle_watcher, idle_cb); |
|
|
1002 | ev_idle_start (loop, idle_cb); |
|
|
1003 | |
|
|
1004 | |
672 | =head2 C<ev_prepare> and C<ev_check> - customise your event loop |
1005 | =head2 C<ev_prepare> and C<ev_check> - customise your event loop |
673 | |
1006 | |
674 | Prepare and check watchers are usually (but not always) used in tandem: |
1007 | Prepare and check watchers are usually (but not always) used in tandem: |
675 | Prepare watchers get invoked before the process blocks and check watchers |
1008 | prepare watchers get invoked before the process blocks and check watchers |
676 | afterwards. |
1009 | afterwards. |
677 | |
1010 | |
678 | Their main purpose is to integrate other event mechanisms into libev. This |
1011 | Their main purpose is to integrate other event mechanisms into libev. This |
679 | could be used, for example, to track variable changes, implement your own |
1012 | could be used, for example, to track variable changes, implement your own |
680 | watchers, integrate net-snmp or a coroutine library and lots more. |
1013 | watchers, integrate net-snmp or a coroutine library and lots more. |
… | |
… | |
683 | to be watched by the other library, registering C<ev_io> watchers for |
1016 | to be watched by the other library, registering C<ev_io> watchers for |
684 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
1017 | them and starting an C<ev_timer> watcher for any timeouts (many libraries |
685 | provide just this functionality). Then, in the check watcher you check for |
1018 | provide just this functionality). Then, in the check watcher you check for |
686 | any events that occured (by checking the pending status of all watchers |
1019 | any events that occured (by checking the pending status of all watchers |
687 | and stopping them) and call back into the library. The I/O and timer |
1020 | and stopping them) and call back into the library. The I/O and timer |
688 | callbacks will never actually be called (but must be valid neverthelles, |
1021 | callbacks will never actually be called (but must be valid nevertheless, |
689 | because you never know, you know?). |
1022 | because you never know, you know?). |
690 | |
1023 | |
691 | As another example, the Perl Coro module uses these hooks to integrate |
1024 | As another example, the Perl Coro module uses these hooks to integrate |
692 | coroutines into libev programs, by yielding to other active coroutines |
1025 | coroutines into libev programs, by yielding to other active coroutines |
693 | during each prepare and only letting the process block if no coroutines |
1026 | during each prepare and only letting the process block if no coroutines |
694 | are ready to run (its actually more complicated, it only runs coroutines |
1027 | are ready to run (it's actually more complicated: it only runs coroutines |
695 | with priority higher than the event loop and one lower priority once, |
1028 | with priority higher than or equal to the event loop and one coroutine |
696 | using idle watchers to keep the event loop from blocking if lower-priority |
1029 | of lower priority, but only once, using idle watchers to keep the event |
697 | coroutines exist, thus mapping low-priority coroutines to idle/background |
1030 | loop from blocking if lower-priority coroutines are active, thus mapping |
698 | tasks). |
1031 | low-priority coroutines to idle/background tasks). |
699 | |
1032 | |
700 | =over 4 |
1033 | =over 4 |
701 | |
1034 | |
702 | =item ev_prepare_init (ev_prepare *, callback) |
1035 | =item ev_prepare_init (ev_prepare *, callback) |
703 | |
1036 | |
… | |
… | |
707 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
1040 | parameters of any kind. There are C<ev_prepare_set> and C<ev_check_set> |
708 | macros, but using them is utterly, utterly and completely pointless. |
1041 | macros, but using them is utterly, utterly and completely pointless. |
709 | |
1042 | |
710 | =back |
1043 | =back |
711 | |
1044 | |
|
|
1045 | Example: *TODO*. |
|
|
1046 | |
|
|
1047 | |
712 | =head1 OTHER FUNCTIONS |
1048 | =head1 OTHER FUNCTIONS |
713 | |
1049 | |
714 | There are some other functions of possible interest. Described. Here. Now. |
1050 | There are some other functions of possible interest. Described. Here. Now. |
715 | |
1051 | |
716 | =over 4 |
1052 | =over 4 |
… | |
… | |
718 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
1054 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
719 | |
1055 | |
720 | This function combines a simple timer and an I/O watcher, calls your |
1056 | This function combines a simple timer and an I/O watcher, calls your |
721 | callback on whichever event happens first and automatically stop both |
1057 | callback on whichever event happens first and automatically stop both |
722 | watchers. This is useful if you want to wait for a single event on an fd |
1058 | watchers. This is useful if you want to wait for a single event on an fd |
723 | or timeout without havign to allocate/configure/start/stop/free one or |
1059 | or timeout without having to allocate/configure/start/stop/free one or |
724 | more watchers yourself. |
1060 | more watchers yourself. |
725 | |
1061 | |
726 | If C<fd> is less than 0, then no I/O watcher will be started and events |
1062 | If C<fd> is less than 0, then no I/O watcher will be started and events |
727 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
1063 | is being ignored. Otherwise, an C<ev_io> watcher for the given C<fd> and |
728 | C<events> set will be craeted and started. |
1064 | C<events> set will be craeted and started. |
… | |
… | |
731 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
1067 | started. Otherwise an C<ev_timer> watcher with after = C<timeout> (and |
732 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
1068 | repeat = 0) will be started. While C<0> is a valid timeout, it is of |
733 | dubious value. |
1069 | dubious value. |
734 | |
1070 | |
735 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
1071 | The callback has the type C<void (*cb)(int revents, void *arg)> and gets |
736 | passed an events set like normal event callbacks (with a combination of |
1072 | passed an C<revents> set like normal event callbacks (a combination of |
737 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
1073 | C<EV_ERROR>, C<EV_READ>, C<EV_WRITE> or C<EV_TIMEOUT>) and the C<arg> |
738 | value passed to C<ev_once>: |
1074 | value passed to C<ev_once>: |
739 | |
1075 | |
740 | static void stdin_ready (int revents, void *arg) |
1076 | static void stdin_ready (int revents, void *arg) |
741 | { |
1077 | { |
… | |
… | |
762 | |
1098 | |
763 | Feed an event as if the given signal occured (loop must be the default loop!). |
1099 | Feed an event as if the given signal occured (loop must be the default loop!). |
764 | |
1100 | |
765 | =back |
1101 | =back |
766 | |
1102 | |
|
|
1103 | |
|
|
1104 | =head1 LIBEVENT EMULATION |
|
|
1105 | |
|
|
1106 | Libev offers a compatibility emulation layer for libevent. It cannot |
|
|
1107 | emulate the internals of libevent, so here are some usage hints: |
|
|
1108 | |
|
|
1109 | =over 4 |
|
|
1110 | |
|
|
1111 | =item * Use it by including <event.h>, as usual. |
|
|
1112 | |
|
|
1113 | =item * The following members are fully supported: ev_base, ev_callback, |
|
|
1114 | ev_arg, ev_fd, ev_res, ev_events. |
|
|
1115 | |
|
|
1116 | =item * Avoid using ev_flags and the EVLIST_*-macros, while it is |
|
|
1117 | maintained by libev, it does not work exactly the same way as in libevent (consider |
|
|
1118 | it a private API). |
|
|
1119 | |
|
|
1120 | =item * Priorities are not currently supported. Initialising priorities |
|
|
1121 | will fail and all watchers will have the same priority, even though there |
|
|
1122 | is an ev_pri field. |
|
|
1123 | |
|
|
1124 | =item * Other members are not supported. |
|
|
1125 | |
|
|
1126 | =item * The libev emulation is I<not> ABI compatible to libevent, you need |
|
|
1127 | to use the libev header file and library. |
|
|
1128 | |
|
|
1129 | =back |
|
|
1130 | |
|
|
1131 | =head1 C++ SUPPORT |
|
|
1132 | |
|
|
1133 | TBD. |
|
|
1134 | |
767 | =head1 AUTHOR |
1135 | =head1 AUTHOR |
768 | |
1136 | |
769 | Marc Lehmann <libev@schmorp.de>. |
1137 | Marc Lehmann <libev@schmorp.de>. |
770 | |
1138 | |