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

Comparing libev/ev.3 (file contents):
Revision 1.8 by root, Fri Nov 23 15:26:08 2007 UTC vs.
Revision 1.11 by root, Sat Nov 24 07:14:26 2007 UTC

127.\} 127.\}
128.rm #[ #] #H #V #F C 128.rm #[ #] #H #V #F C
129.\" ======================================================================== 129.\" ========================================================================
130.\" 130.\"
131.IX Title ""<STANDARD INPUT>" 1" 131.IX Title ""<STANDARD INPUT>" 1"
132.TH "<STANDARD INPUT>" 1 "2007-11-23" "perl v5.8.8" "User Contributed Perl Documentation" 132.TH "<STANDARD INPUT>" 1 "2007-11-24" "perl v5.8.8" "User Contributed Perl Documentation"
133.SH "NAME" 133.SH "NAME"
134libev \- a high performance full\-featured event loop written in C 134libev \- a high performance full\-featured event loop written in C
135.SH "SYNOPSIS" 135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS" 136.IX Header "SYNOPSIS"
137.Vb 1 137.Vb 1
173.IX Header "TIME REPRESENTATION" 173.IX Header "TIME REPRESENTATION"
174Libev represents time as a single floating point number, representing the 174Libev represents time as a single floating point number, representing the
175(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near 175(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near
176the beginning of 1970, details are complicated, don't ask). This type is 176the beginning of 1970, details are complicated, don't ask). This type is
177called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases 177called \f(CW\*(C`ev_tstamp\*(C'\fR, which is what you should use too. It usually aliases
178to the double type in C. 178to the \f(CW\*(C`double\*(C'\fR type in C, and when you need to do any calculations on
179it, you should treat it as such.
179.SH "GLOBAL FUNCTIONS" 180.SH "GLOBAL FUNCTIONS"
180.IX Header "GLOBAL FUNCTIONS" 181.IX Header "GLOBAL FUNCTIONS"
181These functions can be called anytime, even before initialising the 182These functions can be called anytime, even before initialising the
182library in any way. 183library in any way.
183.IP "ev_tstamp ev_time ()" 4 184.IP "ev_tstamp ev_time ()" 4
199.Sp 200.Sp
200Usually, it's a good idea to terminate if the major versions mismatch, 201Usually, it's a good idea to terminate if the major versions mismatch,
201as this indicates an incompatible change. Minor versions are usually 202as this indicates an incompatible change. Minor versions are usually
202compatible to older versions, so a larger minor version alone is usually 203compatible to older versions, so a larger minor version alone is usually
203not a problem. 204not a problem.
205.Sp
206Example: make sure we haven't accidentally been linked against the wrong
207version:
208.Sp
209.Vb 3
210\& assert (("libev version mismatch",
211\& ev_version_major () == EV_VERSION_MAJOR
212\& && ev_version_minor () >= EV_VERSION_MINOR));
213.Ve
204.IP "unsigned int ev_supported_backends ()" 4 214.IP "unsigned int ev_supported_backends ()" 4
205.IX Item "unsigned int ev_supported_backends ()" 215.IX Item "unsigned int ev_supported_backends ()"
206Return the set of all backends (i.e. their corresponding \f(CW\*(C`EV_BACKEND_*\*(C'\fR 216Return the set of all backends (i.e. their corresponding \f(CW\*(C`EV_BACKEND_*\*(C'\fR
207value) compiled into this binary of libev (independent of their 217value) compiled into this binary of libev (independent of their
208availability on the system you are running on). See \f(CW\*(C`ev_default_loop\*(C'\fR for 218availability on the system you are running on). See \f(CW\*(C`ev_default_loop\*(C'\fR for
209a description of the set values. 219a description of the set values.
220.Sp
221Example: make sure we have the epoll method, because yeah this is cool and
222a must have and can we have a torrent of it please!!!11
223.Sp
224.Vb 2
225\& assert (("sorry, no epoll, no sex",
226\& ev_supported_backends () & EVBACKEND_EPOLL));
227.Ve
210.IP "unsigned int ev_recommended_backends ()" 4 228.IP "unsigned int ev_recommended_backends ()" 4
211.IX Item "unsigned int ev_recommended_backends ()" 229.IX Item "unsigned int ev_recommended_backends ()"
212Return the set of all backends compiled into this binary of libev and also 230Return the set of all backends compiled into this binary of libev and also
213recommended for this platform. This set is often smaller than the one 231recommended for this platform. This set is often smaller than the one
214returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on 232returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on
215most BSDs and will not be autodetected unless you explicitly request it 233most BSDs and will not be autodetected unless you explicitly request it
216(assuming you know what you are doing). This is the set of backends that 234(assuming you know what you are doing). This is the set of backends that
217libev will probe for if you specify no backends explicitly. 235libev will probe for if you specify no backends explicitly.
236.IP "unsigned int ev_embeddable_backends ()" 4
237.IX Item "unsigned int ev_embeddable_backends ()"
238Returns the set of backends that are embeddable in other event loops. This
239is the theoretical, all\-platform, value. To find which backends
240might be supported on the current system, you would need to look at
241\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for
242recommended ones.
243.Sp
244See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
218.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 245.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4
219.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" 246.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))"
220Sets the allocation function to use (the prototype is similar to the 247Sets the allocation function to use (the prototype is similar to the
221realloc C function, the semantics are identical). It is used to allocate 248realloc C function, the semantics are identical). It is used to allocate
222and free memory (no surprises here). If it returns zero when memory 249and free memory (no surprises here). If it returns zero when memory
224destructive action. The default is your system realloc function. 251destructive action. The default is your system realloc function.
225.Sp 252.Sp
226You could override this function in high-availability programs to, say, 253You could override this function in high-availability programs to, say,
227free some memory if it cannot allocate memory, to use a special allocator, 254free some memory if it cannot allocate memory, to use a special allocator,
228or even to sleep a while and retry until some memory is available. 255or even to sleep a while and retry until some memory is available.
256.Sp
257Example: replace the libev allocator with one that waits a bit and then
258retries: better than mine).
259.Sp
260.Vb 6
261\& static void *
262\& persistent_realloc (void *ptr, long size)
263\& {
264\& for (;;)
265\& {
266\& void *newptr = realloc (ptr, size);
267.Ve
268.Sp
269.Vb 2
270\& if (newptr)
271\& return newptr;
272.Ve
273.Sp
274.Vb 3
275\& sleep (60);
276\& }
277\& }
278.Ve
279.Sp
280.Vb 2
281\& ...
282\& ev_set_allocator (persistent_realloc);
283.Ve
229.IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4 284.IP "ev_set_syserr_cb (void (*cb)(const char *msg));" 4
230.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));" 285.IX Item "ev_set_syserr_cb (void (*cb)(const char *msg));"
231Set the callback function to call on a retryable syscall error (such 286Set the callback function to call on a retryable syscall error (such
232as failed select, poll, epoll_wait). The message is a printable string 287as failed select, poll, epoll_wait). The message is a printable string
233indicating the system call or subsystem causing the problem. If this 288indicating the system call or subsystem causing the problem. If this
234callback is set, then libev will expect it to remedy the sitution, no 289callback is set, then libev will expect it to remedy the sitution, no
235matter what, when it returns. That is, libev will generally retry the 290matter what, when it returns. That is, libev will generally retry the
236requested operation, or, if the condition doesn't go away, do bad stuff 291requested operation, or, if the condition doesn't go away, do bad stuff
237(such as abort). 292(such as abort).
293.Sp
294Example: do the same thing as libev does internally:
295.Sp
296.Vb 6
297\& static void
298\& fatal_error (const char *msg)
299\& {
300\& perror (msg);
301\& abort ();
302\& }
303.Ve
304.Sp
305.Vb 2
306\& ...
307\& ev_set_syserr_cb (fatal_error);
308.Ve
238.SH "FUNCTIONS CONTROLLING THE EVENT LOOP" 309.SH "FUNCTIONS CONTROLLING THE EVENT LOOP"
239.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" 310.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP"
240An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two 311An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two
241types of such loops, the \fIdefault\fR loop, which supports signals and child 312types of such loops, the \fIdefault\fR loop, which supports signals and child
242events, and dynamically created loops which do not. 313events, and dynamically created loops which do not.
376.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)" 447.IX Item "struct ev_loop *ev_loop_new (unsigned int flags)"
377Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is 448Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is
378always distinct from the default loop. Unlike the default loop, it cannot 449always distinct from the default loop. Unlike the default loop, it cannot
379handle signal and child watchers, and attempts to do so will be greeted by 450handle signal and child watchers, and attempts to do so will be greeted by
380undefined behaviour (or a failed assertion if assertions are enabled). 451undefined behaviour (or a failed assertion if assertions are enabled).
452.Sp
453Example: try to create a event loop that uses epoll and nothing else.
454.Sp
455.Vb 3
456\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
457\& if (!epoller)
458\& fatal ("no epoll found here, maybe it hides under your chair");
459.Ve
381.IP "ev_default_destroy ()" 4 460.IP "ev_default_destroy ()" 4
382.IX Item "ev_default_destroy ()" 461.IX Item "ev_default_destroy ()"
383Destroys the default loop again (frees all memory and kernel state 462Destroys the default loop again (frees all memory and kernel state
384etc.). This stops all registered event watchers (by not touching them in 463etc.). This stops all registered event watchers (by not touching them in
385any way whatsoever, although you cannot rely on this :). 464any way whatsoever, although you cannot rely on this :).
419Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in 498Returns one of the \f(CW\*(C`EVBACKEND_*\*(C'\fR flags indicating the event backend in
420use. 499use.
421.IP "ev_tstamp ev_now (loop)" 4 500.IP "ev_tstamp ev_now (loop)" 4
422.IX Item "ev_tstamp ev_now (loop)" 501.IX Item "ev_tstamp ev_now (loop)"
423Returns the current \*(L"event loop time\*(R", which is the time the event loop 502Returns the current \*(L"event loop time\*(R", which is the time the event loop
424got events and started processing them. This timestamp does not change 503received events and started processing them. This timestamp does not
425as long as callbacks are being processed, and this is also the base time 504change as long as callbacks are being processed, and this is also the base
426used for relative timers. You can treat it as the timestamp of the event 505time used for relative timers. You can treat it as the timestamp of the
427occuring (or more correctly, the mainloop finding out about it). 506event occuring (or more correctly, libev finding out about it).
428.IP "ev_loop (loop, int flags)" 4 507.IP "ev_loop (loop, int flags)" 4
429.IX Item "ev_loop (loop, int flags)" 508.IX Item "ev_loop (loop, int flags)"
430Finally, this is it, the event handler. This function usually is called 509Finally, this is it, the event handler. This function usually is called
431after you initialised all your watchers and you want to start handling 510after you initialised all your watchers and you want to start handling
432events. 511events.
433.Sp 512.Sp
434If the flags argument is specified as \f(CW0\fR, it will not return until 513If the flags argument is specified as \f(CW0\fR, it will not return until
435either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called. 514either no event watchers are active anymore or \f(CW\*(C`ev_unloop\*(C'\fR was called.
515.Sp
516Please note that an explicit \f(CW\*(C`ev_unloop\*(C'\fR is usually better than
517relying on all watchers to be stopped when deciding when a program has
518finished (especially in interactive programs), but having a program that
519automatically loops as long as it has to and no longer by virtue of
520relying on its watchers stopping correctly is a thing of beauty.
436.Sp 521.Sp
437A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle 522A flags value of \f(CW\*(C`EVLOOP_NONBLOCK\*(C'\fR will look for new events, will handle
438those events and any outstanding ones, but will not block your process in 523those events and any outstanding ones, but will not block your process in
439case there are no events and will return after one iteration of the loop. 524case there are no events and will return after one iteration of the loop.
440.Sp 525.Sp
465\& - Call all queued watchers in reverse order (i.e. check watchers first). 550\& - Call all queued watchers in reverse order (i.e. check watchers first).
466\& Signals and child watchers are implemented as I/O watchers, and will 551\& Signals and child watchers are implemented as I/O watchers, and will
467\& be handled here by queueing them when their watcher gets executed. 552\& be handled here by queueing them when their watcher gets executed.
468\& - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK 553\& - If ev_unloop has been called or EVLOOP_ONESHOT or EVLOOP_NONBLOCK
469\& were used, return, otherwise continue with step *. 554\& were used, return, otherwise continue with step *.
555.Ve
556.Sp
557Example: queue some jobs and then loop until no events are outsanding
558anymore.
559.Sp
560.Vb 4
561\& ... queue jobs here, make sure they register event watchers as long
562\& ... as they still have work to do (even an idle watcher will do..)
563\& ev_loop (my_loop, 0);
564\& ... jobs done. yeah!
470.Ve 565.Ve
471.IP "ev_unloop (loop, how)" 4 566.IP "ev_unloop (loop, how)" 4
472.IX Item "ev_unloop (loop, how)" 567.IX Item "ev_unloop (loop, how)"
473Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it 568Can be used to make a call to \f(CW\*(C`ev_loop\*(C'\fR return early (but only after it
474has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either 569has processed all outstanding events). The \f(CW\*(C`how\*(C'\fR argument must be either
488example, libev itself uses this for its internal signal pipe: It is not 583example, libev itself uses this for its internal signal pipe: It is not
489visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if 584visible to the libev user and should not keep \f(CW\*(C`ev_loop\*(C'\fR from exiting if
490no event watchers registered by it are active. It is also an excellent 585no event watchers registered by it are active. It is also an excellent
491way to do this for generic recurring timers or from within third-party 586way to do this for generic recurring timers or from within third-party
492libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR. 587libraries. Just remember to \fIunref after start\fR and \fIref before stop\fR.
588.Sp
589Example: create a signal watcher, but keep it from keeping \f(CW\*(C`ev_loop\*(C'\fR
590running when nothing else is active.
591.Sp
592.Vb 4
593\& struct dv_signal exitsig;
594\& ev_signal_init (&exitsig, sig_cb, SIGINT);
595\& ev_signal_start (myloop, &exitsig);
596\& evf_unref (myloop);
597.Ve
598.Sp
599Example: for some weird reason, unregister the above signal handler again.
600.Sp
601.Vb 2
602\& ev_ref (myloop);
603\& ev_signal_stop (myloop, &exitsig);
604.Ve
493.SH "ANATOMY OF A WATCHER" 605.SH "ANATOMY OF A WATCHER"
494.IX Header "ANATOMY OF A WATCHER" 606.IX Header "ANATOMY OF A WATCHER"
495A watcher is a structure that you create and register to record your 607A watcher is a structure that you create and register to record your
496interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to 608interest in some event. For instance, if you want to wait for \s-1STDIN\s0 to
497become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that: 609become readable, you would create an \f(CW\*(C`ev_io\*(C'\fR watcher for that:
533*)\*(C'\fR), and you can stop watching for events at any time by calling the 645*)\*(C'\fR), and you can stop watching for events at any time by calling the
534corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR. 646corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR.
535.PP 647.PP
536As long as your watcher is active (has been started but not stopped) you 648As long as your watcher is active (has been started but not stopped) you
537must not touch the values stored in it. Most specifically you must never 649must not touch the values stored in it. Most specifically you must never
538reinitialise it or call its set macro. 650reinitialise it or call its \f(CW\*(C`set\*(C'\fR macro.
539.PP
540You can check whether an event is active by calling the \f(CW\*(C`ev_is_active
541(watcher *)\*(C'\fR macro. To see whether an event is outstanding (but the
542callback for it has not been called yet) you can use the \f(CW\*(C`ev_is_pending
543(watcher *)\*(C'\fR macro.
544.PP 651.PP
545Each and every callback receives the event loop pointer as first, the 652Each and every callback receives the event loop pointer as first, the
546registered watcher structure as second, and a bitset of received events as 653registered watcher structure as second, and a bitset of received events as
547third argument. 654third argument.
548.PP 655.PP
606Libev will usually signal a few \*(L"dummy\*(R" events together with an error, 713Libev will usually signal a few \*(L"dummy\*(R" events together with an error,
607for example it might indicate that a fd is readable or writable, and if 714for example it might indicate that a fd is readable or writable, and if
608your callbacks is well-written it can just attempt the operation and cope 715your callbacks is well-written it can just attempt the operation and cope
609with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multithreaded 716with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multithreaded
610programs, though, so beware. 717programs, though, so beware.
718.Sh "\s-1SUMMARY\s0 \s-1OF\s0 \s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0"
719.IX Subsection "SUMMARY OF GENERIC WATCHER FUNCTIONS"
720In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type,
721e.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.
722.ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4
723.el .IP "\f(CWev_init\fR (ev_TYPE *watcher, callback)" 4
724.IX Item "ev_init (ev_TYPE *watcher, callback)"
725This macro initialises the generic portion of a watcher. The contents
726of the watcher object can be arbitrary (so \f(CW\*(C`malloc\*(C'\fR will do). Only
727the generic parts of the watcher are initialised, you \fIneed\fR to call
728the type-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR macro afterwards to initialise the
729type-specific parts. For each type there is also a \f(CW\*(C`ev_TYPE_init\*(C'\fR macro
730which rolls both calls into one.
731.Sp
732You can reinitialise a watcher at any time as long as it has been stopped
733(or never started) and there are no pending events outstanding.
734.Sp
735The callbakc is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher,
736int revents)\*(C'\fR.
737.ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4
738.el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4
739.IX Item "ev_TYPE_set (ev_TYPE *, [args])"
740This macro initialises the type-specific parts of a watcher. You need to
741call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can
742call \f(CW\*(C`ev_TYPE_set\*(C'\fR any number of times. You must not, however, call this
743macro on a watcher that is active (it can be pending, however, which is a
744difference to the \f(CW\*(C`ev_init\*(C'\fR macro).
745.Sp
746Although some watcher types do not have type-specific arguments
747(e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro.
748.ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4
749.el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4
750.IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])"
751This convinience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro
752calls into a single call. This is the most convinient method to initialise
753a watcher. The same limitations apply, of course.
754.ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4
755.el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4
756.IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)"
757Starts (activates) the given watcher. Only active watchers will receive
758events. If the watcher is already active nothing will happen.
759.ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4
760.el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4
761.IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)"
762Stops the given watcher again (if active) and clears the pending
763status. It is possible that stopped watchers are pending (for example,
764non-repeating timers are being stopped when they become pending), but
765\&\f(CW\*(C`ev_TYPE_stop\*(C'\fR ensures that the watcher is neither active nor pending. If
766you want to free or reuse the memory used by the watcher it is therefore a
767good idea to always call its \f(CW\*(C`ev_TYPE_stop\*(C'\fR function.
768.IP "bool ev_is_active (ev_TYPE *watcher)" 4
769.IX Item "bool ev_is_active (ev_TYPE *watcher)"
770Returns a true value iff the watcher is active (i.e. it has been started
771and not yet been stopped). As long as a watcher is active you must not modify
772it.
773.IP "bool ev_is_pending (ev_TYPE *watcher)" 4
774.IX Item "bool ev_is_pending (ev_TYPE *watcher)"
775Returns a true value iff the watcher is pending, (i.e. it has outstanding
776events but its callback has not yet been invoked). As long as a watcher
777is pending (but not active) you must not call an init function on it (but
778\&\f(CW\*(C`ev_TYPE_set\*(C'\fR is safe) and you must make sure the watcher is available to
779libev (e.g. you cnanot \f(CW\*(C`free ()\*(C'\fR it).
780.IP "callback = ev_cb (ev_TYPE *watcher)" 4
781.IX Item "callback = ev_cb (ev_TYPE *watcher)"
782Returns the callback currently set on the watcher.
783.IP "ev_cb_set (ev_TYPE *watcher, callback)" 4
784.IX Item "ev_cb_set (ev_TYPE *watcher, callback)"
785Change the callback. You can change the callback at virtually any time
786(modulo threads).
611.Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" 787.Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0"
612.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" 788.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER"
613Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change 789Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change
614and read at any time, libev will completely ignore it. This can be used 790and read at any time, libev will completely ignore it. This can be used
615to associate arbitrary data with your watcher. If you need more data and 791to associate arbitrary data with your watcher. If you need more data and
685\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR, which don't suffer from this 861\&\f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or \f(CW\*(C`EVBACKEND_POLL\*(C'\fR, which don't suffer from this
686problem. Also note that it is quite easy to have your callback invoked 862problem. Also note that it is quite easy to have your callback invoked
687when the readyness condition is no longer valid even when employing 863when the readyness condition is no longer valid even when employing
688typical ways of handling events, so its a good idea to use non-blocking 864typical ways of handling events, so its a good idea to use non-blocking
689I/O unconditionally. 865I/O unconditionally.
866.PP
867Example: call \f(CW\*(C`stdin_readable_cb\*(C'\fR when \s-1STDIN_FILENO\s0 has become, well
868readable, but only once. Since it is likely line\-buffered, you could
869attempt to read a whole line in the callback:
870.PP
871.Vb 6
872\& static void
873\& stdin_readable_cb (struct ev_loop *loop, struct ev_io *w, int revents)
874\& {
875\& ev_io_stop (loop, w);
876\& .. read from stdin here (or from w->fd) and haqndle any I/O errors
877\& }
878.Ve
879.PP
880.Vb 6
881\& ...
882\& struct ev_loop *loop = ev_default_init (0);
883\& struct ev_io stdin_readable;
884\& ev_io_init (&stdin_readable, stdin_readable_cb, STDIN_FILENO, EV_READ);
885\& ev_io_start (loop, &stdin_readable);
886\& ev_loop (loop, 0);
887.Ve
690.ie n .Sh """ev_timer"" \- relative and optionally recurring timeouts" 888.ie n .Sh """ev_timer"" \- relative and optionally recurring timeouts"
691.el .Sh "\f(CWev_timer\fP \- relative and optionally recurring timeouts" 889.el .Sh "\f(CWev_timer\fP \- relative and optionally recurring timeouts"
692.IX Subsection "ev_timer - relative and optionally recurring timeouts" 890.IX Subsection "ev_timer - relative and optionally recurring timeouts"
693Timer watchers are simple relative timers that generate an event after a 891Timer watchers are simple relative timers that generate an event after a
694given time, and optionally repeating in regular intervals after that. 892given time, and optionally repeating in regular intervals after that.
744seconds of inactivity on the socket. The easiest way to do this is to 942seconds of inactivity on the socket. The easiest way to do this is to
745configure an \f(CW\*(C`ev_timer\*(C'\fR with after=repeat=60 and calling ev_timer_again each 943configure an \f(CW\*(C`ev_timer\*(C'\fR with after=repeat=60 and calling ev_timer_again each
746time you successfully read or write some data. If you go into an idle 944time you successfully read or write some data. If you go into an idle
747state where you do not expect data to travel on the socket, you can stop 945state where you do not expect data to travel on the socket, you can stop
748the timer, and again will automatically restart it if need be. 946the timer, and again will automatically restart it if need be.
947.PP
948Example: create a timer that fires after 60 seconds.
949.PP
950.Vb 5
951\& static void
952\& one_minute_cb (struct ev_loop *loop, struct ev_timer *w, int revents)
953\& {
954\& .. one minute over, w is actually stopped right here
955\& }
956.Ve
957.PP
958.Vb 3
959\& struct ev_timer mytimer;
960\& ev_timer_init (&mytimer, one_minute_cb, 60., 0.);
961\& ev_timer_start (loop, &mytimer);
962.Ve
963.PP
964Example: create a timeout timer that times out after 10 seconds of
965inactivity.
966.PP
967.Vb 5
968\& static void
969\& timeout_cb (struct ev_loop *loop, struct ev_timer *w, int revents)
970\& {
971\& .. ten seconds without any activity
972\& }
973.Ve
974.PP
975.Vb 4
976\& struct ev_timer mytimer;
977\& ev_timer_init (&mytimer, timeout_cb, 0., 10.); /* note, only repeat used */
978\& ev_timer_again (&mytimer); /* start timer */
979\& ev_loop (loop, 0);
980.Ve
981.PP
982.Vb 3
983\& // and in some piece of code that gets executed on any "activity":
984\& // reset the timeout to start ticking again at 10 seconds
985\& ev_timer_again (&mytimer);
986.Ve
749.ie n .Sh """ev_periodic"" \- to cron or not to cron" 987.ie n .Sh """ev_periodic"" \- to cron or not to cron"
750.el .Sh "\f(CWev_periodic\fP \- to cron or not to cron" 988.el .Sh "\f(CWev_periodic\fP \- to cron or not to cron"
751.IX Subsection "ev_periodic - to cron or not to cron" 989.IX Subsection "ev_periodic - to cron or not to cron"
752Periodic watchers are also timers of a kind, but they are very versatile 990Periodic watchers are also timers of a kind, but they are very versatile
753(and unfortunately a bit complex). 991(and unfortunately a bit complex).
845.IX Item "ev_periodic_again (loop, ev_periodic *)" 1083.IX Item "ev_periodic_again (loop, ev_periodic *)"
846Simply stops and restarts the periodic watcher again. This is only useful 1084Simply stops and restarts the periodic watcher again. This is only useful
847when you changed some parameters or the reschedule callback would return 1085when you changed some parameters or the reschedule callback would return
848a different time than the last time it was called (e.g. in a crond like 1086a different time than the last time it was called (e.g. in a crond like
849program when the crontabs have changed). 1087program when the crontabs have changed).
1088.PP
1089Example: call a callback every hour, or, more precisely, whenever the
1090system clock is divisible by 3600. The callback invocation times have
1091potentially a lot of jittering, but good long-term stability.
1092.PP
1093.Vb 5
1094\& static void
1095\& clock_cb (struct ev_loop *loop, struct ev_io *w, int revents)
1096\& {
1097\& ... its now a full hour (UTC, or TAI or whatever your clock follows)
1098\& }
1099.Ve
1100.PP
1101.Vb 3
1102\& struct ev_periodic hourly_tick;
1103\& ev_periodic_init (&hourly_tick, clock_cb, 0., 3600., 0);
1104\& ev_periodic_start (loop, &hourly_tick);
1105.Ve
1106.PP
1107Example: the same as above, but use a reschedule callback to do it:
1108.PP
1109.Vb 1
1110\& #include <math.h>
1111.Ve
1112.PP
1113.Vb 5
1114\& static ev_tstamp
1115\& my_scheduler_cb (struct ev_periodic *w, ev_tstamp now)
1116\& {
1117\& return fmod (now, 3600.) + 3600.;
1118\& }
1119.Ve
1120.PP
1121.Vb 1
1122\& ev_periodic_init (&hourly_tick, clock_cb, 0., 0., my_scheduler_cb);
1123.Ve
1124.PP
1125Example: call a callback every hour, starting now:
1126.PP
1127.Vb 4
1128\& struct ev_periodic hourly_tick;
1129\& ev_periodic_init (&hourly_tick, clock_cb,
1130\& fmod (ev_now (loop), 3600.), 3600., 0);
1131\& ev_periodic_start (loop, &hourly_tick);
1132.Ve
850.ie n .Sh """ev_signal"" \- signal me when a signal gets signalled" 1133.ie n .Sh """ev_signal"" \- signal me when a signal gets signalled"
851.el .Sh "\f(CWev_signal\fP \- signal me when a signal gets signalled" 1134.el .Sh "\f(CWev_signal\fP \- signal me when a signal gets signalled"
852.IX Subsection "ev_signal - signal me when a signal gets signalled" 1135.IX Subsection "ev_signal - signal me when a signal gets signalled"
853Signal watchers will trigger an event when the process receives a specific 1136Signal watchers will trigger an event when the process receives a specific
854signal one or more times. Even though signals are very asynchronous, libev 1137signal one or more times. Even though signals are very asynchronous, libev
884\&\fIany\fR process if \f(CW\*(C`pid\*(C'\fR is specified as \f(CW0\fR). The callback can look 1167\&\fIany\fR process if \f(CW\*(C`pid\*(C'\fR is specified as \f(CW0\fR). The callback can look
885at the \f(CW\*(C`rstatus\*(C'\fR member of the \f(CW\*(C`ev_child\*(C'\fR watcher structure to see 1168at the \f(CW\*(C`rstatus\*(C'\fR member of the \f(CW\*(C`ev_child\*(C'\fR watcher structure to see
886the status word (use the macros from \f(CW\*(C`sys/wait.h\*(C'\fR and see your systems 1169the status word (use the macros from \f(CW\*(C`sys/wait.h\*(C'\fR and see your systems
887\&\f(CW\*(C`waitpid\*(C'\fR documentation). The \f(CW\*(C`rpid\*(C'\fR member contains the pid of the 1170\&\f(CW\*(C`waitpid\*(C'\fR documentation). The \f(CW\*(C`rpid\*(C'\fR member contains the pid of the
888process causing the status change. 1171process causing the status change.
1172.PP
1173Example: try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0.
1174.PP
1175.Vb 5
1176\& static void
1177\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1178\& {
1179\& ev_unloop (loop, EVUNLOOP_ALL);
1180\& }
1181.Ve
1182.PP
1183.Vb 3
1184\& struct ev_signal signal_watcher;
1185\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1186\& ev_signal_start (loop, &sigint_cb);
1187.Ve
889.ie n .Sh """ev_idle"" \- when you've got nothing better to do" 1188.ie n .Sh """ev_idle"" \- when you've got nothing better to do"
890.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do" 1189.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do"
891.IX Subsection "ev_idle - when you've got nothing better to do" 1190.IX Subsection "ev_idle - when you've got nothing better to do"
892Idle watchers trigger events when there are no other events are pending 1191Idle watchers trigger events when there are no other events are pending
893(prepare, check and other idle watchers do not count). That is, as long 1192(prepare, check and other idle watchers do not count). That is, as long
907.IP "ev_idle_init (ev_signal *, callback)" 4 1206.IP "ev_idle_init (ev_signal *, callback)" 4
908.IX Item "ev_idle_init (ev_signal *, callback)" 1207.IX Item "ev_idle_init (ev_signal *, callback)"
909Initialises and configures the idle watcher \- it has no parameters of any 1208Initialises and configures the idle watcher \- it has no parameters of any
910kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless, 1209kind. There is a \f(CW\*(C`ev_idle_set\*(C'\fR macro, but using it is utterly pointless,
911believe me. 1210believe me.
1211.PP
1212Example: dynamically allocate an \f(CW\*(C`ev_idle\*(C'\fR, start it, and in the
1213callback, free it. Alos, use no error checking, as usual.
1214.PP
1215.Vb 7
1216\& static void
1217\& idle_cb (struct ev_loop *loop, struct ev_idle *w, int revents)
1218\& {
1219\& free (w);
1220\& // now do something you wanted to do when the program has
1221\& // no longer asnything immediate to do.
1222\& }
1223.Ve
1224.PP
1225.Vb 3
1226\& struct ev_idle *idle_watcher = malloc (sizeof (struct ev_idle));
1227\& ev_idle_init (idle_watcher, idle_cb);
1228\& ev_idle_start (loop, idle_cb);
1229.Ve
912.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop" 1230.ie n .Sh """ev_prepare""\fP and \f(CW""ev_check"" \- customise your event loop"
913.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop" 1231.el .Sh "\f(CWev_prepare\fP and \f(CWev_check\fP \- customise your event loop"
914.IX Subsection "ev_prepare and ev_check - customise your event loop" 1232.IX Subsection "ev_prepare and ev_check - customise your event loop"
915Prepare and check watchers are usually (but not always) used in tandem: 1233Prepare and check watchers are usually (but not always) used in tandem:
916prepare watchers get invoked before the process blocks and check watchers 1234prepare watchers get invoked before the process blocks and check watchers
917afterwards. 1235afterwards.
918.PP 1236.PP
919Their main purpose is to integrate other event mechanisms into libev. This 1237Their main purpose is to integrate other event mechanisms into libev and
920could be used, for example, to track variable changes, implement your own 1238their use is somewhat advanced. This could be used, for example, to track
921watchers, integrate net-snmp or a coroutine library and lots more. 1239variable changes, implement your own watchers, integrate net-snmp or a
1240coroutine library and lots more.
922.PP 1241.PP
923This is done by examining in each prepare call which file descriptors need 1242This is done by examining in each prepare call which file descriptors need
924to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for 1243to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for
925them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries 1244them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries
926provide just this functionality). Then, in the check watcher you check for 1245provide just this functionality). Then, in the check watcher you check for
944.IX Item "ev_check_init (ev_check *, callback)" 1263.IX Item "ev_check_init (ev_check *, callback)"
945.PD 1264.PD
946Initialises and configures the prepare or check watcher \- they have no 1265Initialises and configures the prepare or check watcher \- they have no
947parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR 1266parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR
948macros, but using them is utterly, utterly and completely pointless. 1267macros, but using them is utterly, utterly and completely pointless.
1268.PP
1269Example: *TODO*.
1270.ie n .Sh """ev_embed"" \- when one backend isn't enough"
1271.el .Sh "\f(CWev_embed\fP \- when one backend isn't enough"
1272.IX Subsection "ev_embed - when one backend isn't enough"
1273This is a rather advanced watcher type that lets you embed one event loop
1274into another (currently only \f(CW\*(C`ev_io\*(C'\fR events are supported in the embedded
1275loop, other types of watchers might be handled in a delayed or incorrect
1276fashion and must not be used).
1277.PP
1278There are primarily two reasons you would want that: work around bugs and
1279prioritise I/O.
1280.PP
1281As an example for a bug workaround, the kqueue backend might only support
1282sockets on some platform, so it is unusable as generic backend, but you
1283still want to make use of it because you have many sockets and it scales
1284so nicely. In this case, you would create a kqueue-based loop and embed it
1285into your default loop (which might use e.g. poll). Overall operation will
1286be a bit slower because first libev has to poll and then call kevent, but
1287at least you can use both at what they are best.
1288.PP
1289As for prioritising I/O: rarely you have the case where some fds have
1290to be watched and handled very quickly (with low latency), and even
1291priorities and idle watchers might have too much overhead. In this case
1292you would put all the high priority stuff in one loop and all the rest in
1293a second one, and embed the second one in the first.
1294.PP
1295As long as the watcher is active, the callback will be invoked every time
1296there might be events pending in the embedded loop. The callback must then
1297call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke
1298their callbacks (you could also start an idle watcher to give the embedded
1299loop strictly lower priority for example). You can also set the callback
1300to \f(CW0\fR, in which case the embed watcher will automatically execute the
1301embedded loop sweep.
1302.PP
1303As long as the watcher is started it will automatically handle events. The
1304callback will be invoked whenever some events have been handled. You can
1305set the callback to \f(CW0\fR to avoid having to specify one if you are not
1306interested in that.
1307.PP
1308Also, there have not currently been made special provisions for forking:
1309when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops,
1310but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers
1311yourself.
1312.PP
1313Unfortunately, not all backends are embeddable, only the ones returned by
1314\&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any
1315portable one.
1316.PP
1317So when you want to use this feature you will always have to be prepared
1318that you cannot get an embeddable loop. The recommended way to get around
1319this is to have a separate variables for your embeddable loop, try to
1320create it, and if that fails, use the normal loop for everything:
1321.PP
1322.Vb 3
1323\& struct ev_loop *loop_hi = ev_default_init (0);
1324\& struct ev_loop *loop_lo = 0;
1325\& struct ev_embed embed;
1326.Ve
1327.PP
1328.Vb 5
1329\& // see if there is a chance of getting one that works
1330\& // (remember that a flags value of 0 means autodetection)
1331\& loop_lo = ev_embeddable_backends () & ev_recommended_backends ()
1332\& ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ())
1333\& : 0;
1334.Ve
1335.PP
1336.Vb 8
1337\& // if we got one, then embed it, otherwise default to loop_hi
1338\& if (loop_lo)
1339\& {
1340\& ev_embed_init (&embed, 0, loop_lo);
1341\& ev_embed_start (loop_hi, &embed);
1342\& }
1343\& else
1344\& loop_lo = loop_hi;
1345.Ve
1346.IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4
1347.IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)"
1348.PD 0
1349.IP "ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop)" 4
1350.IX Item "ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop)"
1351.PD
1352Configures the watcher to embed the given loop, which must be
1353embeddable. If the callback is \f(CW0\fR, then \f(CW\*(C`ev_embed_sweep\*(C'\fR will be
1354invoked automatically, otherwise it is the responsibility of the callback
1355to invoke it (it will continue to be called until the sweep has been done,
1356if you do not want thta, you need to temporarily stop the embed watcher).
1357.IP "ev_embed_sweep (loop, ev_embed *)" 4
1358.IX Item "ev_embed_sweep (loop, ev_embed *)"
1359Make a single, non-blocking sweep over the embedded loop. This works
1360similarly to \f(CW\*(C`ev_loop (embedded_loop, EVLOOP_NONBLOCK)\*(C'\fR, but in the most
1361apropriate way for embedded loops.
949.SH "OTHER FUNCTIONS" 1362.SH "OTHER FUNCTIONS"
950.IX Header "OTHER FUNCTIONS" 1363.IX Header "OTHER FUNCTIONS"
951There are some other functions of possible interest. Described. Here. Now. 1364There are some other functions of possible interest. Described. Here. Now.
952.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 1365.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
953.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 1366.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
982.Ve 1395.Ve
983.Sp 1396.Sp
984.Vb 1 1397.Vb 1
985\& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 1398\& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
986.Ve 1399.Ve
987.IP "ev_feed_event (loop, watcher, int events)" 4 1400.IP "ev_feed_event (ev_loop *, watcher *, int revents)" 4
988.IX Item "ev_feed_event (loop, watcher, int events)" 1401.IX Item "ev_feed_event (ev_loop *, watcher *, int revents)"
989Feeds the given event set into the event loop, as if the specified event 1402Feeds the given event set into the event loop, as if the specified event
990had happened for the specified watcher (which must be a pointer to an 1403had happened for the specified watcher (which must be a pointer to an
991initialised but not necessarily started event watcher). 1404initialised but not necessarily started event watcher).
992.IP "ev_feed_fd_event (loop, int fd, int revents)" 4 1405.IP "ev_feed_fd_event (ev_loop *, int fd, int revents)" 4
993.IX Item "ev_feed_fd_event (loop, int fd, int revents)" 1406.IX Item "ev_feed_fd_event (ev_loop *, int fd, int revents)"
994Feed an event on the given fd, as if a file descriptor backend detected 1407Feed an event on the given fd, as if a file descriptor backend detected
995the given events it. 1408the given events it.
996.IP "ev_feed_signal_event (loop, int signum)" 4 1409.IP "ev_feed_signal_event (ev_loop *loop, int signum)" 4
997.IX Item "ev_feed_signal_event (loop, int signum)" 1410.IX Item "ev_feed_signal_event (ev_loop *loop, int signum)"
998Feed an event as if the given signal occured (loop must be the default loop!). 1411Feed an event as if the given signal occured (\f(CW\*(C`loop\*(C'\fR must be the default
1412loop!).
999.SH "LIBEVENT EMULATION" 1413.SH "LIBEVENT EMULATION"
1000.IX Header "LIBEVENT EMULATION" 1414.IX Header "LIBEVENT EMULATION"
1001Libev offers a compatibility emulation layer for libevent. It cannot 1415Libev offers a compatibility emulation layer for libevent. It cannot
1002emulate the internals of libevent, so here are some usage hints: 1416emulate the internals of libevent, so here are some usage hints:
1003.IP "* Use it by including <event.h>, as usual." 4 1417.IP "* Use it by including <event.h>, as usual." 4

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines