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

Comparing libev/ev.3 (file contents):
Revision 1.23 by root, Tue Nov 27 08:20:42 2007 UTC vs.
Revision 1.27 by root, Tue Nov 27 20:15:01 2007 UTC

132.TH "<STANDARD INPUT>" 1 "2007-11-27" "perl v5.8.8" "User Contributed Perl Documentation" 132.TH "<STANDARD INPUT>" 1 "2007-11-27" "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 2
138\& /* this is the only header you need */
138\& #include <ev.h> 139\& #include <ev.h>
140.Ve
141.PP
142.Vb 3
143\& /* what follows is a fully working example program */
144\& ev_io stdin_watcher;
145\& ev_timer timeout_watcher;
146.Ve
147.PP
148.Vb 8
149\& /* called when data readable on stdin */
150\& static void
151\& stdin_cb (EV_P_ struct ev_io *w, int revents)
152\& {
153\& /* puts ("stdin ready"); */
154\& ev_io_stop (EV_A_ w); /* just a syntax example */
155\& ev_unloop (EV_A_ EVUNLOOP_ALL); /* leave all loop calls */
156\& }
157.Ve
158.PP
159.Vb 6
160\& static void
161\& timeout_cb (EV_P_ struct ev_timer *w, int revents)
162\& {
163\& /* puts ("timeout"); */
164\& ev_unloop (EV_A_ EVUNLOOP_ONE); /* leave one loop call */
165\& }
166.Ve
167.PP
168.Vb 4
169\& int
170\& main (void)
171\& {
172\& struct ev_loop *loop = ev_default_loop (0);
173.Ve
174.PP
175.Vb 3
176\& /* initialise an io watcher, then start it */
177\& ev_io_init (&stdin_watcher, stdin_cb, /*STDIN_FILENO*/ 0, EV_READ);
178\& ev_io_start (loop, &stdin_watcher);
179.Ve
180.PP
181.Vb 3
182\& /* simple non-repeating 5.5 second timeout */
183\& ev_timer_init (&timeout_watcher, timeout_cb, 5.5, 0.);
184\& ev_timer_start (loop, &timeout_watcher);
185.Ve
186.PP
187.Vb 2
188\& /* loop till timeout or data ready */
189\& ev_loop (loop, 0);
190.Ve
191.PP
192.Vb 2
193\& return 0;
194\& }
139.Ve 195.Ve
140.SH "DESCRIPTION" 196.SH "DESCRIPTION"
141.IX Header "DESCRIPTION" 197.IX Header "DESCRIPTION"
142Libev is an event loop: you register interest in certain events (such as a 198Libev is an event loop: you register interest in certain events (such as a
143file descriptor being readable or a timeout occuring), and it will manage 199file descriptor being readable or a timeout occuring), and it will manage
240might be supported on the current system, you would need to look at 296might 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 297\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for
242recommended ones. 298recommended ones.
243.Sp 299.Sp
244See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. 300See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
245.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 301.IP "ev_set_allocator (void *(*cb)(void *ptr, size_t size))" 4
246.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" 302.IX Item "ev_set_allocator (void *(*cb)(void *ptr, size_t size))"
247Sets the allocation function to use (the prototype is similar to the 303Sets the allocation function to use (the prototype and semantics are
248realloc C function, the semantics are identical). It is used to allocate 304identical to the realloc C function). It is used to allocate and free
249and free memory (no surprises here). If it returns zero when memory 305memory (no surprises here). If it returns zero when memory needs to be
250needs to be allocated, the library might abort or take some potentially 306allocated, the library might abort or take some potentially destructive
251destructive action. The default is your system realloc function. 307action. The default is your system realloc function.
252.Sp 308.Sp
253You could override this function in high-availability programs to, say, 309You could override this function in high-availability programs to, say,
254free some memory if it cannot allocate memory, to use a special allocator, 310free some memory if it cannot allocate memory, to use a special allocator,
255or even to sleep a while and retry until some memory is available. 311or even to sleep a while and retry until some memory is available.
256.Sp 312.Sp
257Example: replace the libev allocator with one that waits a bit and then 313Example: replace the libev allocator with one that waits a bit and then
258retries: better than mine). 314retries: better than mine).
259.Sp 315.Sp
260.Vb 6 316.Vb 6
261\& static void * 317\& static void *
262\& persistent_realloc (void *ptr, long size) 318\& persistent_realloc (void *ptr, size_t size)
263\& { 319\& {
264\& for (;;) 320\& for (;;)
265\& { 321\& {
266\& void *newptr = realloc (ptr, size); 322\& void *newptr = realloc (ptr, size);
267.Ve 323.Ve
707\&\f(CW\*(C`ev_loop\*(C'\fR has gathered them, but before it invokes any callbacks for any 763\&\f(CW\*(C`ev_loop\*(C'\fR has gathered them, but before it invokes any callbacks for any
708received events. Callbacks of both watcher types can start and stop as 764received events. Callbacks of both watcher types can start and stop as
709many watchers as they want, and all of them will be taken into account 765many watchers as they want, and all of them will be taken into account
710(for example, a \f(CW\*(C`ev_prepare\*(C'\fR watcher might start an idle watcher to keep 766(for example, a \f(CW\*(C`ev_prepare\*(C'\fR watcher might start an idle watcher to keep
711\&\f(CW\*(C`ev_loop\*(C'\fR from blocking). 767\&\f(CW\*(C`ev_loop\*(C'\fR from blocking).
768.ie n .IP """EV_EMBED""" 4
769.el .IP "\f(CWEV_EMBED\fR" 4
770.IX Item "EV_EMBED"
771The embedded event loop specified in the \f(CW\*(C`ev_embed\*(C'\fR watcher needs attention.
772.ie n .IP """EV_FORK""" 4
773.el .IP "\f(CWEV_FORK\fR" 4
774.IX Item "EV_FORK"
775The event loop has been resumed in the child process after fork (see
776\&\f(CW\*(C`ev_fork\*(C'\fR).
712.ie n .IP """EV_ERROR""" 4 777.ie n .IP """EV_ERROR""" 4
713.el .IP "\f(CWEV_ERROR\fR" 4 778.el .IP "\f(CWEV_ERROR\fR" 4
714.IX Item "EV_ERROR" 779.IX Item "EV_ERROR"
715An unspecified error has occured, the watcher has been stopped. This might 780An unspecified error has occured, the watcher has been stopped. This might
716happen because the watcher could not be properly started because libev 781happen because the watcher could not be properly started because libev
1613similarly to \f(CW\*(C`ev_loop (embedded_loop, EVLOOP_NONBLOCK)\*(C'\fR, but in the most 1678similarly to \f(CW\*(C`ev_loop (embedded_loop, EVLOOP_NONBLOCK)\*(C'\fR, but in the most
1614apropriate way for embedded loops. 1679apropriate way for embedded loops.
1615.IP "struct ev_loop *loop [read\-only]" 4 1680.IP "struct ev_loop *loop [read\-only]" 4
1616.IX Item "struct ev_loop *loop [read-only]" 1681.IX Item "struct ev_loop *loop [read-only]"
1617The embedded event loop. 1682The embedded event loop.
1683.ie n .Sh """ev_fork"" \- the audacity to resume the event loop after a fork"
1684.el .Sh "\f(CWev_fork\fP \- the audacity to resume the event loop after a fork"
1685.IX Subsection "ev_fork - the audacity to resume the event loop after a fork"
1686Fork watchers are called when a \f(CW\*(C`fork ()\*(C'\fR was detected (usually because
1687whoever is a good citizen cared to tell libev about it by calling
1688\&\f(CW\*(C`ev_default_fork\*(C'\fR or \f(CW\*(C`ev_loop_fork\*(C'\fR). The invocation is done before the
1689event loop blocks next and before \f(CW\*(C`ev_check\*(C'\fR watchers are being called,
1690and only in the child after the fork. If whoever good citizen calling
1691\&\f(CW\*(C`ev_default_fork\*(C'\fR cheats and calls it in the wrong process, the fork
1692handlers will be invoked, too, of course.
1693.IP "ev_fork_init (ev_signal *, callback)" 4
1694.IX Item "ev_fork_init (ev_signal *, callback)"
1695Initialises and configures the fork watcher \- it has no parameters of any
1696kind. There is a \f(CW\*(C`ev_fork_set\*(C'\fR macro, but using it is utterly pointless,
1697believe me.
1618.SH "OTHER FUNCTIONS" 1698.SH "OTHER FUNCTIONS"
1619.IX Header "OTHER FUNCTIONS" 1699.IX Header "OTHER FUNCTIONS"
1620There are some other functions of possible interest. Described. Here. Now. 1700There are some other functions of possible interest. Described. Here. Now.
1621.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 1701.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
1622.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 1702.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
1791\& : io (this, &myclass::io_cb), 1871\& : io (this, &myclass::io_cb),
1792\& idle (this, &myclass::idle_cb) 1872\& idle (this, &myclass::idle_cb)
1793\& { 1873\& {
1794\& io.start (fd, ev::READ); 1874\& io.start (fd, ev::READ);
1795\& } 1875\& }
1876.Ve
1877.SH "MACRO MAGIC"
1878.IX Header "MACRO MAGIC"
1879Libev can be compiled with a variety of options, the most fundemantal is
1880\&\f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. This option determines wether (most) functions and
1881callbacks have an initial \f(CW\*(C`struct ev_loop *\*(C'\fR argument.
1882.PP
1883To make it easier to write programs that cope with either variant, the
1884following macros are defined:
1885.ie n .IP """EV_A""\fR, \f(CW""EV_A_""" 4
1886.el .IP "\f(CWEV_A\fR, \f(CWEV_A_\fR" 4
1887.IX Item "EV_A, EV_A_"
1888This provides the loop \fIargument\fR for functions, if one is required (\*(L"ev
1889loop argument\*(R"). The \f(CW\*(C`EV_A\*(C'\fR form is used when this is the sole argument,
1890\&\f(CW\*(C`EV_A_\*(C'\fR is used when other arguments are following. Example:
1891.Sp
1892.Vb 3
1893\& ev_unref (EV_A);
1894\& ev_timer_add (EV_A_ watcher);
1895\& ev_loop (EV_A_ 0);
1896.Ve
1897.Sp
1898It assumes the variable \f(CW\*(C`loop\*(C'\fR of type \f(CW\*(C`struct ev_loop *\*(C'\fR is in scope,
1899which is often provided by the following macro.
1900.ie n .IP """EV_P""\fR, \f(CW""EV_P_""" 4
1901.el .IP "\f(CWEV_P\fR, \f(CWEV_P_\fR" 4
1902.IX Item "EV_P, EV_P_"
1903This provides the loop \fIparameter\fR for functions, if one is required (\*(L"ev
1904loop parameter\*(R"). The \f(CW\*(C`EV_P\*(C'\fR form is used when this is the sole parameter,
1905\&\f(CW\*(C`EV_P_\*(C'\fR is used when other parameters are following. Example:
1906.Sp
1907.Vb 2
1908\& // this is how ev_unref is being declared
1909\& static void ev_unref (EV_P);
1910.Ve
1911.Sp
1912.Vb 2
1913\& // this is how you can declare your typical callback
1914\& static void cb (EV_P_ ev_timer *w, int revents)
1915.Ve
1916.Sp
1917It declares a parameter \f(CW\*(C`loop\*(C'\fR of type \f(CW\*(C`struct ev_loop *\*(C'\fR, quite
1918suitable for use with \f(CW\*(C`EV_A\*(C'\fR.
1919.ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4
1920.el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4
1921.IX Item "EV_DEFAULT, EV_DEFAULT_"
1922Similar to the other two macros, this gives you the value of the default
1923loop, if multiple loops are supported (\*(L"ev loop default\*(R").
1924.PP
1925Example: Declare and initialise a check watcher, working regardless of
1926wether multiple loops are supported or not.
1927.PP
1928.Vb 5
1929\& static void
1930\& check_cb (EV_P_ ev_timer *w, int revents)
1931\& {
1932\& ev_check_stop (EV_A_ w);
1933\& }
1934.Ve
1935.PP
1936.Vb 4
1937\& ev_check check;
1938\& ev_check_init (&check, check_cb);
1939\& ev_check_start (EV_DEFAULT_ &check);
1940\& ev_loop (EV_DEFAULT_ 0);
1796.Ve 1941.Ve
1797.SH "EMBEDDING" 1942.SH "EMBEDDING"
1798.IX Header "EMBEDDING" 1943.IX Header "EMBEDDING"
1799Libev can (and often is) directly embedded into host 1944Libev can (and often is) directly embedded into host
1800applications. Examples of applications that embed it include the Deliantra 1945applications. Examples of applications that embed it include the Deliantra
2020defined to be \f(CW0\fR, then they are not. 2165defined to be \f(CW0\fR, then they are not.
2021.IP "\s-1EV_STAT_ENABLE\s0" 4 2166.IP "\s-1EV_STAT_ENABLE\s0" 4
2022.IX Item "EV_STAT_ENABLE" 2167.IX Item "EV_STAT_ENABLE"
2023If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If 2168If undefined or defined to be \f(CW1\fR, then stat watchers are supported. If
2024defined to be \f(CW0\fR, then they are not. 2169defined to be \f(CW0\fR, then they are not.
2170.IP "\s-1EV_FORK_ENABLE\s0" 4
2171.IX Item "EV_FORK_ENABLE"
2172If undefined or defined to be \f(CW1\fR, then fork watchers are supported. If
2173defined to be \f(CW0\fR, then they are not.
2025.IP "\s-1EV_MINIMAL\s0" 4 2174.IP "\s-1EV_MINIMAL\s0" 4
2026.IX Item "EV_MINIMAL" 2175.IX Item "EV_MINIMAL"
2027If you need to shave off some kilobytes of code at the expense of some 2176If you need to shave off some kilobytes of code at the expense of some
2028speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override 2177speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override
2029some inlining decisions, saves roughly 30% codesize of amd64. 2178some inlining decisions, saves roughly 30% codesize of amd64.
2179.IP "\s-1EV_PID_HASHSIZE\s0" 4
2180.IX Item "EV_PID_HASHSIZE"
2181\&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by
2182pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more
2183than enough. If you need to manage thousands of children you might want to
2184increase this value.
2030.IP "\s-1EV_COMMON\s0" 4 2185.IP "\s-1EV_COMMON\s0" 4
2031.IX Item "EV_COMMON" 2186.IX Item "EV_COMMON"
2032By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining 2187By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining
2033this macro to a something else you can include more and other types of 2188this macro to a something else you can include more and other types of
2034members. You have to define it each time you include one of the files, 2189members. You have to define it each time you include one of the files,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines