… | |
… | |
89 | Return the set of all backends compiled into this binary of libev and also |
89 | Return the set of all backends compiled into this binary of libev and also |
90 | recommended for this platform. This set is often smaller than the one |
90 | recommended for this platform. This set is often smaller than the one |
91 | returned by C<ev_supported_backends>, as for example kqueue is broken on |
91 | returned by C<ev_supported_backends>, as for example kqueue is broken on |
92 | most BSDs and will not be autodetected unless you explicitly request it |
92 | most BSDs and will not be autodetected unless you explicitly request it |
93 | (assuming you know what you are doing). This is the set of backends that |
93 | (assuming you know what you are doing). This is the set of backends that |
94 | C<EVFLAG_AUTO> will probe for. |
94 | libev will probe for if you specify no backends explicitly. |
95 | |
95 | |
96 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
96 | =item ev_set_allocator (void *(*cb)(void *ptr, long size)) |
97 | |
97 | |
98 | Sets the allocation function to use (the prototype is similar to the |
98 | Sets the allocation function to use (the prototype is similar to the |
99 | realloc C function, the semantics are identical). It is used to allocate |
99 | realloc C function, the semantics are identical). It is used to allocate |
… | |
… | |
141 | |
141 | |
142 | If you don't know what event loop to use, use the one returned from this |
142 | If you don't know what event loop to use, use the one returned from this |
143 | function. |
143 | function. |
144 | |
144 | |
145 | The flags argument can be used to specify special behaviour or specific |
145 | The flags argument can be used to specify special behaviour or specific |
146 | backends to use, and is usually specified as C<0> (or EVFLAG_AUTO). |
146 | backends to use, and is usually specified as C<0> (or C<EVFLAG_AUTO>). |
147 | |
147 | |
148 | It supports the following flags: |
148 | The following flags are supported: |
149 | |
149 | |
150 | =over 4 |
150 | =over 4 |
151 | |
151 | |
152 | =item C<EVFLAG_AUTO> |
152 | =item C<EVFLAG_AUTO> |
153 | |
153 | |
… | |
… | |
189 | result in some caching, there is still a syscall per such incident |
189 | result in some caching, there is still a syscall per such incident |
190 | (because the fd could point to a different file description now), so its |
190 | (because the fd could point to a different file description now), so its |
191 | best to avoid that. Also, dup()ed file descriptors might not work very |
191 | best to avoid that. Also, dup()ed file descriptors might not work very |
192 | well if you register events for both fds. |
192 | well if you register events for both fds. |
193 | |
193 | |
|
|
194 | Please note that epoll sometimes generates spurious notifications, so you |
|
|
195 | need to use non-blocking I/O or other means to avoid blocking when no data |
|
|
196 | (or space) is available. |
|
|
197 | |
194 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
198 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
195 | |
199 | |
196 | Kqueue deserves special mention, as at the time of this writing, it |
200 | Kqueue deserves special mention, as at the time of this writing, it |
197 | was broken on all BSDs except NetBSD (usually it doesn't work with |
201 | was broken on all BSDs except NetBSD (usually it doesn't work with |
198 | anything but sockets and pipes, except on Darwin, where of course its |
202 | anything but sockets and pipes, except on Darwin, where of course its |
199 | completely useless). For this reason its not being "autodetected" unless |
203 | completely useless). For this reason its not being "autodetected" |
200 | you explicitly specify the flags (i.e. you don't use EVFLAG_AUTO). |
204 | unless you explicitly specify it explicitly in the flags (i.e. using |
|
|
205 | C<EVBACKEND_KQUEUE>). |
201 | |
206 | |
202 | It scales in the same way as the epoll backend, but the interface to the |
207 | It scales in the same way as the epoll backend, but the interface to the |
203 | kernel is more efficient (which says nothing about its actual speed, of |
208 | kernel is more efficient (which says nothing about its actual speed, of |
204 | course). While starting and stopping an I/O watcher does not cause an |
209 | course). While starting and stopping an I/O watcher does not cause an |
205 | extra syscall as with epoll, it still adds up to four event changes per |
210 | extra syscall as with epoll, it still adds up to four event changes per |
… | |
… | |
212 | =item C<EVBACKEND_PORT> (value 32, Solaris 10) |
217 | =item C<EVBACKEND_PORT> (value 32, Solaris 10) |
213 | |
218 | |
214 | This uses the Solaris 10 port mechanism. As with everything on Solaris, |
219 | This uses the Solaris 10 port mechanism. As with everything on Solaris, |
215 | it's really slow, but it still scales very well (O(active_fds)). |
220 | it's really slow, but it still scales very well (O(active_fds)). |
216 | |
221 | |
|
|
222 | Please note that solaris ports can result in a lot of spurious |
|
|
223 | notifications, so you need to use non-blocking I/O or other means to avoid |
|
|
224 | blocking when no data (or space) is available. |
|
|
225 | |
217 | =item C<EVBACKEND_ALL> |
226 | =item C<EVBACKEND_ALL> |
218 | |
227 | |
219 | Try all backends (even potentially broken ones that wouldn't be tried |
228 | Try all backends (even potentially broken ones that wouldn't be tried |
220 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
229 | with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as |
221 | C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. |
230 | C<EVBACKEND_ALL & ~EVBACKEND_KQUEUE>. |
… | |
… | |
224 | |
233 | |
225 | If one or more of these are ored into the flags value, then only these |
234 | If one or more of these are ored into the flags value, then only these |
226 | backends will be tried (in the reverse order as given here). If none are |
235 | backends will be tried (in the reverse order as given here). If none are |
227 | specified, most compiled-in backend will be tried, usually in reverse |
236 | specified, most compiled-in backend will be tried, usually in reverse |
228 | order of their flag values :) |
237 | order of their flag values :) |
|
|
238 | |
|
|
239 | The most typical usage is like this: |
|
|
240 | |
|
|
241 | if (!ev_default_loop (0)) |
|
|
242 | fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); |
|
|
243 | |
|
|
244 | Restrict libev to the select and poll backends, and do not allow |
|
|
245 | environment settings to be taken into account: |
|
|
246 | |
|
|
247 | ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); |
|
|
248 | |
|
|
249 | Use whatever libev has to offer, but make sure that kqueue is used if |
|
|
250 | available (warning, breaks stuff, best use only with your own private |
|
|
251 | event loop and only if you know the OS supports your types of fds): |
|
|
252 | |
|
|
253 | ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); |
229 | |
254 | |
230 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
255 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
231 | |
256 | |
232 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
257 | Similar to C<ev_default_loop>, but always creates a new event loop that is |
233 | always distinct from the default loop. Unlike the default loop, it cannot |
258 | always distinct from the default loop. Unlike the default loop, it cannot |
… | |
… | |
289 | |
314 | |
290 | Finally, this is it, the event handler. This function usually is called |
315 | Finally, this is it, the event handler. This function usually is called |
291 | after you initialised all your watchers and you want to start handling |
316 | after you initialised all your watchers and you want to start handling |
292 | events. |
317 | events. |
293 | |
318 | |
294 | If the flags argument is specified as 0, it will not return until either |
319 | If the flags argument is specified as C<0>, it will not return until |
295 | no event watchers are active anymore or C<ev_unloop> was called. |
320 | either no event watchers are active anymore or C<ev_unloop> was called. |
296 | |
321 | |
297 | A flags value of C<EVLOOP_NONBLOCK> will look for new events, will handle |
322 | A flags value of C<EVLOOP_NONBLOCK> will look for new events, will handle |
298 | those events and any outstanding ones, but will not block your process in |
323 | those events and any outstanding ones, but will not block your process in |
299 | case there are no events and will return after one iteration of the loop. |
324 | case there are no events and will return after one iteration of the loop. |
300 | |
325 | |
301 | A flags value of C<EVLOOP_ONESHOT> will look for new events (waiting if |
326 | A flags value of C<EVLOOP_ONESHOT> will look for new events (waiting if |
302 | neccessary) and will handle those and any outstanding ones. It will block |
327 | neccessary) and will handle those and any outstanding ones. It will block |
303 | your process until at least one new event arrives, and will return after |
328 | your process until at least one new event arrives, and will return after |
304 | one iteration of the loop. |
329 | one iteration of the loop. This is useful if you are waiting for some |
|
|
330 | external event in conjunction with something not expressible using other |
|
|
331 | libev watchers. However, a pair of C<ev_prepare>/C<ev_check> watchers is |
|
|
332 | usually a better approach for this kind of thing. |
305 | |
333 | |
306 | This flags value could be used to implement alternative looping |
|
|
307 | constructs, but the C<prepare> and C<check> watchers provide a better and |
|
|
308 | more generic mechanism. |
|
|
309 | |
|
|
310 | Here are the gory details of what ev_loop does: |
334 | Here are the gory details of what C<ev_loop> does: |
311 | |
335 | |
312 | 1. If there are no active watchers (reference count is zero), return. |
336 | * If there are no active watchers (reference count is zero), return. |
313 | 2. Queue and immediately call all prepare watchers. |
337 | - Queue prepare watchers and then call all outstanding watchers. |
314 | 3. If we have been forked, recreate the kernel state. |
338 | - If we have been forked, recreate the kernel state. |
315 | 4. Update the kernel state with all outstanding changes. |
339 | - Update the kernel state with all outstanding changes. |
316 | 5. Update the "event loop time". |
340 | - Update the "event loop time". |
317 | 6. Calculate for how long to block. |
341 | - Calculate for how long to block. |
318 | 7. Block the process, waiting for events. |
342 | - Block the process, waiting for any events. |
|
|
343 | - Queue all outstanding I/O (fd) events. |
319 | 8. Update the "event loop time" and do time jump handling. |
344 | - Update the "event loop time" and do time jump handling. |
320 | 9. Queue all outstanding timers. |
345 | - Queue all outstanding timers. |
321 | 10. Queue all outstanding periodics. |
346 | - Queue all outstanding periodics. |
322 | 11. If no events are pending now, queue all idle watchers. |
347 | - If no events are pending now, queue all idle watchers. |
323 | 12. Queue all check watchers. |
348 | - Queue all check watchers. |
324 | 13. Call all queued watchers in reverse order (i.e. check watchers first). |
349 | - Call all queued watchers in reverse order (i.e. check watchers first). |
|
|
350 | Signals and child watchers are implemented as I/O watchers, and will |
|
|
351 | be handled here by queueing them when their watcher gets executed. |
325 | 14. If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
352 | - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK |
326 | was used, return, otherwise continue with step #1. |
353 | were used, return, otherwise continue with step *. |
327 | |
354 | |
328 | =item ev_unloop (loop, how) |
355 | =item ev_unloop (loop, how) |
329 | |
356 | |
330 | Can be used to make a call to C<ev_loop> return early (but only after it |
357 | Can be used to make a call to C<ev_loop> return early (but only after it |
331 | has processed all outstanding events). The C<how> argument must be either |
358 | has processed all outstanding events). The C<how> argument must be either |
… | |
… | |
528 | |
555 | |
529 | Configures an C<ev_io> watcher. The fd is the file descriptor to rceeive |
556 | Configures an C<ev_io> watcher. The fd is the file descriptor to rceeive |
530 | events for and events is either C<EV_READ>, C<EV_WRITE> or C<EV_READ | |
557 | events for and events is either C<EV_READ>, C<EV_WRITE> or C<EV_READ | |
531 | EV_WRITE> to receive the given events. |
558 | EV_WRITE> to receive the given events. |
532 | |
559 | |
|
|
560 | Please note that most of the more scalable backend mechanisms (for example |
|
|
561 | epoll and solaris ports) can result in spurious readyness notifications |
|
|
562 | for file descriptors, so you practically need to use non-blocking I/O (and |
|
|
563 | treat callback invocation as hint only), or retest separately with a safe |
|
|
564 | interface before doing I/O (XLib can do this), or force the use of either |
|
|
565 | C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>, which don't suffer from this |
|
|
566 | problem. Also note that it is quite easy to have your callback invoked |
|
|
567 | when the readyness condition is no longer valid even when employing |
|
|
568 | typical ways of handling events, so its a good idea to use non-blocking |
|
|
569 | I/O unconditionally. |
|
|
570 | |
533 | =back |
571 | =back |
534 | |
572 | |
535 | =head2 C<ev_timer> - relative and optionally recurring timeouts |
573 | =head2 C<ev_timer> - relative and optionally recurring timeouts |
536 | |
574 | |
537 | Timer watchers are simple relative timers that generate an event after a |
575 | Timer watchers are simple relative timers that generate an event after a |