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

Comparing libev/ev.3 (file contents):
Revision 1.60 by root, Mon Jan 28 12:23:02 2008 UTC vs.
Revision 1.66 by root, Sun May 18 10:36:38 2008 UTC

129. ds Ae AE 129. ds Ae AE
130.\} 130.\}
131.rm #[ #] #H #V #F C 131.rm #[ #] #H #V #F C
132.\" ======================================================================== 132.\" ========================================================================
133.\" 133.\"
134.IX Title "EV 1" 134.IX Title "LIBEV 3"
135.TH EV 1 "2008-01-28" "perl v5.10.0" "User Contributed Perl Documentation" 135.TH LIBEV 3 "2008-05-11" "libev-3.33" "libev - high perfromance 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"
144.Vb 1 144.Vb 1
145\& #include <ev.h> 145\& #include <ev.h>
146.Ve 146.Ve
147.Sh "\s-1EXAMPLE\s0 \s-1PROGRAM\s0" 147.Sh "\s-1EXAMPLE\s0 \s-1PROGRAM\s0"
148.IX Subsection "EXAMPLE PROGRAM" 148.IX Subsection "EXAMPLE PROGRAM"
149.Vb 1 149.Vb 2
150\& // a single header file is required
150\& #include <ev.h> 151\& #include <ev.h>
151\& 152\&
153\& // every watcher type has its own typedef\*(Aqd struct
154\& // with the name ev_<type>
152\& ev_io stdin_watcher; 155\& ev_io stdin_watcher;
153\& ev_timer timeout_watcher; 156\& ev_timer timeout_watcher;
154\& 157\&
158\& // all watcher callbacks have a similar signature
155\& /* called when data readable on stdin */ 159\& // this callback is called when data is readable on stdin
156\& static void 160\& static void
157\& stdin_cb (EV_P_ struct ev_io *w, int revents) 161\& stdin_cb (EV_P_ struct ev_io *w, int revents)
158\& { 162\& {
159\& /* puts ("stdin ready"); */ 163\& puts ("stdin ready");
160\& ev_io_stop (EV_A_ w); /* just a syntax example */ 164\& // for one\-shot events, one must manually stop the watcher
161\& ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */ 165\& // with its corresponding stop function.
166\& ev_io_stop (EV_A_ w);
167\&
168\& // this causes all nested ev_loop\*(Aqs to stop iterating
169\& ev_unloop (EV_A_ EVUNLOOP_ALL);
162\& } 170\& }
163\& 171\&
172\& // another callback, this time for a time\-out
164\& static void 173\& static void
165\& timeout_cb (EV_P_ struct ev_timer *w, int revents) 174\& timeout_cb (EV_P_ struct ev_timer *w, int revents)
166\& { 175\& {
167\& /* puts ("timeout"); */ 176\& puts ("timeout");
168\& ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */ 177\& // this causes the innermost ev_loop to stop iterating
178\& ev_unloop (EV_A_ EVUNLOOP_ONE);
169\& } 179\& }
170\& 180\&
171\& int 181\& int
172\& main (void) 182\& main (void)
173\& { 183\& {
184\& // use the default event loop unless you have special needs
174\& struct ev_loop *loop = ev_default_loop (0); 185\& struct ev_loop *loop = ev_default_loop (0);
175\& 186\&
176\& /* initialise an io watcher, then start it */ 187\& // initialise an io watcher, then start it
188\& // this one will watch for stdin to become readable
177\& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ); 189\& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
178\& ev_io_start (loop, &stdin_watcher); 190\& ev_io_start (loop, &stdin_watcher);
179\& 191\&
192\& // initialise a timer watcher, then start it
180\& /* simple non\-repeating 5.5 second timeout */ 193\& // simple non\-repeating 5.5 second timeout
181\& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.); 194\& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
182\& ev_timer_start (loop, &timeout_watcher); 195\& ev_timer_start (loop, &timeout_watcher);
183\& 196\&
184\& /* loop till timeout or data ready */ 197\& // now wait for events to arrive
185\& ev_loop (loop, 0); 198\& ev_loop (loop, 0);
186\& 199\&
200\& // unloop was called, so exit
187\& return 0; 201\& return 0;
188\& } 202\& }
189.Ve 203.Ve
190.SH "DESCRIPTION" 204.SH "DESCRIPTION"
191.IX Header "DESCRIPTION" 205.IX Header "DESCRIPTION"
192The newest version of this document is also available as a html-formatted 206The newest version of this document is also available as an html-formatted
193web page you might find easier to navigate when reading it for the first 207web page you might find easier to navigate when reading it for the first
194time: <http://cvs.schmorp.de/libev/ev.html>. 208time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>.
195.PP 209.PP
196Libev is an event loop: you register interest in certain events (such as a 210Libev is an event loop: you register interest in certain events (such as a
197file descriptor being readable or a timeout occurring), and it will manage 211file descriptor being readable or a timeout occurring), and it will manage
198these event sources and provide your program with events. 212these event sources and provide your program with events.
199.PP 213.PP
221It also is quite fast (see this 235It also is quite fast (see this
222benchmark comparing it to libevent 236benchmark comparing it to libevent
223for example). 237for example).
224.Sh "\s-1CONVENTIONS\s0" 238.Sh "\s-1CONVENTIONS\s0"
225.IX Subsection "CONVENTIONS" 239.IX Subsection "CONVENTIONS"
226Libev is very configurable. In this manual the default configuration will 240Libev is very configurable. In this manual the default (and most common)
227be described, which supports multiple event loops. For more info about 241configuration will be described, which supports multiple event loops. For
228various configuration options please have a look at \fB\s-1EMBED\s0\fR section in 242more info about various configuration options please have a look at
229this manual. If libev was configured without support for multiple event 243\&\fB\s-1EMBED\s0\fR section in this manual. If libev was configured without support
230loops, then all functions taking an initial argument of name \f(CW\*(C`loop\*(C'\fR 244for multiple event loops, then all functions taking an initial argument of
231(which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have this argument. 245name \f(CW\*(C`loop\*(C'\fR (which is always of type \f(CW\*(C`struct ev_loop *\*(C'\fR) will not have
246this argument.
232.Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0" 247.Sh "\s-1TIME\s0 \s-1REPRESENTATION\s0"
233.IX Subsection "TIME REPRESENTATION" 248.IX Subsection "TIME REPRESENTATION"
234Libev represents time as a single floating point number, representing the 249Libev represents time as a single floating point number, representing the
235(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near 250(fractional) number of seconds since the (\s-1POSIX\s0) epoch (somewhere near
236the beginning of 1970, details are complicated, don't ask). This type is 251the beginning of 1970, details are complicated, don't ask). This type is
313.Sp 328.Sp
314See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. 329See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
315.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 330.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4
316.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" 331.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))"
317Sets the allocation function to use (the prototype is similar \- the 332Sets the allocation function to use (the prototype is similar \- the
318semantics is identical \- to the realloc C function). It is used to 333semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is
319allocate and free memory (no surprises here). If it returns zero when 334used to allocate and free memory (no surprises here). If it returns zero
320memory needs to be allocated, the library might abort or take some 335when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort
321potentially destructive action. The default is your system realloc 336or take some potentially destructive action.
322function. 337.Sp
338Since some systems (at least OpenBSD and Darwin) fail to implement
339correct \f(CW\*(C`realloc\*(C'\fR semantics, libev will use a wrapper around the system
340\&\f(CW\*(C`realloc\*(C'\fR and \f(CW\*(C`free\*(C'\fR functions by default.
323.Sp 341.Sp
324You could override this function in high-availability programs to, say, 342You could override this function in high-availability programs to, say,
325free some memory if it cannot allocate memory, to use a special allocator, 343free some memory if it cannot allocate memory, to use a special allocator,
326or even to sleep a while and retry until some memory is available. 344or even to sleep a while and retry until some memory is available.
327.Sp 345.Sp
328Example: Replace the libev allocator with one that waits a bit and then 346Example: Replace the libev allocator with one that waits a bit and then
329retries). 347retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR).
330.Sp 348.Sp
331.Vb 6 349.Vb 6
332\& static void * 350\& static void *
333\& persistent_realloc (void *ptr, size_t size) 351\& persistent_realloc (void *ptr, size_t size)
334\& { 352\& {
372.SH "FUNCTIONS CONTROLLING THE EVENT LOOP" 390.SH "FUNCTIONS CONTROLLING THE EVENT LOOP"
373.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" 391.IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP"
374An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two 392An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two
375types of such loops, the \fIdefault\fR loop, which supports signals and child 393types of such loops, the \fIdefault\fR loop, which supports signals and child
376events, and dynamically created loops which do not. 394events, and dynamically created loops which do not.
377.PP
378If you use threads, a common model is to run the default event loop
379in your main thread (or in a separate thread) and for each thread you
380create, you also create another event loop. Libev itself does no locking
381whatsoever, so if you mix calls to the same event loop in different
382threads, make sure you lock (this is usually a bad idea, though, even if
383done correctly, because it's hideous and inefficient).
384.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 395.IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4
385.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" 396.IX Item "struct ev_loop *ev_default_loop (unsigned int flags)"
386This will initialise the default event loop if it hasn't been initialised 397This will initialise the default event loop if it hasn't been initialised
387yet and return it. If the default loop could not be initialised, returns 398yet and return it. If the default loop could not be initialised, returns
388false. If it already was initialised it simply returns it (and ignores the 399false. If it already was initialised it simply returns it (and ignores the
389flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards). 400flags. If that is troubling you, check \f(CW\*(C`ev_backend ()\*(C'\fR afterwards).
390.Sp 401.Sp
391If you don't know what event loop to use, use the one returned from this 402If you don't know what event loop to use, use the one returned from this
392function. 403function.
404.Sp
405Note that this function is \fInot\fR thread-safe, so if you want to use it
406from multiple threads, you have to lock (note also that this is unlikely,
407as loops cannot bes hared easily between threads anyway).
393.Sp 408.Sp
394The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and 409The default loop is the only loop that can handle \f(CW\*(C`ev_signal\*(C'\fR and
395\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler 410\&\f(CW\*(C`ev_child\*(C'\fR watchers, and to do this, it always registers a handler
396for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either 411for \f(CW\*(C`SIGCHLD\*(C'\fR. If this is a problem for your app you can either
397create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you 412create a dynamic loop with \f(CW\*(C`ev_loop_new\*(C'\fR that doesn't do that, or you
425enabling this flag. 440enabling this flag.
426.Sp 441.Sp
427This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop, 442This works by calling \f(CW\*(C`getpid ()\*(C'\fR on every iteration of the loop,
428and thus this might slow down your event loop if you do a lot of loop 443and thus this might slow down your event loop if you do a lot of loop
429iterations and little real work, but is usually not noticeable (on my 444iterations and little real work, but is usually not noticeable (on my
430Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence 445GNU/Linux system for example, \f(CW\*(C`getpid\*(C'\fR is actually a simple 5\-insn sequence
431without a syscall and thus \fIvery\fR fast, but my Linux system also has 446without a syscall and thus \fIvery\fR fast, but my GNU/Linux system also has
432\&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster). 447\&\f(CW\*(C`pthread_atfork\*(C'\fR which is even faster).
433.Sp 448.Sp
434The big advantage of this flag is that you can forget about fork (and 449The big advantage of this flag is that you can forget about fork (and
435forget about forgetting to tell libev about forking) when you use this 450forget about forgetting to tell libev about forking) when you use this
436flag. 451flag.
467For few fds, this backend is a bit little slower than poll and select, 482For few fds, this backend is a bit little slower than poll and select,
468but it scales phenomenally better. While poll and select usually scale 483but it scales phenomenally better. While poll and select usually scale
469like O(total_fds) where n is the total number of fds (or the highest fd), 484like O(total_fds) where n is the total number of fds (or the highest fd),
470epoll scales either O(1) or O(active_fds). The epoll design has a number 485epoll scales either O(1) or O(active_fds). The epoll design has a number
471of shortcomings, such as silently dropping events in some hard-to-detect 486of shortcomings, such as silently dropping events in some hard-to-detect
472cases and rewiring a syscall per fd change, no fork support and bad 487cases and requiring a syscall per fd change, no fork support and bad
473support for dup. 488support for dup.
474.Sp 489.Sp
475While stopping, setting and starting an I/O watcher in the same iteration 490While stopping, setting and starting an I/O watcher in the same iteration
476will result in some caching, there is still a syscall per such incident 491will result in some caching, there is still a syscall per such incident
477(because the fd could point to a different file description now), so its 492(because the fd could point to a different file description now), so its
585Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is 600Similar to \f(CW\*(C`ev_default_loop\*(C'\fR, but always creates a new event loop that is
586always distinct from the default loop. Unlike the default loop, it cannot 601always distinct from the default loop. Unlike the default loop, it cannot
587handle signal and child watchers, and attempts to do so will be greeted by 602handle signal and child watchers, and attempts to do so will be greeted by
588undefined behaviour (or a failed assertion if assertions are enabled). 603undefined behaviour (or a failed assertion if assertions are enabled).
589.Sp 604.Sp
605Note that this function \fIis\fR thread-safe, and the recommended way to use
606libev with threads is indeed to create one loop per thread, and using the
607default loop in the \*(L"main\*(R" or \*(L"initial\*(R" thread.
608.Sp
590Example: Try to create a event loop that uses epoll and nothing else. 609Example: Try to create a event loop that uses epoll and nothing else.
591.Sp 610.Sp
592.Vb 3 611.Vb 3
593\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV); 612\& struct ev_loop *epoller = ev_loop_new (EVBACKEND_EPOLL | EVFLAG_NOENV);
594\& if (!epoller) 613\& if (!epoller)
639.IP "ev_loop_fork (loop)" 4 658.IP "ev_loop_fork (loop)" 4
640.IX Item "ev_loop_fork (loop)" 659.IX Item "ev_loop_fork (loop)"
641Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by 660Like \f(CW\*(C`ev_default_fork\*(C'\fR, but acts on an event loop created by
642\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop 661\&\f(CW\*(C`ev_loop_new\*(C'\fR. Yes, you have to call this on every allocated event loop
643after fork, and how you do this is entirely your own problem. 662after fork, and how you do this is entirely your own problem.
663.IP "int ev_is_default_loop (loop)" 4
664.IX Item "int ev_is_default_loop (loop)"
665Returns true when the given loop actually is the default loop, false otherwise.
644.IP "unsigned int ev_loop_count (loop)" 4 666.IP "unsigned int ev_loop_count (loop)" 4
645.IX Item "unsigned int ev_loop_count (loop)" 667.IX Item "unsigned int ev_loop_count (loop)"
646Returns the count of loop iterations for the loop, which is identical to 668Returns the count of loop iterations for the loop, which is identical to
647the number of times libev did poll for new events. It starts at \f(CW0\fR and 669the number of times libev did poll for new events. It starts at \f(CW0\fR and
648happily wraps around with enough iterations. 670happily wraps around with enough iterations.
914.ie n .IP """EV_FORK""" 4 936.ie n .IP """EV_FORK""" 4
915.el .IP "\f(CWEV_FORK\fR" 4 937.el .IP "\f(CWEV_FORK\fR" 4
916.IX Item "EV_FORK" 938.IX Item "EV_FORK"
917The event loop has been resumed in the child process after fork (see 939The event loop has been resumed in the child process after fork (see
918\&\f(CW\*(C`ev_fork\*(C'\fR). 940\&\f(CW\*(C`ev_fork\*(C'\fR).
941.ie n .IP """EV_ASYNC""" 4
942.el .IP "\f(CWEV_ASYNC\fR" 4
943.IX Item "EV_ASYNC"
944The given async watcher has been asynchronously notified (see \f(CW\*(C`ev_async\*(C'\fR).
919.ie n .IP """EV_ERROR""" 4 945.ie n .IP """EV_ERROR""" 4
920.el .IP "\f(CWEV_ERROR\fR" 4 946.el .IP "\f(CWEV_ERROR\fR" 4
921.IX Item "EV_ERROR" 947.IX Item "EV_ERROR"
922An unspecified error has occured, the watcher has been stopped. This might 948An unspecified error has occured, the watcher has been stopped. This might
923happen because the watcher could not be properly started because libev 949happen because the watcher could not be properly started because libev
1197To support fork in your programs, you either have to call 1223To support fork in your programs, you either have to call
1198\&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child, 1224\&\f(CW\*(C`ev_default_fork ()\*(C'\fR or \f(CW\*(C`ev_loop_fork ()\*(C'\fR after a fork in the child,
1199enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or 1225enable \f(CW\*(C`EVFLAG_FORKCHECK\*(C'\fR, or resort to \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR or
1200\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR. 1226\&\f(CW\*(C`EVBACKEND_POLL\*(C'\fR.
1201.PP 1227.PP
1228\fIThe special problem of \s-1SIGPIPE\s0\fR
1229.IX Subsection "The special problem of SIGPIPE"
1230.PP
1231While not really specific to libev, it is easy to forget about \s-1SIGPIPE:\s0
1232when reading from a pipe whose other end has been closed, your program
1233gets send a \s-1SIGPIPE\s0, which, by default, aborts your program. For most
1234programs this is sensible behaviour, for daemons, this is usually
1235undesirable.
1236.PP
1237So when you encounter spurious, unexplained daemon exits, make sure you
1238ignore \s-1SIGPIPE\s0 (and maybe make sure you log the exit status of your daemon
1239somewhere, as that would have given you a big clue).
1240.PP
1202\fIWatcher-Specific Functions\fR 1241\fIWatcher-Specific Functions\fR
1203.IX Subsection "Watcher-Specific Functions" 1242.IX Subsection "Watcher-Specific Functions"
1204.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4 1243.IP "ev_io_init (ev_io *, callback, int fd, int events)" 4
1205.IX Item "ev_io_init (ev_io *, callback, int fd, int events)" 1244.IX Item "ev_io_init (ev_io *, callback, int fd, int events)"
1206.PD 0 1245.PD 0
1281The timer itself will do a best-effort at avoiding drift, that is, if you 1320The timer itself will do a best-effort at avoiding drift, that is, if you
1282configure a timer to trigger every 10 seconds, then it will trigger at 1321configure a timer to trigger every 10 seconds, then it will trigger at
1283exactly 10 second intervals. If, however, your program cannot keep up with 1322exactly 10 second intervals. If, however, your program cannot keep up with
1284the timer (because it takes longer than those 10 seconds to do stuff) the 1323the timer (because it takes longer than those 10 seconds to do stuff) the
1285timer will not fire more than once per event loop iteration. 1324timer will not fire more than once per event loop iteration.
1286.IP "ev_timer_again (loop)" 4 1325.IP "ev_timer_again (loop, ev_timer *)" 4
1287.IX Item "ev_timer_again (loop)" 1326.IX Item "ev_timer_again (loop, ev_timer *)"
1288This will act as if the timer timed out and restart it again if it is 1327This will act as if the timer timed out and restart it again if it is
1289repeating. The exact semantics are: 1328repeating. The exact semantics are:
1290.Sp 1329.Sp
1291If the timer is pending, its pending status is cleared. 1330If the timer is pending, its pending status is cleared.
1292.Sp 1331.Sp
1402In this configuration the watcher triggers an event at the wallclock time 1441In this configuration the watcher triggers an event at the wallclock time
1403\&\f(CW\*(C`at\*(C'\fR and doesn't repeat. It will not adjust when a time jump occurs, 1442\&\f(CW\*(C`at\*(C'\fR and doesn't repeat. It will not adjust when a time jump occurs,
1404that is, if it is to be run at January 1st 2011 then it will run when the 1443that is, if it is to be run at January 1st 2011 then it will run when the
1405system time reaches or surpasses this time. 1444system time reaches or surpasses this time.
1406.IP "\(bu" 4 1445.IP "\(bu" 4
1407non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0) 1446repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)
1408.Sp 1447.Sp
1409In this mode the watcher will always be scheduled to time out at the next 1448In this mode the watcher will always be scheduled to time out at the next
1410\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative) 1449\&\f(CW\*(C`at + N * interval\*(C'\fR time (for some integer N, which can also be negative)
1411and then repeat, regardless of any time jumps. 1450and then repeat, regardless of any time jumps.
1412.Sp 1451.Sp
1472.IX Item "ev_periodic_again (loop, ev_periodic *)" 1511.IX Item "ev_periodic_again (loop, ev_periodic *)"
1473Simply stops and restarts the periodic watcher again. This is only useful 1512Simply stops and restarts the periodic watcher again. This is only useful
1474when you changed some parameters or the reschedule callback would return 1513when you changed some parameters or the reschedule callback would return
1475a different time than the last time it was called (e.g. in a crond like 1514a different time than the last time it was called (e.g. in a crond like
1476program when the crontabs have changed). 1515program when the crontabs have changed).
1516.IP "ev_tstamp ev_periodic_at (ev_periodic *)" 4
1517.IX Item "ev_tstamp ev_periodic_at (ev_periodic *)"
1518When active, returns the absolute time that the watcher is supposed to
1519trigger next.
1477.IP "ev_tstamp offset [read\-write]" 4 1520.IP "ev_tstamp offset [read\-write]" 4
1478.IX Item "ev_tstamp offset [read-write]" 1521.IX Item "ev_tstamp offset [read-write]"
1479When repeating, this contains the offset value, otherwise this is the 1522When repeating, this contains the offset value, otherwise this is the
1480absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). 1523absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR).
1481.Sp 1524.Sp
1489.IP "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read\-write]" 4 1532.IP "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read\-write]" 4
1490.IX Item "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write]" 1533.IX Item "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write]"
1491The current reschedule callback, or \f(CW0\fR, if this functionality is 1534The current reschedule callback, or \f(CW0\fR, if this functionality is
1492switched off. Can be changed any time, but changes only take effect when 1535switched off. Can be changed any time, but changes only take effect when
1493the periodic timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. 1536the periodic timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called.
1494.IP "ev_tstamp at [read\-only]" 4
1495.IX Item "ev_tstamp at [read-only]"
1496When active, contains the absolute time that the watcher is supposed to
1497trigger next.
1498.PP 1537.PP
1499\fIExamples\fR 1538\fIExamples\fR
1500.IX Subsection "Examples" 1539.IX Subsection "Examples"
1501.PP 1540.PP
1502Example: Call a callback every hour, or, more precisely, whenever the 1541Example: Call a callback every hour, or, more precisely, whenever the
1549first watcher gets started will libev actually register a signal watcher 1588first watcher gets started will libev actually register a signal watcher
1550with the kernel (thus it coexists with your own signal handlers as long 1589with the kernel (thus it coexists with your own signal handlers as long
1551as you don't register any with libev). Similarly, when the last signal 1590as you don't register any with libev). Similarly, when the last signal
1552watcher for a signal is stopped libev will reset the signal handler to 1591watcher for a signal is stopped libev will reset the signal handler to
1553\&\s-1SIG_DFL\s0 (regardless of what it was set to before). 1592\&\s-1SIG_DFL\s0 (regardless of what it was set to before).
1593.PP
1594If possible and supported, libev will install its handlers with
1595\&\f(CW\*(C`SA_RESTART\*(C'\fR behaviour enabled, so syscalls should not be unduly
1596interrupted. If you have a problem with syscalls getting interrupted by
1597signals you can block all signals in an \f(CW\*(C`ev_check\*(C'\fR watcher and unblock
1598them in an \f(CW\*(C`ev_prepare\*(C'\fR watcher.
1554.PP 1599.PP
1555\fIWatcher-Specific Functions and Data Members\fR 1600\fIWatcher-Specific Functions and Data Members\fR
1556.IX Subsection "Watcher-Specific Functions and Data Members" 1601.IX Subsection "Watcher-Specific Functions and Data Members"
1557.IP "ev_signal_init (ev_signal *, callback, int signum)" 4 1602.IP "ev_signal_init (ev_signal *, callback, int signum)" 4
1558.IX Item "ev_signal_init (ev_signal *, callback, int signum)" 1603.IX Item "ev_signal_init (ev_signal *, callback, int signum)"
1563Configures the watcher to trigger on the given signal number (usually one 1608Configures the watcher to trigger on the given signal number (usually one
1564of the \f(CW\*(C`SIGxxx\*(C'\fR constants). 1609of the \f(CW\*(C`SIGxxx\*(C'\fR constants).
1565.IP "int signum [read\-only]" 4 1610.IP "int signum [read\-only]" 4
1566.IX Item "int signum [read-only]" 1611.IX Item "int signum [read-only]"
1567The signal the watcher watches out for. 1612The signal the watcher watches out for.
1613.PP
1614\fIExamples\fR
1615.IX Subsection "Examples"
1616.PP
1617Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0.
1618.PP
1619.Vb 5
1620\& static void
1621\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents)
1622\& {
1623\& ev_unloop (loop, EVUNLOOP_ALL);
1624\& }
1625\&
1626\& struct ev_signal signal_watcher;
1627\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT);
1628\& ev_signal_start (loop, &sigint_cb);
1629.Ve
1568.ie n .Sh """ev_child"" \- watch out for process status changes" 1630.ie n .Sh """ev_child"" \- watch out for process status changes"
1569.el .Sh "\f(CWev_child\fP \- watch out for process status changes" 1631.el .Sh "\f(CWev_child\fP \- watch out for process status changes"
1570.IX Subsection "ev_child - watch out for process status changes" 1632.IX Subsection "ev_child - watch out for process status changes"
1571Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to 1633Child watchers trigger when your process receives a \s-1SIGCHLD\s0 in response to
1572some child status changes (most typically when a child of yours dies). 1634some child status changes (most typically when a child of yours dies). It
1635is permissible to install a child watcher \fIafter\fR the child has been
1636forked (which implies it might have already exited), as long as the event
1637loop isn't entered (or is continued from a watcher).
1638.PP
1639Only the default event loop is capable of handling signals, and therefore
1640you can only rgeister child watchers in the default event loop.
1641.PP
1642\fIProcess Interaction\fR
1643.IX Subsection "Process Interaction"
1644.PP
1645Libev grabs \f(CW\*(C`SIGCHLD\*(C'\fR as soon as the default event loop is
1646initialised. This is necessary to guarantee proper behaviour even if
1647the first child watcher is started after the child exits. The occurance
1648of \f(CW\*(C`SIGCHLD\*(C'\fR is recorded asynchronously, but child reaping is done
1649synchronously as part of the event loop processing. Libev always reaps all
1650children, even ones not watched.
1651.PP
1652\fIOverriding the Built-In Processing\fR
1653.IX Subsection "Overriding the Built-In Processing"
1654.PP
1655Libev offers no special support for overriding the built-in child
1656processing, but if your application collides with libev's default child
1657handler, you can override it easily by installing your own handler for
1658\&\f(CW\*(C`SIGCHLD\*(C'\fR after initialising the default loop, and making sure the
1659default loop never gets destroyed. You are encouraged, however, to use an
1660event-based approach to child reaping and thus use libev's support for
1661that, so other libev users can use \f(CW\*(C`ev_child\*(C'\fR watchers freely.
1573.PP 1662.PP
1574\fIWatcher-Specific Functions and Data Members\fR 1663\fIWatcher-Specific Functions and Data Members\fR
1575.IX Subsection "Watcher-Specific Functions and Data Members" 1664.IX Subsection "Watcher-Specific Functions and Data Members"
1576.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4 1665.IP "ev_child_init (ev_child *, callback, int pid, int trace)" 4
1577.IX Item "ev_child_init (ev_child *, callback, int pid, int trace)" 1666.IX Item "ev_child_init (ev_child *, callback, int pid, int trace)"
1599\&\f(CW\*(C`waitpid\*(C'\fR and \f(CW\*(C`sys/wait.h\*(C'\fR documentation for details). 1688\&\f(CW\*(C`waitpid\*(C'\fR and \f(CW\*(C`sys/wait.h\*(C'\fR documentation for details).
1600.PP 1689.PP
1601\fIExamples\fR 1690\fIExamples\fR
1602.IX Subsection "Examples" 1691.IX Subsection "Examples"
1603.PP 1692.PP
1604Example: Try to exit cleanly on \s-1SIGINT\s0 and \s-1SIGTERM\s0. 1693Example: \f(CW\*(C`fork()\*(C'\fR a new process and install a child handler to wait for
1694its completion.
1605.PP 1695.PP
1606.Vb 5 1696.Vb 1
1697\& ev_child cw;
1698\&
1607\& static void 1699\& static void
1608\& sigint_cb (struct ev_loop *loop, struct ev_signal *w, int revents) 1700\& child_cb (EV_P_ struct ev_child *w, int revents)
1609\& { 1701\& {
1610\& ev_unloop (loop, EVUNLOOP_ALL); 1702\& ev_child_stop (EV_A_ w);
1703\& printf ("process %d exited with status %x\en", w\->rpid, w\->rstatus);
1611\& } 1704\& }
1612\& 1705\&
1613\& struct ev_signal signal_watcher; 1706\& pid_t pid = fork ();
1614\& ev_signal_init (&signal_watcher, sigint_cb, SIGINT); 1707\&
1615\& ev_signal_start (loop, &sigint_cb); 1708\& if (pid < 0)
1709\& // error
1710\& else if (pid == 0)
1711\& {
1712\& // the forked child executes here
1713\& exit (1);
1714\& }
1715\& else
1716\& {
1717\& ev_child_init (&cw, child_cb, pid, 0);
1718\& ev_child_start (EV_DEFAULT_ &cw);
1719\& }
1616.Ve 1720.Ve
1617.ie n .Sh """ev_stat"" \- did the file attributes just change?" 1721.ie n .Sh """ev_stat"" \- did the file attributes just change?"
1618.el .Sh "\f(CWev_stat\fP \- did the file attributes just change?" 1722.el .Sh "\f(CWev_stat\fP \- did the file attributes just change?"
1619.IX Subsection "ev_stat - did the file attributes just change?" 1723.IX Subsection "ev_stat - did the file attributes just change?"
1620This watches a filesystem path for attribute changes. That is, it calls 1724This watches a filesystem path for attribute changes. That is, it calls
1643as even with OS-supported change notifications, this can be 1747as even with OS-supported change notifications, this can be
1644resource-intensive. 1748resource-intensive.
1645.PP 1749.PP
1646At the time of this writing, only the Linux inotify interface is 1750At the time of this writing, only the Linux inotify interface is
1647implemented (implementing kqueue support is left as an exercise for the 1751implemented (implementing kqueue support is left as an exercise for the
1752reader, note, however, that the author sees no way of implementing ev_stat
1648reader). Inotify will be used to give hints only and should not change the 1753semantics with kqueue). Inotify will be used to give hints only and should
1649semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs 1754not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev
1650to fall back to regular polling again even with inotify, but changes are 1755sometimes needs to fall back to regular polling again even with inotify,
1651usually detected immediately, and if the file exists there will be no 1756but changes are usually detected immediately, and if the file exists there
1652polling. 1757will be no polling.
1758.PP
1759\fI\s-1ABI\s0 Issues (Largefile Support)\fR
1760.IX Subsection "ABI Issues (Largefile Support)"
1761.PP
1762Libev by default (unless the user overrides this) uses the default
1763compilation environment, which means that on systems with optionally
1764disabled large file support, you get the 32 bit version of the stat
1765structure. When using the library from programs that change the \s-1ABI\s0 to
1766use 64 bit file offsets the programs will fail. In that case you have to
1767compile libev with the same flags to get binary compatibility. This is
1768obviously the case with any flags that change the \s-1ABI\s0, but the problem is
1769most noticably with ev_stat and largefile support.
1653.PP 1770.PP
1654\fIInotify\fR 1771\fIInotify\fR
1655.IX Subsection "Inotify" 1772.IX Subsection "Inotify"
1656.PP 1773.PP
1657When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only 1774When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only
1658available on Linux) and present at runtime, it will be used to speed up 1775available on Linux) and present at runtime, it will be used to speed up
1659change detection where possible. The inotify descriptor will be created lazily 1776change detection where possible. The inotify descriptor will be created lazily
1660when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. 1777when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started.
1661.PP 1778.PP
1662Inotify presense does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers 1779Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers
1663except that changes might be detected earlier, and in some cases, to avoid 1780except that changes might be detected earlier, and in some cases, to avoid
1664making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presense of inotify support 1781making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support
1665there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. 1782there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling.
1666.PP 1783.PP
1667(There is no support for kqueue, as apparently it cannot be used to 1784(There is no support for kqueue, as apparently it cannot be used to
1668implement this functionality, due to the requirement of having a file 1785implement this functionality, due to the requirement of having a file
1669descriptor open on the object at all times). 1786descriptor open on the object at all times).
1673.PP 1790.PP
1674The \f(CW\*(C`stat ()\*(C'\fR syscall only supports full-second resolution portably, and 1791The \f(CW\*(C`stat ()\*(C'\fR syscall only supports full-second resolution portably, and
1675even on systems where the resolution is higher, many filesystems still 1792even on systems where the resolution is higher, many filesystems still
1676only support whole seconds. 1793only support whole seconds.
1677.PP 1794.PP
1678That means that, if the time is the only thing that changes, you might 1795That means that, if the time is the only thing that changes, you can
1679miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and calls 1796easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and
1680your callback, which does something. When there is another update within 1797calls your callback, which does something. When there is another update
1681the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it. 1798within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat
1799data does not change.
1682.PP 1800.PP
1683The solution to this is to delay acting on a change for a second (or till 1801The solution to this is to delay acting on a change for slightly more
1684the next second boundary), using a roughly one-second delay \f(CW\*(C`ev_timer\*(C'\fR 1802than second (or till slightly after the next full second boundary), using
1685(\f(CW\*(C`ev_timer_set (w, 0., 1.01); ev_timer_again (loop, w)\*(C'\fR). The \f(CW.01\fR 1803a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02);
1686is added to work around small timing inconsistencies of some operating 1804ev_timer_again (loop, w)\*(C'\fR).
1687systems. 1805.PP
1806The \f(CW.02\fR offset is added to work around small timing inconsistencies
1807of some operating systems (where the second counter of the current time
1808might be be delayed. One such system is the Linux kernel, where a call to
1809\&\f(CW\*(C`gettimeofday\*(C'\fR might return a timestamp with a full second later than
1810a subsequent \f(CW\*(C`time\*(C'\fR call \- if the equivalent of \f(CW\*(C`time ()\*(C'\fR is used to
1811update file times then there will be a small window where the kernel uses
1812the previous second to update file times but libev might already execute
1813the timer callback).
1688.PP 1814.PP
1689\fIWatcher-Specific Functions and Data Members\fR 1815\fIWatcher-Specific Functions and Data Members\fR
1690.IX Subsection "Watcher-Specific Functions and Data Members" 1816.IX Subsection "Watcher-Specific Functions and Data Members"
1691.IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4 1817.IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4
1692.IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 1818.IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)"
1698\&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to 1824\&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to
1699be detected and should normally be specified as \f(CW0\fR to let libev choose 1825be detected and should normally be specified as \f(CW0\fR to let libev choose
1700a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same 1826a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same
1701path for as long as the watcher is active. 1827path for as long as the watcher is active.
1702.Sp 1828.Sp
1703The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, 1829The callback will receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, relative
1704relative to the attributes at the time the watcher was started (or the 1830to the attributes at the time the watcher was started (or the last change
1705last change was detected). 1831was detected).
1706.IP "ev_stat_stat (ev_stat *)" 4 1832.IP "ev_stat_stat (loop, ev_stat *)" 4
1707.IX Item "ev_stat_stat (ev_stat *)" 1833.IX Item "ev_stat_stat (loop, ev_stat *)"
1708Updates the stat buffer immediately with new values. If you change the 1834Updates the stat buffer immediately with new values. If you change the
1709watched path in your callback, you could call this fucntion to avoid 1835watched path in your callback, you could call this function to avoid
1710detecting this change (while introducing a race condition). Can also be 1836detecting this change (while introducing a race condition if you are not
1711useful simply to find out the new values. 1837the only one changing the path). Can also be useful simply to find out the
1838new values.
1712.IP "ev_statdata attr [read\-only]" 4 1839.IP "ev_statdata attr [read\-only]" 4
1713.IX Item "ev_statdata attr [read-only]" 1840.IX Item "ev_statdata attr [read-only]"
1714The most-recently detected attributes of the file. Although the type is of 1841The most-recently detected attributes of the file. Although the type is
1715\&\f(CW\*(C`ev_statdata\*(C'\fR, this is usually the (or one of the) \f(CW\*(C`struct stat\*(C'\fR types 1842\&\f(CW\*(C`ev_statdata\*(C'\fR, this is usually the (or one of the) \f(CW\*(C`struct stat\*(C'\fR types
1843suitable for your system, but you can only rely on the POSIX-standardised
1716suitable for your system. If the \f(CW\*(C`st_nlink\*(C'\fR member is \f(CW0\fR, then there 1844members to be present. If the \f(CW\*(C`st_nlink\*(C'\fR member is \f(CW0\fR, then there was
1717was some error while \f(CW\*(C`stat\*(C'\fRing the file. 1845some error while \f(CW\*(C`stat\*(C'\fRing the file.
1718.IP "ev_statdata prev [read\-only]" 4 1846.IP "ev_statdata prev [read\-only]" 4
1719.IX Item "ev_statdata prev [read-only]" 1847.IX Item "ev_statdata prev [read-only]"
1720The previous attributes of the file. The callback gets invoked whenever 1848The previous attributes of the file. The callback gets invoked whenever
1721\&\f(CW\*(C`prev\*(C'\fR != \f(CW\*(C`attr\*(C'\fR. 1849\&\f(CW\*(C`prev\*(C'\fR != \f(CW\*(C`attr\*(C'\fR, or, more precisely, one or more of these members
1850differ: \f(CW\*(C`st_dev\*(C'\fR, \f(CW\*(C`st_ino\*(C'\fR, \f(CW\*(C`st_mode\*(C'\fR, \f(CW\*(C`st_nlink\*(C'\fR, \f(CW\*(C`st_uid\*(C'\fR,
1851\&\f(CW\*(C`st_gid\*(C'\fR, \f(CW\*(C`st_rdev\*(C'\fR, \f(CW\*(C`st_size\*(C'\fR, \f(CW\*(C`st_atime\*(C'\fR, \f(CW\*(C`st_mtime\*(C'\fR, \f(CW\*(C`st_ctime\*(C'\fR.
1722.IP "ev_tstamp interval [read\-only]" 4 1852.IP "ev_tstamp interval [read\-only]" 4
1723.IX Item "ev_tstamp interval [read-only]" 1853.IX Item "ev_tstamp interval [read-only]"
1724The specified interval. 1854The specified interval.
1725.IP "const char *path [read\-only]" 4 1855.IP "const char *path [read\-only]" 4
1726.IX Item "const char *path [read-only]" 1856.IX Item "const char *path [read-only]"
1780\& } 1910\& }
1781\& 1911\&
1782\& ... 1912\& ...
1783\& ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); 1913\& ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.);
1784\& ev_stat_start (loop, &passwd); 1914\& ev_stat_start (loop, &passwd);
1785\& ev_timer_init (&timer, timer_cb, 0., 1.01); 1915\& ev_timer_init (&timer, timer_cb, 0., 1.02);
1786.Ve 1916.Ve
1787.ie n .Sh """ev_idle"" \- when you've got nothing better to do..." 1917.ie n .Sh """ev_idle"" \- when you've got nothing better to do..."
1788.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." 1918.el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..."
1789.IX Subsection "ev_idle - when you've got nothing better to do..." 1919.IX Subsection "ev_idle - when you've got nothing better to do..."
1790Idle watchers trigger events when no other events of the same or higher 1920Idle watchers trigger events when no other events of the same or higher
1876.PP 2006.PP
1877It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) 2007It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR)
1878priority, to ensure that they are being run before any other watchers 2008priority, to ensure that they are being run before any other watchers
1879after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, 2009after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers,
1880too) should not activate (\*(L"feed\*(R") events into libev. While libev fully 2010too) should not activate (\*(L"feed\*(R") events into libev. While libev fully
1881supports this, they will be called before other \f(CW\*(C`ev_check\*(C'\fR watchers 2011supports this, they might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers
1882did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other 2012did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other
1883(non-libev) event loops those other event loops might be in an unusable 2013(non-libev) event loops those other event loops might be in an unusable
1884state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to 2014state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to
1885coexist peacefully with others). 2015coexist peacefully with others).
1886.PP 2016.PP
1900.IX Subsection "Examples" 2030.IX Subsection "Examples"
1901.PP 2031.PP
1902There are a number of principal ways to embed other event loops or modules 2032There are a number of principal ways to embed other event loops or modules
1903into libev. Here are some ideas on how to include libadns into libev 2033into libev. Here are some ideas on how to include libadns into libev
1904(there is a Perl module named \f(CW\*(C`EV::ADNS\*(C'\fR that does this, which you could 2034(there is a Perl module named \f(CW\*(C`EV::ADNS\*(C'\fR that does this, which you could
1905use for an actually working example. Another Perl module named \f(CW\*(C`EV::Glib\*(C'\fR 2035use as a working example. Another Perl module named \f(CW\*(C`EV::Glib\*(C'\fR embeds a
1906embeds a Glib main context into libev, and finally, \f(CW\*(C`Glib::EV\*(C'\fR embeds \s-1EV\s0 2036Glib main context into libev, and finally, \f(CW\*(C`Glib::EV\*(C'\fR embeds \s-1EV\s0 into the
1907into the Glib event loop). 2037Glib event loop).
1908.PP 2038.PP
1909Method 1: Add \s-1IO\s0 watchers and a timeout watcher in a prepare handler, 2039Method 1: Add \s-1IO\s0 watchers and a timeout watcher in a prepare handler,
1910and in a check watcher, destroy them and call into libadns. What follows 2040and in a check watcher, destroy them and call into libadns. What follows
1911is pseudo-code only of course. This requires you to either use a low 2041is pseudo-code only of course. This requires you to either use a low
1912priority for the check watcher or use \f(CW\*(C`ev_clear_pending\*(C'\fR explicitly, as 2042priority for the check watcher or use \f(CW\*(C`ev_clear_pending\*(C'\fR explicitly, as
2173.IP "ev_fork_init (ev_signal *, callback)" 4 2303.IP "ev_fork_init (ev_signal *, callback)" 4
2174.IX Item "ev_fork_init (ev_signal *, callback)" 2304.IX Item "ev_fork_init (ev_signal *, callback)"
2175Initialises and configures the fork watcher \- it has no parameters of any 2305Initialises and configures the fork watcher \- it has no parameters of any
2176kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless, 2306kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless,
2177believe me. 2307believe me.
2308.ie n .Sh """ev_async"" \- how to wake up another event loop"
2309.el .Sh "\f(CWev_async\fP \- how to wake up another event loop"
2310.IX Subsection "ev_async - how to wake up another event loop"
2311In general, you cannot use an \f(CW\*(C`ev_loop\*(C'\fR from multiple threads or other
2312asynchronous sources such as signal handlers (as opposed to multiple event
2313loops \- those are of course safe to use in different threads).
2314.PP
2315Sometimes, however, you need to wake up another event loop you do not
2316control, for example because it belongs to another thread. This is what
2317\&\f(CW\*(C`ev_async\*(C'\fR watchers do: as long as the \f(CW\*(C`ev_async\*(C'\fR watcher is active, you
2318can signal it by calling \f(CW\*(C`ev_async_send\*(C'\fR, which is thread\- and signal
2319safe.
2320.PP
2321This functionality is very similar to \f(CW\*(C`ev_signal\*(C'\fR watchers, as signals,
2322too, are asynchronous in nature, and signals, too, will be compressed
2323(i.e. the number of callback invocations may be less than the number of
2324\&\f(CW\*(C`ev_async_sent\*(C'\fR calls).
2325.PP
2326Unlike \f(CW\*(C`ev_signal\*(C'\fR watchers, \f(CW\*(C`ev_async\*(C'\fR works with any event loop, not
2327just the default loop.
2328.PP
2329\fIQueueing\fR
2330.IX Subsection "Queueing"
2331.PP
2332\&\f(CW\*(C`ev_async\*(C'\fR does not support queueing of data in any way. The reason
2333is that the author does not know of a simple (or any) algorithm for a
2334multiple-writer-single-reader queue that works in all cases and doesn't
2335need elaborate support such as pthreads.
2336.PP
2337That means that if you want to queue data, you have to provide your own
2338queue. But at least I can tell you would implement locking around your
2339queue:
2340.IP "queueing from a signal handler context" 4
2341.IX Item "queueing from a signal handler context"
2342To implement race-free queueing, you simply add to the queue in the signal
2343handler but you block the signal handler in the watcher callback. Here is an example that does that for
2344some fictitiuous \s-1SIGUSR1\s0 handler:
2345.Sp
2346.Vb 1
2347\& static ev_async mysig;
2348\&
2349\& static void
2350\& sigusr1_handler (void)
2351\& {
2352\& sometype data;
2353\&
2354\& // no locking etc.
2355\& queue_put (data);
2356\& ev_async_send (EV_DEFAULT_ &mysig);
2357\& }
2358\&
2359\& static void
2360\& mysig_cb (EV_P_ ev_async *w, int revents)
2361\& {
2362\& sometype data;
2363\& sigset_t block, prev;
2364\&
2365\& sigemptyset (&block);
2366\& sigaddset (&block, SIGUSR1);
2367\& sigprocmask (SIG_BLOCK, &block, &prev);
2368\&
2369\& while (queue_get (&data))
2370\& process (data);
2371\&
2372\& if (sigismember (&prev, SIGUSR1)
2373\& sigprocmask (SIG_UNBLOCK, &block, 0);
2374\& }
2375.Ve
2376.Sp
2377(Note: pthreads in theory requires you to use \f(CW\*(C`pthread_setmask\*(C'\fR
2378instead of \f(CW\*(C`sigprocmask\*(C'\fR when you use threads, but libev doesn't do it
2379either...).
2380.IP "queueing from a thread context" 4
2381.IX Item "queueing from a thread context"
2382The strategy for threads is different, as you cannot (easily) block
2383threads but you can easily preempt them, so to queue safely you need to
2384employ a traditional mutex lock, such as in this pthread example:
2385.Sp
2386.Vb 2
2387\& static ev_async mysig;
2388\& static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
2389\&
2390\& static void
2391\& otherthread (void)
2392\& {
2393\& // only need to lock the actual queueing operation
2394\& pthread_mutex_lock (&mymutex);
2395\& queue_put (data);
2396\& pthread_mutex_unlock (&mymutex);
2397\&
2398\& ev_async_send (EV_DEFAULT_ &mysig);
2399\& }
2400\&
2401\& static void
2402\& mysig_cb (EV_P_ ev_async *w, int revents)
2403\& {
2404\& pthread_mutex_lock (&mymutex);
2405\&
2406\& while (queue_get (&data))
2407\& process (data);
2408\&
2409\& pthread_mutex_unlock (&mymutex);
2410\& }
2411.Ve
2412.PP
2413\fIWatcher-Specific Functions and Data Members\fR
2414.IX Subsection "Watcher-Specific Functions and Data Members"
2415.IP "ev_async_init (ev_async *, callback)" 4
2416.IX Item "ev_async_init (ev_async *, callback)"
2417Initialises and configures the async watcher \- it has no parameters of any
2418kind. There is a \f(CW\*(C`ev_asynd_set\*(C'\fR macro, but using it is utterly pointless,
2419believe me.
2420.IP "ev_async_send (loop, ev_async *)" 4
2421.IX Item "ev_async_send (loop, ev_async *)"
2422Sends/signals/activates the given \f(CW\*(C`ev_async\*(C'\fR watcher, that is, feeds
2423an \f(CW\*(C`EV_ASYNC\*(C'\fR event on the watcher into the event loop. Unlike
2424\&\f(CW\*(C`ev_feed_event\*(C'\fR, this call is safe to do in other threads, signal or
2425similar contexts (see the dicusssion of \f(CW\*(C`EV_ATOMIC_T\*(C'\fR in the embedding
2426section below on what exactly this means).
2427.Sp
2428This call incurs the overhead of a syscall only once per loop iteration,
2429so while the overhead might be noticable, it doesn't apply to repeated
2430calls to \f(CW\*(C`ev_async_send\*(C'\fR.
2431.IP "bool = ev_async_pending (ev_async *)" 4
2432.IX Item "bool = ev_async_pending (ev_async *)"
2433Returns a non-zero value when \f(CW\*(C`ev_async_send\*(C'\fR has been called on the
2434watcher but the event has not yet been processed (or even noted) by the
2435event loop.
2436.Sp
2437\&\f(CW\*(C`ev_async_send\*(C'\fR sets a flag in the watcher and wakes up the loop. When
2438the loop iterates next and checks for the watcher to have become active,
2439it will reset the flag again. \f(CW\*(C`ev_async_pending\*(C'\fR can be used to very
2440quickly check wether invoking the loop might be a good idea.
2441.Sp
2442Not that this does \fInot\fR check wether the watcher itself is pending, only
2443wether it has been requested to make this watcher pending.
2178.SH "OTHER FUNCTIONS" 2444.SH "OTHER FUNCTIONS"
2179.IX Header "OTHER FUNCTIONS" 2445.IX Header "OTHER FUNCTIONS"
2180There are some other functions of possible interest. Described. Here. Now. 2446There are some other functions of possible interest. Described. Here. Now.
2181.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 2447.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
2182.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 2448.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
2239it a private \s-1API\s0). 2505it a private \s-1API\s0).
2240.IP "\(bu" 4 2506.IP "\(bu" 4
2241Priorities are not currently supported. Initialising priorities 2507Priorities are not currently supported. Initialising priorities
2242will fail and all watchers will have the same priority, even though there 2508will fail and all watchers will have the same priority, even though there
2243is an ev_pri field. 2509is an ev_pri field.
2510.IP "\(bu" 4
2511In libevent, the last base created gets the signals, in libev, the
2512first base created (== the default loop) gets the signals.
2244.IP "\(bu" 4 2513.IP "\(bu" 4
2245Other members are not supported. 2514Other members are not supported.
2246.IP "\(bu" 4 2515.IP "\(bu" 4
2247The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need 2516The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need
2248to use the libev header file and library. 2517to use the libev header file and library.
2406\& 2675\&
2407\& io.start (fd, ev::READ); 2676\& io.start (fd, ev::READ);
2408\& } 2677\& }
2409\& }; 2678\& };
2410.Ve 2679.Ve
2680.SH "OTHER LANGUAGE BINDINGS"
2681.IX Header "OTHER LANGUAGE BINDINGS"
2682Libev does not offer other language bindings itself, but bindings for a
2683numbe rof languages exist in the form of third-party packages. If you know
2684any interesting language binding in addition to the ones listed here, drop
2685me a note.
2686.IP "Perl" 4
2687.IX Item "Perl"
2688The \s-1EV\s0 module implements the full libev \s-1API\s0 and is actually used to test
2689libev. \s-1EV\s0 is developed together with libev. Apart from the \s-1EV\s0 core module,
2690there are additional modules that implement libev-compatible interfaces
2691to \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
2692\&\f(CW\*(C`libglib\*(C'\fR event core (\f(CW\*(C`Glib::EV\*(C'\fR and \f(CW\*(C`EV::Glib\*(C'\fR).
2693.Sp
2694It can be found and installed via \s-1CPAN\s0, its homepage is found at
2695<http://software.schmorp.de/pkg/EV>.
2696.IP "Ruby" 4
2697.IX Item "Ruby"
2698Tony Arcieri has written a ruby extension that offers access to a subset
2699of the libev \s-1API\s0 and adds filehandle abstractions, asynchronous \s-1DNS\s0 and
2700more on top of it. It can be found via gem servers. Its homepage is at
2701<http://rev.rubyforge.org/>.
2702.IP "D" 4
2703.IX Item "D"
2704Leandro Lucarella has written a D language binding (\fIev.d\fR) for libev, to
2705be found at <http://git.llucax.com.ar/?p=software/ev.d.git;a=summary>.
2411.SH "MACRO MAGIC" 2706.SH "MACRO MAGIC"
2412.IX Header "MACRO MAGIC" 2707.IX Header "MACRO MAGIC"
2413Libev can be compiled with a variety of options, the most fundamantal 2708Libev can be compiled with a variety of options, the most fundamantal
2414of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most) 2709of which is \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines whether (most)
2415functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument. 2710functions and callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument.
2451.ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4 2746.ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4
2452.el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4 2747.el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4
2453.IX Item "EV_DEFAULT, EV_DEFAULT_" 2748.IX Item "EV_DEFAULT, EV_DEFAULT_"
2454Similar to the other two macros, this gives you the value of the default 2749Similar to the other two macros, this gives you the value of the default
2455loop, if multiple loops are supported (\*(L"ev loop default\*(R"). 2750loop, if multiple loops are supported (\*(L"ev loop default\*(R").
2751.ie n .IP """EV_DEFAULT_UC""\fR, \f(CW""EV_DEFAULT_UC_""" 4
2752.el .IP "\f(CWEV_DEFAULT_UC\fR, \f(CWEV_DEFAULT_UC_\fR" 4
2753.IX Item "EV_DEFAULT_UC, EV_DEFAULT_UC_"
2754Usage identical to \f(CW\*(C`EV_DEFAULT\*(C'\fR and \f(CW\*(C`EV_DEFAULT_\*(C'\fR, but requires that the
2755default loop has been initialised (\f(CW\*(C`UC\*(C'\fR == unchecked). Their behaviour
2756is undefined when the default loop has not been initialised by a previous
2757execution of \f(CW\*(C`EV_DEFAULT\*(C'\fR, \f(CW\*(C`EV_DEFAULT_\*(C'\fR or \f(CW\*(C`ev_default_init (...)\*(C'\fR.
2758.Sp
2759It is often prudent to use \f(CW\*(C`EV_DEFAULT\*(C'\fR when initialising the first
2760watcher in a function but use \f(CW\*(C`EV_DEFAULT_UC\*(C'\fR afterwards.
2456.PP 2761.PP
2457Example: Declare and initialise a check watcher, utilising the above 2762Example: Declare and initialise a check watcher, utilising the above
2458macros so it will work regardless of whether multiple loops are supported 2763macros so it will work regardless of whether multiple loops are supported
2459or not. 2764or not.
2460.PP 2765.PP
2570.Vb 1 2875.Vb 1
2571\& libev.m4 2876\& libev.m4
2572.Ve 2877.Ve
2573.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" 2878.Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0"
2574.IX Subsection "PREPROCESSOR SYMBOLS/MACROS" 2879.IX Subsection "PREPROCESSOR SYMBOLS/MACROS"
2575Libev can be configured via a variety of preprocessor symbols you have to define 2880Libev can be configured via a variety of preprocessor symbols you have to
2576before including any of its files. The default is not to build for multiplicity 2881define before including any of its files. The default in the absense of
2577and only include the select backend. 2882autoconf is noted for every option.
2578.IP "\s-1EV_STANDALONE\s0" 4 2883.IP "\s-1EV_STANDALONE\s0" 4
2579.IX Item "EV_STANDALONE" 2884.IX Item "EV_STANDALONE"
2580Must always be \f(CW1\fR if you do not use autoconf configuration, which 2885Must always be \f(CW1\fR if you do not use autoconf configuration, which
2581keeps libev from including \fIconfig.h\fR, and it also defines dummy 2886keeps libev from including \fIconfig.h\fR, and it also defines dummy
2582implementations for some libevent functions (such as logging, which is not 2887implementations for some libevent functions (such as logging, which is not
2601note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. 2906note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though.
2602.IP "\s-1EV_USE_NANOSLEEP\s0" 4 2907.IP "\s-1EV_USE_NANOSLEEP\s0" 4
2603.IX Item "EV_USE_NANOSLEEP" 2908.IX Item "EV_USE_NANOSLEEP"
2604If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available 2909If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available
2605and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. 2910and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR.
2911.IP "\s-1EV_USE_EVENTFD\s0" 4
2912.IX Item "EV_USE_EVENTFD"
2913If defined to be \f(CW1\fR, then libev will assume that \f(CW\*(C`eventfd ()\*(C'\fR is
2914available and will probe for kernel support at runtime. This will improve
2915\&\f(CW\*(C`ev_signal\*(C'\fR and \f(CW\*(C`ev_async\*(C'\fR performance and reduce resource consumption.
2916If undefined, it will be enabled if the headers indicate GNU/Linux + Glibc
29172.7 or newer, otherwise disabled.
2606.IP "\s-1EV_USE_SELECT\s0" 4 2918.IP "\s-1EV_USE_SELECT\s0" 4
2607.IX Item "EV_USE_SELECT" 2919.IX Item "EV_USE_SELECT"
2608If undefined or defined to be \f(CW1\fR, libev will compile in support for the 2920If undefined or defined to be \f(CW1\fR, libev will compile in support for the
2609\&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no 2921\&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no
2610other method takes over, select will be it. Otherwise the select backend 2922other method takes over, select will be it. Otherwise the select backend
2641takes precedence over select. 2953takes precedence over select.
2642.IP "\s-1EV_USE_EPOLL\s0" 4 2954.IP "\s-1EV_USE_EPOLL\s0" 4
2643.IX Item "EV_USE_EPOLL" 2955.IX Item "EV_USE_EPOLL"
2644If defined to be \f(CW1\fR, libev will compile in support for the Linux 2956If defined to be \f(CW1\fR, libev will compile in support for the Linux
2645\&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime, 2957\&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime,
2646otherwise another method will be used as fallback. This is the 2958otherwise another method will be used as fallback. This is the preferred
2647preferred backend for GNU/Linux systems. 2959backend for GNU/Linux systems. If undefined, it will be enabled if the
2960headers indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled.
2648.IP "\s-1EV_USE_KQUEUE\s0" 4 2961.IP "\s-1EV_USE_KQUEUE\s0" 4
2649.IX Item "EV_USE_KQUEUE" 2962.IX Item "EV_USE_KQUEUE"
2650If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style 2963If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style
2651\&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime, 2964\&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime,
2652otherwise another method will be used as fallback. This is the preferred 2965otherwise another method will be used as fallback. This is the preferred
2667reserved for future expansion, works like the \s-1USE\s0 symbols above. 2980reserved for future expansion, works like the \s-1USE\s0 symbols above.
2668.IP "\s-1EV_USE_INOTIFY\s0" 4 2981.IP "\s-1EV_USE_INOTIFY\s0" 4
2669.IX Item "EV_USE_INOTIFY" 2982.IX Item "EV_USE_INOTIFY"
2670If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify 2983If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify
2671interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will 2984interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will
2672be detected at runtime. 2985be detected at runtime. If undefined, it will be enabled if the headers
2986indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled.
2987.IP "\s-1EV_ATOMIC_T\s0" 4
2988.IX Item "EV_ATOMIC_T"
2989Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose
2990access is atomic with respect to other threads or signal contexts. No such
2991type is easily found in the C language, so you can provide your own type
2992that you know is safe for your purposes. It is used both for signal handler \*(L"locking\*(R"
2993as well as for signal and thread safety in \f(CW\*(C`ev_async\*(C'\fR watchers.
2994.Sp
2995In the absense of this define, libev will use \f(CW\*(C`sig_atomic_t volatile\*(C'\fR
2996(from \fIsignal.h\fR), which is usually good enough on most platforms.
2673.IP "\s-1EV_H\s0" 4 2997.IP "\s-1EV_H\s0" 4
2674.IX Item "EV_H" 2998.IX Item "EV_H"
2675The name of the \fIev.h\fR header file used to include it. The default if 2999The name of the \fIev.h\fR header file used to include it. The default if
2676undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be 3000undefined is \f(CW"ev.h"\fR in \fIevent.h\fR, \fIev.c\fR and \fIev++.h\fR. This can be
2677used to virtually rename the \fIev.h\fR header file in case of conflicts. 3001used to virtually rename the \fIev.h\fR header file in case of conflicts.
2735defined to be \f(CW0\fR, then they are not. 3059defined to be \f(CW0\fR, then they are not.
2736.IP "\s-1EV_FORK_ENABLE\s0" 4 3060.IP "\s-1EV_FORK_ENABLE\s0" 4
2737.IX Item "EV_FORK_ENABLE" 3061.IX Item "EV_FORK_ENABLE"
2738If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If 3062If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If
2739defined to be \f(CW0\fR, then they are not. 3063defined to be \f(CW0\fR, then they are not.
3064.IP "\s-1EV_ASYNC_ENABLE\s0" 4
3065.IX Item "EV_ASYNC_ENABLE"
3066If undefined or defined to be \f(CW1\fR, then async watchers are supported. If
3067defined to be \f(CW0\fR, then they are not.
2740.IP "\s-1EV_MINIMAL\s0" 4 3068.IP "\s-1EV_MINIMAL\s0" 4
2741.IX Item "EV_MINIMAL" 3069.IX Item "EV_MINIMAL"
2742If you need to shave off some kilobytes of code at the expense of some 3070If you need to shave off some kilobytes of code at the expense of some
2743speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override 3071speed, define this symbol to \f(CW1\fR. Currently this is used to override some
2744some inlining decisions, saves roughly 30% codesize of amd64. 3072inlining decisions, saves roughly 30% codesize of amd64. It also selects a
3073much smaller 2\-heap for timer management over the default 4\-heap.
2745.IP "\s-1EV_PID_HASHSIZE\s0" 4 3074.IP "\s-1EV_PID_HASHSIZE\s0" 4
2746.IX Item "EV_PID_HASHSIZE" 3075.IX Item "EV_PID_HASHSIZE"
2747\&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by 3076\&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by
2748pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more 3077pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more
2749than enough. If you need to manage thousands of children you might want to 3078than enough. If you need to manage thousands of children you might want to
2753\&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by 3082\&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by
2754inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), 3083inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR),
2755usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR 3084usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR
2756watchers you might want to increase this value (\fImust\fR be a power of 3085watchers you might want to increase this value (\fImust\fR be a power of
2757two). 3086two).
3087.IP "\s-1EV_USE_4HEAP\s0" 4
3088.IX Item "EV_USE_4HEAP"
3089Heaps are not very cache-efficient. To improve the cache-efficiency of the
3090timer and periodics heap, libev uses a 4\-heap when this symbol is defined
3091to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has a
3092noticable after performance with many (thousands) of watchers.
3093.Sp
3094The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
3095(disabled).
3096.IP "\s-1EV_HEAP_CACHE_AT\s0" 4
3097.IX Item "EV_HEAP_CACHE_AT"
3098Heaps are not very cache-efficient. To improve the cache-efficiency of the
3099timer and periodics heap, libev can cache the timestamp (\fIat\fR) within
3100the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR),
3101which uses 8\-12 bytes more per watcher and a few hundred bytes more code,
3102but avoids random read accesses on heap changes. This noticably improves
3103performance noticably with with many (hundreds) of watchers.
3104.Sp
3105The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR
3106(disabled).
2758.IP "\s-1EV_COMMON\s0" 4 3107.IP "\s-1EV_COMMON\s0" 4
2759.IX Item "EV_COMMON" 3108.IX Item "EV_COMMON"
2760By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining 3109By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining
2761this macro to a something else you can include more and other types of 3110this macro to a something else you can include more and other types of
2762members. You have to define it each time you include one of the files, 3111members. You have to define it each time you include one of the files,
2844.PP 3193.PP
2845.Vb 2 3194.Vb 2
2846\& #include "ev_cpp.h" 3195\& #include "ev_cpp.h"
2847\& #include "ev.c" 3196\& #include "ev.c"
2848.Ve 3197.Ve
3198.SH "THREADS AND COROUTINES"
3199.IX Header "THREADS AND COROUTINES"
3200.Sh "\s-1THREADS\s0"
3201.IX Subsection "THREADS"
3202Libev itself is completely threadsafe, but it uses no locking. This
3203means that you can use as many loops as you want in parallel, as long as
3204only one thread ever calls into one libev function with the same loop
3205parameter.
3206.PP
3207Or put differently: calls with different loop parameters can be done in
3208parallel from multiple threads, calls with the same loop parameter must be
3209done serially (but can be done from different threads, as long as only one
3210thread ever is inside a call at any point in time, e.g. by using a mutex
3211per loop).
3212.PP
3213If you want to know which design is best for your problem, then I cannot
3214help you but by giving some generic advice:
3215.IP "\(bu" 4
3216most applications have a main thread: use the default libev loop
3217in that thread, or create a seperate thread running only the default loop.
3218.Sp
3219This helps integrating other libraries or software modules that use libev
3220themselves and don't care/know about threading.
3221.IP "\(bu" 4
3222one loop per thread is usually a good model.
3223.Sp
3224Doing this is almost never wrong, sometimes a better-performance model
3225exists, but it is always a good start.
3226.IP "\(bu" 4
3227other models exist, such as the leader/follower pattern, where one
3228loop is handed through multiple threads in a kind of round-robbin fashion.
3229.Sp
3230Chosing a model is hard \- look around, learn, know that usually you cna do
3231better than you currently do :\-)
3232.IP "\(bu" 4
3233often you need to talk to some other thread which blocks in the
3234event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other
3235threads safely (or from signal contexts...).
3236.Sh "\s-1COROUTINES\s0"
3237.IX Subsection "COROUTINES"
3238Libev is much more accomodating to coroutines (\*(L"cooperative threads\*(R"):
3239libev fully supports nesting calls to it's functions from different
3240coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two
3241different coroutines and switch freely between both coroutines running the
3242loop, as long as you don't confuse yourself). The only exception is that
3243you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks.
3244.PP
3245Care has been invested into making sure that libev does not keep local
3246state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine
3247switches.
2849.SH "COMPLEXITIES" 3248.SH "COMPLEXITIES"
2850.IX Header "COMPLEXITIES" 3249.IX Header "COMPLEXITIES"
2851In this section the complexities of (many of) the algorithms used inside 3250In this section the complexities of (many of) the algorithms used inside
2852libev will be explained. For complexity discussions about backends see the 3251libev will be explained. For complexity discussions about backends see the
2853documentation for \f(CW\*(C`ev_default_init\*(C'\fR. 3252documentation for \f(CW\*(C`ev_default_init\*(C'\fR.
2864have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers. 3263have to skip roughly seven (\f(CW\*(C`ld 100\*(C'\fR) of these watchers.
2865.IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4 3264.IP "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 4
2866.IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)" 3265.IX Item "Changing timer/periodic watchers (by autorepeat or calling again): O(log skipped_other_timers)"
2867That means that changing a timer costs less than removing/adding them 3266That means that changing a timer costs less than removing/adding them
2868as only the relative motion in the event queue has to be paid for. 3267as only the relative motion in the event queue has to be paid for.
2869.IP "Starting io/check/prepare/idle/signal/child watchers: O(1)" 4 3268.IP "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)" 4
2870.IX Item "Starting io/check/prepare/idle/signal/child watchers: O(1)" 3269.IX Item "Starting io/check/prepare/idle/signal/child/fork/async watchers: O(1)"
2871These just add the watcher into an array or at the head of a list. 3270These just add the watcher into an array or at the head of a list.
2872.IP "Stopping check/prepare/idle watchers: O(1)" 4 3271.IP "Stopping check/prepare/idle/fork/async watchers: O(1)" 4
2873.IX Item "Stopping check/prepare/idle watchers: O(1)" 3272.IX Item "Stopping check/prepare/idle/fork/async watchers: O(1)"
2874.PD 0 3273.PD 0
2875.IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4 3274.IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4
2876.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" 3275.IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))"
2877.PD 3276.PD
2878These watchers are stored in lists then need to be walked to find the 3277These watchers are stored in lists then need to be walked to find the
2879correct watcher to remove. The lists are usually short (you don't usually 3278correct watcher to remove. The lists are usually short (you don't usually
2880have many watchers waiting for the same fd or signal). 3279have many watchers waiting for the same fd or signal).
2881.IP "Finding the next timer in each loop iteration: O(1)" 4 3280.IP "Finding the next timer in each loop iteration: O(1)" 4
2882.IX Item "Finding the next timer in each loop iteration: O(1)" 3281.IX Item "Finding the next timer in each loop iteration: O(1)"
2883By virtue of using a binary heap, the next timer is always found at the 3282By virtue of using a binary or 4\-heap, the next timer is always found at a
2884beginning of the storage array. 3283fixed position in the storage array.
2885.IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 3284.IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4
2886.IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 3285.IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)"
2887A change means an I/O watcher gets started or stopped, which requires 3286A change means an I/O watcher gets started or stopped, which requires
2888libev to recalculate its status (and possibly tell the kernel, depending 3287libev to recalculate its status (and possibly tell the kernel, depending
2889on backend and wether \f(CW\*(C`ev_io_set\*(C'\fR was used). 3288on backend and wether \f(CW\*(C`ev_io_set\*(C'\fR was used).
2894.IX Item "Priority handling: O(number_of_priorities)" 3293.IX Item "Priority handling: O(number_of_priorities)"
2895.PD 3294.PD
2896Priorities are implemented by allocating some space for each 3295Priorities are implemented by allocating some space for each
2897priority. When doing priority-based operations, libev usually has to 3296priority. When doing priority-based operations, libev usually has to
2898linearly search all the priorities, but starting/stopping and activating 3297linearly search all the priorities, but starting/stopping and activating
2899watchers becomes O(1) w.r.t. prioritiy handling. 3298watchers becomes O(1) w.r.t. priority handling.
3299.IP "Sending an ev_async: O(1)" 4
3300.IX Item "Sending an ev_async: O(1)"
3301.PD 0
3302.IP "Processing ev_async_send: O(number_of_async_watchers)" 4
3303.IX Item "Processing ev_async_send: O(number_of_async_watchers)"
3304.IP "Processing signals: O(max_signal_number)" 4
3305.IX Item "Processing signals: O(max_signal_number)"
3306.PD
3307Sending involves a syscall \fIiff\fR there were no other \f(CW\*(C`ev_async_send\*(C'\fR
3308calls in the current loop iteration. Checking for async and signal events
3309involves iterating over all running async watchers or all signal numbers.
2900.SH "Win32 platform limitations and workarounds" 3310.SH "Win32 platform limitations and workarounds"
2901.IX Header "Win32 platform limitations and workarounds" 3311.IX Header "Win32 platform limitations and workarounds"
2902Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev 3312Win32 doesn't support any of the standards (e.g. \s-1POSIX\s0) that libev
2903requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0 3313requires, and its I/O model is fundamentally incompatible with the \s-1POSIX\s0
2904model. Libev still offers limited functionality on this platform in 3314model. Libev still offers limited functionality on this platform in
2905the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket 3315the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket
2906descriptors. This only applies when using Win32 natively, not when using 3316descriptors. This only applies when using Win32 natively, not when using
2907e.g. cygwin. 3317e.g. cygwin.
2908.PP 3318.PP
3319Lifting these limitations would basically require the full
3320re-implementation of the I/O system. If you are into these kinds of
3321things, then note that glib does exactly that for you in a very portable
3322way (note also that glib is the slowest event library known to man).
3323.PP
2909There is no supported compilation method available on windows except 3324There is no supported compilation method available on windows except
2910embedding it into other applications. 3325embedding it into other applications.
2911.PP 3326.PP
2912Due to the many, low, and arbitrary limits on the win32 platform and the 3327Due to the many, low, and arbitrary limits on the win32 platform and
2913abysmal performance of winsockets, using a large number of sockets is not 3328the abysmal performance of winsockets, using a large number of sockets
2914recommended (and not reasonable). If your program needs to use more than 3329is not recommended (and not reasonable). If your program needs to use
2915a hundred or so sockets, then likely it needs to use a totally different 3330more than a hundred or so sockets, then likely it needs to use a totally
2916implementation for windows, as libev offers the \s-1POSIX\s0 model, which cannot 3331different implementation for windows, as libev offers the \s-1POSIX\s0 readyness
2917be implemented efficiently on windows (microsoft monopoly games). 3332notification model, which cannot be implemented efficiently on windows
3333(microsoft monopoly games).
2918.IP "The winsocket select function" 4 3334.IP "The winsocket select function" 4
2919.IX Item "The winsocket select function" 3335.IX Item "The winsocket select function"
2920The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it requires 3336The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it requires
2921socket \fIhandles\fR and not socket \fIfile descriptors\fR. This makes select 3337socket \fIhandles\fR and not socket \fIfile descriptors\fR. This makes select
2922very inefficient, and also requires a mapping from file descriptors 3338very inefficient, and also requires a mapping from file descriptors
2934.Sp 3350.Sp
2935Note that winsockets handling of fd sets is O(n), so you can easily get a 3351Note that winsockets handling of fd sets is O(n), so you can easily get a
2936complexity in the O(nA\*^X) range when using win32. 3352complexity in the O(nA\*^X) range when using win32.
2937.IP "Limited number of file descriptors" 4 3353.IP "Limited number of file descriptors" 4
2938.IX Item "Limited number of file descriptors" 3354.IX Item "Limited number of file descriptors"
2939Windows has numerous arbitrary (and low) limits on things. Early versions 3355Windows has numerous arbitrary (and low) limits on things.
2940of winsocket's select only supported waiting for a max. of \f(CW64\fR handles 3356.Sp
2941(probably owning to the fact that all windows kernels can only wait for 3357Early versions of winsocket's select only supported waiting for a maximum
2942\&\f(CW64\fR things at the same time internally; microsoft recommends spawning a 3358of \f(CW64\fR handles (probably owning to the fact that all windows kernels
2943chain of threads and wait for 63 handles and the previous thread in each). 3359can only wait for \f(CW64\fR things at the same time internally; microsoft
3360recommends spawning a chain of threads and wait for 63 handles and the
3361previous thread in each. Great).
2944.Sp 3362.Sp
2945Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR 3363Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR
2946to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select 3364to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select
2947call (which might be in libev or elsewhere, for example, perl does its own 3365call (which might be in libev or elsewhere, for example, perl does its own
2948select emulation on windows). 3366select emulation on windows).
2956.Sp 3374.Sp
2957This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on 3375This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on
2958windows version and/or the phase of the moon). To get more, you need to 3376windows version and/or the phase of the moon). To get more, you need to
2959wrap all I/O functions and provide your own fd management, but the cost of 3377wrap all I/O functions and provide your own fd management, but the cost of
2960calling select (O(nA\*^X)) will likely make this unworkable. 3378calling select (O(nA\*^X)) will likely make this unworkable.
3379.SH "PORTABILITY REQUIREMENTS"
3380.IX Header "PORTABILITY REQUIREMENTS"
3381In addition to a working ISO-C implementation, libev relies on a few
3382additional extensions:
3383.ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4
3384.el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4
3385.IX Item "sig_atomic_t volatile must be thread-atomic as well"
3386The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as
3387\&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic w.r.t. accesses from different
3388threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is
3389believed to be sufficiently portable.
3390.ie n .IP """sigprocmask"" must work in a threaded environment" 4
3391.el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4
3392.IX Item "sigprocmask must work in a threaded environment"
3393Libev uses \f(CW\*(C`sigprocmask\*(C'\fR to temporarily block signals. This is not
3394allowed in a threaded program (\f(CW\*(C`pthread_sigmask\*(C'\fR has to be used). Typical
3395pthread implementations will either allow \f(CW\*(C`sigprocmask\*(C'\fR in the \*(L"main
3396thread\*(R" or will block signals process-wide, both behaviours would
3397be compatible with libev. Interaction between \f(CW\*(C`sigprocmask\*(C'\fR and
3398\&\f(CW\*(C`pthread_sigmask\*(C'\fR could complicate things, however.
3399.Sp
3400The most portable way to handle signals is to block signals in all threads
3401except the initial one, and run the default loop in the initial thread as
3402well.
3403.ie n .IP """long"" must be large enough for common memory allocation sizes" 4
3404.el .IP "\f(CWlong\fR must be large enough for common memory allocation sizes" 4
3405.IX Item "long must be large enough for common memory allocation sizes"
3406To improve portability and simplify using libev, libev uses \f(CW\*(C`long\*(C'\fR
3407internally instead of \f(CW\*(C`size_t\*(C'\fR when allocating its data structures. On
3408non-POSIX systems (Microsoft...) this might be unexpectedly low, but
3409is still at least 31 bits everywhere, which is enough for hundreds of
3410millions of watchers.
3411.ie n .IP """double"" must hold a time value in seconds with enough accuracy" 4
3412.el .IP "\f(CWdouble\fR must hold a time value in seconds with enough accuracy" 4
3413.IX Item "double must hold a time value in seconds with enough accuracy"
3414The type \f(CW\*(C`double\*(C'\fR is used to represent timestamps. It is required to
3415have at least 51 bits of mantissa (and 9 bits of exponent), which is good
3416enough for at least into the year 4000. This requirement is fulfilled by
3417implementations implementing \s-1IEEE\s0 754 (basically all existing ones).
3418.PP
3419If you know of other additional requirements drop me a note.
2961.SH "AUTHOR" 3420.SH "AUTHOR"
2962.IX Header "AUTHOR" 3421.IX Header "AUTHOR"
2963Marc Lehmann <libev@schmorp.de>. 3422Marc Lehmann <libev@schmorp.de>.
2964.SH "POD ERRORS" 3423.SH "POD ERRORS"
2965.IX Header "POD ERRORS" 3424.IX Header "POD ERRORS"
2966Hey! \fBThe above document had some coding errors, which are explained below:\fR 3425Hey! \fBThe above document had some coding errors, which are explained below:\fR
2967.IP "Around line 2686:" 4 3426.IP "Around line 3052:" 4
2968.IX Item "Around line 2686:" 3427.IX Item "Around line 3052:"
2969You forgot a '=back' before '=head2' 3428You forgot a '=back' before '=head2'

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines