… | |
… | |
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 | |
… | |
… | |
198 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
198 | =item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) |
199 | |
199 | |
200 | 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 |
201 | 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 |
202 | 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 |
203 | completely useless). For this reason its not being "autodetected" unless |
203 | completely useless). For this reason its not being "autodetected" |
204 | 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>). |
205 | |
206 | |
206 | 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 |
207 | 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 |
208 | 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 |
209 | 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 |
… | |
… | |
233 | 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 |
234 | 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 |
235 | specified, most compiled-in backend will be tried, usually in reverse |
236 | specified, most compiled-in backend will be tried, usually in reverse |
236 | order of their flag values :) |
237 | order of their flag values :) |
237 | |
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); |
|
|
254 | |
238 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
255 | =item struct ev_loop *ev_loop_new (unsigned int flags) |
239 | |
256 | |
240 | 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 |
241 | 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 |
242 | handle signal and child watchers, and attempts to do so will be greeted by |
259 | handle signal and child watchers, and attempts to do so will be greeted by |
… | |
… | |
297 | |
314 | |
298 | 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 |
299 | 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 |
300 | events. |
317 | events. |
301 | |
318 | |
302 | 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 |
303 | 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. |
304 | |
321 | |
305 | 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 |
306 | 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 |
307 | 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. |
308 | |
325 | |
309 | 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 |
310 | 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 |
311 | 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 |
312 | 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. |
313 | |
333 | |
314 | This flags value could be used to implement alternative looping |
|
|
315 | constructs, but the C<prepare> and C<check> watchers provide a better and |
|
|
316 | more generic mechanism. |
|
|
317 | |
|
|
318 | Here are the gory details of what ev_loop does: |
334 | Here are the gory details of what C<ev_loop> does: |
319 | |
335 | |
320 | 1. If there are no active watchers (reference count is zero), return. |
336 | * If there are no active watchers (reference count is zero), return. |
321 | 2. Queue and immediately call all prepare watchers. |
337 | - Queue prepare watchers and then call all outstanding watchers. |
322 | 3. If we have been forked, recreate the kernel state. |
338 | - If we have been forked, recreate the kernel state. |
323 | 4. Update the kernel state with all outstanding changes. |
339 | - Update the kernel state with all outstanding changes. |
324 | 5. Update the "event loop time". |
340 | - Update the "event loop time". |
325 | 6. Calculate for how long to block. |
341 | - Calculate for how long to block. |
326 | 7. Block the process, waiting for events. |
342 | - Block the process, waiting for any events. |
|
|
343 | - Queue all outstanding I/O (fd) events. |
327 | 8. Update the "event loop time" and do time jump handling. |
344 | - Update the "event loop time" and do time jump handling. |
328 | 9. Queue all outstanding timers. |
345 | - Queue all outstanding timers. |
329 | 10. Queue all outstanding periodics. |
346 | - Queue all outstanding periodics. |
330 | 11. If no events are pending now, queue all idle watchers. |
347 | - If no events are pending now, queue all idle watchers. |
331 | 12. Queue all check watchers. |
348 | - Queue all check watchers. |
332 | 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. |
333 | 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 |
334 | was used, return, otherwise continue with step #1. |
353 | were used, return, otherwise continue with step *. |
335 | |
354 | |
336 | =item ev_unloop (loop, how) |
355 | =item ev_unloop (loop, how) |
337 | |
356 | |
338 | 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 |
339 | 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 |