ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.3
(Generate patch)

Comparing libev/ev.3 (file contents):
Revision 1.5 by root, Fri Nov 23 04:36:03 2007 UTC vs.
Revision 1.9 by root, Fri Nov 23 16:17:12 2007 UTC

173.IX Header "TIME REPRESENTATION" 173.IX Header "TIME REPRESENTATION"
174Libev represents time as a single floating point number, representing the 174Libev represents time as a single floating point number, representing the
175(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near 175(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near
176the beginning of 1970, details are complicated, don't ask). This type is 176the beginning of 1970, details are complicated, don't ask). This type is
177called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases 177called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases
178to the double type in C. 178to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on
179it, you should treat it as such.
179.SH "GLOBAL FUNCTIONS" 180.SH "GLOBAL FUNCTIONS"
180.IX Header "GLOBAL FUNCTIONS" 181.IX Header "GLOBAL FUNCTIONS"
181These functions can be called anytime, even before initialising the 182These functions can be called anytime, even before initialising the
182library in any way. 183library in any way.
183.IP "ev_tstamp ev_time ()" 4 184.IP "ev_tstamp ev_time ()" 4
199.Sp 200.Sp
200Usually, it's a good idea to terminate if the major versions mismatch, 201Usually, it's a good idea to terminate if the major versions mismatch,
201as this indicates an incompatible change. Minor versions are usually 202as this indicates an incompatible change. Minor versions are usually
202compatible to older versions, so a larger minor version alone is usually 203compatible to older versions, so a larger minor version alone is usually
203not a problem. 204not a problem.
205.Sp
206Example: make sure we haven't accidentally been linked against the wrong
207version:
208.Sp
209.Vb 3
210\& assert (("libev version mismatch",
211\& ev_version_major () == EV_VERSION_MAJOR
212\& && ev_version_minor () >= EV_VERSION_MINOR));
213.Ve
214.IP "unsigned int ev_supported_backends ()" 4
215.IX Item "unsigned int ev_supported_backends ()"
216Return the set of all backends (i.e. their corresponding \f(CW\*(C`EV_BACKEND_*\*(C'\fR
217value) compiled into this binary of libev (independent of their
218availability on the system you are running on). See \f(CW\*(C`ev_default_loop\*(C'\fR for
219a description of the set values.
220.Sp
221Example: make sure we have the epoll method, because yeah this is cool and
222a must have and can we have a torrent of it please!!!11
223.Sp
224.Vb 2
225\& assert (("sorry, no epoll, no sex",
226\& ev_supported_backends () & EVBACKEND_EPOLL));
227.Ve
228.IP "unsigned int ev_recommended_backends ()" 4
229.IX Item "unsigned int ev_recommended_backends ()"
230Return the set of all backends compiled into this binary of libev and also
231recommended for this platform. This set is often smaller than the one
232returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on
233most BSDs and will not be autodetected unless you explicitly request it
234(assuming you know what you are doing). This is the set of backends that
235libev will probe for if you specify no backends explicitly.
204.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 236.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4
205.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" 237.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))"
206Sets the allocation function to use (the prototype is similar to the 238Sets the allocation function to use (the prototype is similar to the
207realloc C function, the semantics are identical). It is used to allocate 239realloc C function, the semantics are identical). It is used to allocate
208and free memory (no surprises here). If it returns zero when memory 240and free memory (no surprises here). If it returns zero when memory
210destructive action. The default is your system realloc function. 242destructive action. The default is your system realloc function.
211.Sp 243.Sp
212You could override this function in high-availability programs to, say, 244You could override this function in high-availability programs to, say,
213free some memory if it cannot allocate memory, to use a special allocator, 245free some memory if it cannot allocate memory, to use a special allocator,
214or even to sleep a while and retry until some memory is available. 246or even to sleep a while and retry until some memory is available.
247.Sp
248Example: replace the libev allocator with one that waits a bit and then
249retries: better than mine).
250.Sp
251.Vb 6
252\& static void *
253\& persistent_realloc (void *ptr, long size)
254\& {
255\& for (;;)
256\& {
257\& void *newptr = realloc (ptr, size);
258.Ve
259.Sp
260.Vb 2
261\& if (newptr)
262\& return newptr;
263.Ve
264.Sp
265.Vb 3
266\& sleep (60);
267\& }
268\& }
269.Ve
270.Sp
271.Vb 2
272\& ...
273\& ev_set_allocator (persistent_realloc);
274.Ve
215.IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4 275.IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4
216.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));" 276.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));"
217Set the callback function to call on a retryable syscall error (such 277Set the callback function to call on a retryable syscall error (such
218as failed select, poll, epoll_wait). The message is a printable string 278as failed select, poll, epoll_wait). The message is a printable string
219indicating the system call or subsystem causing the problem. If this 279indicating the system call or subsystem causing the problem. If this
220callback is set, then libev will expect it to remedy the sitution, no 280callback is set, then libev will expect it to remedy the sitution, no
221matter what, when it returns. That is, libev will generally retry the 281matter what, when it returns. That is, libev will generally retry the
222requested operation, or, if the condition doesn't go away, do bad stuff 282requested operation, or, if the condition doesn't go away, do bad stuff
223(such as abort). 283(such as abort).
284.Sp
285Example: do the same thing as libev does internally:
286.Sp
287.Vb 6
288\& static void
289\& fatal_error (const char *msg)
290\& {
291\& perror (msg);
292\& abort ();
293\& }
294.Ve
295.Sp
296.Vb 2
297\& ...
298\& ev_set_syserr_cb (fatal_error);
299.Ve
224.SH "FUNCTIONS CONTROLLING THE EVENT LOOP" 300.SH "FUNCTIONS CONTROLLING THE EVENT LOOP"
225.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" 301.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP"
226An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two 302An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two
227types of such loops, the \fIdefault\fR loop, which supports signals and child 303types of such loops, the \fIdefault\fR loop, which supports signals and child
228events, and dynamically created loops which do not. 304events, and dynamically created loops which do not.
236.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 312.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4
237.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" 313.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)"
238This will initialise the default event loop if it hasn't been initialised 314This will initialise the default event loop if it hasn't been initialised
239yet and return it. If the default loop could not be initialised, returns 315yet and return it. If the default loop could not be initialised, returns
240false. If it already was initialised it simply returns it (and ignores the 316false. If it already was initialised it simply returns it (and ignores the
241flags). 317flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards).
242.Sp 318.Sp
243If you don't know what event loop to use, use the one returned from this 319If you don't know what event loop to use, use the one returned from this
244function. 320function.
245.Sp 321.Sp
246The flags argument can be used to specify special behaviour or specific 322The flags argument can be used to specify special behaviour or specific
247backends to use, and is usually specified as 0 (or \s-1EVFLAG_AUTO\s0). 323backends to use, and is usually specified as \f(CW0\fR (or \f(CW\*(C`EVFLAG_AUTO\*(C'\fR).
248.Sp 324.Sp
249It supports the following flags: 325The following flags are supported:
250.RS 4 326.RS 4
251.ie n .IP """EVFLAG_AUTO""" 4 327.ie n .IP """EVFLAG_AUTO""" 4
252.el .IP "\f(CWEVFLAG_AUTO\fR" 4 328.el .IP "\f(CWEVFLAG_AUTO\fR" 4
253.IX Item "EVFLAG_AUTO" 329.IX Item "EVFLAG_AUTO"
254The default flags value. Use this if you have no clue (it's the right 330The default flags value. Use this if you have no clue (it's the right
260or setgid) then libev will \fInot\fR look at the environment variable 336or setgid) then libev will \fInot\fR look at the environment variable
261\&\f(CW\*(C`LIBEV_FLAGS\*(C'\fR. Otherwise (the default), this environment variable will 337\&\f(CW\*(C`LIBEV_FLAGS\*(C'\fR. Otherwise (the default), this environment variable will
262override the flags completely if it is found in the environment. This is 338override the flags completely if it is found in the environment. This is
263useful to try out specific backends to test their performance, or to work 339useful to try out specific backends to test their performance, or to work
264around bugs. 340around bugs.
265.ie n .IP """EVMETHOD_SELECT"" (value 1, portable select backend)" 4 341.ie n .IP """EVBACKEND_SELECT"" (value 1, portable select backend)" 4
266.el .IP "\f(CWEVMETHOD_SELECT\fR (value 1, portable select backend)" 4 342.el .IP "\f(CWEVBACKEND_SELECT\fR (value 1, portable select backend)" 4
267.IX Item "EVMETHOD_SELECT (value 1, portable select backend)" 343.IX Item "EVBACKEND_SELECT (value 1, portable select backend)"
268This is your standard \fIselect\fR\|(2) backend. Not \fIcompletely\fR standard, as 344This is your standard \fIselect\fR\|(2) backend. Not \fIcompletely\fR standard, as
269libev tries to roll its own fd_set with no limits on the number of fds, 345libev tries to roll its own fd_set with no limits on the number of fds,
270but if that fails, expect a fairly low limit on the number of fds when 346but if that fails, expect a fairly low limit on the number of fds when
271using this backend. It doesn't scale too well (O(highest_fd)), but its usually 347using this backend. It doesn't scale too well (O(highest_fd)), but its usually
272the fastest backend for a low number of fds. 348the fastest backend for a low number of fds.
273.ie n .IP """EVMETHOD_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 349.ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4
274.el .IP "\f(CWEVMETHOD_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 350.el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4
275.IX Item "EVMETHOD_POLL (value 2, poll backend, available everywhere except on windows)" 351.IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)"
276And this is your standard \fIpoll\fR\|(2) backend. It's more complicated than 352And this is your standard \fIpoll\fR\|(2) backend. It's more complicated than
277select, but handles sparse fds better and has no artificial limit on the 353select, but handles sparse fds better and has no artificial limit on the
278number of fds you can use (except it will slow down considerably with a 354number of fds you can use (except it will slow down considerably with a
279lot of inactive fds). It scales similarly to select, i.e. O(total_fds). 355lot of inactive fds). It scales similarly to select, i.e. O(total_fds).
280.ie n .IP """EVMETHOD_EPOLL"" (value 4, Linux)" 4 356.ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4
281.el .IP "\f(CWEVMETHOD_EPOLL\fR (value 4, Linux)" 4 357.el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4
282.IX Item "EVMETHOD_EPOLL (value 4, Linux)" 358.IX Item "EVBACKEND_EPOLL (value 4, Linux)"
283For few fds, this backend is a bit little slower than poll and select, 359For few fds, this backend is a bit little slower than poll and select,
284but it scales phenomenally better. While poll and select usually scale like 360but it scales phenomenally better. While poll and select usually scale like
285O(total_fds) where n is the total number of fds (or the highest fd), epoll scales 361O(total_fds) where n is the total number of fds (or the highest fd), epoll scales
286either O(1) or O(active_fds). 362either O(1) or O(active_fds).
287.Sp 363.Sp
288While stopping and starting an I/O watcher in the same iteration will 364While stopping and starting an I/O watcher in the same iteration will
289result in some caching, there is still a syscall per such incident 365result in some caching, there is still a syscall per such incident
290(because the fd could point to a different file description now), so its 366(because the fd could point to a different file description now), so its
291best to avoid that. Also, \fIdup()\fRed file descriptors might not work very 367best to avoid that. Also, \fIdup()\fRed file descriptors might not work very
292well if you register events for both fds. 368well if you register events for both fds.
369.Sp
370Please note that epoll sometimes generates spurious notifications, so you
371need to use non-blocking I/O or other means to avoid blocking when no data
372(or space) is available.
293.ie n .IP """EVMETHOD_KQUEUE"" (value 8, most \s-1BSD\s0 clones)" 4 373.ie n .IP """EVBACKEND_KQUEUE"" (value 8, most \s-1BSD\s0 clones)" 4
294.el .IP "\f(CWEVMETHOD_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4 374.el .IP "\f(CWEVBACKEND_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4
295.IX Item "EVMETHOD_KQUEUE (value 8, most BSD clones)" 375.IX Item "EVBACKEND_KQUEUE (value 8, most BSD clones)"
296Kqueue deserves special mention, as at the time of this writing, it 376Kqueue deserves special mention, as at the time of this writing, it
297was broken on all BSDs except NetBSD (usually it doesn't work with 377was broken on all BSDs except NetBSD (usually it doesn't work with
298anything but sockets and pipes, except on Darwin, where of course its 378anything but sockets and pipes, except on Darwin, where of course its
299completely useless). For this reason its not being \*(L"autodetected\*(R" unless 379completely useless). For this reason its not being \*(L"autodetected\*(R"
300you explicitly specify the flags (i.e. you don't use \s-1EVFLAG_AUTO\s0). 380unless you explicitly specify it explicitly in the flags (i.e. using
381\&\f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR).
301.Sp 382.Sp
302It scales in the same way as the epoll backend, but the interface to the 383It scales in the same way as the epoll backend, but the interface to the
303kernel is more efficient (which says nothing about its actual speed, of 384kernel is more efficient (which says nothing about its actual speed, of
304course). While starting and stopping an I/O watcher does not cause an 385course). While starting and stopping an I/O watcher does not cause an
305extra syscall as with epoll, it still adds up to four event changes per 386extra syscall as with epoll, it still adds up to four event changes per
306incident, so its best to avoid that. 387incident, so its best to avoid that.
307.ie n .IP """EVMETHOD_DEVPOLL"" (value 16, Solaris 8)" 4 388.ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4
308.el .IP "\f(CWEVMETHOD_DEVPOLL\fR (value 16, Solaris 8)" 4 389.el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4
309.IX Item "EVMETHOD_DEVPOLL (value 16, Solaris 8)" 390.IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)"
310This is not implemented yet (and might never be). 391This is not implemented yet (and might never be).
311.ie n .IP """EVMETHOD_PORT"" (value 32, Solaris 10)" 4 392.ie n .IP """EVBACKEND_PORT"" (value 32, Solaris 10)" 4
312.el .IP "\f(CWEVMETHOD_PORT\fR (value 32, Solaris 10)" 4 393.el .IP "\f(CWEVBACKEND_PORT\fR (value 32, Solaris 10)" 4
313.IX Item "EVMETHOD_PORT (value 32, Solaris 10)" 394.IX Item "EVBACKEND_PORT (value 32, Solaris 10)"
314This uses the Solaris 10 port mechanism. As with everything on Solaris, 395This uses the Solaris 10 port mechanism. As with everything on Solaris,
315it's really slow, but it still scales very well (O(active_fds)). 396it's really slow, but it still scales very well (O(active_fds)).
397.Sp
398Please note that solaris ports can result in a lot of spurious
399notifications, so you need to use non-blocking I/O or other means to avoid
400blocking when no data (or space) is available.
316.ie n .IP """EVMETHOD_ALL""" 4 401.ie n .IP """EVBACKEND_ALL""" 4
317.el .IP "\f(CWEVMETHOD_ALL\fR" 4 402.el .IP "\f(CWEVBACKEND_ALL\fR" 4
318.IX Item "EVMETHOD_ALL" 403.IX Item "EVBACKEND_ALL"
319Try all backends (even potentially broken ones that wouldn't be tried 404Try all backends (even potentially broken ones that wouldn't be tried
320with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as 405with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as
321\&\f(CW\*(C`EVMETHOD_ALL & ~EVMETHOD_KQUEUE\*(C'\fR. 406\&\f(CW\*(C`EVBACKEND_ALL & ~EVBACKEND_KQUEUE\*(C'\fR.
322.RE 407.RE
323.RS 4 408.RS 4
324.Sp 409.Sp
325If one or more of these are ored into the flags value, then only these 410If one or more of these are ored into the flags value, then only these
326backends will be tried (in the reverse order as given here). If none are 411backends will be tried (in the reverse order as given here). If none are
327specified, most compiled-in backend will be tried, usually in reverse 412specified, most compiled-in backend will be tried, usually in reverse
328order of their flag values :) 413order of their flag values :)
414.Sp
415The most typical usage is like this:
416.Sp
417.Vb 2
418\& if (!ev_default_loop (0))
419\& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
420.Ve
421.Sp
422Restrict libev to the select and poll backends, and do not allow
423environment settings to be taken into account:
424.Sp
425.Vb 1
426\& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
427.Ve
428.Sp
429Use whatever libev has to offer, but make sure that kqueue is used if
430available (warning, breaks stuff, best use only with your own private
431event loop and only if you know the \s-1OS\s0 supports your types of fds):
432.Sp
433.Vb 1
434\& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
435.Ve
329.RE 436.RE
330.IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4 437.IP "struct ev_loop *ev_loop_new (unsigned int flags)" 4
331.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" 438.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)"
332Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is 439Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is
333always distinct from the default loop. Unlike the default loop, it cannot 440always distinct from the default loop. Unlike the default loop, it cannot
334handle signal and child watchers, and attempts to do so will be greeted by 441handle signal and child watchers, and attempts to do so will be greeted by
335undefined behaviour (or a failed assertion if assertions are enabled). 442undefined behaviour (or a failed assertion if assertions are enabled).
443.Sp
444Example: try to create a event loop that uses epoll and nothing else.
445.Sp
446.Vb 3
447\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
448\& if (!epoller)
449\& fatal ("no epoll found here, maybe it hides under your chair");
450.Ve
336.IP "ev_default_destroy ()" 4 451.IP "ev_default_destroy ()" 4
337.IX Item "ev_default_destroy ()" 452.IX Item "ev_default_destroy ()"
338Destroys the default loop again (frees all memory and kernel state 453Destroys the default loop again (frees all memory and kernel state
339etc.). This stops all registered event watchers (by not touching them in 454etc.). This stops all registered event watchers (by not touching them in
340any way whatsoever, although you cannot rely on this :). 455any way whatsoever, although you cannot rely on this :).
358quite nicely into a call to \f(CW\*(C`pthread_atfork\*(C'\fR: 473quite nicely into a call to \f(CW\*(C`pthread_atfork\*(C'\fR:
359.Sp 474.Sp
360.Vb 1 475.Vb 1
361\& pthread_atfork (0, 0, ev_default_fork); 476\& pthread_atfork (0, 0, ev_default_fork);
362.Ve 477.Ve
478.Sp
479At the moment, \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR are safe to use
480without calling this function, so if you force one of those backends you
481do not need to care.
363.IP "ev_loop_fork (loop)" 4 482.IP "ev_loop_fork (loop)" 4
364.IX Item "ev_loop_fork (loop)" 483.IX Item "ev_loop_fork (loop)"
365Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by 484Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by
366\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop 485\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop
367after fork, and how you do this is entirely your own problem. 486after fork, and how you do this is entirely your own problem.
368.IP "unsigned int ev_method (loop)" 4 487.IP "unsigned int ev_backend (loop)" 4
369.IX Item "unsigned int ev_method (loop)" 488.IX Item "unsigned int ev_backend (loop)"
370Returns one of the \f(CW\*(C`EVMETHOD_*\*(C'\fR flags indicating the event backend in 489Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in
371use. 490use.
372.IP "ev_tstamp ev_now (loop)" 4 491.IP "ev_tstamp ev_now (loop)" 4
373.IX Item "ev_tstamp ev_now (loop)" 492.IX Item "ev_tstamp ev_now (loop)"
374Returns the current \*(L"event loop time\*(R", which is the time the event loop 493Returns the current \*(L"event loop time\*(R", which is the time the event loop
375got events and started processing them. This timestamp does not change 494received events and started processing them. This timestamp does not
376as long as callbacks are being processed, and this is also the base time 495change as long as callbacks are being processed, and this is also the base
377used for relative timers. You can treat it as the timestamp of the event 496time used for relative timers. You can treat it as the timestamp of the
378occuring (or more correctly, the mainloop finding out about it). 497event occuring (or more correctly, libev finding out about it).
379.IP "ev_loop (loop, int flags)" 4 498.IP "ev_loop (loop, int flags)" 4
380.IX Item "ev_loop (loop, int flags)" 499.IX Item "ev_loop (loop, int flags)"
381Finally, this is it, the event handler. This function usually is called 500Finally, this is it, the event handler. This function usually is called
382after you initialised all your watchers and you want to start handling 501after you initialised all your watchers and you want to start handling
383events. 502events.
384.Sp 503.Sp
385If the flags argument is specified as 0, it will not return until either 504If the flags argument is specified as \f(CW0\fR, it will not return until
386no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. 505either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called.
506.Sp
507Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than
508relying on all watchers to be stopped when deciding when a program has
509finished (especially in interactive programs), but having a program that
510automatically loops as long as it has to and no longer by virtue of
511relying on its watchers stopping correctly is a thing of beauty.
387.Sp 512.Sp
388A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle 513A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle
389those events and any outstanding ones, but will not block your process in 514those events and any outstanding ones, but will not block your process in
390case there are no events and will return after one iteration of the loop. 515case there are no events and will return after one iteration of the loop.
391.Sp 516.Sp
392A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if 517A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if
393neccessary) and will handle those and any outstanding ones. It will block 518neccessary) and will handle those and any outstanding ones. It will block
394your process until at least one new event arrives, and will return after 519your process until at least one new event arrives, and will return after
395one iteration of the loop. 520one iteration of the loop. This is useful if you are waiting for some
521external event in conjunction with something not expressible using other
522libev watchers. However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is
523usually a better approach for this kind of thing.
396.Sp 524.Sp
397This flags value could be used to implement alternative looping
398constructs, but the \f(CW\*(C`prepare\*(C'\fR and \f(CW\*(C`check\*(C'\fR watchers provide a better and
399more generic mechanism.
400.Sp
401Here are the gory details of what ev_loop does: 525Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does:
402.Sp 526.Sp
403.Vb 15 527.Vb 18
404\& 1. If there are no active watchers (reference count is zero), return. 528\& * If there are no active watchers (reference count is zero), return.
405\& 2. Queue and immediately call all prepare watchers. 529\& - Queue prepare watchers and then call all outstanding watchers.
406\& 3. If we have been forked, recreate the kernel state. 530\& - If we have been forked, recreate the kernel state.
407\& 4. Update the kernel state with all outstanding changes. 531\& - Update the kernel state with all outstanding changes.
408\& 5. Update the "event loop time". 532\& - Update the "event loop time".
409\& 6. Calculate for how long to block. 533\& - Calculate for how long to block.
410\& 7. Block the process, waiting for events. 534\& - Block the process, waiting for any events.
535\& - Queue all outstanding I/O (fd) events.
411\& 8. Update the "event loop time" and do time jump handling. 536\& - Update the "event loop time" and do time jump handling.
412\& 9. Queue all outstanding timers. 537\& - Queue all outstanding timers.
413\& 10. Queue all outstanding periodics. 538\& - Queue all outstanding periodics.
414\& 11. If no events are pending now, queue all idle watchers. 539\& - If no events are pending now, queue all idle watchers.
415\& 12. Queue all check watchers. 540\& - Queue all check watchers.
416\& 13. Call all queued watchers in reverse order (i.e. check watchers first). 541\& - Call all queued watchers in reverse order (i.e. check watchers first).
542\& Signals and child watchers are implemented as I/O watchers, and will
543\& be handled here by queueing them when their watcher gets executed.
417\& 14. If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK 544\& - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
418\& was used, return, otherwise continue with step #1. 545\& were used, return, otherwise continue with step *.
546.Ve
547.Sp
548Example: queue some jobs and then loop until no events are outsanding
549anymore.
550.Sp
551.Vb 4
552\& ... queue jobs here, make sure they register event watchers as long
553\& ... as they still have work to do (even an idle watcher will do..)
554\& ev_loop (my_loop, 0);
555\& ... jobs done. yeah!
419.Ve 556.Ve
420.IP "ev_unloop (loop, how)" 4 557.IP "ev_unloop (loop, how)" 4
421.IX Item "ev_unloop (loop, how)" 558.IX Item "ev_unloop (loop, how)"
422Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it 559Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it
423has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either 560has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either
437example, libev itself uses this for its internal signal pipe: It is not 574example, libev itself uses this for its internal signal pipe: It is not
438visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if 575visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if
439no event watchers registered by it are active. It is also an excellent 576no event watchers registered by it are active. It is also an excellent
440way to do this for generic recurring timers or from within third-party 577way to do this for generic recurring timers or from within third-party
441libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR. 578libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR.
579.Sp
580Example: create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR
581running when nothing else is active.
582.Sp
583.Vb 4
584\& struct dv_signal exitsig;
585\& ev_signal_init (&exitsig, sig_cb, SIGINT);
586\& ev_signal_start (myloop, &exitsig);
587\& evf_unref (myloop);
588.Ve
589.Sp
590Example: for some weird reason, unregister the above signal handler again.
591.Sp
592.Vb 2
593\& ev_ref (myloop);
594\& ev_signal_stop (myloop, &exitsig);
595.Ve
442.SH "ANATOMY OF A WATCHER" 596.SH "ANATOMY OF A WATCHER"
443.IX Header "ANATOMY OF A WATCHER" 597.IX Header "ANATOMY OF A WATCHER"
444A watcher is a structure that you create and register to record your 598A watcher is a structure that you create and register to record your
445interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to 599interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to
446become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: 600become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that:
482*)\*(C'\fR), and you can stop watching for events at any time by calling the 636*)\*(C'\fR), and you can stop watching for events at any time by calling the
483corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR. 637corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR.
484.PP 638.PP
485As long as your watcher is active (has been started but not stopped) you 639As long as your watcher is active (has been started but not stopped) you
486must not touch the values stored in it. Most specifically you must never 640must not touch the values stored in it. Most specifically you must never
487reinitialise it or call its set method. 641reinitialise it or call its set macro.
488.PP 642.PP
489You can check whether an event is active by calling the \f(CW\*(C`ev_is_active 643You can check whether an event is active by calling the \f(CW\*(C`ev_is_active
490(watcher *)\*(C'\fR macro. To see whether an event is outstanding (but the 644(watcher *)\*(C'\fR macro. To see whether an event is outstanding (but the
491callback for it has not been called yet) you can use the \f(CW\*(C`ev_is_pending 645callback for it has not been called yet) you can use the \f(CW\*(C`ev_is_pending
492(watcher *)\*(C'\fR macro. 646(watcher *)\*(C'\fR macro.
612descriptors correctly if you register interest in two or more fds pointing 766descriptors correctly if you register interest in two or more fds pointing
613to the same underlying file/socket etc. description (that is, they share 767to the same underlying file/socket etc. description (that is, they share
614the same underlying \*(L"file open\*(R"). 768the same underlying \*(L"file open\*(R").
615.PP 769.PP
616If you must do this, then force the use of a known-to-be-good backend 770If you must do this, then force the use of a known-to-be-good backend
617(at the time of this writing, this includes only \s-1EVMETHOD_SELECT\s0 and 771(at the time of this writing, this includes only \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and
618\&\s-1EVMETHOD_POLL\s0). 772\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR).
619.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 773.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4
620.IX Item "ev_io_init (ev_io *, callback, int fd, int events)" 774.IX Item "ev_io_init (ev_io *, callback, int fd, int events)"
621.PD 0 775.PD 0
622.IP "ev_io_set (ev_io *, int fd, int events)" 4 776.IP "ev_io_set (ev_io *, int fd, int events)" 4
623.IX Item "ev_io_set (ev_io *, int fd, int events)" 777.IX Item "ev_io_set (ev_io *, int fd, int events)"
624.PD 778.PD
625Configures an \f(CW\*(C`ev_io\*(C'\fR watcher. The fd is the file descriptor to rceeive 779Configures an \f(CW\*(C`ev_io\*(C'\fR watcher. The fd is the file descriptor to rceeive
626events for and events is either \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_READ | 780events for and events is either \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_READ |
627EV_WRITE\*(C'\fR to receive the given events. 781EV_WRITE\*(C'\fR to receive the given events.
782.Sp
783Please note that most of the more scalable backend mechanisms (for example
784epoll and solaris ports) can result in spurious readyness notifications
785for file descriptors, so you practically need to use non-blocking I/O (and
786treat callback invocation as hint only), or retest separately with a safe
787interface before doing I/O (XLib can do this), or force the use of either
788\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR, which don't suffer from this
789problem. Also note that it is quite easy to have your callback invoked
790when the readyness condition is no longer valid even when employing
791typical ways of handling events, so its a good idea to use non-blocking
792I/O unconditionally.
793.PP
794Example: call \f(CW\*(C`stdin_readable_cb\*(C'\fR when \s-1STDIN_FILENO\s0 has become, well
795readable, but only once. Since it is likely line\-buffered, you could
796attempt to read a whole line in the callback:
797.PP
798.Vb 6
799\& static void
800\& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents)
801\& {
802\& ev_io_stop (loop, w);
803\& .. read from stdin here (or from w->fd) and haqndle any I/O errors
804\& }
805.Ve
806.PP
807.Vb 6
808\& ...
809\& struct ev_loop *loop = ev_default_init (0);
810\& struct ev_io stdin_readable;
811\& ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ);
812\& ev_io_start (loop, &stdin_readable);
813\& ev_loop (loop, 0);
814.Ve
628.ie n .Sh """ev_timer"" \- relative and optionally recurring timeouts" 815.ie n .Sh """ev_timer"" \- relative and optionally recurring timeouts"
629.el .Sh "\f(CWev_timer\fP \- relative and optionally recurring timeouts" 816.el .Sh "\f(CWev_timer\fP \- relative and optionally recurring timeouts"
630.IX Subsection "ev_timer - relative and optionally recurring timeouts" 817.IX Subsection "ev_timer - relative and optionally recurring timeouts"
631Timer watchers are simple relative timers that generate an event after a 818Timer watchers are simple relative timers that generate an event after a
632given time, and optionally repeating in regular intervals after that. 819given time, and optionally repeating in regular intervals after that.
682seconds of inactivity on the socket. The easiest way to do this is to 869seconds of inactivity on the socket. The easiest way to do this is to
683configure an \f(CW\*(C`ev_timer\*(C'\fR with after=repeat=60 and calling ev_timer_again each 870configure an \f(CW\*(C`ev_timer\*(C'\fR with after=repeat=60 and calling ev_timer_again each
684time you successfully read or write some data. If you go into an idle 871time you successfully read or write some data. If you go into an idle
685state where you do not expect data to travel on the socket, you can stop 872state where you do not expect data to travel on the socket, you can stop
686the timer, and again will automatically restart it if need be. 873the timer, and again will automatically restart it if need be.
874.PP
875Example: create a timer that fires after 60 seconds.
876.PP
877.Vb 5
878\& static void
879\& one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents)
880\& {
881\& .. one minute over, w is actually stopped right here
882\& }
883.Ve
884.PP
885.Vb 3
886\& struct ev_timer mytimer;
887\& ev_timer_init (&mytimer, one_minute_cb, 60., 0.);
888\& ev_timer_start (loop, &mytimer);
889.Ve
890.PP
891Example: create a timeout timer that times out after 10 seconds of
892inactivity.
893.PP
894.Vb 5
895\& static void
896\& timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents)
897\& {
898\& .. ten seconds without any activity
899\& }
900.Ve
901.PP
902.Vb 4
903\& struct ev_timer mytimer;
904\& ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */
905\& ev_timer_again (&mytimer); /* start timer */
906\& ev_loop (loop, 0);
907.Ve
908.PP
909.Vb 3
910\& // and in some piece of code that gets executed on any "activity":
911\& // reset the timeout to start ticking again at 10 seconds
912\& ev_timer_again (&mytimer);
913.Ve
687.ie n .Sh """ev_periodic"" \- to cron or not to cron" 914.ie n .Sh """ev_periodic"" \- to cron or not to cron"
688.el .Sh "\f(CWev_periodic\fP \- to cron or not to cron" 915.el .Sh "\f(CWev_periodic\fP \- to cron or not to cron"
689.IX Subsection "ev_periodic - to cron or not to cron" 916.IX Subsection "ev_periodic - to cron or not to cron"
690Periodic watchers are also timers of a kind, but they are very versatile 917Periodic watchers are also timers of a kind, but they are very versatile
691(and unfortunately a bit complex). 918(and unfortunately a bit complex).
783.IX Item "ev_periodic_again (loop, ev_periodic *)" 1010.IX Item "ev_periodic_again (loop, ev_periodic *)"
784Simply stops and restarts the periodic watcher again. This is only useful 1011Simply stops and restarts the periodic watcher again. This is only useful
785when you changed some parameters or the reschedule callback would return 1012when you changed some parameters or the reschedule callback would return
786a different time than the last time it was called (e.g. in a crond like 1013a different time than the last time it was called (e.g. in a crond like
787program when the crontabs have changed). 1014program when the crontabs have changed).
1015.PP
1016Example: call a callback every hour, or, more precisely, whenever the
1017system clock is divisible by 3600. The callback invocation times have
1018potentially a lot of jittering, but good long-term stability.
1019.PP
1020.Vb 5
1021\& static void
1022\& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents)
1023\& {
1024\& ... its now a full hour (UTC, or TAI or whatever your clock follows)
1025\& }
1026.Ve
1027.PP
1028.Vb 3
1029\& struct ev_periodic hourly_tick;
1030\& ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0);
1031\& ev_periodic_start (loop, &hourly_tick);
1032.Ve
1033.PP
1034Example: the same as above, but use a reschedule callback to do it:
1035.PP
1036.Vb 1
1037\& #include <math.h>
1038.Ve
1039.PP
1040.Vb 5
1041\& static ev_tstamp
1042\& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now)
1043\& {
1044\& return fmod (now, 3600.) + 3600.;
1045\& }
1046.Ve
1047.PP
1048.Vb 1
1049\& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb);
1050.Ve
1051.PP
1052Example: call a callback every hour, starting now:
1053.PP
1054.Vb 4
1055\& struct ev_periodic hourly_tick;
1056\& ev_periodic_init (&hourly_tick, clock_cb,
1057\& fmod (ev_now (loop), 3600.), 3600., 0);
1058\& ev_periodic_start (loop, &hourly_tick);
1059.Ve
788.ie n .Sh """ev_signal"" \- signal me when a signal gets signalled" 1060.ie n .Sh """ev_signal"" \- signal me when a signal gets signalled"
789.el .Sh "\f(CWev_signal\fP \- signal me when a signal gets signalled" 1061.el .Sh "\f(CWev_signal\fP \- signal me when a signal gets signalled"
790.IX Subsection "ev_signal - signal me when a signal gets signalled" 1062.IX Subsection "ev_signal - signal me when a signal gets signalled"
791Signal watchers will trigger an event when the process receives a specific 1063Signal watchers will trigger an event when the process receives a specific
792signal one or more times. Even though signals are very asynchronous, libev 1064signal one or more times. Even though signals are very asynchronous, libev
822\&\fIany\fR process if \f(CW\*(C`pid\*(C'\fR is specified as \f(CW0\fR). The callback can look 1094\&\fIany\fR process if \f(CW\*(C`pid\*(C'\fR is specified as \f(CW0\fR). The callback can look
823at the \f(CW\*(C`rstatus\*(C'\fR member of the \f(CW\*(C`ev_child\*(C'\fR watcher structure to see 1095at the \f(CW\*(C`rstatus\*(C'\fR member of the \f(CW\*(C`ev_child\*(C'\fR watcher structure to see
824the status word (use the macros from \f(CW\*(C`sys/wait.h\*(C'\fR and see your systems 1096the status word (use the macros from \f(CW\*(C`sys/wait.h\*(C'\fR and see your systems
825\&\f(CW\*(C`waitpid\*(C'\fR documentation). The \f(CW\*(C`rpid\*(C'\fR member contains the pid of the 1097\&\f(CW\*(C`waitpid\*(C'\fR documentation). The \f(CW\*(C`rpid\*(C'\fR member contains the pid of the
826process causing the status change. 1098process causing the status change.
1099.PP
1100Example: try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0.
1101.PP
1102.Vb 5
1103\& static void
1104\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1105\& {
1106\& ev_unloop (loop, EVUNLOOP_ALL);
1107\& }
1108.Ve
1109.PP
1110.Vb 3
1111\& struct ev_signal signal_watcher;
1112\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1113\& ev_signal_start (loop, &sigint_cb);
1114.Ve
827.ie n .Sh """ev_idle"" \- when you've got nothing better to do" 1115.ie n .Sh """ev_idle"" \- when you've got nothing better to do"
828.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do" 1116.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do"
829.IX Subsection "ev_idle - when you've got nothing better to do" 1117.IX Subsection "ev_idle - when you've got nothing better to do"
830Idle watchers trigger events when there are no other events are pending 1118Idle watchers trigger events when there are no other events are pending
831(prepare, check and other idle watchers do not count). That is, as long 1119(prepare, check and other idle watchers do not count). That is, as long
845.IP "ev_idle_init (ev_signal *, callback)" 4 1133.IP "ev_idle_init (ev_signal *, callback)" 4
846.IX Item "ev_idle_init (ev_signal *, callback)" 1134.IX Item "ev_idle_init (ev_signal *, callback)"
847Initialises and configures the idle watcher \- it has no parameters of any 1135Initialises and configures the idle watcher \- it has no parameters of any
848kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, 1136kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless,
849believe me. 1137believe me.
1138.PP
1139Example: dynamically allocate an \f(CW\*(C`ev_idle\*(C'\fR, start it, and in the
1140callback, free it. Alos, use no error checking, as usual.
1141.PP
1142.Vb 7
1143\& static void
1144\& idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents)
1145\& {
1146\& free (w);
1147\& // now do something you wanted to do when the program has
1148\& // no longer asnything immediate to do.
1149\& }
1150.Ve
1151.PP
1152.Vb 3
1153\& struct ev_idle *idle_watcher = malloc (sizeof (struct ev_idle));
1154\& ev_idle_init (idle_watcher, idle_cb);
1155\& ev_idle_start (loop, idle_cb);
1156.Ve
850.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop" 1157.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop"
851.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop" 1158.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop"
852.IX Subsection "ev_prepare and ev_check - customise your event loop" 1159.IX Subsection "ev_prepare and ev_check - customise your event loop"
853Prepare and check watchers are usually (but not always) used in tandem: 1160Prepare and check watchers are usually (but not always) used in tandem:
854prepare watchers get invoked before the process blocks and check watchers 1161prepare watchers get invoked before the process blocks and check watchers
882.IX Item "ev_check_init (ev_check *, callback)" 1189.IX Item "ev_check_init (ev_check *, callback)"
883.PD 1190.PD
884Initialises and configures the prepare or check watcher \- they have no 1191Initialises and configures the prepare or check watcher \- they have no
885parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR 1192parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR
886macros, but using them is utterly, utterly and completely pointless. 1193macros, but using them is utterly, utterly and completely pointless.
1194.PP
1195Example: *TODO*.
887.SH "OTHER FUNCTIONS" 1196.SH "OTHER FUNCTIONS"
888.IX Header "OTHER FUNCTIONS" 1197.IX Header "OTHER FUNCTIONS"
889There are some other functions of possible interest. Described. Here. Now. 1198There are some other functions of possible interest. Described. Here. Now.
890.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 1199.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
891.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 1200.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines