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

Comparing libev/ev.3 (file contents):
Revision 1.70 by root, Sun Jul 6 03:36:51 2008 UTC vs.
Revision 1.71 by root, Mon Sep 29 03:31:14 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-07-05" "libev-3.43" "libev - high performance full featured event loop" 135.TH LIBEV 3 "2008-09-29" "libev-3.44" "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
781.IP "ev_unref (loop)" 4 821.IP "ev_unref (loop)" 4
782.IX Item "ev_unref (loop)" 822.IX Item "ev_unref (loop)"
783.PD 823.PD
784Ref/unref can be used to add or remove a reference count on the event 824Ref/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 825loop: 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 826count is nonzero, \f(CW\*(C`ev_loop\*(C'\fR will not return on its own.
827.Sp
787a watcher you never unregister that should not keep \f(CW\*(C`ev_loop\*(C'\fR from 828If 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 829from returning, call \fIev_unref()\fR after starting, and \fIev_ref()\fR before
830stopping it.
831.Sp
789example, libev itself uses this for its internal signal pipe: It is not 832As 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 833not 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 834if 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 835way 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 836libraries. 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, 837(but only if the watcher wasn't active before, or was active before,
795respectively). 838respectively).
796.Sp 839.Sp
815.PD 0 858.PD 0
816.IP "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 4 859.IP "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 4
817.IX Item "ev_set_timeout_collect_interval (loop, ev_tstamp interval)" 860.IX Item "ev_set_timeout_collect_interval (loop, ev_tstamp interval)"
818.PD 861.PD
819These advanced functions influence the time that libev will spend waiting 862These 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 863for events. Both time intervals are by default \f(CW0\fR, meaning that libev
821invoke timer/periodic callbacks and I/O callbacks with minimum latency. 864will try to invoke timer/periodic callbacks and I/O callbacks with minimum
865latency.
822.Sp 866.Sp
823Setting these to a higher value (the \f(CW\*(C`interval\*(C'\fR \fImust\fR be >= \f(CW0\fR) 867Setting 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 868allows libev to delay invocation of I/O and timer/periodic callbacks
825increase efficiency of loop iterations. 869to increase efficiency of loop iterations (or to increase power-saving
870opportunities).
826.Sp 871.Sp
827The background is that sometimes your program runs just fast enough to 872The 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 873one (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 874program 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 875events, 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. 876overhead for the actual polling but can deliver many events at once.
832.Sp 877.Sp
833By setting a higher \fIio collect interval\fR you allow libev to spend more 878By 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, 879time 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 881\&\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. 882introduce an additional \f(CW\*(C`ev_sleep ()\*(C'\fR call into most loop iterations.
838.Sp 883.Sp
839Likewise, by setting a higher \fItimeout collect interval\fR you allow libev 884Likewise, by setting a higher \fItimeout collect interval\fR you allow libev
840to spend more time collecting timeouts, at the expense of increased 885to 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 886latency/jitter/inexactness (the watcher callback will be called
842will not be affected. Setting this to a non-null value will not introduce 887later). \f(CW\*(C`ev_io\*(C'\fR watchers will not be affected. Setting this to a non-null
843any overhead in libev. 888value will not introduce any overhead in libev.
844.Sp 889.Sp
845Many (busy) programs can usually benefit by setting the I/O collect 890Many (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 891interval 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 892interactive 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, 893usually 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. 894as this approaches the timing granularity of most systems.
895.Sp
896Setting the \fItimeout collect interval\fR can improve the opportunity for
897saving power, as the program will \*(L"bundle\*(R" timer callback invocations that
898are \*(L"near\*(R" in time together, by delaying some, thus reducing the number of
899times the process sleeps and wakes up again. Another useful technique to
900reduce iterations/wake\-ups is to use \f(CW\*(C`ev_periodic\*(C'\fR watchers and make sure
901they fire on, say, one-second boundaries only.
850.IP "ev_loop_verify (loop)" 4 902.IP "ev_loop_verify (loop)" 4
851.IX Item "ev_loop_verify (loop)" 903.IX Item "ev_loop_verify (loop)"
852This function only does something when \f(CW\*(C`EV_VERIFY\*(C'\fR support has been 904This 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 905compiled 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 906through all internal structures and checks them for validity. If anything
855an error message to standard error and call \f(CW\*(C`abort ()\*(C'\fR. 907is found to be inconsistent, it will print an error message to standard
908error and call \f(CW\*(C`abort ()\*(C'\fR.
856.Sp 909.Sp
857This can be used to catch bugs inside libev itself: under normal 910This can be used to catch bugs inside libev itself: under normal
858circumstances, this function will never abort as of course libev keeps its 911circumstances, this function will never abort as of course libev keeps its
859data structures consistent. 912data structures consistent.
860.SH "ANATOMY OF A WATCHER" 913.SH "ANATOMY OF A WATCHER"
978happen because the watcher could not be properly started because libev 1031happen 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 1032ran 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 1033problem. You best act on it by reporting the problem and somehow coping
981with the watcher being stopped. 1034with the watcher being stopped.
982.Sp 1035.Sp
983Libev will usually signal a few \*(L"dummy\*(R" events together with an error, 1036Libev 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 1037example 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 1038callbacks 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 1039the error from \fIread()\fR or \fIwrite()\fR. This will not work in multi-threaded
987programs, though, so beware. 1040programs, though, as the fd could already be closed and reused for another
1041thing, so beware.
988.Sh "\s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0" 1042.Sh "\s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0"
989.IX Subsection "GENERIC WATCHER FUNCTIONS" 1043.IX Subsection "GENERIC WATCHER FUNCTIONS"
990In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type, 1044In 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. 1045e.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 1046.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 1056You 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. 1057(or never started) and there are no pending events outstanding.
1004.Sp 1058.Sp
1005The callback is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher, 1059The callback is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher,
1006int revents)\*(C'\fR. 1060int revents)\*(C'\fR.
1061.Sp
1062Example: Initialise an \f(CW\*(C`ev_io\*(C'\fR watcher in two steps.
1063.Sp
1064.Vb 3
1065\& ev_io w;
1066\& ev_init (&w, my_cb);
1067\& ev_io_set (&w, STDIN_FILENO, EV_READ);
1068.Ve
1007.ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4 1069.ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4
1008.el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4 1070.el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4
1009.IX Item "ev_TYPE_set (ev_TYPE *, [args])" 1071.IX Item "ev_TYPE_set (ev_TYPE *, [args])"
1010This macro initialises the type-specific parts of a watcher. You need to 1072This 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 1073call \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 1075macro 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). 1076difference to the \f(CW\*(C`ev_init\*(C'\fR macro).
1015.Sp 1077.Sp
1016Although some watcher types do not have type-specific arguments 1078Although 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. 1079(e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro.
1080.Sp
1081See \f(CW\*(C`ev_init\*(C'\fR, above, for an example.
1018.ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4 1082.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 1083.el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4
1020.IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])" 1084.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 1085This 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 1086calls into a single call. This is the most convenient method to initialise
1023a watcher. The same limitations apply, of course. 1087a watcher. The same limitations apply, of course.
1088.Sp
1089Example: Initialise and set an \f(CW\*(C`ev_io\*(C'\fR watcher in one step.
1090.Sp
1091.Vb 1
1092\& ev_io_init (&w, my_cb, STDIN_FILENO, EV_READ);
1093.Ve
1024.ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4 1094.ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4
1025.el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4 1095.el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4
1026.IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)" 1096.IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)"
1027Starts (activates) the given watcher. Only active watchers will receive 1097Starts (activates) the given watcher. Only active watchers will receive
1028events. If the watcher is already active nothing will happen. 1098events. If the watcher is already active nothing will happen.
1099.Sp
1100Example: Start the \f(CW\*(C`ev_io\*(C'\fR watcher that is being abused as example in this
1101whole section.
1102.Sp
1103.Vb 1
1104\& ev_io_start (EV_DEFAULT_UC, &w);
1105.Ve
1029.ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4 1106.ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4
1030.el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4 1107.el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4
1031.IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)" 1108.IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)"
1032Stops the given watcher again (if active) and clears the pending 1109Stops the given watcher again (if active) and clears the pending
1033status. It is possible that stopped watchers are pending (for example, 1110status. It is possible that stopped watchers are pending (for example,
1086or might not have been adjusted to be within valid range. 1163or might not have been adjusted to be within valid range.
1087.IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4 1164.IP "ev_invoke (loop, ev_TYPE *watcher, int revents)" 4
1088.IX Item "ev_invoke (loop, ev_TYPE *watcher, int revents)" 1165.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 1166Invoke 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 1167\&\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. 1168can deal with that fact, as both are simply passed through to the
1169callback.
1092.IP "int ev_clear_pending (loop, ev_TYPE *watcher)" 4 1170.IP "int ev_clear_pending (loop, ev_TYPE *watcher)" 4
1093.IX Item "int ev_clear_pending (loop, ev_TYPE *watcher)" 1171.IX Item "int ev_clear_pending (loop, ev_TYPE *watcher)"
1094If the watcher is pending, this function returns clears its pending status 1172If 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 1173returns 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. 1174watcher isn't pending it does nothing and returns \f(CW0\fR.
1175.Sp
1176Sometimes it can be useful to \*(L"poll\*(R" a watcher instead of waiting for its
1177callback 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" 1178.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" 1179.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 1180Each 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 1181and 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 1182to 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 1183don'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 1184member, you can also \*(L"subclass\*(R" the watcher type and provide your own
1104data: 1185data:
1105.PP 1186.PP
1108\& { 1189\& {
1109\& struct ev_io io; 1190\& struct ev_io io;
1110\& int otherfd; 1191\& int otherfd;
1111\& void *somedata; 1192\& void *somedata;
1112\& struct whatever *mostinteresting; 1193\& struct whatever *mostinteresting;
1113\& } 1194\& };
1195\&
1196\& ...
1197\& struct my_io w;
1198\& ev_io_init (&w.io, my_cb, fd, EV_READ);
1114.Ve 1199.Ve
1115.PP 1200.PP
1116And since your callback will be called with a pointer to the watcher, you 1201And since your callback will be called with a pointer to the watcher, you
1117can cast it back to your own type: 1202can cast it back to your own type:
1118.PP 1203.PP
1125.Ve 1210.Ve
1126.PP 1211.PP
1127More interesting and less C\-conformant ways of casting your callback type 1212More interesting and less C\-conformant ways of casting your callback type
1128instead have been omitted. 1213instead have been omitted.
1129.PP 1214.PP
1130Another common scenario is having some data structure with multiple 1215Another common scenario is to use some data structure with multiple
1131watchers: 1216embedded watchers:
1132.PP 1217.PP
1133.Vb 6 1218.Vb 6
1134\& struct my_biggy 1219\& struct my_biggy
1135\& { 1220\& {
1136\& int some_data; 1221\& int some_data;
1137\& ev_timer t1; 1222\& ev_timer t1;
1138\& ev_timer t2; 1223\& ev_timer t2;
1139\& } 1224\& }
1140.Ve 1225.Ve
1141.PP 1226.PP
1142In this case getting the pointer to \f(CW\*(C`my_biggy\*(C'\fR is a bit more complicated, 1227In 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: 1228complicated: Either you store the address of your \f(CW\*(C`my_biggy\*(C'\fR struct
1229in the \f(CW\*(C`data\*(C'\fR member of the watcher (for woozies), or you need to use
1230some pointer arithmetic using \f(CW\*(C`offsetof\*(C'\fR inside your watchers (for real
1231programmers):
1144.PP 1232.PP
1145.Vb 1 1233.Vb 1
1146\& #include <stddef.h> 1234\& #include <stddef.h>
1147\& 1235\&
1148\& static void 1236\& static void
1187In general you can register as many read and/or write event watchers per 1275In 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 1276fd 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 1277descriptors to non-blocking mode is also usually a good idea (but not
1190required if you know what you are doing). 1278required if you know what you are doing).
1191.PP 1279.PP
1192If you must do this, then force the use of a known-to-be-good backend 1280If 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 1281known-to-be-good backend (at the time of this writing, this includes only
1194\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR). 1282\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR and \f(CW\*(C`EVBACKEND_POLL\*(C'\fR).
1195.PP 1283.PP
1196Another thing you have to watch out for is that it is quite easy to 1284Another 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 1285receive \*(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 1286be 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 1287because 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 1288lot of those (for example Solaris ports), it is very easy to get into
1201this situation even with a relatively standard program structure. Thus 1289this 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 1290it 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. 1291\&\f(CW\*(C`EAGAIN\*(C'\fR is far preferable to a program hanging until some data arrives.
1204.PP 1292.PP
1205If you cannot run the fd in non-blocking mode (for example you should not 1293If 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 1294not 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 1295re-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 1296interface such as poll (fortunately in our Xlib example, Xlib already
1209its own, so its quite safe to use). 1297does this on its own, so its quite safe to use). Some people additionally
1298use \f(CW\*(C`SIGALRM\*(C'\fR and an interval timer, just to be sure you won't block
1299indefinitely.
1300.PP
1301But really, best use non-blocking mode.
1210.PP 1302.PP
1211\fIThe special problem of disappearing file descriptors\fR 1303\fIThe special problem of disappearing file descriptors\fR
1212.IX Subsection "The special problem of disappearing file descriptors" 1304.IX Subsection "The special problem of disappearing file descriptors"
1213.PP 1305.PP
1214Some backends (e.g. kqueue, epoll) need to be told about closing a file 1306Some 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, 1307descriptor (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 1308such 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 1309descriptor, but when it goes away, the operating system will silently drop
1218this interest. If another file descriptor with the same number then is 1310this 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 1311registered with libev, there is no efficient way to see that this is, in
1220fact, a different file descriptor. 1312fact, a different file descriptor.
1221.PP 1313.PP
1255\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. 1347\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
1256.PP 1348.PP
1257\fIThe special problem of \s-1SIGPIPE\s0\fR 1349\fIThe special problem of \s-1SIGPIPE\s0\fR
1258.IX Subsection "The special problem of SIGPIPE" 1350.IX Subsection "The special problem of SIGPIPE"
1259.PP 1351.PP
1260While not really specific to libev, it is easy to forget about \s-1SIGPIPE:\s0 1352While 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 1353when 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 1354sent a \s-1SIGPIPE\s0, which, by default, aborts your program. For most programs
1263programs this is sensible behaviour, for daemons, this is usually 1355this is sensible behaviour, for daemons, this is usually undesirable.
1264undesirable.
1265.PP 1356.PP
1266So when you encounter spurious, unexplained daemon exits, make sure you 1357So 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 1358ignore \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). 1359somewhere, as that would have given you a big clue).
1269.PP 1360.PP
1274.PD 0 1365.PD 0
1275.IP "ev_io_set (ev_io *, int fd, int events)" 4 1366.IP "ev_io_set (ev_io *, int fd, int events)" 4
1276.IX Item "ev_io_set (ev_io *, int fd, int events)" 1367.IX Item "ev_io_set (ev_io *, int fd, int events)"
1277.PD 1368.PD
1278Configures an \f(CW\*(C`ev_io\*(C'\fR watcher. The \f(CW\*(C`fd\*(C'\fR is the file descriptor to 1369Configures 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 1370receive 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. 1371\&\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 1372.IP "int fd [read\-only]" 4
1282.IX Item "int fd [read-only]" 1373.IX Item "int fd [read-only]"
1283The file descriptor being watched. 1374The file descriptor being watched.
1284.IP "int events [read\-only]" 4 1375.IP "int events [read\-only]" 4
1285.IX Item "int events [read-only]" 1376.IX Item "int events [read-only]"
1295.Vb 6 1386.Vb 6
1296\& static void 1387\& static void
1297\& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents) 1388\& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents)
1298\& { 1389\& {
1299\& ev_io_stop (loop, w); 1390\& ev_io_stop (loop, w);
1300\& .. read from stdin here (or from w\->fd) and haqndle any I/O errors 1391\& .. read from stdin here (or from w\->fd) and handle any I/O errors
1301\& } 1392\& }
1302\& 1393\&
1303\& ... 1394\& ...
1304\& struct ev_loop *loop = ev_default_init (0); 1395\& struct ev_loop *loop = ev_default_init (0);
1305\& struct ev_io stdin_readable; 1396\& struct ev_io stdin_readable;
1313Timer watchers are simple relative timers that generate an event after a 1404Timer watchers are simple relative timers that generate an event after a
1314given time, and optionally repeating in regular intervals after that. 1405given time, and optionally repeating in regular intervals after that.
1315.PP 1406.PP
1316The timers are based on real time, that is, if you register an event that 1407The 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 1408times 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 1409year, it will still time out after (roughly) one hour. \*(L"Roughly\*(R" because
1319detecting time jumps is hard, and some inaccuracies are unavoidable (the 1410detecting time jumps is hard, and some inaccuracies are unavoidable (the
1320monotonic clock option helps a lot here). 1411monotonic clock option helps a lot here).
1412.PP
1413The callback is guaranteed to be invoked only \fIafter\fR its timeout has
1414passed, but if multiple timers become ready during the same loop iteration
1415then order of execution is undefined.
1416.PP
1417\fIThe special problem of time updates\fR
1418.IX Subsection "The special problem of time updates"
1419.PP
1420Establishing the current time is a costly operation (it usually takes at
1421least two system calls): \s-1EV\s0 therefore updates its idea of the current
1422time only before and after \f(CW\*(C`ev_loop\*(C'\fR collects new events, which causes a
1423growing difference between \f(CW\*(C`ev_now ()\*(C'\fR and \f(CW\*(C`ev_time ()\*(C'\fR when handling
1424lots of events in one iteration.
1321.PP 1425.PP
1322The relative timeouts are calculated relative to the \f(CW\*(C`ev_now ()\*(C'\fR 1426The 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 1427time. This is usually the right thing as this timestamp refers to the time
1324of the event triggering whatever timeout you are modifying/starting. If 1428of 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 1429you 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: 1430timeout on the current time, use something like this to adjust for this:
1327.PP 1431.PP
1328.Vb 1 1432.Vb 1
1329\& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.); 1433\& ev_timer_set (&timer, after + ev_now () \- ev_time (), 0.);
1330.Ve 1434.Ve
1331.PP 1435.PP
1332The callback is guaranteed to be invoked only after its timeout has passed, 1436If 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 1437update 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. 1438()\*(C'\fR.
1335.PP 1439.PP
1336\fIWatcher-Specific Functions and Data Members\fR 1440\fIWatcher-Specific Functions and Data Members\fR
1337.IX Subsection "Watcher-Specific Functions and Data Members" 1441.IX Subsection "Watcher-Specific Functions and Data Members"
1338.IP "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)" 4 1442.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)" 1443.IX Item "ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat)"
1388\& ev_timer_again (loop, timer); 1492\& ev_timer_again (loop, timer);
1389.Ve 1493.Ve
1390.Sp 1494.Sp
1391This is more slightly efficient then stopping/starting the timer each time 1495This is more slightly efficient then stopping/starting the timer each time
1392you want to modify its timeout value. 1496you want to modify its timeout value.
1497.Sp
1498Note, however, that it is often even more efficient to remember the
1499time of the last activity and let the timer time-out naturally. In the
1500callback, you then check whether the time-out is real, or, if there was
1501some activity, you reschedule the watcher to time-out in \*(L"last_activity +
1502timeout \- ev_now ()\*(R" seconds.
1393.IP "ev_tstamp repeat [read\-write]" 4 1503.IP "ev_tstamp repeat [read\-write]" 4
1394.IX Item "ev_tstamp repeat [read-write]" 1504.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 1505The 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), 1506or \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. 1507which is also when any modifications are taken into account.
1398.PP 1508.PP
1399\fIExamples\fR 1509\fIExamples\fR
1400.IX Subsection "Examples" 1510.IX Subsection "Examples"
1401.PP 1511.PP
1447to trigger the event (unlike an \f(CW\*(C`ev_timer\*(C'\fR, which would still trigger 1557to 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). 1558roughly 10 seconds later as it uses a relative timeout).
1449.PP 1559.PP
1450\&\f(CW\*(C`ev_periodic\*(C'\fRs can also be used to implement vastly more complex timers, 1560\&\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 1561such as triggering an event on each \*(L"midnight, local time\*(R", or other
1452complicated, rules. 1562complicated rules.
1453.PP 1563.PP
1454As with timers, the callback is guaranteed to be invoked only when the 1564As 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 1565time (\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. 1566during the same loop iteration, then order of execution is undefined.
1457.PP 1567.PP
1458\fIWatcher-Specific Functions and Data Members\fR 1568\fIWatcher-Specific Functions and Data Members\fR
1459.IX Subsection "Watcher-Specific Functions and Data Members" 1569.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 1570.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)" 1571.IX Item "ev_periodic_init (ev_periodic *, callback, ev_tstamp at, ev_tstamp interval, reschedule_cb)"
1462.PD 0 1572.PD 0
1463.IP "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)" 4 1573.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)" 1574.IX Item "ev_periodic_set (ev_periodic *, ev_tstamp after, ev_tstamp repeat, reschedule_cb)"
1465.PD 1575.PD
1466Lots of arguments, lets sort it out... There are basically three modes of 1576Lots of arguments, lets sort it out... There are basically three modes of
1467operation, and we will explain them from simplest to complex: 1577operation, and we will explain them from simplest to most complex:
1468.RS 4 1578.RS 4
1469.IP "\(bu" 4 1579.IP "\(bu" 4
1470absolute timer (at = time, interval = reschedule_cb = 0) 1580absolute timer (at = time, interval = reschedule_cb = 0)
1471.Sp 1581.Sp
1472In this configuration the watcher triggers an event after the wall clock 1582In 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 1583time \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 1584jump 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. 1585only run when the system clock reaches or surpasses this time.
1476.IP "\(bu" 4 1586.IP "\(bu" 4
1477repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) 1587repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)
1478.Sp 1588.Sp
1479In this mode the watcher will always be scheduled to time out at the next 1589In 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) 1590\&\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. 1591and then repeat, regardless of any time jumps.
1482.Sp 1592.Sp
1483This can be used to create timers that do not drift with respect to system 1593This 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 1594system clock, for example, here is a \f(CW\*(C`ev_periodic\*(C'\fR that triggers each
1485the hour: 1595hour, on the hour:
1486.Sp 1596.Sp
1487.Vb 1 1597.Vb 1
1488\& ev_periodic_set (&periodic, 0., 3600., 0); 1598\& ev_periodic_set (&periodic, 0., 3600., 0);
1489.Ve 1599.Ve
1490.Sp 1600.Sp
1576.PP 1686.PP
1577\fIExamples\fR 1687\fIExamples\fR
1578.IX Subsection "Examples" 1688.IX Subsection "Examples"
1579.PP 1689.PP
1580Example: Call a callback every hour, or, more precisely, whenever the 1690Example: Call a callback every hour, or, more precisely, whenever the
1581system clock is divisible by 3600. The callback invocation times have 1691system time is divisible by 3600. The callback invocation times have
1582potentially a lot of jitter, but good long-term stability. 1692potentially a lot of jitter, but good long-term stability.
1583.PP 1693.PP
1584.Vb 5 1694.Vb 5
1585\& static void 1695\& static void
1586\& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents) 1696\& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents)
1599\& #include <math.h> 1709\& #include <math.h>
1600\& 1710\&
1601\& static ev_tstamp 1711\& static ev_tstamp
1602\& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now) 1712\& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now)
1603\& { 1713\& {
1604\& return fmod (now, 3600.) + 3600.; 1714\& return now + (3600. \- fmod (now, 3600.));
1605\& } 1715\& }
1606\& 1716\&
1607\& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb); 1717\& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb);
1608.Ve 1718.Ve
1609.PP 1719.PP
1621Signal watchers will trigger an event when the process receives a specific 1731Signal watchers will trigger an event when the process receives a specific
1622signal one or more times. Even though signals are very asynchronous, libev 1732signal 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 1733will try it's best to deliver signals synchronously, i.e. as part of the
1624normal event processing, like any other event. 1734normal event processing, like any other event.
1625.PP 1735.PP
1736If you want signals asynchronously, just use \f(CW\*(C`sigaction\*(C'\fR as you would
1737do without libev and forget about sharing the signal. You can even use
1738\&\f(CW\*(C`ev_async\*(C'\fR from a signal handler to synchronously wake up an event loop.
1739.PP
1626You can configure as many watchers as you like per signal. Only when the 1740You can configure as many watchers as you like per signal. Only when the
1627first watcher gets started will libev actually register a signal watcher 1741first watcher gets started will libev actually register a signal handler
1628with the kernel (thus it coexists with your own signal handlers as long 1742with 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 1743you 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 1744the 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). 1745signal handler to \s-1SIG_DFL\s0 (regardless of what it was set to before).
1632.PP 1746.PP
1633If possible and supported, libev will install its handlers with 1747If 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 1748\&\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 1749interrupted. 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 1750signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock
1668.Ve 1782.Ve
1669.ie n .Sh """ev_child"" \- watch out for process status changes" 1783.ie n .Sh """ev_child"" \- watch out for process status changes"
1670.el .Sh "\f(CWev_child\fP \- watch out for process status changes" 1784.el .Sh "\f(CWev_child\fP \- watch out for process status changes"
1671.IX Subsection "ev_child - watch out for process status changes" 1785.IX Subsection "ev_child - watch out for process status changes"
1672Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to 1786Child 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 1787some 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 1788exits). 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 1789has been forked (which implies it might have already exited), as long
1676loop isn't entered (or is continued from a watcher). 1790as the event loop isn't entered (or is continued from a watcher), i.e.,
1791forking and then immediately registering a watcher for the child is fine,
1792but forking and registering a watcher a few event loop iterations later is
1793not.
1677.PP 1794.PP
1678Only the default event loop is capable of handling signals, and therefore 1795Only the default event loop is capable of handling signals, and therefore
1679you can only register child watchers in the default event loop. 1796you can only register child watchers in the default event loop.
1680.PP 1797.PP
1681\fIProcess Interaction\fR 1798\fIProcess Interaction\fR
1696handler, you can override it easily by installing your own handler for 1813handler, 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 1814\&\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 1815default loop never gets destroyed. You are encouraged, however, to use an
1699event-based approach to child reaping and thus use libev's support for 1816event-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. 1817that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely.
1818.PP
1819\fIStopping the Child Watcher\fR
1820.IX Subsection "Stopping the Child Watcher"
1821.PP
1822Currently, the child watcher never gets stopped, even when the
1823child terminates, so normally one needs to stop the watcher in the
1824callback. Future versions of libev might stop the watcher automatically
1825when a child exit is detected.
1701.PP 1826.PP
1702\fIWatcher-Specific Functions and Data Members\fR 1827\fIWatcher-Specific Functions and Data Members\fR
1703.IX Subsection "Watcher-Specific Functions and Data Members" 1828.IX Subsection "Watcher-Specific Functions and Data Members"
1704.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 1829.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)" 1830.IX Item "ev_child_init (ev_child *, callback, int pid, int trace)"
1771the stat buffer having unspecified contents. 1896the stat buffer having unspecified contents.
1772.PP 1897.PP
1773The path \fIshould\fR be absolute and \fImust not\fR end in a slash. If it is 1898The 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. 1899relative and your working directory changes, the behaviour is undefined.
1775.PP 1900.PP
1776Since there is no standard to do this, the portable implementation simply 1901Since 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 1902implementation 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 1903it changed somehow. You can specify a recommended polling interval for
1779a polling interval of \f(CW0\fR (highly recommended!) then a \fIsuitable, 1904this 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 1905then a \fIsuitable, unspecified default\fR value will be used (which
1781five seconds, although this might change dynamically). Libev will also 1906you 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 1907dynamically). Libev will also impose a minimum interval which is currently
1783usually overkill. 1908around \f(CW0.1\fR, but thats usually overkill.
1784.PP 1909.PP
1785This watcher type is not meant for massive numbers of stat watchers, 1910This watcher type is not meant for massive numbers of stat watchers,
1786as even with OS-supported change notifications, this can be 1911as even with OS-supported change notifications, this can be
1787resource-intensive. 1912resource-intensive.
1788.PP 1913.PP
1789At the time of this writing, only the Linux inotify interface is 1914At the time of this writing, the only OS-specific interface implemented
1790implemented (implementing kqueue support is left as an exercise for the 1915is the Linux inotify interface (implementing kqueue support is left as
1791reader, note, however, that the author sees no way of implementing ev_stat 1916an 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 1917of 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 1918.PP
1798\fI\s-1ABI\s0 Issues (Largefile Support)\fR 1919\fI\s-1ABI\s0 Issues (Largefile Support)\fR
1799.IX Subsection "ABI Issues (Largefile Support)" 1920.IX Subsection "ABI Issues (Largefile Support)"
1800.PP 1921.PP
1801Libev by default (unless the user overrides this) uses the default 1922Libev by default (unless the user overrides this) uses the default
1811file interfaces available by default (as e.g. FreeBSD does) and not 1932file interfaces available by default (as e.g. FreeBSD does) and not
1812optional. Libev cannot simply switch on large file support because it has 1933optional. Libev cannot simply switch on large file support because it has
1813to exchange stat structures with application programs compiled using the 1934to exchange stat structures with application programs compiled using the
1814default compilation environment. 1935default compilation environment.
1815.PP 1936.PP
1816\fIInotify\fR 1937\fIInotify and Kqueue\fR
1817.IX Subsection "Inotify" 1938.IX Subsection "Inotify and Kqueue"
1818.PP 1939.PP
1819When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only 1940When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only
1820available on Linux) and present at runtime, it will be used to speed up 1941available with Linux) and present at runtime, it will be used to speed up
1821change detection where possible. The inotify descriptor will be created lazily 1942change detection where possible. The inotify descriptor will be created lazily
1822when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. 1943when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started.
1823.PP 1944.PP
1824Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers 1945Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers
1825except that changes might be detected earlier, and in some cases, to avoid 1946except that changes might be detected earlier, and in some cases, to avoid
1826making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support 1947making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support
1827there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. 1948there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling,
1949but as long as the path exists, libev usually gets away without polling.
1828.PP 1950.PP
1829(There is no support for kqueue, as apparently it cannot be used to 1951There is no support for kqueue, as apparently it cannot be used to
1830implement this functionality, due to the requirement of having a file 1952implement this functionality, due to the requirement of having a file
1831descriptor open on the object at all times). 1953descriptor open on the object at all times, and detecting renames, unlinks
1954etc. is difficult.
1832.PP 1955.PP
1833\fIThe special problem of stat time resolution\fR 1956\fIThe special problem of stat time resolution\fR
1834.IX Subsection "The special problem of stat time resolution" 1957.IX Subsection "The special problem of stat time resolution"
1835.PP 1958.PP
1836The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, and 1959The \f(CW\*(C`stat ()\*(C'\fR system call only supports full-second resolution portably, and
1837even on systems where the resolution is higher, many file systems still 1960even on systems where the resolution is higher, most file systems still
1838only support whole seconds. 1961only support whole seconds.
1839.PP 1962.PP
1840That means that, if the time is the only thing that changes, you can 1963That means that, if the time is the only thing that changes, you can
1841easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and 1964easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and
1842calls your callback, which does something. When there is another update 1965calls your callback, which does something. When there is another update
1843within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat 1966within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect unless the
1844data does not change. 1967stat data does change in other ways (e.g. file size).
1845.PP 1968.PP
1846The solution to this is to delay acting on a change for slightly more 1969The solution to this is to delay acting on a change for slightly more
1847than a second (or till slightly after the next full second boundary), using 1970than a second (or till slightly after the next full second boundary), using
1848a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); 1971a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02);
1849ev_timer_again (loop, w)\*(C'\fR). 1972ev_timer_again (loop, w)\*(C'\fR).
1869\&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to 1992\&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to
1870be detected and should normally be specified as \f(CW0\fR to let libev choose 1993be detected and should normally be specified as \f(CW0\fR to let libev choose
1871a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same 1994a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same
1872path for as long as the watcher is active. 1995path for as long as the watcher is active.
1873.Sp 1996.Sp
1874The callback will receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, relative 1997The callback will receive an \f(CW\*(C`EV_STAT\*(C'\fR event when a change was detected,
1875to the attributes at the time the watcher was started (or the last change 1998relative to the attributes at the time the watcher was started (or the
1876was detected). 1999last change was detected).
1877.IP "ev_stat_stat (loop, ev_stat *)" 4 2000.IP "ev_stat_stat (loop, ev_stat *)" 4
1878.IX Item "ev_stat_stat (loop, ev_stat *)" 2001.IX Item "ev_stat_stat (loop, ev_stat *)"
1879Updates the stat buffer immediately with new values. If you change the 2002Updates the stat buffer immediately with new values. If you change the
1880watched path in your callback, you could call this function to avoid 2003watched path in your callback, you could call this function to avoid
1881detecting this change (while introducing a race condition if you are not 2004detecting this change (while introducing a race condition if you are not
1961.Ve 2084.Ve
1962.ie n .Sh """ev_idle"" \- when you've got nothing better to do..." 2085.ie n .Sh """ev_idle"" \- when you've got nothing better to do..."
1963.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." 2086.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..."
1964.IX Subsection "ev_idle - when you've got nothing better to do..." 2087.IX Subsection "ev_idle - when you've got nothing better to do..."
1965Idle watchers trigger events when no other events of the same or higher 2088Idle watchers trigger events when no other events of the same or higher
1966priority are pending (prepare, check and other idle watchers do not 2089priority are pending (prepare, check and other idle watchers do not count
1967count). 2090as receiving \*(L"events\*(R").
1968.PP 2091.PP
1969That is, as long as your process is busy handling sockets or timeouts 2092That is, as long as your process is busy handling sockets or timeouts
1970(or even signals, imagine) of the same or higher priority it will not be 2093(or even signals, imagine) of the same or higher priority it will not be
1971triggered. But when your process is idle (or only lower-priority watchers 2094triggered. But when your process is idle (or only lower-priority watchers
1972are pending), the idle watchers are being called once per event loop 2095are pending), the idle watchers are being called once per event loop
2009\& ev_idle_start (loop, idle_cb); 2132\& ev_idle_start (loop, idle_cb);
2010.Ve 2133.Ve
2011.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop!" 2134.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop!"
2012.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop!" 2135.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop!"
2013.IX Subsection "ev_prepare and ev_check - customise your event loop!" 2136.IX Subsection "ev_prepare and ev_check - customise your event loop!"
2014Prepare and check watchers are usually (but not always) used in tandem: 2137Prepare and check watchers are usually (but not always) used in pairs:
2015prepare watchers get invoked before the process blocks and check watchers 2138prepare watchers get invoked before the process blocks and check watchers
2016afterwards. 2139afterwards.
2017.PP 2140.PP
2018You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter 2141You \fImust not\fR call \f(CW\*(C`ev_loop\*(C'\fR or similar functions that enter
2019the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR 2142the current event loop from either \f(CW\*(C`ev_prepare\*(C'\fR or \f(CW\*(C`ev_check\*(C'\fR
2022those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking, 2145those watchers, i.e. the sequence will always be \f(CW\*(C`ev_prepare\*(C'\fR, blocking,
2023\&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be 2146\&\f(CW\*(C`ev_check\*(C'\fR so if you have one watcher of each kind they will always be
2024called in pairs bracketing the blocking call. 2147called in pairs bracketing the blocking call.
2025.PP 2148.PP
2026Their main purpose is to integrate other event mechanisms into libev and 2149Their main purpose is to integrate other event mechanisms into libev and
2027their use is somewhat advanced. This could be used, for example, to track 2150their use is somewhat advanced. They could be used, for example, to track
2028variable changes, implement your own watchers, integrate net-snmp or a 2151variable changes, implement your own watchers, integrate net-snmp or a
2029coroutine library and lots more. They are also occasionally useful if 2152coroutine library and lots more. They are also occasionally useful if
2030you cache some data and want to flush it before blocking (for example, 2153you cache some data and want to flush it before blocking (for example,
2031in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR 2154in X programs you might want to do an \f(CW\*(C`XFlush ()\*(C'\fR in an \f(CW\*(C`ev_prepare\*(C'\fR
2032watcher). 2155watcher).
2033.PP 2156.PP
2034This is done by examining in each prepare call which file descriptors need 2157This is done by examining in each prepare call which file descriptors
2035to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for 2158need to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers
2036them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries 2159for them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many
2037provide just this functionality). Then, in the check watcher you check for 2160libraries provide exactly this functionality). Then, in the check watcher,
2038any events that occurred (by checking the pending status of all watchers 2161you check for any events that occurred (by checking the pending status
2039and stopping them) and call back into the library. The I/O and timer 2162of all watchers and stopping them) and call back into the library. The
2040callbacks will never actually be called (but must be valid nevertheless, 2163I/O and timer callbacks will never actually be called (but must be valid
2041because you never know, you know?). 2164nevertheless, because you never know, you know?).
2042.PP 2165.PP
2043As another example, the Perl Coro module uses these hooks to integrate 2166As another example, the Perl Coro module uses these hooks to integrate
2044coroutines into libev programs, by yielding to other active coroutines 2167coroutines into libev programs, by yielding to other active coroutines
2045during each prepare and only letting the process block if no coroutines 2168during each prepare and only letting the process block if no coroutines
2046are ready to run (it's actually more complicated: it only runs coroutines 2169are ready to run (it's actually more complicated: it only runs coroutines
2049loop from blocking if lower-priority coroutines are active, thus mapping 2172loop from blocking if lower-priority coroutines are active, thus mapping
2050low-priority coroutines to idle/background tasks). 2173low-priority coroutines to idle/background tasks).
2051.PP 2174.PP
2052It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) 2175It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR)
2053priority, to ensure that they are being run before any other watchers 2176priority, to ensure that they are being run before any other watchers
2177after the poll (this doesn't matter for \f(CW\*(C`ev_prepare\*(C'\fR watchers).
2178.PP
2054after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, 2179Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, too) should not
2055too) should not activate (\*(L"feed\*(R") events into libev. While libev fully 2180activate (\*(L"feed\*(R") events into libev. While libev fully supports this, they
2056supports this, they might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers 2181might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers did their job. As
2057did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other 2182\&\f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other (non-libev) event
2058(non-libev) event loops those other event loops might be in an unusable 2183loops those other event loops might be in an unusable state until their
2059state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to 2184\&\f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to coexist peacefully with
2060coexist peacefully with others). 2185others).
2061.PP 2186.PP
2062\fIWatcher-Specific Functions and Data Members\fR 2187\fIWatcher-Specific Functions and Data Members\fR
2063.IX Subsection "Watcher-Specific Functions and Data Members" 2188.IX Subsection "Watcher-Specific Functions and Data Members"
2064.IP "ev_prepare_init (ev_prepare *, callback)" 4 2189.IP "ev_prepare_init (ev_prepare *, callback)" 4
2065.IX Item "ev_prepare_init (ev_prepare *, callback)" 2190.IX Item "ev_prepare_init (ev_prepare *, callback)"
2067.IP "ev_check_init (ev_check *, callback)" 4 2192.IP "ev_check_init (ev_check *, callback)" 4
2068.IX Item "ev_check_init (ev_check *, callback)" 2193.IX Item "ev_check_init (ev_check *, callback)"
2069.PD 2194.PD
2070Initialises and configures the prepare or check watcher \- they have no 2195Initialises and configures the prepare or check watcher \- they have no
2071parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR 2196parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR
2072macros, but using them is utterly, utterly and completely pointless. 2197macros, but using them is utterly, utterly, utterly and completely
2198pointless.
2073.PP 2199.PP
2074\fIExamples\fR 2200\fIExamples\fR
2075.IX Subsection "Examples" 2201.IX Subsection "Examples"
2076.PP 2202.PP
2077There are a number of principal ways to embed other event loops or modules 2203There are a number of principal ways to embed other event loops or modules
2173\& 2299\&
2174\& // do not ever call adns_afterpoll 2300\& // do not ever call adns_afterpoll
2175.Ve 2301.Ve
2176.PP 2302.PP
2177Method 4: Do not use a prepare or check watcher because the module you 2303Method 4: Do not use a prepare or check watcher because the module you
2178want to embed is too inflexible to support it. Instead, you can override 2304want to embed is not flexible enough to support it. Instead, you can
2179their poll function. The drawback with this solution is that the main 2305override their poll function. The drawback with this solution is that the
2180loop is now no longer controllable by \s-1EV\s0. The \f(CW\*(C`Glib::EV\*(C'\fR module does 2306main loop is now no longer controllable by \s-1EV\s0. The \f(CW\*(C`Glib::EV\*(C'\fR module uses
2181this. 2307this approach, effectively embedding \s-1EV\s0 as a client into the horrible
2308libglib event loop.
2182.PP 2309.PP
2183.Vb 4 2310.Vb 4
2184\& static gint 2311\& static gint
2185\& event_poll_func (GPollFD *fds, guint nfds, gint timeout) 2312\& event_poll_func (GPollFD *fds, guint nfds, gint timeout)
2186\& { 2313\& {
2218prioritise I/O. 2345prioritise I/O.
2219.PP 2346.PP
2220As an example for a bug workaround, the kqueue backend might only support 2347As an example for a bug workaround, the kqueue backend might only support
2221sockets on some platform, so it is unusable as generic backend, but you 2348sockets on some platform, so it is unusable as generic backend, but you
2222still want to make use of it because you have many sockets and it scales 2349still want to make use of it because you have many sockets and it scales
2223so nicely. In this case, you would create a kqueue-based loop and embed it 2350so nicely. In this case, you would create a kqueue-based loop and embed
2224into your default loop (which might use e.g. poll). Overall operation will 2351it into your default loop (which might use e.g. poll). Overall operation
2225be a bit slower because first libev has to poll and then call kevent, but 2352will be a bit slower because first libev has to call \f(CW\*(C`poll\*(C'\fR and then
2226at least you can use both at what they are best. 2353\&\f(CW\*(C`kevent\*(C'\fR, but at least you can use both mechanisms for what they are
2354best: \f(CW\*(C`kqueue\*(C'\fR for scalable sockets and \f(CW\*(C`poll\*(C'\fR if you want it to work :)
2227.PP 2355.PP
2228As for prioritising I/O: rarely you have the case where some fds have 2356As for prioritising I/O: under rare circumstances you have the case where
2229to be watched and handled very quickly (with low latency), and even 2357some fds have to be watched and handled very quickly (with low latency),
2230priorities and idle watchers might have too much overhead. In this case 2358and even priorities and idle watchers might have too much overhead. In
2231you would put all the high priority stuff in one loop and all the rest in 2359this case you would put all the high priority stuff in one loop and all
2232a second one, and embed the second one in the first. 2360the rest in a second one, and embed the second one in the first.
2233.PP 2361.PP
2234As long as the watcher is active, the callback will be invoked every time 2362As long as the watcher is active, the callback will be invoked every time
2235there might be events pending in the embedded loop. The callback must then 2363there might be events pending in the embedded loop. The callback must then
2236call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke 2364call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke
2237their callbacks (you could also start an idle watcher to give the embedded 2365their callbacks (you could also start an idle watcher to give the embedded
2245interested in that. 2373interested in that.
2246.PP 2374.PP
2247Also, there have not currently been made special provisions for forking: 2375Also, there have not currently been made special provisions for forking:
2248when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops, 2376when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops,
2249but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers 2377but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers
2250yourself. 2378yourself \- but you can use a fork watcher to handle this automatically,
2379and future versions of libev might do just that.
2251.PP 2380.PP
2252Unfortunately, not all backends are embeddable, only the ones returned by 2381Unfortunately, not all backends are embeddable: only the ones returned by
2253\&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any 2382\&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any
2254portable one. 2383portable one.
2255.PP 2384.PP
2256So when you want to use this feature you will always have to be prepared 2385So when you want to use this feature you will always have to be prepared
2257that you cannot get an embeddable loop. The recommended way to get around 2386that you cannot get an embeddable loop. The recommended way to get around
2258this is to have a separate variables for your embeddable loop, try to 2387this is to have a separate variables for your embeddable loop, try to
2259create it, and if that fails, use the normal loop for everything. 2388create it, and if that fails, use the normal loop for everything.
2389.PP
2390\fI\f(CI\*(C`ev_embed\*(C'\fI and fork\fR
2391.IX Subsection "ev_embed and fork"
2392.PP
2393While the \f(CW\*(C`ev_embed\*(C'\fR watcher is running, forks in the embedding loop will
2394automatically be applied to the embedded loop as well, so no special
2395fork handling is required in that case. When the watcher is not running,
2396however, it is still the task of the libev user to call \f(CW\*(C`ev_loop_fork ()\*(C'\fR
2397as applicable.
2260.PP 2398.PP
2261\fIWatcher-Specific Functions and Data Members\fR 2399\fIWatcher-Specific Functions and Data Members\fR
2262.IX Subsection "Watcher-Specific Functions and Data Members" 2400.IX Subsection "Watcher-Specific Functions and Data Members"
2263.IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4 2401.IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4
2264.IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 2402.IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)"
2378is that the author does not know of a simple (or any) algorithm for a 2516is that the author does not know of a simple (or any) algorithm for a
2379multiple-writer-single-reader queue that works in all cases and doesn't 2517multiple-writer-single-reader queue that works in all cases and doesn't
2380need elaborate support such as pthreads. 2518need elaborate support such as pthreads.
2381.PP 2519.PP
2382That means that if you want to queue data, you have to provide your own 2520That means that if you want to queue data, you have to provide your own
2383queue. But at least I can tell you would implement locking around your 2521queue. But at least I can tell you how to implement locking around your
2384queue: 2522queue:
2385.IP "queueing from a signal handler context" 4 2523.IP "queueing from a signal handler context" 4
2386.IX Item "queueing from a signal handler context" 2524.IX Item "queueing from a signal handler context"
2387To implement race-free queueing, you simply add to the queue in the signal 2525To implement race-free queueing, you simply add to the queue in the signal
2388handler but you block the signal handler in the watcher callback. Here is an example that does that for 2526handler but you block the signal handler in the watcher callback. Here is an example that does that for
2459.IX Subsection "Watcher-Specific Functions and Data Members" 2597.IX Subsection "Watcher-Specific Functions and Data Members"
2460.IP "ev_async_init (ev_async *, callback)" 4 2598.IP "ev_async_init (ev_async *, callback)" 4
2461.IX Item "ev_async_init (ev_async *, callback)" 2599.IX Item "ev_async_init (ev_async *, callback)"
2462Initialises and configures the async watcher \- it has no parameters of any 2600Initialises and configures the async watcher \- it has no parameters of any
2463kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless, 2601kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless,
2464believe me. 2602trust me.
2465.IP "ev_async_send (loop, ev_async *)" 4 2603.IP "ev_async_send (loop, ev_async *)" 4
2466.IX Item "ev_async_send (loop, ev_async *)" 2604.IX Item "ev_async_send (loop, ev_async *)"
2467Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds 2605Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds
2468an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike 2606an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike
2469\&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do in other threads, signal or 2607\&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do from other threads, signal or
2470similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding 2608similar contexts (see the discussion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding
2471section below on what exactly this means). 2609section below on what exactly this means).
2472.Sp 2610.Sp
2473This call incurs the overhead of a system call only once per loop iteration, 2611This call incurs the overhead of a system call only once per loop iteration,
2474so while the overhead might be noticeable, it doesn't apply to repeated 2612so while the overhead might be noticeable, it doesn't apply to repeated
2662.Sp 2800.Sp
2663The prototype of the \f(CW\*(C`function\*(C'\fR must be \f(CW\*(C`void (*)(ev::TYPE &w, int)\*(C'\fR. 2801The prototype of the \f(CW\*(C`function\*(C'\fR must be \f(CW\*(C`void (*)(ev::TYPE &w, int)\*(C'\fR.
2664.Sp 2802.Sp
2665See the method\-\f(CW\*(C`set\*(C'\fR above for more details. 2803See the method\-\f(CW\*(C`set\*(C'\fR above for more details.
2666.Sp 2804.Sp
2667Example: 2805Example: Use a plain function as callback.
2668.Sp 2806.Sp
2669.Vb 2 2807.Vb 2
2670\& static void io_cb (ev::io &w, int revents) { } 2808\& static void io_cb (ev::io &w, int revents) { }
2671\& iow.set <io_cb> (); 2809\& iow.set <io_cb> ();
2672.Ve 2810.Ve
2708the constructor. 2846the constructor.
2709.PP 2847.PP
2710.Vb 4 2848.Vb 4
2711\& class myclass 2849\& class myclass
2712\& { 2850\& {
2713\& ev::io io; void io_cb (ev::io &w, int revents); 2851\& ev::io io ; void io_cb (ev::io &w, int revents);
2714\& ev:idle idle void idle_cb (ev::idle &w, int revents); 2852\& ev::idle idle; void idle_cb (ev::idle &w, int revents);
2715\& 2853\&
2716\& myclass (int fd) 2854\& myclass (int fd)
2717\& { 2855\& {
2718\& io .set <myclass, &myclass::io_cb > (this); 2856\& io .set <myclass, &myclass::io_cb > (this);
2719\& idle.set <myclass, &myclass::idle_cb> (this); 2857\& idle.set <myclass, &myclass::idle_cb> (this);
2731.IP "Perl" 4 2869.IP "Perl" 4
2732.IX Item "Perl" 2870.IX Item "Perl"
2733The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test 2871The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test
2734libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module, 2872libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module,
2735there are additional modules that implement libev-compatible interfaces 2873there are additional modules that implement libev-compatible interfaces
2736to \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 2874to \f(CW\*(C`libadns\*(C'\fR (\f(CW\*(C`EV::ADNS\*(C'\fR, but \f(CW\*(C`AnyEvent::DNS\*(C'\fR is preferred nowadays),
2737\&\f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR and \f(CW\*(C`EV::Glib\*(C'\fR). 2875\&\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
2876and \f(CW\*(C`EV::Glib\*(C'\fR).
2738.Sp 2877.Sp
2739It can be found and installed via \s-1CPAN\s0, its homepage is at 2878It can be found and installed via \s-1CPAN\s0, its homepage is at
2740<http://software.schmorp.de/pkg/EV>. 2879<http://software.schmorp.de/pkg/EV>.
2741.IP "Python" 4 2880.IP "Python" 4
2742.IX Item "Python" 2881.IX Item "Python"
2753more on top of it. It can be found via gem servers. Its homepage is at 2892more on top of it. It can be found via gem servers. Its homepage is at
2754<http://rev.rubyforge.org/>. 2893<http://rev.rubyforge.org/>.
2755.IP "D" 4 2894.IP "D" 4
2756.IX Item "D" 2895.IX Item "D"
2757Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to 2896Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to
2758be found at <http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>. 2897be found at <http://proj.llucax.com.ar/wiki/evd>.
2759.SH "MACRO MAGIC" 2898.SH "MACRO MAGIC"
2760.IX Header "MACRO MAGIC" 2899.IX Header "MACRO MAGIC"
2761Libev can be compiled with a variety of options, the most fundamental 2900Libev can be compiled with a variety of options, the most fundamental
2762of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) 2901of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most)
2763functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. 2902functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument.
2930.Ve 3069.Ve
2931.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" 3070.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0"
2932.IX Subsection "PREPROCESSOR SYMBOLS/MACROS" 3071.IX Subsection "PREPROCESSOR SYMBOLS/MACROS"
2933Libev can be configured via a variety of preprocessor symbols you have to 3072Libev can be configured via a variety of preprocessor symbols you have to
2934define before including any of its files. The default in the absence of 3073define before including any of its files. The default in the absence of
2935autoconf is noted for every option. 3074autoconf is documented for every option.
2936.IP "\s-1EV_STANDALONE\s0" 4 3075.IP "\s-1EV_STANDALONE\s0" 4
2937.IX Item "EV_STANDALONE" 3076.IX Item "EV_STANDALONE"
2938Must always be \f(CW1\fR if you do not use autoconf configuration, which 3077Must always be \f(CW1\fR if you do not use autoconf configuration, which
2939keeps libev from including \fIconfig.h\fR, and it also defines dummy 3078keeps libev from including \fIconfig.h\fR, and it also defines dummy
2940implementations for some libevent functions (such as logging, which is not 3079implementations for some libevent functions (such as logging, which is not
3088When doing priority-based operations, libev usually has to linearly search 3227When doing priority-based operations, libev usually has to linearly search
3089all the priorities, so having many of them (hundreds) uses a lot of space 3228all the priorities, so having many of them (hundreds) uses a lot of space
3090and time, so using the defaults of five priorities (\-2 .. +2) is usually 3229and time, so using the defaults of five priorities (\-2 .. +2) is usually
3091fine. 3230fine.
3092.Sp 3231.Sp
3093If your embedding application does not need any priorities, defining these both to 3232If your embedding application does not need any priorities, defining these
3094\&\f(CW0\fR will save some memory and \s-1CPU\s0. 3233both to \f(CW0\fR will save some memory and \s-1CPU\s0.
3095.IP "\s-1EV_PERIODIC_ENABLE\s0" 4 3234.IP "\s-1EV_PERIODIC_ENABLE\s0" 4
3096.IX Item "EV_PERIODIC_ENABLE" 3235.IX Item "EV_PERIODIC_ENABLE"
3097If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If 3236If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If
3098defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of 3237defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of
3099code. 3238code.
3103defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of 3242defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of
3104code. 3243code.
3105.IP "\s-1EV_EMBED_ENABLE\s0" 4 3244.IP "\s-1EV_EMBED_ENABLE\s0" 4
3106.IX Item "EV_EMBED_ENABLE" 3245.IX Item "EV_EMBED_ENABLE"
3107If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If 3246If undefined or defined to be \f(CW1\fR, then embed watchers are supported. If
3108defined to be \f(CW0\fR, then they are not. 3247defined to be \f(CW0\fR, then they are not. Embed watchers rely on most other
3248watcher types, which therefore must not be disabled.
3109.IP "\s-1EV_STAT_ENABLE\s0" 4 3249.IP "\s-1EV_STAT_ENABLE\s0" 4
3110.IX Item "EV_STAT_ENABLE" 3250.IX Item "EV_STAT_ENABLE"
3111If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If 3251If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If
3112defined to be \f(CW0\fR, then they are not. 3252defined to be \f(CW0\fR, then they are not.
3113.IP "\s-1EV_FORK_ENABLE\s0" 4 3253.IP "\s-1EV_FORK_ENABLE\s0" 4
3138watchers you might want to increase this value (\fImust\fR be a power of 3278watchers you might want to increase this value (\fImust\fR be a power of
3139two). 3279two).
3140.IP "\s-1EV_USE_4HEAP\s0" 4 3280.IP "\s-1EV_USE_4HEAP\s0" 4
3141.IX Item "EV_USE_4HEAP" 3281.IX Item "EV_USE_4HEAP"
3142Heaps are not very cache-efficient. To improve the cache-efficiency of the 3282Heaps are not very cache-efficient. To improve the cache-efficiency of the
3143timer and periodics heap, libev uses a 4\-heap when this symbol is defined 3283timer and periodics heaps, libev uses a 4\-heap when this symbol is defined
3144to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has 3284to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has noticeably
3145noticeably faster performance with many (thousands) of watchers. 3285faster performance with many (thousands) of watchers.
3146.Sp 3286.Sp
3147The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR 3287The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
3148(disabled). 3288(disabled).
3149.IP "\s-1EV_HEAP_CACHE_AT\s0" 4 3289.IP "\s-1EV_HEAP_CACHE_AT\s0" 4
3150.IX Item "EV_HEAP_CACHE_AT" 3290.IX Item "EV_HEAP_CACHE_AT"
3151Heaps are not very cache-efficient. To improve the cache-efficiency of the 3291Heaps are not very cache-efficient. To improve the cache-efficiency of the
3152timer and periodics heap, libev can cache the timestamp (\fIat\fR) within 3292timer and periodics heaps, libev can cache the timestamp (\fIat\fR) within
3153the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), 3293the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR),
3154which uses 8\-12 bytes more per watcher and a few hundred bytes more code, 3294which uses 8\-12 bytes more per watcher and a few hundred bytes more code,
3155but avoids random read accesses on heap changes. This improves performance 3295but avoids random read accesses on heap changes. This improves performance
3156noticeably with with many (hundreds) of watchers. 3296noticeably with many (hundreds) of watchers.
3157.Sp 3297.Sp
3158The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR 3298The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
3159(disabled). 3299(disabled).
3160.IP "\s-1EV_VERIFY\s0" 4 3300.IP "\s-1EV_VERIFY\s0" 4
3161.IX Item "EV_VERIFY" 3301.IX Item "EV_VERIFY"
3166called once per loop, which can slow down libev. If set to \f(CW3\fR, then the 3306called once per loop, which can slow down libev. If set to \f(CW3\fR, then the
3167verification code will be called very frequently, which will slow down 3307verification code will be called very frequently, which will slow down
3168libev considerably. 3308libev considerably.
3169.Sp 3309.Sp
3170The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be 3310The default is \f(CW1\fR, unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set, in which case it will be
3171\&\f(CW0.\fR 3311\&\f(CW0\fR.
3172.IP "\s-1EV_COMMON\s0" 4 3312.IP "\s-1EV_COMMON\s0" 4
3173.IX Item "EV_COMMON" 3313.IX Item "EV_COMMON"
3174By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining 3314By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining
3175this macro to a something else you can include more and other types of 3315this macro to a something else you can include more and other types of
3176members. You have to define it each time you include one of the files, 3316members. You have to define it each time you include one of the files,
3262.Ve 3402.Ve
3263.SH "THREADS AND COROUTINES" 3403.SH "THREADS AND COROUTINES"
3264.IX Header "THREADS AND COROUTINES" 3404.IX Header "THREADS AND COROUTINES"
3265.Sh "\s-1THREADS\s0" 3405.Sh "\s-1THREADS\s0"
3266.IX Subsection "THREADS" 3406.IX Subsection "THREADS"
3267Libev itself is completely thread-safe, but it uses no locking. This 3407All libev functions are reentrant and thread-safe unless explicitly
3408documented otherwise, but it uses no locking itself. This means that you
3268means that you can use as many loops as you want in parallel, as long as 3409can use as many loops as you want in parallel, as long as there are no
3269only one thread ever calls into one libev function with the same loop 3410concurrent calls into any libev function with the same loop parameter
3270parameter. 3411(\f(CW\*(C`ev_default_*\*(C'\fR calls have an implicit default loop parameter, of
3412course): libev guarantees that different event loops share no data
3413structures that need any locking.
3271.PP 3414.PP
3272Or put differently: calls with different loop parameters can be done in 3415Or to put it differently: calls with different loop parameters can be done
3273parallel from multiple threads, calls with the same loop parameter must be 3416concurrently from multiple threads, calls with the same loop parameter
3274done serially (but can be done from different threads, as long as only one 3417must be done serially (but can be done from different threads, as long as
3275thread ever is inside a call at any point in time, e.g. by using a mutex 3418only one thread ever is inside a call at any point in time, e.g. by using
3276per loop). 3419a mutex per loop).
3420.PP
3421Specifically to support threads (and signal handlers), libev implements
3422so-called \f(CW\*(C`ev_async\*(C'\fR watchers, which allow some limited form of
3423concurrency on the same event loop, namely waking it up \*(L"from the
3424outside\*(R".
3277.PP 3425.PP
3278If you want to know which design (one loop, locking, or multiple loops 3426If you want to know which design (one loop, locking, or multiple loops
3279without or something else still) is best for your problem, then I cannot 3427without or something else still) is best for your problem, then I cannot
3280help you. I can give some generic advice however: 3428help you, but here is some generic advice:
3281.IP "\(bu" 4 3429.IP "\(bu" 4
3282most applications have a main thread: use the default libev loop 3430most applications have a main thread: use the default libev loop
3283in that thread, or create a separate thread running only the default loop. 3431in that thread, or create a separate thread running only the default loop.
3284.Sp 3432.Sp
3285This helps integrating other libraries or software modules that use libev 3433This helps integrating other libraries or software modules that use libev
3295.Sp 3443.Sp
3296Choosing a model is hard \- look around, learn, know that usually you can do 3444Choosing a model is hard \- look around, learn, know that usually you can do
3297better than you currently do :\-) 3445better than you currently do :\-)
3298.IP "\(bu" 4 3446.IP "\(bu" 4
3299often you need to talk to some other thread which blocks in the 3447often you need to talk to some other thread which blocks in the
3448event loop.
3449.Sp
3300event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other 3450\&\f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other threads safely
3301threads safely (or from signal contexts...). 3451(or from signal contexts...).
3452.Sp
3453An example use would be to communicate signals or other events that only
3454work in the default loop by registering the signal watcher with the
3455default loop and triggering an \f(CW\*(C`ev_async\*(C'\fR watcher from the default loop
3456watcher callback into the event loop interested in the signal.
3302.Sh "\s-1COROUTINES\s0" 3457.Sh "\s-1COROUTINES\s0"
3303.IX Subsection "COROUTINES" 3458.IX Subsection "COROUTINES"
3304Libev is much more accommodating to coroutines (\*(L"cooperative threads\*(R"): 3459Libev is much more accommodating to coroutines (\*(L"cooperative threads\*(R"):
3305libev fully supports nesting calls to it's functions from different 3460libev fully supports nesting calls to it's functions from different
3306coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two 3461coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two
3307different coroutines and switch freely between both coroutines running the 3462different coroutines and switch freely between both coroutines running the
3308loop, as long as you don't confuse yourself). The only exception is that 3463loop, as long as you don't confuse yourself). The only exception is that
3309you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. 3464you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks.
3310.PP 3465.PP
3311Care has been invested into making sure that libev does not keep local 3466Care has been taken to ensure that libev does not keep local state inside
3312state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine 3467\&\f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine switches.
3313switches.
3314.SH "COMPLEXITIES" 3468.SH "COMPLEXITIES"
3315.IX Header "COMPLEXITIES" 3469.IX Header "COMPLEXITIES"
3316In this section the complexities of (many of) the algorithms used inside 3470In this section the complexities of (many of) the algorithms used inside
3317libev will be explained. For complexity discussions about backends see the 3471libev will be explained. For complexity discussions about backends see the
3318documentation for \f(CW\*(C`ev_default_init\*(C'\fR. 3472documentation for \f(CW\*(C`ev_default_init\*(C'\fR.
3359.IX Item "Priority handling: O(number_of_priorities)" 3513.IX Item "Priority handling: O(number_of_priorities)"
3360.PD 3514.PD
3361Priorities are implemented by allocating some space for each 3515Priorities are implemented by allocating some space for each
3362priority. When doing priority-based operations, libev usually has to 3516priority. When doing priority-based operations, libev usually has to
3363linearly search all the priorities, but starting/stopping and activating 3517linearly search all the priorities, but starting/stopping and activating
3364watchers becomes O(1) w.r.t. priority handling. 3518watchers becomes O(1) with respect to priority handling.
3365.IP "Sending an ev_async: O(1)" 4 3519.IP "Sending an ev_async: O(1)" 4
3366.IX Item "Sending an ev_async: O(1)" 3520.IX Item "Sending an ev_async: O(1)"
3367.PD 0 3521.PD 0
3368.IP "Processing ev_async_send: O(number_of_async_watchers)" 4 3522.IP "Processing ev_async_send: O(number_of_async_watchers)" 4
3369.IX Item "Processing ev_async_send: O(number_of_async_watchers)" 3523.IX Item "Processing ev_async_send: O(number_of_async_watchers)"
3392.PP 3546.PP
3393Not a libev limitation but worth mentioning: windows apparently doesn't 3547Not a libev limitation but worth mentioning: windows apparently doesn't
3394accept large writes: instead of resulting in a partial write, windows will 3548accept large writes: instead of resulting in a partial write, windows will
3395either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large, 3549either accept everything or return \f(CW\*(C`ENOBUFS\*(C'\fR if the buffer is too large,
3396so make sure you only write small amounts into your sockets (less than a 3550so make sure you only write small amounts into your sockets (less than a
3397megabyte seems safe, but thsi apparently depends on the amount of memory 3551megabyte seems safe, but this apparently depends on the amount of memory
3398available). 3552available).
3399.PP 3553.PP
3400Due to the many, low, and arbitrary limits on the win32 platform and 3554Due to the many, low, and arbitrary limits on the win32 platform and
3401the abysmal performance of winsockets, using a large number of sockets 3555the abysmal performance of winsockets, using a large number of sockets
3402is not recommended (and not reasonable). If your program needs to use 3556is not recommended (and not reasonable). If your program needs to use
3415\& 3569\&
3416\& #include "ev.h" 3570\& #include "ev.h"
3417.Ve 3571.Ve
3418.PP 3572.PP
3419And compile the following \fIevwrap.c\fR file into your project (make sure 3573And compile the following \fIevwrap.c\fR file into your project (make sure
3420you do \fInot\fR compile the \fIev.c\fR or any other embedded soruce files!): 3574you do \fInot\fR compile the \fIev.c\fR or any other embedded source files!):
3421.PP 3575.PP
3422.Vb 2 3576.Vb 2
3423\& #include "evwrap.h" 3577\& #include "evwrap.h"
3424\& #include "ev.c" 3578\& #include "ev.c"
3425.Ve 3579.Ve
3483calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally. 3637calls them using an \f(CW\*(C`ev_watcher *\*(C'\fR internally.
3484.ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 3638.ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4
3485.el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4 3639.el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4
3486.IX Item "sig_atomic_t volatile must be thread-atomic as well" 3640.IX Item "sig_atomic_t volatile must be thread-atomic as well"
3487The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as 3641The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as
3488\&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic w.r.t. accesses from different 3642\&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic with respect to accesses from different
3489threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is 3643threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is
3490believed to be sufficiently portable. 3644believed to be sufficiently portable.
3491.ie n .IP """sigprocmask"" must work in a threaded environment" 4 3645.ie n .IP """sigprocmask"" must work in a threaded environment" 4
3492.el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4 3646.el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4
3493.IX Item "sigprocmask must work in a threaded environment" 3647.IX Item "sigprocmask must work in a threaded environment"
3569If you need, for some reason, empty reports from valgrind for your project 3723If you need, for some reason, empty reports from valgrind for your project
3570I suggest using suppression lists. 3724I suggest using suppression lists.
3571.SH "AUTHOR" 3725.SH "AUTHOR"
3572.IX Header "AUTHOR" 3726.IX Header "AUTHOR"
3573Marc Lehmann <libev@schmorp.de>. 3727Marc Lehmann <libev@schmorp.de>.
3574.SH "POD ERRORS"
3575.IX Header "POD ERRORS"
3576Hey! \fBThe above document had some coding errors, which are explained below:\fR
3577.IP "Around line 3122:" 4
3578.IX Item "Around line 3122:"
3579You forgot a '=back' before '=head2'

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines