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

Comparing libev/ev.3 (file contents):
Revision 1.68 by root, Tue Jun 17 10:16:00 2008 UTC vs.
Revision 1.72 by root, Tue Oct 21 20:06:52 2008 UTC

130.\} 130.\}
131.rm #[ #] #H #V #F C 131.rm #[ #] #H #V #F C
132.\" ======================================================================== 132.\" ========================================================================
133.\" 133.\"
134.IX Title "LIBEV 3" 134.IX Title "LIBEV 3"
135.TH LIBEV 3 "2008-06-09" "libev-3.42" "libev - high performance full featured event loop" 135.TH LIBEV 3 "2008-10-21" "libev-3.45" "libev - high performance full featured event loop"
136.\" For nroff, turn off justification. Always turn off hyphenation; it makes 136.\" For nroff, turn off justification. Always turn off hyphenation; it makes
137.\" way too many mistakes in technical documents. 137.\" way too many mistakes in technical documents.
138.if n .ad l 138.if n .ad l
139.nh 139.nh
140.SH "NAME" 140.SH "NAME"
344might be supported on the current system, you would need to look at 344might be supported on the current system, you would need to look at
345\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for 345\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for
346recommended ones. 346recommended ones.
347.Sp 347.Sp
348See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. 348See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
349.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 349.IP "ev_set_allocator (void *(*cb)(void *ptr, long size)) [\s-1NOT\s0 \s-1REENTRANT\s0]" 4
350.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" 350.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size)) [NOT REENTRANT]"
351Sets the allocation function to use (the prototype is similar \- the 351Sets the allocation function to use (the prototype is similar \- the
352semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is 352semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is
353used to allocate and free memory (no surprises here). If it returns zero 353used to allocate and free memory (no surprises here). If it returns zero
354when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort 354when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort
355or take some potentially destructive action. 355or take some potentially destructive action.
381\& } 381\& }
382\& 382\&
383\& ... 383\& ...
384\& ev_set_allocator (persistent_realloc); 384\& ev_set_allocator (persistent_realloc);
385.Ve 385.Ve
386.IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4 386.IP "ev_set_syserr_cb (void (*cb)(const char *msg)); [\s-1NOT\s0 \s-1REENTRANT\s0]" 4
387.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));" 387.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg)); [NOT REENTRANT]"
388Set the callback function to call on a retryable system call error (such 388Set the callback function to call on a retryable system call error (such
389as failed select, poll, epoll_wait). The message is a printable string 389as failed select, poll, epoll_wait). The message is a printable string
390indicating the system call or subsystem causing the problem. If this 390indicating the system call or subsystem causing the problem. If this
391callback is set, then libev will expect it to remedy the situation, no 391callback is set, then libev will expect it to remedy the situation, no
392matter what, when it returns. That is, libev will generally retry the 392matter what, when it returns. That is, libev will generally retry the
484parallelism (most of the file descriptors should be busy). If you are 484parallelism (most of the file descriptors should be busy). If you are
485writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many 485writing a server, you should \f(CW\*(C`accept ()\*(C'\fR in a loop to accept as many
486connections as possible during one iteration. You might also want to have 486connections as possible during one iteration. You might also want to have
487a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of 487a look at \f(CW\*(C`ev_set_io_collect_interval ()\*(C'\fR to increase the amount of
488readiness notifications you get per iteration. 488readiness notifications you get per iteration.
489.Sp
490This backend maps \f(CW\*(C`EV_READ\*(C'\fR to the \f(CW\*(C`readfds\*(C'\fR set and \f(CW\*(C`EV_WRITE\*(C'\fR to the
491\&\f(CW\*(C`writefds\*(C'\fR set (and to work around Microsoft Windows bugs, also onto the
492\&\f(CW\*(C`exceptfds\*(C'\fR set on that platform).
489.ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4 493.ie n .IP """EVBACKEND_POLL"" (value 2, poll backend, available everywhere except on windows)" 4
490.el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4 494.el .IP "\f(CWEVBACKEND_POLL\fR (value 2, poll backend, available everywhere except on windows)" 4
491.IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)" 495.IX Item "EVBACKEND_POLL (value 2, poll backend, available everywhere except on windows)"
492And this is your standard \fIpoll\fR\|(2) backend. It's more complicated 496And this is your standard \fIpoll\fR\|(2) backend. It's more complicated
493than select, but handles sparse fds better and has no artificial 497than select, but handles sparse fds better and has no artificial
494limit on the number of fds you can use (except it will slow down 498limit on the number of fds you can use (except it will slow down
495considerably with a lot of inactive fds). It scales similarly to select, 499considerably with a lot of inactive fds). It scales similarly to select,
496i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for 500i.e. O(total_fds). See the entry for \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR, above, for
497performance tips. 501performance tips.
502.Sp
503This backend maps \f(CW\*(C`EV_READ\*(C'\fR to \f(CW\*(C`POLLIN | POLLERR | POLLHUP\*(C'\fR, and
504\&\f(CW\*(C`EV_WRITE\*(C'\fR to \f(CW\*(C`POLLOUT | POLLERR | POLLHUP\*(C'\fR.
498.ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4 505.ie n .IP """EVBACKEND_EPOLL"" (value 4, Linux)" 4
499.el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4 506.el .IP "\f(CWEVBACKEND_EPOLL\fR (value 4, Linux)" 4
500.IX Item "EVBACKEND_EPOLL (value 4, Linux)" 507.IX Item "EVBACKEND_EPOLL (value 4, Linux)"
501For few fds, this backend is a bit little slower than poll and select, 508For few fds, this backend is a bit little slower than poll and select,
502but it scales phenomenally better. While poll and select usually scale 509but it scales phenomenally better. While poll and select usually scale
515Please note that epoll sometimes generates spurious notifications, so you 522Please note that epoll sometimes generates spurious notifications, so you
516need to use non-blocking I/O or other means to avoid blocking when no data 523need to use non-blocking I/O or other means to avoid blocking when no data
517(or space) is available. 524(or space) is available.
518.Sp 525.Sp
519Best performance from this backend is achieved by not unregistering all 526Best performance from this backend is achieved by not unregistering all
520watchers for a file descriptor until it has been closed, if possible, i.e. 527watchers for a file descriptor until it has been closed, if possible,
521keep at least one watcher active per fd at all times. 528i.e. keep at least one watcher active per fd at all times. Stopping and
529starting a watcher (without re-setting it) also usually doesn't cause
530extra overhead.
522.Sp 531.Sp
523While nominally embeddable in other event loops, this feature is broken in 532While nominally embeddable in other event loops, this feature is broken in
524all kernel versions tested so far. 533all kernel versions tested so far.
534.Sp
535This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as
536\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
525.ie n .IP """EVBACKEND_KQUEUE"" (value 8, most \s-1BSD\s0 clones)" 4 537.ie n .IP """EVBACKEND_KQUEUE"" (value 8, most \s-1BSD\s0 clones)" 4
526.el .IP "\f(CWEVBACKEND_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4 538.el .IP "\f(CWEVBACKEND_KQUEUE\fR (value 8, most \s-1BSD\s0 clones)" 4
527.IX Item "EVBACKEND_KQUEUE (value 8, most BSD clones)" 539.IX Item "EVBACKEND_KQUEUE (value 8, most BSD clones)"
528Kqueue deserves special mention, as at the time of this writing, it 540Kqueue deserves special mention, as at the time of this writing, it was
529was broken on all BSDs except NetBSD (usually it doesn't work reliably 541broken on all BSDs except NetBSD (usually it doesn't work reliably with
530with anything but sockets and pipes, except on Darwin, where of course 542anything but sockets and pipes, except on Darwin, where of course it's
531it's completely useless). For this reason it's not being \*(L"auto-detected\*(R" 543completely useless). For this reason it's not being \*(L"auto-detected\*(R" unless
532unless you explicitly specify it explicitly in the flags (i.e. using 544you explicitly specify it in the flags (i.e. using \f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR) or
533\&\f(CW\*(C`EVBACKEND_KQUEUE\*(C'\fR) or libev was compiled on a known-to-be-good (\-enough) 545libev was compiled on a known-to-be-good (\-enough) system like NetBSD.
534system like NetBSD.
535.Sp 546.Sp
536You still can embed kqueue into a normal poll or select backend and use it 547You still can embed kqueue into a normal poll or select backend and use it
537only for sockets (after having made sure that sockets work with kqueue on 548only for sockets (after having made sure that sockets work with kqueue on
538the target platform). See \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. 549the target platform). See \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
539.Sp 550.Sp
540It scales in the same way as the epoll backend, but the interface to the 551It scales in the same way as the epoll backend, but the interface to the
541kernel is more efficient (which says nothing about its actual speed, of 552kernel is more efficient (which says nothing about its actual speed, of
542course). While stopping, setting and starting an I/O watcher does never 553course). While stopping, setting and starting an I/O watcher does never
543cause an extra system call as with \f(CW\*(C`EVBACKEND_EPOLL\*(C'\fR, it still adds up to 554cause an extra system call as with \f(CW\*(C`EVBACKEND_EPOLL\*(C'\fR, it still adds up to
544two event changes per incident, support for \f(CW\*(C`fork ()\*(C'\fR is very bad and it 555two event changes per incident. Support for \f(CW\*(C`fork ()\*(C'\fR is very bad and it
545drops fds silently in similarly hard-to-detect cases. 556drops fds silently in similarly hard-to-detect cases.
546.Sp 557.Sp
547This backend usually performs well under most conditions. 558This backend usually performs well under most conditions.
548.Sp 559.Sp
549While nominally embeddable in other event loops, this doesn't work 560While nominally embeddable in other event loops, this doesn't work
550everywhere, so you might need to test for this. And since it is broken 561everywhere, so you might need to test for this. And since it is broken
551almost everywhere, you should only use it when you have a lot of sockets 562almost everywhere, you should only use it when you have a lot of sockets
552(for which it usually works), by embedding it into another event loop 563(for which it usually works), by embedding it into another event loop
553(e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR) and using it only for 564(e.g. \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR) and, did I mention it,
554sockets. 565using it only for sockets.
566.Sp
567This backend maps \f(CW\*(C`EV_READ\*(C'\fR into an \f(CW\*(C`EVFILT_READ\*(C'\fR kevent with
568\&\f(CW\*(C`NOTE_EOF\*(C'\fR, and \f(CW\*(C`EV_WRITE\*(C'\fR into an \f(CW\*(C`EVFILT_WRITE\*(C'\fR kevent with
569\&\f(CW\*(C`NOTE_EOF\*(C'\fR.
555.ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4 570.ie n .IP """EVBACKEND_DEVPOLL"" (value 16, Solaris 8)" 4
556.el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4 571.el .IP "\f(CWEVBACKEND_DEVPOLL\fR (value 16, Solaris 8)" 4
557.IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)" 572.IX Item "EVBACKEND_DEVPOLL (value 16, Solaris 8)"
558This is not implemented yet (and might never be, unless you send me an 573This is not implemented yet (and might never be, unless you send me an
559implementation). According to reports, \f(CW\*(C`/dev/poll\*(C'\fR only supports sockets 574implementation). According to reports, \f(CW\*(C`/dev/poll\*(C'\fR only supports sockets
572While this backend scales well, it requires one system call per active 587While this backend scales well, it requires one system call per active
573file descriptor per loop iteration. For small and medium numbers of file 588file descriptor per loop iteration. For small and medium numbers of file
574descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend 589descriptors a \*(L"slow\*(R" \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR backend
575might perform better. 590might perform better.
576.Sp 591.Sp
577On the positive side, ignoring the spurious readiness notifications, this 592On the positive side, with the exception of the spurious readiness
578backend actually performed to specification in all tests and is fully 593notifications, this backend actually performed fully to specification
579embeddable, which is a rare feat among the OS-specific backends. 594in all tests and is fully embeddable, which is a rare feat among the
595OS-specific backends.
596.Sp
597This backend maps \f(CW\*(C`EV_READ\*(C'\fR and \f(CW\*(C`EV_WRITE\*(C'\fR in the same way as
598\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
580.ie n .IP """EVBACKEND_ALL""" 4 599.ie n .IP """EVBACKEND_ALL""" 4
581.el .IP "\f(CWEVBACKEND_ALL\fR" 4 600.el .IP "\f(CWEVBACKEND_ALL\fR" 4
582.IX Item "EVBACKEND_ALL" 601.IX Item "EVBACKEND_ALL"
583Try all backends (even potentially broken ones that wouldn't be tried 602Try all backends (even potentially broken ones that wouldn't be tried
584with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as 603with \f(CW\*(C`EVFLAG_AUTO\*(C'\fR). Since this is a mask, you can do stuff such as
590.Sp 609.Sp
591If one or more of these are or'ed into the flags value, then only these 610If one or more of these are or'ed into the flags value, then only these
592backends will be tried (in the reverse order as listed here). If none are 611backends will be tried (in the reverse order as listed here). If none are
593specified, all backends in \f(CW\*(C`ev_recommended_backends ()\*(C'\fR will be tried. 612specified, all backends in \f(CW\*(C`ev_recommended_backends ()\*(C'\fR will be tried.
594.Sp 613.Sp
595The most typical usage is like this: 614Example: This is the most typical usage.
596.Sp 615.Sp
597.Vb 2 616.Vb 2
598\& if (!ev_default_loop (0)) 617\& if (!ev_default_loop (0))
599\& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?"); 618\& fatal ("could not initialise libev, bad $LIBEV_FLAGS in environment?");
600.Ve 619.Ve
601.Sp 620.Sp
602Restrict libev to the select and poll backends, and do not allow 621Example: Restrict libev to the select and poll backends, and do not allow
603environment settings to be taken into account: 622environment settings to be taken into account:
604.Sp 623.Sp
605.Vb 1 624.Vb 1
606\& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV); 625\& ev_default_loop (EVBACKEND_POLL | EVBACKEND_SELECT | EVFLAG_NOENV);
607.Ve 626.Ve
608.Sp 627.Sp
609Use whatever libev has to offer, but make sure that kqueue is used if 628Example: Use whatever libev has to offer, but make sure that kqueue is
610available (warning, breaks stuff, best use only with your own private 629used if available (warning, breaks stuff, best use only with your own
611event loop and only if you know the \s-1OS\s0 supports your types of fds): 630private event loop and only if you know the \s-1OS\s0 supports your types of
631fds):
612.Sp 632.Sp
613.Vb 1 633.Vb 1
614\& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); 634\& ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
615.Ve 635.Ve
616.RE 636.RE
676.Ve 696.Ve
677.IP "ev_loop_fork (loop)" 4 697.IP "ev_loop_fork (loop)" 4
678.IX Item "ev_loop_fork (loop)" 698.IX Item "ev_loop_fork (loop)"
679Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by 699Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by
680\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop 700\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop
681after fork, and how you do this is entirely your own problem. 701after fork that you want to re-use in the child, and how you do this is
702entirely your own problem.
682.IP "int ev_is_default_loop (loop)" 4 703.IP "int ev_is_default_loop (loop)" 4
683.IX Item "int ev_is_default_loop (loop)" 704.IX Item "int ev_is_default_loop (loop)"
684Returns true when the given loop actually is the default loop, false otherwise. 705Returns true when the given loop is, in fact, the default loop, and false
706otherwise.
685.IP "unsigned int ev_loop_count (loop)" 4 707.IP "unsigned int ev_loop_count (loop)" 4
686.IX Item "unsigned int ev_loop_count (loop)" 708.IX Item "unsigned int ev_loop_count (loop)"
687Returns the count of loop iterations for the loop, which is identical to 709Returns the count of loop iterations for the loop, which is identical to
688the number of times libev did poll for new events. It starts at \f(CW0\fR and 710the number of times libev did poll for new events. It starts at \f(CW0\fR and
689happily wraps around with enough iterations. 711happily wraps around with enough iterations.
700Returns the current \*(L"event loop time\*(R", which is the time the event loop 722Returns the current \*(L"event loop time\*(R", which is the time the event loop
701received events and started processing them. This timestamp does not 723received events and started processing them. This timestamp does not
702change as long as callbacks are being processed, and this is also the base 724change as long as callbacks are being processed, and this is also the base
703time used for relative timers. You can treat it as the timestamp of the 725time used for relative timers. You can treat it as the timestamp of the
704event occurring (or more correctly, libev finding out about it). 726event occurring (or more correctly, libev finding out about it).
727.IP "ev_now_update (loop)" 4
728.IX Item "ev_now_update (loop)"
729Establishes the current time by querying the kernel, updating the time
730returned by \f(CW\*(C`ev_now ()\*(C'\fR in the progress. This is a costly operation and
731is usually done automatically within \f(CW\*(C`ev_loop ()\*(C'\fR.
732.Sp
733This function is rarely useful, but when some event callback runs for a
734very long time without entering the event loop, updating libev's idea of
735the current time is a good idea.
736.Sp
737See also \*(L"The special problem of time updates\*(R" in the \f(CW\*(C`ev_timer\*(C'\fR section.
705.IP "ev_loop (loop, int flags)" 4 738.IP "ev_loop (loop, int flags)" 4
706.IX Item "ev_loop (loop, int flags)" 739.IX Item "ev_loop (loop, int flags)"
707Finally, this is it, the event handler. This function usually is called 740Finally, this is it, the event handler. This function usually is called
708after you initialised all your watchers and you want to start handling 741after you initialised all your watchers and you want to start handling
709events. 742events.
711If the flags argument is specified as \f(CW0\fR, it will not return until 744If the flags argument is specified as \f(CW0\fR, it will not return until
712either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. 745either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called.
713.Sp 746.Sp
714Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than 747Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than
715relying on all watchers to be stopped when deciding when a program has 748relying on all watchers to be stopped when deciding when a program has
716finished (especially in interactive programs), but having a program that 749finished (especially in interactive programs), but having a program
717automatically loops as long as it has to and no longer by virtue of 750that automatically loops as long as it has to and no longer by virtue
718relying on its watchers stopping correctly is a thing of beauty. 751of relying on its watchers stopping correctly, that is truly a thing of
752beauty.
719.Sp 753.Sp
720A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle 754A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle
721those events and any outstanding ones, but will not block your process in 755those events and any already outstanding ones, but will not block your
722case there are no events and will return after one iteration of the loop. 756process in case there are no events and will return after one iteration of
757the loop.
723.Sp 758.Sp
724A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if 759A flags value of \f(CW\*(C`EVLOOP_ONESHOT\*(C'\fR will look for new events (waiting if
725necessary) and will handle those and any outstanding ones. It will block 760necessary) and will handle those and any already outstanding ones. It
726your process until at least one new event arrives, and will return after 761will block your process until at least one new event arrives (which could
727one iteration of the loop. This is useful if you are waiting for some 762be an event internal to libev itself, so there is no guarentee that a
728external event in conjunction with something not expressible using other 763user-registered callback will be called), and will return after one
764iteration of the loop.
765.Sp
766This is useful if you are waiting for some external event in conjunction
767with something not expressible using other libev watchers (i.e. "roll your
729libev watchers. However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is 768own \f(CW\*(C`ev_loop\*(C'\fR"). However, a pair of \f(CW\*(C`ev_prepare\*(C'\fR/\f(CW\*(C`ev_check\*(C'\fR watchers is
730usually a better approach for this kind of thing. 769usually a better approach for this kind of thing.
731.Sp 770.Sp
732Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does: 771Here are the gory details of what \f(CW\*(C`ev_loop\*(C'\fR does:
733.Sp 772.Sp
734.Vb 10 773.Vb 10
735\& \- Before the first iteration, call any pending watchers. 774\& \- Before the first iteration, call any pending watchers.
736\& * If EVFLAG_FORKCHECK was used, check for a fork. 775\& * If EVFLAG_FORKCHECK was used, check for a fork.
737\& \- If a fork was detected, queue and call all fork watchers. 776\& \- If a fork was detected (by any means), queue and call all fork watchers.
738\& \- Queue and call all prepare watchers. 777\& \- Queue and call all prepare watchers.
739\& \- If we have been forked, recreate the kernel state. 778\& \- If we have been forked, detach and recreate the kernel state
779\& as to not disturb the other process.
740\& \- Update the kernel state with all outstanding changes. 780\& \- Update the kernel state with all outstanding changes.
741\& \- Update the "event loop time". 781\& \- Update the "event loop time" (ev_now ()).
742\& \- Calculate for how long to sleep or block, if at all 782\& \- Calculate for how long to sleep or block, if at all
743\& (active idle watchers, EVLOOP_NONBLOCK or not having 783\& (active idle watchers, EVLOOP_NONBLOCK or not having
744\& any active watchers at all will result in not sleeping). 784\& any active watchers at all will result in not sleeping).
745\& \- Sleep if the I/O and timer collect interval say so. 785\& \- Sleep if the I/O and timer collect interval say so.
746\& \- Block the process, waiting for any events. 786\& \- Block the process, waiting for any events.
747\& \- Queue all outstanding I/O (fd) events. 787\& \- Queue all outstanding I/O (fd) events.
748\& \- Update the "event loop time" and do time jump handling. 788\& \- Update the "event loop time" (ev_now ()), and do time jump adjustments.
749\& \- Queue all outstanding timers. 789\& \- Queue all expired timers.
750\& \- Queue all outstanding periodics. 790\& \- Queue all expired periodics.
751\& \- If no events are pending now, queue all idle watchers. 791\& \- Unless any events are pending now, queue all idle watchers.
752\& \- Queue all check watchers. 792\& \- Queue all check watchers.
753\& \- Call all queued watchers in reverse order (i.e. check watchers first). 793\& \- Call all queued watchers in reverse order (i.e. check watchers first).
754\& Signals and child watchers are implemented as I/O watchers, and will 794\& Signals and child watchers are implemented as I/O watchers, and will
755\& be handled here by queueing them when their watcher gets executed. 795\& be handled here by queueing them when their watcher gets executed.
756\& \- If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK 796\& \- If ev_unloop has been called, or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
763.Sp 803.Sp
764.Vb 4 804.Vb 4
765\& ... queue jobs here, make sure they register event watchers as long 805\& ... queue jobs here, make sure they register event watchers as long
766\& ... as they still have work to do (even an idle watcher will do..) 806\& ... as they still have work to do (even an idle watcher will do..)
767\& ev_loop (my_loop, 0); 807\& ev_loop (my_loop, 0);
768\& ... jobs done. yeah! 808\& ... jobs done or somebody called unloop. yeah!
769.Ve 809.Ve
770.IP "ev_unloop (loop, how)" 4 810.IP "ev_unloop (loop, how)" 4
771.IX Item "ev_unloop (loop, how)" 811.IX Item "ev_unloop (loop, how)"
772Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it 812Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it
773has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either 813has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either
774\&\f(CW\*(C`EVUNLOOP_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_loop\*(C'\fR call return, or 814\&\f(CW\*(C`EVUNLOOP_ONE\*(C'\fR, which will make the innermost \f(CW\*(C`ev_loop\*(C'\fR call return, or
775\&\f(CW\*(C`EVUNLOOP_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_loop\*(C'\fR calls return. 815\&\f(CW\*(C`EVUNLOOP_ALL\*(C'\fR, which will make all nested \f(CW\*(C`ev_loop\*(C'\fR calls return.
776.Sp 816.Sp
777This \*(L"unloop state\*(R" will be cleared when entering \f(CW\*(C`ev_loop\*(C'\fR again. 817This \*(L"unloop state\*(R" will be cleared when entering \f(CW\*(C`ev_loop\*(C'\fR again.
818.Sp
819It is safe to call \f(CW\*(C`ev_unloop\*(C'\fR from otuside any \f(CW\*(C`ev_loop\*(C'\fR calls.
778.IP "ev_ref (loop)" 4 820.IP "ev_ref (loop)" 4
779.IX Item "ev_ref (loop)" 821.IX Item "ev_ref (loop)"
780.PD 0 822.PD 0
781.IP "ev_unref (loop)" 4 823.IP "ev_unref (loop)" 4
782.IX Item "ev_unref (loop)" 824.IX Item "ev_unref (loop)"
783.PD 825.PD
784Ref/unref can be used to add or remove a reference count on the event 826Ref/unref can be used to add or remove a reference count on the event
785loop: Every watcher keeps one reference, and as long as the reference 827loop: Every watcher keeps one reference, and as long as the reference
786count is nonzero, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own. If you have 828count is nonzero, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own.
829.Sp
787a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR from 830If you have a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR
788returning, \fIev_unref()\fR after starting, and \fIev_ref()\fR before stopping it. For 831from returning, call \fIev_unref()\fR after starting, and \fIev_ref()\fR before
832stopping it.
833.Sp
789example, libev itself uses this for its internal signal pipe: It is not 834As an example, libev itself uses this for its internal signal pipe: It is
790visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if 835not visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting
791no event watchers registered by it are active. It is also an excellent 836if no event watchers registered by it are active. It is also an excellent
792way to do this for generic recurring timers or from within third-party 837way to do this for generic recurring timers or from within third-party
793libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR 838libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR
794(but only if the watcher wasn't active before, or was active before, 839(but only if the watcher wasn't active before, or was active before,
795respectively). 840respectively).
796.Sp 841.Sp
815.PD 0 860.PD 0
816.IP "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 4 861.IP "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 4
817.IX Item "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 862.IX Item "ev_set_timeout_collect_interval (loop, ev_tstamp interval)"
818.PD 863.PD
819These advanced functions influence the time that libev will spend waiting 864These advanced functions influence the time that libev will spend waiting
820for events. Both are by default \f(CW0\fR, meaning that libev will try to 865for events. Both time intervals are by default \f(CW0\fR, meaning that libev
821invoke timer/periodic callbacks and I/O callbacks with minimum latency. 866will try to invoke timer/periodic callbacks and I/O callbacks with minimum
867latency.
822.Sp 868.Sp
823Setting these to a higher value (the \f(CW\*(C`interval\*(C'\fR \fImust\fR be >= \f(CW0\fR) 869Setting these to a higher value (the \f(CW\*(C`interval\*(C'\fR \fImust\fR be >= \f(CW0\fR)
824allows libev to delay invocation of I/O and timer/periodic callbacks to 870allows libev to delay invocation of I/O and timer/periodic callbacks
825increase efficiency of loop iterations. 871to increase efficiency of loop iterations (or to increase power-saving
872opportunities).
826.Sp 873.Sp
827The background is that sometimes your program runs just fast enough to 874The idea is that sometimes your program runs just fast enough to handle
828handle one (or very few) event(s) per loop iteration. While this makes 875one (or very few) event(s) per loop iteration. While this makes the
829the program responsive, it also wastes a lot of \s-1CPU\s0 time to poll for new 876program responsive, it also wastes a lot of \s-1CPU\s0 time to poll for new
830events, especially with backends like \f(CW\*(C`select ()\*(C'\fR which have a high 877events, especially with backends like \f(CW\*(C`select ()\*(C'\fR which have a high
831overhead for the actual polling but can deliver many events at once. 878overhead for the actual polling but can deliver many events at once.
832.Sp 879.Sp
833By setting a higher \fIio collect interval\fR you allow libev to spend more 880By setting a higher \fIio collect interval\fR you allow libev to spend more
834time collecting I/O events, so you can handle more events per iteration, 881time collecting I/O events, so you can handle more events per iteration,
836\&\f(CW\*(C`ev_timer\*(C'\fR) will be not affected. Setting this to a non-null value will 883\&\f(CW\*(C`ev_timer\*(C'\fR) will be not affected. Setting this to a non-null value will
837introduce an additional \f(CW\*(C`ev_sleep ()\*(C'\fR call into most loop iterations. 884introduce an additional \f(CW\*(C`ev_sleep ()\*(C'\fR call into most loop iterations.
838.Sp 885.Sp
839Likewise, by setting a higher \fItimeout collect interval\fR you allow libev 886Likewise, by setting a higher \fItimeout collect interval\fR you allow libev
840to spend more time collecting timeouts, at the expense of increased 887to spend more time collecting timeouts, at the expense of increased
841latency (the watcher callback will be called later). \f(CW\*(C`ev_io\*(C'\fR watchers 888latency/jitter/inexactness (the watcher callback will be called
842will not be affected. Setting this to a non-null value will not introduce 889later). \f(CW\*(C`ev_io\*(C'\fR watchers will not be affected. Setting this to a non-null
843any overhead in libev. 890value will not introduce any overhead in libev.
844.Sp 891.Sp
845Many (busy) programs can usually benefit by setting the I/O collect 892Many (busy) programs can usually benefit by setting the I/O collect
846interval to a value near \f(CW0.1\fR or so, which is often enough for 893interval to a value near \f(CW0.1\fR or so, which is often enough for
847interactive servers (of course not for games), likewise for timeouts. It 894interactive servers (of course not for games), likewise for timeouts. It
848usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR, 895usually doesn't make much sense to set it to a lower value than \f(CW0.01\fR,
849as this approaches the timing granularity of most systems. 896as this approaches the timing granularity of most systems.
897.Sp
898Setting the \fItimeout collect interval\fR can improve the opportunity for
899saving power, as the program will \*(L"bundle\*(R" timer callback invocations that
900are \*(L"near\*(R" in time together, by delaying some, thus reducing the number of
901times the process sleeps and wakes up again. Another useful technique to
902reduce iterations/wake\-ups is to use \f(CW\*(C`ev_periodic\*(C'\fR watchers and make sure
903they fire on, say, one-second boundaries only.
850.IP "ev_loop_verify (loop)" 4 904.IP "ev_loop_verify (loop)" 4
851.IX Item "ev_loop_verify (loop)" 905.IX Item "ev_loop_verify (loop)"
852This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been 906This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been
853compiled in. It tries to go through all internal structures and checks 907compiled in. which is the default for non-minimal builds. It tries to go
854them for validity. If anything is found to be inconsistent, it will print 908through all internal structures and checks them for validity. If anything
855an error message to standard error and call \f(CW\*(C`abort ()\*(C'\fR. 909is found to be inconsistent, it will print an error message to standard
910error and call \f(CW\*(C`abort ()\*(C'\fR.
856.Sp 911.Sp
857This can be used to catch bugs inside libev itself: under normal 912This can be used to catch bugs inside libev itself: under normal
858circumstances, this function will never abort as of course libev keeps its 913circumstances, this function will never abort as of course libev keeps its
859data structures consistent. 914data structures consistent.
860.SH "ANATOMY OF A WATCHER" 915.SH "ANATOMY OF A WATCHER"
978happen because the watcher could not be properly started because libev 1033happen because the watcher could not be properly started because libev
979ran out of memory, a file descriptor was found to be closed or any other 1034ran out of memory, a file descriptor was found to be closed or any other
980problem. You best act on it by reporting the problem and somehow coping 1035problem. You best act on it by reporting the problem and somehow coping
981with the watcher being stopped. 1036with the watcher being stopped.
982.Sp 1037.Sp
983Libev will usually signal a few \*(L"dummy\*(R" events together with an error, 1038Libev will usually signal a few \*(L"dummy\*(R" events together with an error, for
984for example it might indicate that a fd is readable or writable, and if 1039example it might indicate that a fd is readable or writable, and if your
985your callbacks is well-written it can just attempt the operation and cope 1040callbacks is well-written it can just attempt the operation and cope with
986with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multi-threaded 1041the error from \fIread()\fR or \fIwrite()\fR. This will not work in multi-threaded
987programs, though, so beware. 1042programs, though, as the fd could already be closed and reused for another
1043thing, so beware.
988.Sh "\s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0" 1044.Sh "\s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0"
989.IX Subsection "GENERIC WATCHER FUNCTIONS" 1045.IX Subsection "GENERIC WATCHER FUNCTIONS"
990In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type, 1046In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type,
991e.g. \f(CW\*(C`timer\*(C'\fR for \f(CW\*(C`ev_timer\*(C'\fR watchers and \f(CW\*(C`io\*(C'\fR for \f(CW\*(C`ev_io\*(C'\fR watchers. 1047e.g. \f(CW\*(C`timer\*(C'\fR for \f(CW\*(C`ev_timer\*(C'\fR watchers and \f(CW\*(C`io\*(C'\fR for \f(CW\*(C`ev_io\*(C'\fR watchers.
992.ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4 1048.ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4
1002You can reinitialise a watcher at any time as long as it has been stopped 1058You can reinitialise a watcher at any time as long as it has been stopped
1003(or never started) and there are no pending events outstanding. 1059(or never started) and there are no pending events outstanding.
1004.Sp 1060.Sp
1005The callback is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher, 1061The callback is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher,
1006int revents)\*(C'\fR. 1062int revents)\*(C'\fR.
1063.Sp
1064Example: Initialise an \f(CW\*(C`ev_io\*(C'\fR watcher in two steps.
1065.Sp
1066.Vb 3
1067\& ev_io w;
1068\& ev_init (&w, my_cb);
1069\& ev_io_set (&w, STDIN_FILENO, EV_READ);
1070.Ve
1007.ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4 1071.ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4
1008.el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4 1072.el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4
1009.IX Item "ev_TYPE_set (ev_TYPE *, [args])" 1073.IX Item "ev_TYPE_set (ev_TYPE *, [args])"
1010This macro initialises the type-specific parts of a watcher. You need to 1074This macro initialises the type-specific parts of a watcher. You need to
1011call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can 1075call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can
1013macro on a watcher that is active (it can be pending, however, which is a 1077macro on a watcher that is active (it can be pending, however, which is a
1014difference to the \f(CW\*(C`ev_init\*(C'\fR macro). 1078difference to the \f(CW\*(C`ev_init\*(C'\fR macro).
1015.Sp 1079.Sp
1016Although some watcher types do not have type-specific arguments 1080Although some watcher types do not have type-specific arguments
1017(e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro. 1081(e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro.
1082.Sp
1083See \f(CW\*(C`ev_init\*(C'\fR, above, for an example.
1018.ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4 1084.ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4
1019.el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4 1085.el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4
1020.IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])" 1086.IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])"
1021This convenience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro 1087This convenience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro
1022calls into a single call. This is the most convenient method to initialise 1088calls into a single call. This is the most convenient method to initialise
1023a watcher. The same limitations apply, of course. 1089a watcher. The same limitations apply, of course.
1090.Sp
1091Example: Initialise and set an \f(CW\*(C`ev_io\*(C'\fR watcher in one step.
1092.Sp
1093.Vb 1
1094\& ev_io_init (&w, my_cb, STDIN_FILENO, EV_READ);
1095.Ve
1024.ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4 1096.ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4
1025.el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4 1097.el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4
1026.IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)" 1098.IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)"
1027Starts (activates) the given watcher. Only active watchers will receive 1099Starts (activates) the given watcher. Only active watchers will receive
1028events. If the watcher is already active nothing will happen. 1100events. If the watcher is already active nothing will happen.
1101.Sp
1102Example: Start the \f(CW\*(C`ev_io\*(C'\fR watcher that is being abused as example in this
1103whole section.
1104.Sp
1105.Vb 1
1106\& ev_io_start (EV_DEFAULT_UC, &w);
1107.Ve
1029.ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4 1108.ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4
1030.el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4 1109.el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4
1031.IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)" 1110.IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)"
1032Stops the given watcher again (if active) and clears the pending 1111Stops the given watcher if active, and clears the pending status (whether
1112the watcher was active or not).
1113.Sp
1033status. It is possible that stopped watchers are pending (for example, 1114It is possible that stopped watchers are pending \- for example,
1034non-repeating timers are being stopped when they become pending), but 1115non-repeating timers are being stopped when they become pending \- but
1035\&\f(CW\*(C`ev_TYPE_stop\*(C'\fR ensures that the watcher is neither active nor pending. If 1116calling \f(CW\*(C`ev_TYPE_stop\*(C'\fR ensures that the watcher is neither active nor
1036you want to free or reuse the memory used by the watcher it is therefore a 1117pending. If you want to free or reuse the memory used by the watcher it is
1037good idea to always call its \f(CW\*(C`ev_TYPE_stop\*(C'\fR function. 1118therefore a good idea to always call its \f(CW\*(C`ev_TYPE_stop\*(C'\fR function.
1038.IP "bool ev_is_active (ev_TYPE *watcher)" 4 1119.IP "bool ev_is_active (ev_TYPE *watcher)" 4
1039.IX Item "bool ev_is_active (ev_TYPE *watcher)" 1120.IX Item "bool ev_is_active (ev_TYPE *watcher)"
1040Returns a true value iff the watcher is active (i.e. it has been started 1121Returns a true value iff the watcher is active (i.e. it has been started
1041and not yet been stopped). As long as a watcher is active you must not modify 1122and not yet been stopped). As long as a watcher is active you must not modify
1042it. 1123it.
1086or might not have been adjusted to be within valid range. 1167or might not have been adjusted to be within valid range.
1087.IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 1168.IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4
1088.IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" 1169.IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)"
1089Invoke the \f(CW\*(C`watcher\*(C'\fR with the given \f(CW\*(C`loop\*(C'\fR and \f(CW\*(C`revents\*(C'\fR. Neither 1170Invoke the \f(CW\*(C`watcher\*(C'\fR with the given \f(CW\*(C`loop\*(C'\fR and \f(CW\*(C`revents\*(C'\fR. Neither
1090\&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback 1171\&\f(CW\*(C`loop\*(C'\fR nor \f(CW\*(C`revents\*(C'\fR need to be valid as long as the watcher callback
1091can deal with that fact. 1172can deal with that fact, as both are simply passed through to the
1173callback.
1092.IP "int ev_clear_pending (loop, ev_TYPE *watcher)" 4 1174.IP "int ev_clear_pending (loop, ev_TYPE *watcher)" 4
1093.IX Item "int ev_clear_pending (loop, ev_TYPE *watcher)" 1175.IX Item "int ev_clear_pending (loop, ev_TYPE *watcher)"
1094If the watcher is pending, this function returns clears its pending status 1176If the watcher is pending, this function clears its pending status and
1095and returns its \f(CW\*(C`revents\*(C'\fR bitset (as if its callback was invoked). If the 1177returns its \f(CW\*(C`revents\*(C'\fR bitset (as if its callback was invoked). If the
1096watcher isn't pending it does nothing and returns \f(CW0\fR. 1178watcher isn't pending it does nothing and returns \f(CW0\fR.
1179.Sp
1180Sometimes it can be useful to \*(L"poll\*(R" a watcher instead of waiting for its
1181callback to be invoked, which can be accomplished with this function.
1097.Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" 1182.Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0"
1098.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" 1183.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER"
1099Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change 1184Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change
1100and read at any time, libev will completely ignore it. This can be used 1185and read at any time: libev will completely ignore it. This can be used
1101to associate arbitrary data with your watcher. If you need more data and 1186to associate arbitrary data with your watcher. If you need more data and
1102don't want to allocate memory and store a pointer to it in that data 1187don't want to allocate memory and store a pointer to it in that data
1103member, you can also \*(L"subclass\*(R" the watcher type and provide your own 1188member, you can also \*(L"subclass\*(R" the watcher type and provide your own
1104data: 1189data:
1105.PP 1190.PP
1108\& { 1193\& {
1109\& struct ev_io io; 1194\& struct ev_io io;
1110\& int otherfd; 1195\& int otherfd;
1111\& void *somedata; 1196\& void *somedata;
1112\& struct whatever *mostinteresting; 1197\& struct whatever *mostinteresting;
1113\& } 1198\& };
1199\&
1200\& ...
1201\& struct my_io w;
1202\& ev_io_init (&w.io, my_cb, fd, EV_READ);
1114.Ve 1203.Ve
1115.PP 1204.PP
1116And since your callback will be called with a pointer to the watcher, you 1205And since your callback will be called with a pointer to the watcher, you
1117can cast it back to your own type: 1206can cast it back to your own type:
1118.PP 1207.PP
1125.Ve 1214.Ve
1126.PP 1215.PP
1127More interesting and less C\-conformant ways of casting your callback type 1216More interesting and less C\-conformant ways of casting your callback type
1128instead have been omitted. 1217instead have been omitted.
1129.PP 1218.PP
1130Another common scenario is having some data structure with multiple 1219Another common scenario is to use some data structure with multiple
1131watchers: 1220embedded watchers:
1132.PP 1221.PP
1133.Vb 6 1222.Vb 6
1134\& struct my_biggy 1223\& struct my_biggy
1135\& { 1224\& {
1136\& int some_data; 1225\& int some_data;
1137\& ev_timer t1; 1226\& ev_timer t1;
1138\& ev_timer t2; 1227\& ev_timer t2;
1139\& } 1228\& }
1140.Ve 1229.Ve
1141.PP 1230.PP
1142In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more complicated, 1231In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more
1143you need to use \f(CW\*(C`offsetof\*(C'\fR: 1232complicated: Either you store the address of your \f(CW\*(C`my_biggy\*(C'\fR struct
1233in the \f(CW\*(C`data\*(C'\fR member of the watcher (for woozies), or you need to use
1234some pointer arithmetic using \f(CW\*(C`offsetof\*(C'\fR inside your watchers (for real
1235programmers):
1144.PP 1236.PP
1145.Vb 1 1237.Vb 1
1146\& #include <stddef.h> 1238\& #include <stddef.h>
1147\& 1239\&
1148\& static void 1240\& static void
1187In general you can register as many read and/or write event watchers per 1279In general you can register as many read and/or write event watchers per
1188fd as you want (as long as you don't confuse yourself). Setting all file 1280fd as you want (as long as you don't confuse yourself). Setting all file
1189descriptors to non-blocking mode is also usually a good idea (but not 1281descriptors to non-blocking mode is also usually a good idea (but not
1190required if you know what you are doing). 1282required if you know what you are doing).
1191.PP 1283.PP
1192If you must do this, then force the use of a known-to-be-good backend 1284If you cannot use non-blocking mode, then force the use of a
1193(at the time of this writing, this includes only \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and 1285known-to-be-good backend (at the time of this writing, this includes only
1194\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR). 1286\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR).
1195.PP 1287.PP
1196Another thing you have to watch out for is that it is quite easy to 1288Another thing you have to watch out for is that it is quite easy to
1197receive \*(L"spurious\*(R" readiness notifications, that is your callback might 1289receive \*(L"spurious\*(R" readiness notifications, that is your callback might
1198be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block 1290be called with \f(CW\*(C`EV_READ\*(C'\fR but a subsequent \f(CW\*(C`read\*(C'\fR(2) will actually block
1199because there is no data. Not only are some backends known to create a 1291because there is no data. Not only are some backends known to create a
1200lot of those (for example Solaris ports), it is very easy to get into 1292lot of those (for example Solaris ports), it is very easy to get into
1201this situation even with a relatively standard program structure. Thus 1293this situation even with a relatively standard program structure. Thus
1202it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning 1294it is best to always use non-blocking I/O: An extra \f(CW\*(C`read\*(C'\fR(2) returning
1203\&\f(CW\*(C`EAGAIN\*(C'\fR is far preferable to a program hanging until some data arrives. 1295\&\f(CW\*(C`EAGAIN\*(C'\fR is far preferable to a program hanging until some data arrives.
1204.PP 1296.PP
1205If you cannot run the fd in non-blocking mode (for example you should not 1297If you cannot run the fd in non-blocking mode (for example you should
1206play around with an Xlib connection), then you have to separately re-test 1298not play around with an Xlib connection), then you have to separately
1207whether a file descriptor is really ready with a known-to-be good interface 1299re-test whether a file descriptor is really ready with a known-to-be good
1208such as poll (fortunately in our Xlib example, Xlib already does this on 1300interface such as poll (fortunately in our Xlib example, Xlib already
1209its own, so its quite safe to use). 1301does this on its own, so its quite safe to use). Some people additionally
1302use \f(CW\*(C`SIGALRM\*(C'\fR and an interval timer, just to be sure you won't block
1303indefinitely.
1304.PP
1305But really, best use non-blocking mode.
1210.PP 1306.PP
1211\fIThe special problem of disappearing file descriptors\fR 1307\fIThe special problem of disappearing file descriptors\fR
1212.IX Subsection "The special problem of disappearing file descriptors" 1308.IX Subsection "The special problem of disappearing file descriptors"
1213.PP 1309.PP
1214Some backends (e.g. kqueue, epoll) need to be told about closing a file 1310Some backends (e.g. kqueue, epoll) need to be told about closing a file
1215descriptor (either by calling \f(CW\*(C`close\*(C'\fR explicitly or by any other means, 1311descriptor (either due to calling \f(CW\*(C`close\*(C'\fR explicitly or any other means,
1216such as \f(CW\*(C`dup\*(C'\fR). The reason is that you register interest in some file 1312such as \f(CW\*(C`dup2\*(C'\fR). The reason is that you register interest in some file
1217descriptor, but when it goes away, the operating system will silently drop 1313descriptor, but when it goes away, the operating system will silently drop
1218this interest. If another file descriptor with the same number then is 1314this interest. If another file descriptor with the same number then is
1219registered with libev, there is no efficient way to see that this is, in 1315registered with libev, there is no efficient way to see that this is, in
1220fact, a different file descriptor. 1316fact, a different file descriptor.
1221.PP 1317.PP
1255\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. 1351\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
1256.PP 1352.PP
1257\fIThe special problem of \s-1SIGPIPE\s0\fR 1353\fIThe special problem of \s-1SIGPIPE\s0\fR
1258.IX Subsection "The special problem of SIGPIPE" 1354.IX Subsection "The special problem of SIGPIPE"
1259.PP 1355.PP
1260While not really specific to libev, it is easy to forget about \s-1SIGPIPE:\s0 1356While not really specific to libev, it is easy to forget about \f(CW\*(C`SIGPIPE\*(C'\fR:
1261when reading from a pipe whose other end has been closed, your program 1357when writing to a pipe whose other end has been closed, your program gets
1262gets send a \s-1SIGPIPE\s0, which, by default, aborts your program. For most 1358sent a \s-1SIGPIPE\s0, which, by default, aborts your program. For most programs
1263programs this is sensible behaviour, for daemons, this is usually 1359this is sensible behaviour, for daemons, this is usually undesirable.
1264undesirable.
1265.PP 1360.PP
1266So when you encounter spurious, unexplained daemon exits, make sure you 1361So when you encounter spurious, unexplained daemon exits, make sure you
1267ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon 1362ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon
1268somewhere, as that would have given you a big clue). 1363somewhere, as that would have given you a big clue).
1269.PP 1364.PP
1274.PD 0 1369.PD 0
1275.IP "ev_io_set (ev_io *, int fd, int events)" 4 1370.IP "ev_io_set (ev_io *, int fd, int events)" 4
1276.IX Item "ev_io_set (ev_io *, int fd, int events)" 1371.IX Item "ev_io_set (ev_io *, int fd, int events)"
1277.PD 1372.PD
1278Configures an \f(CW\*(C`ev_io\*(C'\fR watcher. The \f(CW\*(C`fd\*(C'\fR is the file descriptor to 1373Configures an \f(CW\*(C`ev_io\*(C'\fR watcher. The \f(CW\*(C`fd\*(C'\fR is the file descriptor to
1279receive events for and events is either \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or 1374receive events for and \f(CW\*(C`events\*(C'\fR is either \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or
1280\&\f(CW\*(C`EV_READ | EV_WRITE\*(C'\fR to receive the given events. 1375\&\f(CW\*(C`EV_READ | EV_WRITE\*(C'\fR, to express the desire to receive the given events.
1281.IP "int fd [read\-only]" 4 1376.IP "int fd [read\-only]" 4
1282.IX Item "int fd [read-only]" 1377.IX Item "int fd [read-only]"
1283The file descriptor being watched. 1378The file descriptor being watched.
1284.IP "int events [read\-only]" 4 1379.IP "int events [read\-only]" 4
1285.IX Item "int events [read-only]" 1380.IX Item "int events [read-only]"
1295.Vb 6 1390.Vb 6
1296\& static void 1391\& static void
1297\& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) 1392\& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents)
1298\& { 1393\& {
1299\& ev_io_stop (loop, w); 1394\& ev_io_stop (loop, w);
1300\& .. read from stdin here (or from w\->fd) and haqndle any I/O errors 1395\& .. read from stdin here (or from w\->fd) and handle any I/O errors
1301\& } 1396\& }
1302\& 1397\&
1303\& ... 1398\& ...
1304\& struct ev_loop *loop = ev_default_init (0); 1399\& struct ev_loop *loop = ev_default_init (0);
1305\& struct ev_io stdin_readable; 1400\& struct ev_io stdin_readable;
1313Timer watchers are simple relative timers that generate an event after a 1408Timer watchers are simple relative timers that generate an event after a
1314given time, and optionally repeating in regular intervals after that. 1409given time, and optionally repeating in regular intervals after that.
1315.PP 1410.PP
1316The timers are based on real time, that is, if you register an event that 1411The timers are based on real time, that is, if you register an event that
1317times out after an hour and you reset your system clock to January last 1412times out after an hour and you reset your system clock to January last
1318year, it will still time out after (roughly) and hour. \*(L"Roughly\*(R" because 1413year, it will still time out after (roughly) one hour. \*(L"Roughly\*(R" because
1319detecting time jumps is hard, and some inaccuracies are unavoidable (the 1414detecting time jumps is hard, and some inaccuracies are unavoidable (the
1320monotonic clock option helps a lot here). 1415monotonic clock option helps a lot here).
1416.PP
1417The callback is guaranteed to be invoked only \fIafter\fR its timeout has
1418passed, but if multiple timers become ready during the same loop iteration
1419then order of execution is undefined.
1420.PP
1421\fIThe special problem of time updates\fR
1422.IX Subsection "The special problem of time updates"
1423.PP
1424Establishing the current time is a costly operation (it usually takes at
1425least two system calls): \s-1EV\s0 therefore updates its idea of the current
1426time only before and after \f(CW\*(C`ev_loop\*(C'\fR collects new events, which causes a
1427growing difference between \f(CW\*(C`ev_now ()\*(C'\fR and \f(CW\*(C`ev_time ()\*(C'\fR when handling
1428lots of events in one iteration.
1321.PP 1429.PP
1322The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR 1430The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR
1323time. This is usually the right thing as this timestamp refers to the time 1431time. This is usually the right thing as this timestamp refers to the time
1324of the event triggering whatever timeout you are modifying/starting. If 1432of the event triggering whatever timeout you are modifying/starting. If
1325you suspect event processing to be delayed and you \fIneed\fR to base the timeout 1433you suspect event processing to be delayed and you \fIneed\fR to base the
1326on the current time, use something like this to adjust for this: 1434timeout on the current time, use something like this to adjust for this:
1327.PP 1435.PP
1328.Vb 1 1436.Vb 1
1329\& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); 1437\& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.);
1330.Ve 1438.Ve
1331.PP 1439.PP
1332The callback is guaranteed to be invoked only after its timeout has passed, 1440If the event loop is suspended for a long time, you can also force an
1333but if multiple timers become ready during the same loop iteration then 1441update of the time returned by \f(CW\*(C`ev_now ()\*(C'\fR by calling \f(CW\*(C`ev_now_update
1334order of execution is undefined. 1442()\*(C'\fR.
1335.PP 1443.PP
1336\fIWatcher-Specific Functions and Data Members\fR 1444\fIWatcher-Specific Functions and Data Members\fR
1337.IX Subsection "Watcher-Specific Functions and Data Members" 1445.IX Subsection "Watcher-Specific Functions and Data Members"
1338.IP "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" 4 1446.IP "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" 4
1339.IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" 1447.IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)"
1388\& ev_timer_again (loop, timer); 1496\& ev_timer_again (loop, timer);
1389.Ve 1497.Ve
1390.Sp 1498.Sp
1391This is more slightly efficient then stopping/starting the timer each time 1499This is more slightly efficient then stopping/starting the timer each time
1392you want to modify its timeout value. 1500you want to modify its timeout value.
1501.Sp
1502Note, however, that it is often even more efficient to remember the
1503time of the last activity and let the timer time-out naturally. In the
1504callback, you then check whether the time-out is real, or, if there was
1505some activity, you reschedule the watcher to time-out in \*(L"last_activity +
1506timeout \- ev_now ()\*(R" seconds.
1393.IP "ev_tstamp repeat [read\-write]" 4 1507.IP "ev_tstamp repeat [read\-write]" 4
1394.IX Item "ev_tstamp repeat [read-write]" 1508.IX Item "ev_tstamp repeat [read-write]"
1395The current \f(CW\*(C`repeat\*(C'\fR value. Will be used each time the watcher times out 1509The current \f(CW\*(C`repeat\*(C'\fR value. Will be used each time the watcher times out
1396or \f(CW\*(C`ev_timer_again\*(C'\fR is called and determines the next timeout (if any), 1510or \f(CW\*(C`ev_timer_again\*(C'\fR is called, and determines the next timeout (if any),
1397which is also when any modifications are taken into account. 1511which is also when any modifications are taken into account.
1398.PP 1512.PP
1399\fIExamples\fR 1513\fIExamples\fR
1400.IX Subsection "Examples" 1514.IX Subsection "Examples"
1401.PP 1515.PP
1447to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger 1561to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger
1448roughly 10 seconds later as it uses a relative timeout). 1562roughly 10 seconds later as it uses a relative timeout).
1449.PP 1563.PP
1450\&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, 1564\&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers,
1451such as triggering an event on each \*(L"midnight, local time\*(R", or other 1565such as triggering an event on each \*(L"midnight, local time\*(R", or other
1452complicated, rules. 1566complicated rules.
1453.PP 1567.PP
1454As with timers, the callback is guaranteed to be invoked only when the 1568As with timers, the callback is guaranteed to be invoked only when the
1455time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready 1569time (\f(CW\*(C`at\*(C'\fR) has passed, but if multiple periodic timers become ready
1456during the same loop iteration then order of execution is undefined. 1570during the same loop iteration, then order of execution is undefined.
1457.PP 1571.PP
1458\fIWatcher-Specific Functions and Data Members\fR 1572\fIWatcher-Specific Functions and Data Members\fR
1459.IX Subsection "Watcher-Specific Functions and Data Members" 1573.IX Subsection "Watcher-Specific Functions and Data Members"
1460.IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4 1574.IP "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 4
1461.IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)" 1575.IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)"
1462.PD 0 1576.PD 0
1463.IP "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 4 1577.IP "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 4
1464.IX Item "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 1578.IX Item "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)"
1465.PD 1579.PD
1466Lots of arguments, lets sort it out... There are basically three modes of 1580Lots of arguments, lets sort it out... There are basically three modes of
1467operation, and we will explain them from simplest to complex: 1581operation, and we will explain them from simplest to most complex:
1468.RS 4 1582.RS 4
1469.IP "\(bu" 4 1583.IP "\(bu" 4
1470absolute timer (at = time, interval = reschedule_cb = 0) 1584absolute timer (at = time, interval = reschedule_cb = 0)
1471.Sp 1585.Sp
1472In this configuration the watcher triggers an event after the wall clock 1586In this configuration the watcher triggers an event after the wall clock
1473time \f(CW\*(C`at\*(C'\fR has passed and doesn't repeat. It will not adjust when a time 1587time \f(CW\*(C`at\*(C'\fR has passed. It will not repeat and will not adjust when a time
1474jump occurs, that is, if it is to be run at January 1st 2011 then it will 1588jump occurs, that is, if it is to be run at January 1st 2011 then it will
1475run when the system time reaches or surpasses this time. 1589only run when the system clock reaches or surpasses this time.
1476.IP "\(bu" 4 1590.IP "\(bu" 4
1477repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) 1591repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)
1478.Sp 1592.Sp
1479In this mode the watcher will always be scheduled to time out at the next 1593In this mode the watcher will always be scheduled to time out at the next
1480\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) 1594\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative)
1481and then repeat, regardless of any time jumps. 1595and then repeat, regardless of any time jumps.
1482.Sp 1596.Sp
1483This can be used to create timers that do not drift with respect to system 1597This can be used to create timers that do not drift with respect to the
1484time, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each hour, on 1598system clock, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each
1485the hour: 1599hour, on the hour:
1486.Sp 1600.Sp
1487.Vb 1 1601.Vb 1
1488\& ev_periodic_set (&periodic, 0., 3600., 0); 1602\& ev_periodic_set (&periodic, 0., 3600., 0);
1489.Ve 1603.Ve
1490.Sp 1604.Sp
1576.PP 1690.PP
1577\fIExamples\fR 1691\fIExamples\fR
1578.IX Subsection "Examples" 1692.IX Subsection "Examples"
1579.PP 1693.PP
1580Example: Call a callback every hour, or, more precisely, whenever the 1694Example: Call a callback every hour, or, more precisely, whenever the
1581system clock is divisible by 3600. The callback invocation times have 1695system time is divisible by 3600. The callback invocation times have
1582potentially a lot of jitter, but good long-term stability. 1696potentially a lot of jitter, but good long-term stability.
1583.PP 1697.PP
1584.Vb 5 1698.Vb 5
1585\& static void 1699\& static void
1586\& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) 1700\& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents)
1599\& #include <math.h> 1713\& #include <math.h>
1600\& 1714\&
1601\& static ev_tstamp 1715\& static ev_tstamp
1602\& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) 1716\& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now)
1603\& { 1717\& {
1604\& return fmod (now, 3600.) + 3600.; 1718\& return now + (3600. \- fmod (now, 3600.));
1605\& } 1719\& }
1606\& 1720\&
1607\& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); 1721\& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb);
1608.Ve 1722.Ve
1609.PP 1723.PP
1621Signal watchers will trigger an event when the process receives a specific 1735Signal watchers will trigger an event when the process receives a specific
1622signal one or more times. Even though signals are very asynchronous, libev 1736signal one or more times. Even though signals are very asynchronous, libev
1623will try it's best to deliver signals synchronously, i.e. as part of the 1737will try it's best to deliver signals synchronously, i.e. as part of the
1624normal event processing, like any other event. 1738normal event processing, like any other event.
1625.PP 1739.PP
1740If you want signals asynchronously, just use \f(CW\*(C`sigaction\*(C'\fR as you would
1741do without libev and forget about sharing the signal. You can even use
1742\&\f(CW\*(C`ev_async\*(C'\fR from a signal handler to synchronously wake up an event loop.
1743.PP
1626You can configure as many watchers as you like per signal. Only when the 1744You can configure as many watchers as you like per signal. Only when the
1627first watcher gets started will libev actually register a signal watcher 1745first watcher gets started will libev actually register a signal handler
1628with the kernel (thus it coexists with your own signal handlers as long 1746with the kernel (thus it coexists with your own signal handlers as long as
1629as you don't register any with libev). Similarly, when the last signal 1747you don't register any with libev for the same signal). Similarly, when
1630watcher for a signal is stopped libev will reset the signal handler to 1748the last signal watcher for a signal is stopped, libev will reset the
1631\&\s-1SIG_DFL\s0 (regardless of what it was set to before). 1749signal handler to \s-1SIG_DFL\s0 (regardless of what it was set to before).
1632.PP 1750.PP
1633If possible and supported, libev will install its handlers with 1751If possible and supported, libev will install its handlers with
1634\&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so system calls should not be unduly 1752\&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so system calls should not be unduly
1635interrupted. If you have a problem with system calls getting interrupted by 1753interrupted. If you have a problem with system calls getting interrupted by
1636signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock 1754signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock
1651The signal the watcher watches out for. 1769The signal the watcher watches out for.
1652.PP 1770.PP
1653\fIExamples\fR 1771\fIExamples\fR
1654.IX Subsection "Examples" 1772.IX Subsection "Examples"
1655.PP 1773.PP
1656Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. 1774Example: Try to exit cleanly on \s-1SIGINT\s0.
1657.PP 1775.PP
1658.Vb 5 1776.Vb 5
1659\& static void 1777\& static void
1660\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) 1778\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1661\& { 1779\& {
1662\& ev_unloop (loop, EVUNLOOP_ALL); 1780\& ev_unloop (loop, EVUNLOOP_ALL);
1663\& } 1781\& }
1664\& 1782\&
1665\& struct ev_signal signal_watcher; 1783\& struct ev_signal signal_watcher;
1666\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 1784\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1667\& ev_signal_start (loop, &sigint_cb); 1785\& ev_signal_start (loop, &signal_watcher);
1668.Ve 1786.Ve
1669.ie n .Sh """ev_child"" \- watch out for process status changes" 1787.ie n .Sh """ev_child"" \- watch out for process status changes"
1670.el .Sh "\f(CWev_child\fP \- watch out for process status changes" 1788.el .Sh "\f(CWev_child\fP \- watch out for process status changes"
1671.IX Subsection "ev_child - watch out for process status changes" 1789.IX Subsection "ev_child - watch out for process status changes"
1672Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to 1790Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to
1673some child status changes (most typically when a child of yours dies). It 1791some child status changes (most typically when a child of yours dies or
1674is permissible to install a child watcher \fIafter\fR the child has been 1792exits). It is permissible to install a child watcher \fIafter\fR the child
1675forked (which implies it might have already exited), as long as the event 1793has been forked (which implies it might have already exited), as long
1676loop isn't entered (or is continued from a watcher). 1794as the event loop isn't entered (or is continued from a watcher), i.e.,
1795forking and then immediately registering a watcher for the child is fine,
1796but forking and registering a watcher a few event loop iterations later is
1797not.
1677.PP 1798.PP
1678Only the default event loop is capable of handling signals, and therefore 1799Only the default event loop is capable of handling signals, and therefore
1679you can only register child watchers in the default event loop. 1800you can only register child watchers in the default event loop.
1680.PP 1801.PP
1681\fIProcess Interaction\fR 1802\fIProcess Interaction\fR
1696handler, you can override it easily by installing your own handler for 1817handler, you can override it easily by installing your own handler for
1697\&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the 1818\&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the
1698default loop never gets destroyed. You are encouraged, however, to use an 1819default loop never gets destroyed. You are encouraged, however, to use an
1699event-based approach to child reaping and thus use libev's support for 1820event-based approach to child reaping and thus use libev's support for
1700that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely. 1821that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely.
1822.PP
1823\fIStopping the Child Watcher\fR
1824.IX Subsection "Stopping the Child Watcher"
1825.PP
1826Currently, the child watcher never gets stopped, even when the
1827child terminates, so normally one needs to stop the watcher in the
1828callback. Future versions of libev might stop the watcher automatically
1829when a child exit is detected.
1701.PP 1830.PP
1702\fIWatcher-Specific Functions and Data Members\fR 1831\fIWatcher-Specific Functions and Data Members\fR
1703.IX Subsection "Watcher-Specific Functions and Data Members" 1832.IX Subsection "Watcher-Specific Functions and Data Members"
1704.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 1833.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4
1705.IX Item "ev_child_init (ev_child *, callback, int pid, int trace)" 1834.IX Item "ev_child_init (ev_child *, callback, int pid, int trace)"
1771the stat buffer having unspecified contents. 1900the stat buffer having unspecified contents.
1772.PP 1901.PP
1773The path \fIshould\fR be absolute and \fImust not\fR end in a slash. If it is 1902The path \fIshould\fR be absolute and \fImust not\fR end in a slash. If it is
1774relative and your working directory changes, the behaviour is undefined. 1903relative and your working directory changes, the behaviour is undefined.
1775.PP 1904.PP
1776Since there is no standard to do this, the portable implementation simply 1905Since there is no standard kernel interface to do this, the portable
1777calls \f(CW\*(C`stat (2)\*(C'\fR regularly on the path to see if it changed somehow. You 1906implementation simply calls \f(CW\*(C`stat (2)\*(C'\fR regularly on the path to see if
1778can specify a recommended polling interval for this case. If you specify 1907it changed somehow. You can specify a recommended polling interval for
1779a polling interval of \f(CW0\fR (highly recommended!) then a \fIsuitable, 1908this case. If you specify a polling interval of \f(CW0\fR (highly recommended!)
1780unspecified default\fR value will be used (which you can expect to be around 1909then a \fIsuitable, unspecified default\fR value will be used (which
1781five seconds, although this might change dynamically). Libev will also 1910you can expect to be around five seconds, although this might change
1782impose a minimum interval which is currently around \f(CW0.1\fR, but thats 1911dynamically). Libev will also impose a minimum interval which is currently
1783usually overkill. 1912around \f(CW0.1\fR, but thats usually overkill.
1784.PP 1913.PP
1785This watcher type is not meant for massive numbers of stat watchers, 1914This watcher type is not meant for massive numbers of stat watchers,
1786as even with OS-supported change notifications, this can be 1915as even with OS-supported change notifications, this can be
1787resource-intensive. 1916resource-intensive.
1788.PP 1917.PP
1789At the time of this writing, only the Linux inotify interface is 1918At the time of this writing, the only OS-specific interface implemented
1790implemented (implementing kqueue support is left as an exercise for the 1919is the Linux inotify interface (implementing kqueue support is left as
1791reader, note, however, that the author sees no way of implementing ev_stat 1920an exercise for the reader. Note, however, that the author sees no way
1792semantics with kqueue). Inotify will be used to give hints only and should 1921of implementing \f(CW\*(C`ev_stat\*(C'\fR semantics with kqueue).
1793not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev
1794sometimes needs to fall back to regular polling again even with inotify,
1795but changes are usually detected immediately, and if the file exists there
1796will be no polling.
1797.PP 1922.PP
1798\fI\s-1ABI\s0 Issues (Largefile Support)\fR 1923\fI\s-1ABI\s0 Issues (Largefile Support)\fR
1799.IX Subsection "ABI Issues (Largefile Support)" 1924.IX Subsection "ABI Issues (Largefile Support)"
1800.PP 1925.PP
1801Libev by default (unless the user overrides this) uses the default 1926Libev by default (unless the user overrides this) uses the default
1802compilation environment, which means that on systems with optionally 1927compilation environment, which means that on systems with large file
1803disabled large file support, you get the 32 bit version of the stat 1928support disabled by default, you get the 32 bit version of the stat
1804structure. When using the library from programs that change the \s-1ABI\s0 to 1929structure. When using the library from programs that change the \s-1ABI\s0 to
1805use 64 bit file offsets the programs will fail. In that case you have to 1930use 64 bit file offsets the programs will fail. In that case you have to
1806compile libev with the same flags to get binary compatibility. This is 1931compile libev with the same flags to get binary compatibility. This is
1807obviously the case with any flags that change the \s-1ABI\s0, but the problem is 1932obviously the case with any flags that change the \s-1ABI\s0, but the problem is
1808most noticeably with ev_stat and large file support. 1933most noticeably disabled with ev_stat and large file support.
1809.PP 1934.PP
1810\fIInotify\fR 1935The solution for this is to lobby your distribution maker to make large
1936file interfaces available by default (as e.g. FreeBSD does) and not
1937optional. Libev cannot simply switch on large file support because it has
1938to exchange stat structures with application programs compiled using the
1939default compilation environment.
1940.PP
1941\fIInotify and Kqueue\fR
1811.IX Subsection "Inotify" 1942.IX Subsection "Inotify and Kqueue"
1812.PP 1943.PP
1813When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only 1944When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally
1945only available with Linux 2.6.25 or above due to bugs in earlier
1814available on Linux) and present at runtime, it will be used to speed up 1946implementations) and present at runtime, it will be used to speed up
1815change detection where possible. The inotify descriptor will be created lazily 1947change detection where possible. The inotify descriptor will be created
1816when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. 1948lazily when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started.
1817.PP 1949.PP
1818Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers 1950Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers
1819except that changes might be detected earlier, and in some cases, to avoid 1951except that changes might be detected earlier, and in some cases, to avoid
1820making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support 1952making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support
1821there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. 1953there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling,
1954but as long as the path exists, libev usually gets away without polling.
1822.PP 1955.PP
1823(There is no support for kqueue, as apparently it cannot be used to 1956There is no support for kqueue, as apparently it cannot be used to
1824implement this functionality, due to the requirement of having a file 1957implement this functionality, due to the requirement of having a file
1825descriptor open on the object at all times). 1958descriptor open on the object at all times, and detecting renames, unlinks
1959etc. is difficult.
1826.PP 1960.PP
1827\fIThe special problem of stat time resolution\fR 1961\fIThe special problem of stat time resolution\fR
1828.IX Subsection "The special problem of stat time resolution" 1962.IX Subsection "The special problem of stat time resolution"
1829.PP 1963.PP
1830The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, and 1964The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, and
1831even on systems where the resolution is higher, many file systems still 1965even on systems where the resolution is higher, most file systems still
1832only support whole seconds. 1966only support whole seconds.
1833.PP 1967.PP
1834That means that, if the time is the only thing that changes, you can 1968That means that, if the time is the only thing that changes, you can
1835easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and 1969easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and
1836calls your callback, which does something. When there is another update 1970calls your callback, which does something. When there is another update
1837within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat 1971within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect unless the
1838data does not change. 1972stat data does change in other ways (e.g. file size).
1839.PP 1973.PP
1840The solution to this is to delay acting on a change for slightly more 1974The solution to this is to delay acting on a change for slightly more
1841than a second (or till slightly after the next full second boundary), using 1975than a second (or till slightly after the next full second boundary), using
1842a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); 1976a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02);
1843ev_timer_again (loop, w)\*(C'\fR). 1977ev_timer_again (loop, w)\*(C'\fR).
1863\&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to 1997\&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to
1864be detected and should normally be specified as \f(CW0\fR to let libev choose 1998be detected and should normally be specified as \f(CW0\fR to let libev choose
1865a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same 1999a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same
1866path for as long as the watcher is active. 2000path for as long as the watcher is active.
1867.Sp 2001.Sp
1868The callback will receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, relative 2002The callback will receive an \f(CW\*(C`EV_STAT\*(C'\fR event when a change was detected,
1869to the attributes at the time the watcher was started (or the last change 2003relative to the attributes at the time the watcher was started (or the
1870was detected). 2004last change was detected).
1871.IP "ev_stat_stat (loop, ev_stat *)" 4 2005.IP "ev_stat_stat (loop, ev_stat *)" 4
1872.IX Item "ev_stat_stat (loop, ev_stat *)" 2006.IX Item "ev_stat_stat (loop, ev_stat *)"
1873Updates the stat buffer immediately with new values. If you change the 2007Updates the stat buffer immediately with new values. If you change the
1874watched path in your callback, you could call this function to avoid 2008watched path in your callback, you could call this function to avoid
1875detecting this change (while introducing a race condition if you are not 2009detecting this change (while introducing a race condition if you are not
1955.Ve 2089.Ve
1956.ie n .Sh """ev_idle"" \- when you've got nothing better to do..." 2090.ie n .Sh """ev_idle"" \- when you've got nothing better to do..."
1957.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." 2091.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..."
1958.IX Subsection "ev_idle - when you've got nothing better to do..." 2092.IX Subsection "ev_idle - when you've got nothing better to do..."
1959Idle watchers trigger events when no other events of the same or higher 2093Idle watchers trigger events when no other events of the same or higher
1960priority are pending (prepare, check and other idle watchers do not 2094priority are pending (prepare, check and other idle watchers do not count
1961count). 2095as receiving \*(L"events\*(R").
1962.PP 2096.PP
1963That is, as long as your process is busy handling sockets or timeouts 2097That is, as long as your process is busy handling sockets or timeouts
1964(or even signals, imagine) of the same or higher priority it will not be 2098(or even signals, imagine) of the same or higher priority it will not be
1965triggered. But when your process is idle (or only lower-priority watchers 2099triggered. But when your process is idle (or only lower-priority watchers
1966are pending), the idle watchers are being called once per event loop 2100are pending), the idle watchers are being called once per event loop
2003\& ev_idle_start (loop, idle_cb); 2137\& ev_idle_start (loop, idle_cb);
2004.Ve 2138.Ve
2005.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop!" 2139.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop!"
2006.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop!" 2140.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop!"
2007.IX Subsection "ev_prepare and ev_check - customise your event loop!" 2141.IX Subsection "ev_prepare and ev_check - customise your event loop!"
2008Prepare and check watchers are usually (but not always) used in tandem: 2142Prepare and check watchers are usually (but not always) used in pairs:
2009prepare watchers get invoked before the process blocks and check watchers 2143prepare watchers get invoked before the process blocks and check watchers
2010afterwards. 2144afterwards.
2011.PP 2145.PP
2012You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter 2146You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter
2013the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR 2147the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR
2016those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking, 2150those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking,
2017\&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be 2151\&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be
2018called in pairs bracketing the blocking call. 2152called in pairs bracketing the blocking call.
2019.PP 2153.PP
2020Their main purpose is to integrate other event mechanisms into libev and 2154Their main purpose is to integrate other event mechanisms into libev and
2021their use is somewhat advanced. This could be used, for example, to track 2155their use is somewhat advanced. They could be used, for example, to track
2022variable changes, implement your own watchers, integrate net-snmp or a 2156variable changes, implement your own watchers, integrate net-snmp or a
2023coroutine library and lots more. They are also occasionally useful if 2157coroutine library and lots more. They are also occasionally useful if
2024you cache some data and want to flush it before blocking (for example, 2158you cache some data and want to flush it before blocking (for example,
2025in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR 2159in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR
2026watcher). 2160watcher).
2027.PP 2161.PP
2028This is done by examining in each prepare call which file descriptors need 2162This is done by examining in each prepare call which file descriptors
2029to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for 2163need to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers
2030them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries 2164for them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many
2031provide just this functionality). Then, in the check watcher you check for 2165libraries provide exactly this functionality). Then, in the check watcher,
2032any events that occurred (by checking the pending status of all watchers 2166you check for any events that occurred (by checking the pending status
2033and stopping them) and call back into the library. The I/O and timer 2167of all watchers and stopping them) and call back into the library. The
2034callbacks will never actually be called (but must be valid nevertheless, 2168I/O and timer callbacks will never actually be called (but must be valid
2035because you never know, you know?). 2169nevertheless, because you never know, you know?).
2036.PP 2170.PP
2037As another example, the Perl Coro module uses these hooks to integrate 2171As another example, the Perl Coro module uses these hooks to integrate
2038coroutines into libev programs, by yielding to other active coroutines 2172coroutines into libev programs, by yielding to other active coroutines
2039during each prepare and only letting the process block if no coroutines 2173during each prepare and only letting the process block if no coroutines
2040are ready to run (it's actually more complicated: it only runs coroutines 2174are ready to run (it's actually more complicated: it only runs coroutines
2043loop from blocking if lower-priority coroutines are active, thus mapping 2177loop from blocking if lower-priority coroutines are active, thus mapping
2044low-priority coroutines to idle/background tasks). 2178low-priority coroutines to idle/background tasks).
2045.PP 2179.PP
2046It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) 2180It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR)
2047priority, to ensure that they are being run before any other watchers 2181priority, to ensure that they are being run before any other watchers
2182after the poll (this doesn't matter for \f(CW\*(C`ev_prepare\*(C'\fR watchers).
2183.PP
2048after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, 2184Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, too) should not
2049too) should not activate (\*(L"feed\*(R") events into libev. While libev fully 2185activate (\*(L"feed\*(R") events into libev. While libev fully supports this, they
2050supports this, they might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers 2186might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers did their job. As
2051did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other 2187\&\f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other (non-libev) event
2052(non-libev) event loops those other event loops might be in an unusable 2188loops those other event loops might be in an unusable state until their
2053state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to 2189\&\f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to coexist peacefully with
2054coexist peacefully with others). 2190others).
2055.PP 2191.PP
2056\fIWatcher-Specific Functions and Data Members\fR 2192\fIWatcher-Specific Functions and Data Members\fR
2057.IX Subsection "Watcher-Specific Functions and Data Members" 2193.IX Subsection "Watcher-Specific Functions and Data Members"
2058.IP "ev_prepare_init (ev_prepare *, callback)" 4 2194.IP "ev_prepare_init (ev_prepare *, callback)" 4
2059.IX Item "ev_prepare_init (ev_prepare *, callback)" 2195.IX Item "ev_prepare_init (ev_prepare *, callback)"
2061.IP "ev_check_init (ev_check *, callback)" 4 2197.IP "ev_check_init (ev_check *, callback)" 4
2062.IX Item "ev_check_init (ev_check *, callback)" 2198.IX Item "ev_check_init (ev_check *, callback)"
2063.PD 2199.PD
2064Initialises and configures the prepare or check watcher \- they have no 2200Initialises and configures the prepare or check watcher \- they have no
2065parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR 2201parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR
2066macros, but using them is utterly, utterly and completely pointless. 2202macros, but using them is utterly, utterly, utterly and completely
2203pointless.
2067.PP 2204.PP
2068\fIExamples\fR 2205\fIExamples\fR
2069.IX Subsection "Examples" 2206.IX Subsection "Examples"
2070.PP 2207.PP
2071There are a number of principal ways to embed other event loops or modules 2208There are a number of principal ways to embed other event loops or modules
2167\& 2304\&
2168\& // do not ever call adns_afterpoll 2305\& // do not ever call adns_afterpoll
2169.Ve 2306.Ve
2170.PP 2307.PP
2171Method 4: Do not use a prepare or check watcher because the module you 2308Method 4: Do not use a prepare or check watcher because the module you
2172want to embed is too inflexible to support it. Instead, you can override 2309want to embed is not flexible enough to support it. Instead, you can
2173their poll function. The drawback with this solution is that the main 2310override their poll function. The drawback with this solution is that the
2174loop is now no longer controllable by \s-1EV\s0. The \f(CW\*(C`Glib::EV\*(C'\fR module does 2311main loop is now no longer controllable by \s-1EV\s0. The \f(CW\*(C`Glib::EV\*(C'\fR module uses
2175this. 2312this approach, effectively embedding \s-1EV\s0 as a client into the horrible
2313libglib event loop.
2176.PP 2314.PP
2177.Vb 4 2315.Vb 4
2178\& static gint 2316\& static gint
2179\& event_poll_func (GPollFD *fds, guint nfds, gint timeout) 2317\& event_poll_func (GPollFD *fds, guint nfds, gint timeout)
2180\& { 2318\& {
2212prioritise I/O. 2350prioritise I/O.
2213.PP 2351.PP
2214As an example for a bug workaround, the kqueue backend might only support 2352As an example for a bug workaround, the kqueue backend might only support
2215sockets on some platform, so it is unusable as generic backend, but you 2353sockets on some platform, so it is unusable as generic backend, but you
2216still want to make use of it because you have many sockets and it scales 2354still want to make use of it because you have many sockets and it scales
2217so nicely. In this case, you would create a kqueue-based loop and embed it 2355so nicely. In this case, you would create a kqueue-based loop and embed
2218into your default loop (which might use e.g. poll). Overall operation will 2356it into your default loop (which might use e.g. poll). Overall operation
2219be a bit slower because first libev has to poll and then call kevent, but 2357will be a bit slower because first libev has to call \f(CW\*(C`poll\*(C'\fR and then
2220at least you can use both at what they are best. 2358\&\f(CW\*(C`kevent\*(C'\fR, but at least you can use both mechanisms for what they are
2359best: \f(CW\*(C`kqueue\*(C'\fR for scalable sockets and \f(CW\*(C`poll\*(C'\fR if you want it to work :)
2221.PP 2360.PP
2222As for prioritising I/O: rarely you have the case where some fds have 2361As for prioritising I/O: under rare circumstances you have the case where
2223to be watched and handled very quickly (with low latency), and even 2362some fds have to be watched and handled very quickly (with low latency),
2224priorities and idle watchers might have too much overhead. In this case 2363and even priorities and idle watchers might have too much overhead. In
2225you would put all the high priority stuff in one loop and all the rest in 2364this case you would put all the high priority stuff in one loop and all
2226a second one, and embed the second one in the first. 2365the rest in a second one, and embed the second one in the first.
2227.PP 2366.PP
2228As long as the watcher is active, the callback will be invoked every time 2367As long as the watcher is active, the callback will be invoked every time
2229there might be events pending in the embedded loop. The callback must then 2368there might be events pending in the embedded loop. The callback must then
2230call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke 2369call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke
2231their callbacks (you could also start an idle watcher to give the embedded 2370their callbacks (you could also start an idle watcher to give the embedded
2239interested in that. 2378interested in that.
2240.PP 2379.PP
2241Also, there have not currently been made special provisions for forking: 2380Also, there have not currently been made special provisions for forking:
2242when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops, 2381when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops,
2243but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers 2382but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers
2244yourself. 2383yourself \- but you can use a fork watcher to handle this automatically,
2384and future versions of libev might do just that.
2245.PP 2385.PP
2246Unfortunately, not all backends are embeddable, only the ones returned by 2386Unfortunately, not all backends are embeddable: only the ones returned by
2247\&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any 2387\&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any
2248portable one. 2388portable one.
2249.PP 2389.PP
2250So when you want to use this feature you will always have to be prepared 2390So when you want to use this feature you will always have to be prepared
2251that you cannot get an embeddable loop. The recommended way to get around 2391that you cannot get an embeddable loop. The recommended way to get around
2252this is to have a separate variables for your embeddable loop, try to 2392this is to have a separate variables for your embeddable loop, try to
2253create it, and if that fails, use the normal loop for everything. 2393create it, and if that fails, use the normal loop for everything.
2394.PP
2395\fI\f(CI\*(C`ev_embed\*(C'\fI and fork\fR
2396.IX Subsection "ev_embed and fork"
2397.PP
2398While the \f(CW\*(C`ev_embed\*(C'\fR watcher is running, forks in the embedding loop will
2399automatically be applied to the embedded loop as well, so no special
2400fork handling is required in that case. When the watcher is not running,
2401however, it is still the task of the libev user to call \f(CW\*(C`ev_loop_fork ()\*(C'\fR
2402as applicable.
2254.PP 2403.PP
2255\fIWatcher-Specific Functions and Data Members\fR 2404\fIWatcher-Specific Functions and Data Members\fR
2256.IX Subsection "Watcher-Specific Functions and Data Members" 2405.IX Subsection "Watcher-Specific Functions and Data Members"
2257.IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4 2406.IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4
2258.IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 2407.IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)"
2372is that the author does not know of a simple (or any) algorithm for a 2521is that the author does not know of a simple (or any) algorithm for a
2373multiple-writer-single-reader queue that works in all cases and doesn't 2522multiple-writer-single-reader queue that works in all cases and doesn't
2374need elaborate support such as pthreads. 2523need elaborate support such as pthreads.
2375.PP 2524.PP
2376That means that if you want to queue data, you have to provide your own 2525That means that if you want to queue data, you have to provide your own
2377queue. But at least I can tell you would implement locking around your 2526queue. But at least I can tell you how to implement locking around your
2378queue: 2527queue:
2379.IP "queueing from a signal handler context" 4 2528.IP "queueing from a signal handler context" 4
2380.IX Item "queueing from a signal handler context" 2529.IX Item "queueing from a signal handler context"
2381To implement race-free queueing, you simply add to the queue in the signal 2530To implement race-free queueing, you simply add to the queue in the signal
2382handler but you block the signal handler in the watcher callback. Here is an example that does that for 2531handler but you block the signal handler in the watcher callback. Here is
2383some fictitious \s-1SIGUSR1\s0 handler: 2532an example that does that for some fictitious \s-1SIGUSR1\s0 handler:
2384.Sp 2533.Sp
2385.Vb 1 2534.Vb 1
2386\& static ev_async mysig; 2535\& static ev_async mysig;
2387\& 2536\&
2388\& static void 2537\& static void
2453.IX Subsection "Watcher-Specific Functions and Data Members" 2602.IX Subsection "Watcher-Specific Functions and Data Members"
2454.IP "ev_async_init (ev_async *, callback)" 4 2603.IP "ev_async_init (ev_async *, callback)" 4
2455.IX Item "ev_async_init (ev_async *, callback)" 2604.IX Item "ev_async_init (ev_async *, callback)"
2456Initialises and configures the async watcher \- it has no parameters of any 2605Initialises and configures the async watcher \- it has no parameters of any
2457kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless, 2606kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless,
2458believe me. 2607trust me.
2459.IP "ev_async_send (loop, ev_async *)" 4 2608.IP "ev_async_send (loop, ev_async *)" 4
2460.IX Item "ev_async_send (loop, ev_async *)" 2609.IX Item "ev_async_send (loop, ev_async *)"
2461Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds 2610Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds
2462an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike 2611an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike
2463\&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do in other threads, signal or 2612\&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or
2464similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding 2613similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding
2465section below on what exactly this means). 2614section below on what exactly this means).
2466.Sp 2615.Sp
2467This call incurs the overhead of a system call only once per loop iteration, 2616This call incurs the overhead of a system call only once per loop iteration,
2468so while the overhead might be noticeable, it doesn't apply to repeated 2617so while the overhead might be noticeable, it doesn't apply to repeated
2484.IX Header "OTHER FUNCTIONS" 2633.IX Header "OTHER FUNCTIONS"
2485There are some other functions of possible interest. Described. Here. Now. 2634There are some other functions of possible interest. Described. Here. Now.
2486.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 2635.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
2487.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 2636.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
2488This function combines a simple timer and an I/O watcher, calls your 2637This function combines a simple timer and an I/O watcher, calls your
2489callback on whichever event happens first and automatically stop both 2638callback on whichever event happens first and automatically stops both
2490watchers. This is useful if you want to wait for a single event on an fd 2639watchers. This is useful if you want to wait for a single event on an fd
2491or timeout without having to allocate/configure/start/stop/free one or 2640or timeout without having to allocate/configure/start/stop/free one or
2492more watchers yourself. 2641more watchers yourself.
2493.Sp 2642.Sp
2494If \f(CW\*(C`fd\*(C'\fR is less than 0, then no I/O watcher will be started and events 2643If \f(CW\*(C`fd\*(C'\fR is less than 0, then no I/O watcher will be started and the
2495is being ignored. Otherwise, an \f(CW\*(C`ev_io\*(C'\fR watcher for the given \f(CW\*(C`fd\*(C'\fR and 2644\&\f(CW\*(C`events\*(C'\fR argument is being ignored. Otherwise, an \f(CW\*(C`ev_io\*(C'\fR watcher for
2496\&\f(CW\*(C`events\*(C'\fR set will be created and started. 2645the given \f(CW\*(C`fd\*(C'\fR and \f(CW\*(C`events\*(C'\fR set will be created and started.
2497.Sp 2646.Sp
2498If \f(CW\*(C`timeout\*(C'\fR is less than 0, then no timeout watcher will be 2647If \f(CW\*(C`timeout\*(C'\fR is less than 0, then no timeout watcher will be
2499started. Otherwise an \f(CW\*(C`ev_timer\*(C'\fR watcher with after = \f(CW\*(C`timeout\*(C'\fR (and 2648started. Otherwise an \f(CW\*(C`ev_timer\*(C'\fR watcher with after = \f(CW\*(C`timeout\*(C'\fR (and
2500repeat = 0) will be started. While \f(CW0\fR is a valid timeout, it is of 2649repeat = 0) will be started. \f(CW0\fR is a valid timeout.
2501dubious value.
2502.Sp 2650.Sp
2503The callback has the type \f(CW\*(C`void (*cb)(int revents, void *arg)\*(C'\fR and gets 2651The callback has the type \f(CW\*(C`void (*cb)(int revents, void *arg)\*(C'\fR and gets
2504passed an \f(CW\*(C`revents\*(C'\fR set like normal event callbacks (a combination of 2652passed an \f(CW\*(C`revents\*(C'\fR set like normal event callbacks (a combination of
2505\&\f(CW\*(C`EV_ERROR\*(C'\fR, \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_TIMEOUT\*(C'\fR) and the \f(CW\*(C`arg\*(C'\fR 2653\&\f(CW\*(C`EV_ERROR\*(C'\fR, \f(CW\*(C`EV_READ\*(C'\fR, \f(CW\*(C`EV_WRITE\*(C'\fR or \f(CW\*(C`EV_TIMEOUT\*(C'\fR) and the \f(CW\*(C`arg\*(C'\fR
2506value passed to \f(CW\*(C`ev_once\*(C'\fR: 2654value passed to \f(CW\*(C`ev_once\*(C'\fR. Note that it is possible to receive \fIboth\fR
2655a timeout and an io event at the same time \- you probably should give io
2656events precedence.
2657.Sp
2658Example: wait up to ten seconds for data to appear on \s-1STDIN_FILENO\s0.
2507.Sp 2659.Sp
2508.Vb 7 2660.Vb 7
2509\& static void stdin_ready (int revents, void *arg) 2661\& static void stdin_ready (int revents, void *arg)
2510\& { 2662\& {
2663\& if (revents & EV_READ)
2664\& /* stdin might have data for us, joy! */;
2511\& if (revents & EV_TIMEOUT) 2665\& else if (revents & EV_TIMEOUT)
2512\& /* doh, nothing entered */; 2666\& /* doh, nothing entered */;
2513\& else if (revents & EV_READ)
2514\& /* stdin might have data for us, joy! */;
2515\& } 2667\& }
2516\& 2668\&
2517\& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 2669\& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
2518.Ve 2670.Ve
2519.IP "ev_feed_event (ev_loop *, watcher *, int revents)" 4 2671.IP "ev_feed_event (ev_loop *, watcher *, int revents)" 4
2656.Sp 2808.Sp
2657The prototype of the \f(CW\*(C`function\*(C'\fR must be \f(CW\*(C`void (*)(ev::TYPE &w, int)\*(C'\fR. 2809The prototype of the \f(CW\*(C`function\*(C'\fR must be \f(CW\*(C`void (*)(ev::TYPE &w, int)\*(C'\fR.
2658.Sp 2810.Sp
2659See the method\-\f(CW\*(C`set\*(C'\fR above for more details. 2811See the method\-\f(CW\*(C`set\*(C'\fR above for more details.
2660.Sp 2812.Sp
2661Example: 2813Example: Use a plain function as callback.
2662.Sp 2814.Sp
2663.Vb 2 2815.Vb 2
2664\& static void io_cb (ev::io &w, int revents) { } 2816\& static void io_cb (ev::io &w, int revents) { }
2665\& iow.set <io_cb> (); 2817\& iow.set <io_cb> ();
2666.Ve 2818.Ve
2702the constructor. 2854the constructor.
2703.PP 2855.PP
2704.Vb 4 2856.Vb 4
2705\& class myclass 2857\& class myclass
2706\& { 2858\& {
2707\& ev::io io; void io_cb (ev::io &w, int revents); 2859\& ev::io io ; void io_cb (ev::io &w, int revents);
2708\& ev:idle idle void idle_cb (ev::idle &w, int revents); 2860\& ev::idle idle; void idle_cb (ev::idle &w, int revents);
2709\& 2861\&
2710\& myclass (int fd) 2862\& myclass (int fd)
2711\& { 2863\& {
2712\& io .set <myclass, &myclass::io_cb > (this); 2864\& io .set <myclass, &myclass::io_cb > (this);
2713\& idle.set <myclass, &myclass::idle_cb> (this); 2865\& idle.set <myclass, &myclass::idle_cb> (this);
2725.IP "Perl" 4 2877.IP "Perl" 4
2726.IX Item "Perl" 2878.IX Item "Perl"
2727The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test 2879The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test
2728libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module, 2880libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module,
2729there are additional modules that implement libev-compatible interfaces 2881there are additional modules that implement libev-compatible interfaces
2730to \f(CW\*(C`libadns\*(C'\fR (\f(CW\*(C`EV::ADNS\*(C'\fR), \f(CW\*(C`Net::SNMP\*(C'\fR (\f(CW\*(C`Net::SNMP::EV\*(C'\fR) and the 2882to \f(CW\*(C`libadns\*(C'\fR (\f(CW\*(C`EV::ADNS\*(C'\fR, but \f(CW\*(C`AnyEvent::DNS\*(C'\fR is preferred nowadays),
2731\&\f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR and \f(CW\*(C`EV::Glib\*(C'\fR). 2883\&\f(CW\*(C`Net::SNMP\*(C'\fR (\f(CW\*(C`Net::SNMP::EV\*(C'\fR) and the \f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR
2884and \f(CW\*(C`EV::Glib\*(C'\fR).
2732.Sp 2885.Sp
2733It can be found and installed via \s-1CPAN\s0, its homepage is at 2886It can be found and installed via \s-1CPAN\s0, its homepage is at
2734<http://software.schmorp.de/pkg/EV>. 2887<http://software.schmorp.de/pkg/EV>.
2735.IP "Python" 4 2888.IP "Python" 4
2736.IX Item "Python" 2889.IX Item "Python"
2747more on top of it. It can be found via gem servers. Its homepage is at 2900more on top of it. It can be found via gem servers. Its homepage is at
2748<http://rev.rubyforge.org/>. 2901<http://rev.rubyforge.org/>.
2749.IP "D" 4 2902.IP "D" 4
2750.IX Item "D" 2903.IX Item "D"
2751Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to 2904Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to
2752be found at <http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. 2905be found at <http://proj.llucax.com.ar/wiki/evd>.
2753.SH "MACRO MAGIC" 2906.SH "MACRO MAGIC"
2754.IX Header "MACRO MAGIC" 2907.IX Header "MACRO MAGIC"
2755Libev can be compiled with a variety of options, the most fundamental 2908Libev can be compiled with a variety of options, the most fundamental
2756of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) 2909of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most)
2757functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. 2910functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument.
2924.Ve 3077.Ve
2925.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" 3078.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0"
2926.IX Subsection "PREPROCESSOR SYMBOLS/MACROS" 3079.IX Subsection "PREPROCESSOR SYMBOLS/MACROS"
2927Libev can be configured via a variety of preprocessor symbols you have to 3080Libev can be configured via a variety of preprocessor symbols you have to
2928define before including any of its files. The default in the absence of 3081define before including any of its files. The default in the absence of
2929autoconf is noted for every option. 3082autoconf is documented for every option.
2930.IP "\s-1EV_STANDALONE\s0" 4 3083.IP "\s-1EV_STANDALONE\s0" 4
2931.IX Item "EV_STANDALONE" 3084.IX Item "EV_STANDALONE"
2932Must always be \f(CW1\fR if you do not use autoconf configuration, which 3085Must always be \f(CW1\fR if you do not use autoconf configuration, which
2933keeps libev from including \fIconfig.h\fR, and it also defines dummy 3086keeps libev from including \fIconfig.h\fR, and it also defines dummy
2934implementations for some libevent functions (such as logging, which is not 3087implementations for some libevent functions (such as logging, which is not
3082When doing priority-based operations, libev usually has to linearly search 3235When doing priority-based operations, libev usually has to linearly search
3083all the priorities, so having many of them (hundreds) uses a lot of space 3236all the priorities, so having many of them (hundreds) uses a lot of space
3084and time, so using the defaults of five priorities (\-2 .. +2) is usually 3237and time, so using the defaults of five priorities (\-2 .. +2) is usually
3085fine. 3238fine.
3086.Sp 3239.Sp
3087If your embedding application does not need any priorities, defining these both to 3240If your embedding application does not need any priorities, defining these
3088\&\f(CW0\fR will save some memory and \s-1CPU\s0. 3241both to \f(CW0\fR will save some memory and \s-1CPU\s0.
3089.IP "\s-1EV_PERIODIC_ENABLE\s0" 4 3242.IP "\s-1EV_PERIODIC_ENABLE\s0" 4
3090.IX Item "EV_PERIODIC_ENABLE" 3243.IX Item "EV_PERIODIC_ENABLE"
3091If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If 3244If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If
3092defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of 3245defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of
3093code. 3246code.
3097defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of 3250defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of
3098code. 3251code.
3099.IP "\s-1EV_EMBED_ENABLE\s0" 4 3252.IP "\s-1EV_EMBED_ENABLE\s0" 4
3100.IX Item "EV_EMBED_ENABLE" 3253.IX Item "EV_EMBED_ENABLE"
3101If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If 3254If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If
3102defined to be \f(CW0\fR, then they are not. 3255defined to be \f(CW0\fR, then they are not. Embed watchers rely on most other
3256watcher types, which therefore must not be disabled.
3103.IP "\s-1EV_STAT_ENABLE\s0" 4 3257.IP "\s-1EV_STAT_ENABLE\s0" 4
3104.IX Item "EV_STAT_ENABLE" 3258.IX Item "EV_STAT_ENABLE"
3105If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If 3259If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If
3106defined to be \f(CW0\fR, then they are not. 3260defined to be \f(CW0\fR, then they are not.
3107.IP "\s-1EV_FORK_ENABLE\s0" 4 3261.IP "\s-1EV_FORK_ENABLE\s0" 4
3132watchers you might want to increase this value (\fImust\fR be a power of 3286watchers you might want to increase this value (\fImust\fR be a power of
3133two). 3287two).
3134.IP "\s-1EV_USE_4HEAP\s0" 4 3288.IP "\s-1EV_USE_4HEAP\s0" 4
3135.IX Item "EV_USE_4HEAP" 3289.IX Item "EV_USE_4HEAP"
3136Heaps are not very cache-efficient. To improve the cache-efficiency of the 3290Heaps are not very cache-efficient. To improve the cache-efficiency of the
3137timer and periodics heap, libev uses a 4\-heap when this symbol is defined 3291timer and periodics heaps, libev uses a 4\-heap when this symbol is defined
3138to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has 3292to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has noticeably
3139noticeably faster performance with many (thousands) of watchers. 3293faster performance with many (thousands) of watchers.
3140.Sp 3294.Sp
3141The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR 3295The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
3142(disabled). 3296(disabled).
3143.IP "\s-1EV_HEAP_CACHE_AT\s0" 4 3297.IP "\s-1EV_HEAP_CACHE_AT\s0" 4
3144.IX Item "EV_HEAP_CACHE_AT" 3298.IX Item "EV_HEAP_CACHE_AT"
3145Heaps are not very cache-efficient. To improve the cache-efficiency of the 3299Heaps are not very cache-efficient. To improve the cache-efficiency of the
3146timer and periodics heap, libev can cache the timestamp (\fIat\fR) within 3300timer and periodics heaps, libev can cache the timestamp (\fIat\fR) within
3147the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), 3301the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR),
3148which uses 8\-12 bytes more per watcher and a few hundred bytes more code, 3302which uses 8\-12 bytes more per watcher and a few hundred bytes more code,
3149but avoids random read accesses on heap changes. This improves performance 3303but avoids random read accesses on heap changes. This improves performance
3150noticeably with with many (hundreds) of watchers. 3304noticeably with many (hundreds) of watchers.
3151.Sp 3305.Sp
3152The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR 3306The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
3153(disabled). 3307(disabled).
3154.IP "\s-1EV_VERIFY\s0" 4 3308.IP "\s-1EV_VERIFY\s0" 4
3155.IX Item "EV_VERIFY" 3309.IX Item "EV_VERIFY"
3160called once per loop, which can slow down libev. If set to \f(CW3\fR, then the 3314called once per loop, which can slow down libev. If set to \f(CW3\fR, then the
3161verification code will be called very frequently, which will slow down 3315verification code will be called very frequently, which will slow down
3162libev considerably. 3316libev considerably.
3163.Sp 3317.Sp
3164The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be 3318The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be
3165\&\f(CW0.\fR 3319\&\f(CW0\fR.
3166.IP "\s-1EV_COMMON\s0" 4 3320.IP "\s-1EV_COMMON\s0" 4
3167.IX Item "EV_COMMON" 3321.IX Item "EV_COMMON"
3168By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining 3322By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining
3169this macro to a something else you can include more and other types of 3323this macro to a something else you can include more and other types of
3170members. You have to define it each time you include one of the files, 3324members. You have to define it each time you include one of the files,
3252.PP 3406.PP
3253.Vb 2 3407.Vb 2
3254\& #include "ev_cpp.h" 3408\& #include "ev_cpp.h"
3255\& #include "ev.c" 3409\& #include "ev.c"
3256.Ve 3410.Ve
3257.SH "THREADS AND COROUTINES" 3411.SH "INTERACTION WITH OTHER PROGRAMS OR LIBRARIES"
3412.IX Header "INTERACTION WITH OTHER PROGRAMS OR LIBRARIES"
3413.Sh "\s-1THREADS\s0 \s-1AND\s0 \s-1COROUTINES\s0"
3258.IX Header "THREADS AND COROUTINES" 3414.IX Subsection "THREADS AND COROUTINES"
3259.Sh "\s-1THREADS\s0" 3415\fI\s-1THREADS\s0\fR
3260.IX Subsection "THREADS" 3416.IX Subsection "THREADS"
3261Libev itself is completely thread-safe, but it uses no locking. This 3417.PP
3418All libev functions are reentrant and thread-safe unless explicitly
3419documented otherwise, but libev implements no locking itself. This means
3262means that you can use as many loops as you want in parallel, as long as 3420that you can use as many loops as you want in parallel, as long as there
3263only one thread ever calls into one libev function with the same loop 3421are no concurrent calls into any libev function with the same loop
3264parameter. 3422parameter (\f(CW\*(C`ev_default_*\*(C'\fR calls have an implicit default loop parameter,
3423of course): libev guarantees that different event loops share no data
3424structures that need any locking.
3265.PP 3425.PP
3266Or put differently: calls with different loop parameters can be done in 3426Or to put it differently: calls with different loop parameters can be done
3267parallel from multiple threads, calls with the same loop parameter must be 3427concurrently from multiple threads, calls with the same loop parameter
3268done serially (but can be done from different threads, as long as only one 3428must be done serially (but can be done from different threads, as long as
3269thread ever is inside a call at any point in time, e.g. by using a mutex 3429only one thread ever is inside a call at any point in time, e.g. by using
3270per loop). 3430a mutex per loop).
3271.PP 3431.PP
3272If you want to know which design is best for your problem, then I cannot 3432Specifically to support threads (and signal handlers), libev implements
3433so-called \f(CW\*(C`ev_async\*(C'\fR watchers, which allow some limited form of
3434concurrency on the same event loop, namely waking it up \*(L"from the
3435outside\*(R".
3436.PP
3437If you want to know which design (one loop, locking, or multiple loops
3438without or something else still) is best for your problem, then I cannot
3273help you but by giving some generic advice: 3439help you, but here is some generic advice:
3274.IP "\(bu" 4 3440.IP "\(bu" 4
3275most applications have a main thread: use the default libev loop 3441most applications have a main thread: use the default libev loop
3276in that thread, or create a separate thread running only the default loop. 3442in that thread, or create a separate thread running only the default loop.
3277.Sp 3443.Sp
3278This helps integrating other libraries or software modules that use libev 3444This helps integrating other libraries or software modules that use libev
3288.Sp 3454.Sp
3289Choosing a model is hard \- look around, learn, know that usually you can do 3455Choosing a model is hard \- look around, learn, know that usually you can do
3290better than you currently do :\-) 3456better than you currently do :\-)
3291.IP "\(bu" 4 3457.IP "\(bu" 4
3292often you need to talk to some other thread which blocks in the 3458often you need to talk to some other thread which blocks in the
3459event loop.
3460.Sp
3293event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other 3461\&\f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other threads safely
3294threads safely (or from signal contexts...). 3462(or from signal contexts...).
3295.Sh "\s-1COROUTINES\s0" 3463.Sp
3464An example use would be to communicate signals or other events that only
3465work in the default loop by registering the signal watcher with the
3466default loop and triggering an \f(CW\*(C`ev_async\*(C'\fR watcher from the default loop
3467watcher callback into the event loop interested in the signal.
3468.PP
3469\fI\s-1COROUTINES\s0\fR
3296.IX Subsection "COROUTINES" 3470.IX Subsection "COROUTINES"
3471.PP
3297Libev is much more accommodating to coroutines (\*(L"cooperative threads\*(R"): 3472Libev is very accommodating to coroutines (\*(L"cooperative threads\*(R"):
3298libev fully supports nesting calls to it's functions from different 3473libev fully supports nesting calls to its functions from different
3299coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two 3474coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two
3300different coroutines and switch freely between both coroutines running the 3475different coroutines, and switch freely between both coroutines running the
3301loop, as long as you don't confuse yourself). The only exception is that 3476loop, as long as you don't confuse yourself). The only exception is that
3302you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. 3477you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks.
3303.PP 3478.PP
3304Care has been invested into making sure that libev does not keep local 3479Care has been taken to ensure that libev does not keep local state inside
3305state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine 3480\&\f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow for coroutine switches as
3481they do not clal any callbacks.
3482.Sh "\s-1COMPILER\s0 \s-1WARNINGS\s0"
3483.IX Subsection "COMPILER WARNINGS"
3484Depending on your compiler and compiler settings, you might get no or a
3485lot of warnings when compiling libev code. Some people are apparently
3486scared by this.
3487.PP
3488However, these are unavoidable for many reasons. For one, each compiler
3489has different warnings, and each user has different tastes regarding
3490warning options. \*(L"Warn-free\*(R" code therefore cannot be a goal except when
3491targeting a specific compiler and compiler-version.
3492.PP
3493Another reason is that some compiler warnings require elaborate
3494workarounds, or other changes to the code that make it less clear and less
3495maintainable.
3496.PP
3497And of course, some compiler warnings are just plain stupid, or simply
3498wrong (because they don't actually warn about the condition their message
3499seems to warn about). For example, certain older gcc versions had some
3500warnings that resulted an extreme number of false positives. These have
3501been fixed, but some people still insist on making code warn-free with
3502such buggy versions.
3503.PP
3504While libev is written to generate as few warnings as possible,
3505\&\*(L"warn-free\*(R" code is not a goal, and it is recommended not to build libev
3506with any compiler warnings enabled unless you are prepared to cope with
3507them (e.g. by ignoring them). Remember that warnings are just that:
3508warnings, not errors, or proof of bugs.
3509.Sh "\s-1VALGRIND\s0"
3510.IX Subsection "VALGRIND"
3511Valgrind has a special section here because it is a popular tool that is
3512highly useful. Unfortunately, valgrind reports are very hard to interpret.
3513.PP
3514If you think you found a bug (memory leak, uninitialised data access etc.)
3515in libev, then check twice: If valgrind reports something like:
3516.PP
3517.Vb 3
3518\& ==2274== definitely lost: 0 bytes in 0 blocks.
3519\& ==2274== possibly lost: 0 bytes in 0 blocks.
3520\& ==2274== still reachable: 256 bytes in 1 blocks.
3521.Ve
3522.PP
3523Then there is no memory leak, just as memory accounted to global variables
3524is not a memleak \- the memory is still being refernced, and didn't leak.
3525.PP
3526Similarly, under some circumstances, valgrind might report kernel bugs
3527as if it were a bug in libev (e.g. in realloc or in the poll backend,
3528although an acceptable workaround has been found here), or it might be
3529confused.
3530.PP
3531Keep in mind that valgrind is a very good tool, but only a tool. Don't
3532make it into some kind of religion.
3533.PP
3534If you are unsure about something, feel free to contact the mailing list
3535with the full valgrind report and an explanation on why you think this
3536is a bug in libev (best check the archives, too :). However, don't be
3537annoyed when you get a brisk \*(L"this is no bug\*(R" answer and take the chance
3538of learning how to interpret valgrind properly.
3539.PP
3540If you need, for some reason, empty reports from valgrind for your project
3541I suggest using suppression lists.
3542.SH "PORTABILITY NOTES"
3543.IX Header "PORTABILITY NOTES"
3544.Sh "\s-1WIN32\s0 \s-1PLATFORM\s0 \s-1LIMITATIONS\s0 \s-1AND\s0 \s-1WORKAROUNDS\s0"
3545.IX Subsection "WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS"
3546Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev
3547requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0
3548model. Libev still offers limited functionality on this platform in
3549the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket
3550descriptors. This only applies when using Win32 natively, not when using
3551e.g. cygwin.
3552.PP
3553Lifting these limitations would basically require the full
3554re-implementation of the I/O system. If you are into these kinds of
3555things, then note that glib does exactly that for you in a very portable
3556way (note also that glib is the slowest event library known to man).
3557.PP
3558There is no supported compilation method available on windows except
3559embedding it into other applications.
3560.PP
3561Not a libev limitation but worth mentioning: windows apparently doesn't
3562accept large writes: instead of resulting in a partial write, windows will
3563either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large,
3564so make sure you only write small amounts into your sockets (less than a
3565megabyte seems safe, but this apparently depends on the amount of memory
3566available).
3567.PP
3568Due to the many, low, and arbitrary limits on the win32 platform and
3569the abysmal performance of winsockets, using a large number of sockets
3570is not recommended (and not reasonable). If your program needs to use
3571more than a hundred or so sockets, then likely it needs to use a totally
3572different implementation for windows, as libev offers the \s-1POSIX\s0 readiness
3573notification model, which cannot be implemented efficiently on windows
3574(Microsoft monopoly games).
3575.PP
3576A typical way to use libev under windows is to embed it (see the embedding
3577section for details) and use the following \fIevwrap.h\fR header file instead
3578of \fIev.h\fR:
3579.PP
3580.Vb 2
3581\& #define EV_STANDALONE /* keeps ev from requiring config.h */
3582\& #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */
3583\&
3584\& #include "ev.h"
3585.Ve
3586.PP
3587And compile the following \fIevwrap.c\fR file into your project (make sure
3588you do \fInot\fR compile the \fIev.c\fR or any other embedded source files!):
3589.PP
3590.Vb 2
3591\& #include "evwrap.h"
3592\& #include "ev.c"
3593.Ve
3594.IP "The winsocket select function" 4
3595.IX Item "The winsocket select function"
3596The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it
3597requires socket \fIhandles\fR and not socket \fIfile descriptors\fR (it is
3598also extremely buggy). This makes select very inefficient, and also
3599requires a mapping from file descriptors to socket handles (the Microsoft
3600C runtime provides the function \f(CW\*(C`_open_osfhandle\*(C'\fR for this). See the
3601discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, \f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and
3602\&\f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor symbols for more info.
3603.Sp
3604The configuration for a \*(L"naked\*(R" win32 using the Microsoft runtime
3605libraries and raw winsocket select is:
3606.Sp
3607.Vb 2
3608\& #define EV_USE_SELECT 1
3609\& #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */
3610.Ve
3611.Sp
3612Note that winsockets handling of fd sets is O(n), so you can easily get a
3613complexity in the O(nA\*^X) range when using win32.
3614.IP "Limited number of file descriptors" 4
3615.IX Item "Limited number of file descriptors"
3616Windows has numerous arbitrary (and low) limits on things.
3617.Sp
3618Early versions of winsocket's select only supported waiting for a maximum
3619of \f(CW64\fR handles (probably owning to the fact that all windows kernels
3620can only wait for \f(CW64\fR things at the same time internally; Microsoft
3621recommends spawning a chain of threads and wait for 63 handles and the
3622previous thread in each. Great).
3623.Sp
3624Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR
3625to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select
3626call (which might be in libev or elsewhere, for example, perl does its own
3627select emulation on windows).
3628.Sp
3629Another limit is the number of file descriptors in the Microsoft runtime
3630libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR fetish
3631or something like this inside Microsoft). You can increase this by calling
3632\&\f(CW\*(C`_setmaxstdio\*(C'\fR, which can increase this limit to \f(CW2048\fR (another
3633arbitrary limit), but is broken in many versions of the Microsoft runtime
3634libraries.
3635.Sp
3636This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on
3637windows version and/or the phase of the moon). To get more, you need to
3638wrap all I/O functions and provide your own fd management, but the cost of
3639calling select (O(nA\*^X)) will likely make this unworkable.
3640.Sh "\s-1PORTABILITY\s0 \s-1REQUIREMENTS\s0"
3641.IX Subsection "PORTABILITY REQUIREMENTS"
3642In addition to a working ISO-C implementation and of course the
3643backend-specific APIs, libev relies on a few additional extensions:
3644.ie n .IP """void (*)(ev_watcher_type *, int revents)""\fR must have compatible calling conventions regardless of \f(CW""ev_watcher_type *""." 4
3645.el .IP "\f(CWvoid (*)(ev_watcher_type *, int revents)\fR must have compatible calling conventions regardless of \f(CWev_watcher_type *\fR." 4
3646.IX Item "void (*)(ev_watcher_type *, int revents) must have compatible calling conventions regardless of ev_watcher_type *."
3647Libev assumes not only that all watcher pointers have the same internal
3648structure (guaranteed by \s-1POSIX\s0 but not by \s-1ISO\s0 C for example), but it also
3649assumes that the same (machine) code can be used to call any watcher
3650callback: The watcher callbacks have different type signatures, but libev
3651calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally.
3652.ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4
3653.el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4
3654.IX Item "sig_atomic_t volatile must be thread-atomic as well"
3655The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as
3656\&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic with respect to accesses from different
3657threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is
3658believed to be sufficiently portable.
3659.ie n .IP """sigprocmask"" must work in a threaded environment" 4
3660.el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4
3661.IX Item "sigprocmask must work in a threaded environment"
3662Libev uses \f(CW\*(C`sigprocmask\*(C'\fR to temporarily block signals. This is not
3663allowed in a threaded program (\f(CW\*(C`pthread_sigmask\*(C'\fR has to be used). Typical
3664pthread implementations will either allow \f(CW\*(C`sigprocmask\*(C'\fR in the \*(L"main
3665thread\*(R" or will block signals process-wide, both behaviours would
3666be compatible with libev. Interaction between \f(CW\*(C`sigprocmask\*(C'\fR and
3667\&\f(CW\*(C`pthread_sigmask\*(C'\fR could complicate things, however.
3668.Sp
3669The most portable way to handle signals is to block signals in all threads
3670except the initial one, and run the default loop in the initial thread as
3671well.
3672.ie n .IP """long"" must be large enough for common memory allocation sizes" 4
3673.el .IP "\f(CWlong\fR must be large enough for common memory allocation sizes" 4
3674.IX Item "long must be large enough for common memory allocation sizes"
3675To improve portability and simplify its \s-1API\s0, libev uses \f(CW\*(C`long\*(C'\fR internally
3676instead of \f(CW\*(C`size_t\*(C'\fR when allocating its data structures. On non-POSIX
3677systems (Microsoft...) this might be unexpectedly low, but is still at
3678least 31 bits everywhere, which is enough for hundreds of millions of
3306switches. 3679watchers.
3680.ie n .IP """double"" must hold a time value in seconds with enough accuracy" 4
3681.el .IP "\f(CWdouble\fR must hold a time value in seconds with enough accuracy" 4
3682.IX Item "double must hold a time value in seconds with enough accuracy"
3683The type \f(CW\*(C`double\*(C'\fR is used to represent timestamps. It is required to
3684have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3685enough for at least into the year 4000. This requirement is fulfilled by
3686implementations implementing \s-1IEEE\s0 754 (basically all existing ones).
3687.PP
3688If you know of other additional requirements drop me a note.
3307.SH "COMPLEXITIES" 3689.SH "ALGORITHMIC COMPLEXITIES"
3308.IX Header "COMPLEXITIES" 3690.IX Header "ALGORITHMIC COMPLEXITIES"
3309In this section the complexities of (many of) the algorithms used inside 3691In this section the complexities of (many of) the algorithms used inside
3310libev will be explained. For complexity discussions about backends see the 3692libev will be documented. For complexity discussions about backends see
3311documentation for \f(CW\*(C`ev_default_init\*(C'\fR. 3693the documentation for \f(CW\*(C`ev_default_init\*(C'\fR.
3312.PP 3694.PP
3313All of the following are about amortised time: If an array needs to be 3695All of the following are about amortised time: If an array needs to be
3314extended, libev needs to realloc and move the whole array, but this 3696extended, libev needs to realloc and move the whole array, but this
3315happens asymptotically never with higher number of elements, so O(1) might 3697happens asymptotically rarer with higher number of elements, so O(1) might
3316mean it might do a lengthy realloc operation in rare cases, but on average 3698mean that libev does a lengthy realloc operation in rare cases, but on
3317it is much faster and asymptotically approaches constant time. 3699average it is much faster and asymptotically approaches constant time.
3318.IP "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" 4 3700.IP "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" 4
3319.IX Item "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" 3701.IX Item "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)"
3320This means that, when you have a watcher that triggers in one hour and 3702This means that, when you have a watcher that triggers in one hour and
3321there are 100 watchers that would trigger before that then inserting will 3703there are 100 watchers that would trigger before that, then inserting will
3322have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers. 3704have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers.
3323.IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4 3705.IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4
3324.IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 3706.IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)"
3325That means that changing a timer costs less than removing/adding them 3707That means that changing a timer costs less than removing/adding them,
3326as only the relative motion in the event queue has to be paid for. 3708as only the relative motion in the event queue has to be paid for.
3327.IP "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" 4 3709.IP "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" 4
3328.IX Item "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" 3710.IX Item "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)"
3329These just add the watcher into an array or at the head of a list. 3711These just add the watcher into an array or at the head of a list.
3330.IP "Stopping check/prepare/idle/fork/async watchers: O(1)" 4 3712.IP "Stopping check/prepare/idle/fork/async watchers: O(1)" 4
3331.IX Item "Stopping check/prepare/idle/fork/async watchers: O(1)" 3713.IX Item "Stopping check/prepare/idle/fork/async watchers: O(1)"
3332.PD 0 3714.PD 0
3333.IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4 3715.IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4
3334.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" 3716.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))"
3335.PD 3717.PD
3336These watchers are stored in lists then need to be walked to find the 3718These watchers are stored in lists, so they need to be walked to find the
3337correct watcher to remove. The lists are usually short (you don't usually 3719correct watcher to remove. The lists are usually short (you don't usually
3338have many watchers waiting for the same fd or signal). 3720have many watchers waiting for the same fd or signal: one is typical, two
3721is rare).
3339.IP "Finding the next timer in each loop iteration: O(1)" 4 3722.IP "Finding the next timer in each loop iteration: O(1)" 4
3340.IX Item "Finding the next timer in each loop iteration: O(1)" 3723.IX Item "Finding the next timer in each loop iteration: O(1)"
3341By virtue of using a binary or 4\-heap, the next timer is always found at a 3724By virtue of using a binary or 4\-heap, the next timer is always found at a
3342fixed position in the storage array. 3725fixed position in the storage array.
3343.IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 3726.IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4
3352.IX Item "Priority handling: O(number_of_priorities)" 3735.IX Item "Priority handling: O(number_of_priorities)"
3353.PD 3736.PD
3354Priorities are implemented by allocating some space for each 3737Priorities are implemented by allocating some space for each
3355priority. When doing priority-based operations, libev usually has to 3738priority. When doing priority-based operations, libev usually has to
3356linearly search all the priorities, but starting/stopping and activating 3739linearly search all the priorities, but starting/stopping and activating
3357watchers becomes O(1) w.r.t. priority handling. 3740watchers becomes O(1) with respect to priority handling.
3358.IP "Sending an ev_async: O(1)" 4 3741.IP "Sending an ev_async: O(1)" 4
3359.IX Item "Sending an ev_async: O(1)" 3742.IX Item "Sending an ev_async: O(1)"
3360.PD 0 3743.PD 0
3361.IP "Processing ev_async_send: O(number_of_async_watchers)" 4 3744.IP "Processing ev_async_send: O(number_of_async_watchers)" 4
3362.IX Item "Processing ev_async_send: O(number_of_async_watchers)" 3745.IX Item "Processing ev_async_send: O(number_of_async_watchers)"
3364.IX Item "Processing signals: O(max_signal_number)" 3747.IX Item "Processing signals: O(max_signal_number)"
3365.PD 3748.PD
3366Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR 3749Sending involves a system call \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR
3367calls in the current loop iteration. Checking for async and signal events 3750calls in the current loop iteration. Checking for async and signal events
3368involves iterating over all running async watchers or all signal numbers. 3751involves iterating over all running async watchers or all signal numbers.
3369.SH "WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS"
3370.IX Header "WIN32 PLATFORM LIMITATIONS AND WORKAROUNDS"
3371Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev
3372requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0
3373model. Libev still offers limited functionality on this platform in
3374the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket
3375descriptors. This only applies when using Win32 natively, not when using
3376e.g. cygwin.
3377.PP
3378Lifting these limitations would basically require the full
3379re-implementation of the I/O system. If you are into these kinds of
3380things, then note that glib does exactly that for you in a very portable
3381way (note also that glib is the slowest event library known to man).
3382.PP
3383There is no supported compilation method available on windows except
3384embedding it into other applications.
3385.PP
3386Not a libev limitation but worth mentioning: windows apparently doesn't
3387accept large writes: instead of resulting in a partial write, windows will
3388either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large,
3389so make sure you only write small amounts into your sockets (less than a
3390megabyte seems safe, but thsi apparently depends on the amount of memory
3391available).
3392.PP
3393Due to the many, low, and arbitrary limits on the win32 platform and
3394the abysmal performance of winsockets, using a large number of sockets
3395is not recommended (and not reasonable). If your program needs to use
3396more than a hundred or so sockets, then likely it needs to use a totally
3397different implementation for windows, as libev offers the \s-1POSIX\s0 readiness
3398notification model, which cannot be implemented efficiently on windows
3399(Microsoft monopoly games).
3400.PP
3401A typical way to use libev under windows is to embed it (see the embedding
3402section for details) and use the following \fIevwrap.h\fR header file instead
3403of \fIev.h\fR:
3404.PP
3405.Vb 2
3406\& #define EV_STANDALONE /* keeps ev from requiring config.h */
3407\& #define EV_SELECT_IS_WINSOCKET 1 /* configure libev for windows select */
3408\&
3409\& #include "ev.h"
3410.Ve
3411.PP
3412And compile the following \fIevwrap.c\fR file into your project (make sure
3413you do \fInot\fR compile the \fIev.c\fR or any other embedded soruce files!):
3414.PP
3415.Vb 2
3416\& #include "evwrap.h"
3417\& #include "ev.c"
3418.Ve
3419.IP "The winsocket select function" 4
3420.IX Item "The winsocket select function"
3421The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it
3422requires socket \fIhandles\fR and not socket \fIfile descriptors\fR (it is
3423also extremely buggy). This makes select very inefficient, and also
3424requires a mapping from file descriptors to socket handles (the Microsoft
3425C runtime provides the function \f(CW\*(C`_open_osfhandle\*(C'\fR for this). See the
3426discussion of the \f(CW\*(C`EV_SELECT_USE_FD_SET\*(C'\fR, \f(CW\*(C`EV_SELECT_IS_WINSOCKET\*(C'\fR and
3427\&\f(CW\*(C`EV_FD_TO_WIN32_HANDLE\*(C'\fR preprocessor symbols for more info.
3428.Sp
3429The configuration for a \*(L"naked\*(R" win32 using the Microsoft runtime
3430libraries and raw winsocket select is:
3431.Sp
3432.Vb 2
3433\& #define EV_USE_SELECT 1
3434\& #define EV_SELECT_IS_WINSOCKET 1 /* forces EV_SELECT_USE_FD_SET, too */
3435.Ve
3436.Sp
3437Note that winsockets handling of fd sets is O(n), so you can easily get a
3438complexity in the O(nA\*^X) range when using win32.
3439.IP "Limited number of file descriptors" 4
3440.IX Item "Limited number of file descriptors"
3441Windows has numerous arbitrary (and low) limits on things.
3442.Sp
3443Early versions of winsocket's select only supported waiting for a maximum
3444of \f(CW64\fR handles (probably owning to the fact that all windows kernels
3445can only wait for \f(CW64\fR things at the same time internally; Microsoft
3446recommends spawning a chain of threads and wait for 63 handles and the
3447previous thread in each. Great).
3448.Sp
3449Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR
3450to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select
3451call (which might be in libev or elsewhere, for example, perl does its own
3452select emulation on windows).
3453.Sp
3454Another limit is the number of file descriptors in the Microsoft runtime
3455libraries, which by default is \f(CW64\fR (there must be a hidden \fI64\fR fetish
3456or something like this inside Microsoft). You can increase this by calling
3457\&\f(CW\*(C`_setmaxstdio\*(C'\fR, which can increase this limit to \f(CW2048\fR (another
3458arbitrary limit), but is broken in many versions of the Microsoft runtime
3459libraries.
3460.Sp
3461This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on
3462windows version and/or the phase of the moon). To get more, you need to
3463wrap all I/O functions and provide your own fd management, but the cost of
3464calling select (O(nA\*^X)) will likely make this unworkable.
3465.SH "PORTABILITY REQUIREMENTS"
3466.IX Header "PORTABILITY REQUIREMENTS"
3467In addition to a working ISO-C implementation, libev relies on a few
3468additional extensions:
3469.ie n .IP """void (*)(ev_watcher_type *, int revents)""\fR must have compatible calling conventions regardless of \f(CW""ev_watcher_type *""." 4
3470.el .IP "\f(CWvoid (*)(ev_watcher_type *, int revents)\fR must have compatible calling conventions regardless of \f(CWev_watcher_type *\fR." 4
3471.IX Item "void (*)(ev_watcher_type *, int revents) must have compatible calling conventions regardless of ev_watcher_type *."
3472Libev assumes not only that all watcher pointers have the same internal
3473structure (guaranteed by \s-1POSIX\s0 but not by \s-1ISO\s0 C for example), but it also
3474assumes that the same (machine) code can be used to call any watcher
3475callback: The watcher callbacks have different type signatures, but libev
3476calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally.
3477.ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4
3478.el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4
3479.IX Item "sig_atomic_t volatile must be thread-atomic as well"
3480The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as
3481\&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic w.r.t. accesses from different
3482threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is
3483believed to be sufficiently portable.
3484.ie n .IP """sigprocmask"" must work in a threaded environment" 4
3485.el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4
3486.IX Item "sigprocmask must work in a threaded environment"
3487Libev uses \f(CW\*(C`sigprocmask\*(C'\fR to temporarily block signals. This is not
3488allowed in a threaded program (\f(CW\*(C`pthread_sigmask\*(C'\fR has to be used). Typical
3489pthread implementations will either allow \f(CW\*(C`sigprocmask\*(C'\fR in the \*(L"main
3490thread\*(R" or will block signals process-wide, both behaviours would
3491be compatible with libev. Interaction between \f(CW\*(C`sigprocmask\*(C'\fR and
3492\&\f(CW\*(C`pthread_sigmask\*(C'\fR could complicate things, however.
3493.Sp
3494The most portable way to handle signals is to block signals in all threads
3495except the initial one, and run the default loop in the initial thread as
3496well.
3497.ie n .IP """long"" must be large enough for common memory allocation sizes" 4
3498.el .IP "\f(CWlong\fR must be large enough for common memory allocation sizes" 4
3499.IX Item "long must be large enough for common memory allocation sizes"
3500To improve portability and simplify using libev, libev uses \f(CW\*(C`long\*(C'\fR
3501internally instead of \f(CW\*(C`size_t\*(C'\fR when allocating its data structures. On
3502non-POSIX systems (Microsoft...) this might be unexpectedly low, but
3503is still at least 31 bits everywhere, which is enough for hundreds of
3504millions of watchers.
3505.ie n .IP """double"" must hold a time value in seconds with enough accuracy" 4
3506.el .IP "\f(CWdouble\fR must hold a time value in seconds with enough accuracy" 4
3507.IX Item "double must hold a time value in seconds with enough accuracy"
3508The type \f(CW\*(C`double\*(C'\fR is used to represent timestamps. It is required to
3509have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3510enough for at least into the year 4000. This requirement is fulfilled by
3511implementations implementing \s-1IEEE\s0 754 (basically all existing ones).
3512.PP
3513If you know of other additional requirements drop me a note.
3514.SH "COMPILER WARNINGS"
3515.IX Header "COMPILER WARNINGS"
3516Depending on your compiler and compiler settings, you might get no or a
3517lot of warnings when compiling libev code. Some people are apparently
3518scared by this.
3519.PP
3520However, these are unavoidable for many reasons. For one, each compiler
3521has different warnings, and each user has different tastes regarding
3522warning options. \*(L"Warn-free\*(R" code therefore cannot be a goal except when
3523targeting a specific compiler and compiler-version.
3524.PP
3525Another reason is that some compiler warnings require elaborate
3526workarounds, or other changes to the code that make it less clear and less
3527maintainable.
3528.PP
3529And of course, some compiler warnings are just plain stupid, or simply
3530wrong (because they don't actually warn about the condition their message
3531seems to warn about).
3532.PP
3533While libev is written to generate as few warnings as possible,
3534\&\*(L"warn-free\*(R" code is not a goal, and it is recommended not to build libev
3535with any compiler warnings enabled unless you are prepared to cope with
3536them (e.g. by ignoring them). Remember that warnings are just that:
3537warnings, not errors, or proof of bugs.
3538.SH "VALGRIND"
3539.IX Header "VALGRIND"
3540Valgrind has a special section here because it is a popular tool that is
3541highly useful, but valgrind reports are very hard to interpret.
3542.PP
3543If you think you found a bug (memory leak, uninitialised data access etc.)
3544in libev, then check twice: If valgrind reports something like:
3545.PP
3546.Vb 3
3547\& ==2274== definitely lost: 0 bytes in 0 blocks.
3548\& ==2274== possibly lost: 0 bytes in 0 blocks.
3549\& ==2274== still reachable: 256 bytes in 1 blocks.
3550.Ve
3551.PP
3552Then there is no memory leak. Similarly, under some circumstances,
3553valgrind might report kernel bugs as if it were a bug in libev, or it
3554might be confused (it is a very good tool, but only a tool).
3555.PP
3556If you are unsure about something, feel free to contact the mailing list
3557with the full valgrind report and an explanation on why you think this is
3558a bug in libev. However, don't be annoyed when you get a brisk \*(L"this is
3559no bug\*(R" answer and take the chance of learning how to interpret valgrind
3560properly.
3561.PP
3562If you need, for some reason, empty reports from valgrind for your project
3563I suggest using suppression lists.
3564.SH "AUTHOR" 3752.SH "AUTHOR"
3565.IX Header "AUTHOR" 3753.IX Header "AUTHOR"
3566Marc Lehmann <libev@schmorp.de>. 3754Marc Lehmann <libev@schmorp.de>.
3567.SH "POD ERRORS"
3568.IX Header "POD ERRORS"
3569Hey! \fBThe above document had some coding errors, which are explained below:\fR
3570.IP "Around line 3116:" 4
3571.IX Item "Around line 3116:"
3572You forgot a '=back' before '=head2'

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines