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

Comparing libev/ev.3 (file contents):
Revision 1.9 by root, Fri Nov 23 16:17:12 2007 UTC vs.
Revision 1.11 by root, Sat Nov 24 07:14:26 2007 UTC

127.\} 127.\}
128.rm #[ #] #H #V #F C 128.rm #[ #] #H #V #F C
129.\" ======================================================================== 129.\" ========================================================================
130.\" 130.\"
131.IX Title ""<STANDARD INPUT>" 1" 131.IX Title ""<STANDARD INPUT>" 1"
132.TH "<STANDARD INPUT>" 1 "2007-11-23" "perl v5.8.8" "User Contributed Perl Documentation" 132.TH "<STANDARD INPUT>" 1 "2007-11-24" "perl v5.8.8" "User Contributed Perl Documentation"
133.SH "NAME" 133.SH "NAME"
134libev \- a high performance full\-featured event loop written in C 134libev \- a high performance full\-featured event loop written in C
135.SH "SYNOPSIS" 135.SH "SYNOPSIS"
136.IX Header "SYNOPSIS" 136.IX Header "SYNOPSIS"
137.Vb 1 137.Vb 1
231recommended for this platform. This set is often smaller than the one 231recommended for this platform. This set is often smaller than the one
232returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on 232returned by \f(CW\*(C`ev_supported_backends\*(C'\fR, as for example kqueue is broken on
233most BSDs and will not be autodetected unless you explicitly request it 233most BSDs and will not be autodetected unless you explicitly request it
234(assuming you know what you are doing). This is the set of backends that 234(assuming you know what you are doing). This is the set of backends that
235libev will probe for if you specify no backends explicitly. 235libev will probe for if you specify no backends explicitly.
236.IP "unsigned int ev_embeddable_backends ()" 4
237.IX Item "unsigned int ev_embeddable_backends ()"
238Returns the set of backends that are embeddable in other event loops. This
239is the theoretical, all\-platform, value. To find which backends
240might be supported on the current system, you would need to look at
241\&\f(CW\*(C`ev_embeddable_backends () & ev_supported_backends ()\*(C'\fR, likewise for
242recommended ones.
243.Sp
244See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info.
236.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 245.IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4
237.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" 246.IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))"
238Sets the allocation function to use (the prototype is similar to the 247Sets the allocation function to use (the prototype is similar to the
239realloc C function, the semantics are identical). It is used to allocate 248realloc C function, the semantics are identical). It is used to allocate
240and free memory (no surprises here). If it returns zero when memory 249and free memory (no surprises here). If it returns zero when memory
636*)\*(C'\fR), and you can stop watching for events at any time by calling the 645*)\*(C'\fR), and you can stop watching for events at any time by calling the
637corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR. 646corresponding stop function (\f(CW\*(C`ev_<type>_stop (loop, watcher *)\*(C'\fR.
638.PP 647.PP
639As long as your watcher is active (has been started but not stopped) you 648As long as your watcher is active (has been started but not stopped) you
640must not touch the values stored in it. Most specifically you must never 649must not touch the values stored in it. Most specifically you must never
641reinitialise it or call its set macro. 650reinitialise it or call its \f(CW\*(C`set\*(C'\fR macro.
642.PP
643You can check whether an event is active by calling the \f(CW\*(C`ev_is_active
644(watcher *)\*(C'\fR macro. To see whether an event is outstanding (but the
645callback for it has not been called yet) you can use the \f(CW\*(C`ev_is_pending
646(watcher *)\*(C'\fR macro.
647.PP 651.PP
648Each and every callback receives the event loop pointer as first, the 652Each and every callback receives the event loop pointer as first, the
649registered watcher structure as second, and a bitset of received events as 653registered watcher structure as second, and a bitset of received events as
650third argument. 654third argument.
651.PP 655.PP
709Libev will usually signal a few \*(L"dummy\*(R" events together with an error, 713Libev will usually signal a few \*(L"dummy\*(R" events together with an error,
710for example it might indicate that a fd is readable or writable, and if 714for example it might indicate that a fd is readable or writable, and if
711your callbacks is well-written it can just attempt the operation and cope 715your callbacks is well-written it can just attempt the operation and cope
712with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multithreaded 716with the error from \fIread()\fR or \fIwrite()\fR. This will not work in multithreaded
713programs, though, so beware. 717programs, though, so beware.
718.Sh "\s-1SUMMARY\s0 \s-1OF\s0 \s-1GENERIC\s0 \s-1WATCHER\s0 \s-1FUNCTIONS\s0"
719.IX Subsection "SUMMARY OF GENERIC WATCHER FUNCTIONS"
720In the following description, \f(CW\*(C`TYPE\*(C'\fR stands for the watcher type,
721e.g. \f(CW\*(C`timer\*(C'\fR for \f(CW\*(C`ev_timer\*(C'\fR watchers and \f(CW\*(C`io\*(C'\fR for \f(CW\*(C`ev_io\*(C'\fR watchers.
722.ie n .IP """ev_init"" (ev_TYPE *watcher, callback)" 4
723.el .IP "\f(CWev_init\fR (ev_TYPE *watcher, callback)" 4
724.IX Item "ev_init (ev_TYPE *watcher, callback)"
725This macro initialises the generic portion of a watcher. The contents
726of the watcher object can be arbitrary (so \f(CW\*(C`malloc\*(C'\fR will do). Only
727the generic parts of the watcher are initialised, you \fIneed\fR to call
728the type-specific \f(CW\*(C`ev_TYPE_set\*(C'\fR macro afterwards to initialise the
729type-specific parts. For each type there is also a \f(CW\*(C`ev_TYPE_init\*(C'\fR macro
730which rolls both calls into one.
731.Sp
732You can reinitialise a watcher at any time as long as it has been stopped
733(or never started) and there are no pending events outstanding.
734.Sp
735The callbakc is always of type \f(CW\*(C`void (*)(ev_loop *loop, ev_TYPE *watcher,
736int revents)\*(C'\fR.
737.ie n .IP """ev_TYPE_set"" (ev_TYPE *, [args])" 4
738.el .IP "\f(CWev_TYPE_set\fR (ev_TYPE *, [args])" 4
739.IX Item "ev_TYPE_set (ev_TYPE *, [args])"
740This macro initialises the type-specific parts of a watcher. You need to
741call \f(CW\*(C`ev_init\*(C'\fR at least once before you call this macro, but you can
742call \f(CW\*(C`ev_TYPE_set\*(C'\fR any number of times. You must not, however, call this
743macro on a watcher that is active (it can be pending, however, which is a
744difference to the \f(CW\*(C`ev_init\*(C'\fR macro).
745.Sp
746Although some watcher types do not have type-specific arguments
747(e.g. \f(CW\*(C`ev_prepare\*(C'\fR) you still need to call its \f(CW\*(C`set\*(C'\fR macro.
748.ie n .IP """ev_TYPE_init"" (ev_TYPE *watcher, callback, [args])" 4
749.el .IP "\f(CWev_TYPE_init\fR (ev_TYPE *watcher, callback, [args])" 4
750.IX Item "ev_TYPE_init (ev_TYPE *watcher, callback, [args])"
751This convinience macro rolls both \f(CW\*(C`ev_init\*(C'\fR and \f(CW\*(C`ev_TYPE_set\*(C'\fR macro
752calls into a single call. This is the most convinient method to initialise
753a watcher. The same limitations apply, of course.
754.ie n .IP """ev_TYPE_start"" (loop *, ev_TYPE *watcher)" 4
755.el .IP "\f(CWev_TYPE_start\fR (loop *, ev_TYPE *watcher)" 4
756.IX Item "ev_TYPE_start (loop *, ev_TYPE *watcher)"
757Starts (activates) the given watcher. Only active watchers will receive
758events. If the watcher is already active nothing will happen.
759.ie n .IP """ev_TYPE_stop"" (loop *, ev_TYPE *watcher)" 4
760.el .IP "\f(CWev_TYPE_stop\fR (loop *, ev_TYPE *watcher)" 4
761.IX Item "ev_TYPE_stop (loop *, ev_TYPE *watcher)"
762Stops the given watcher again (if active) and clears the pending
763status. It is possible that stopped watchers are pending (for example,
764non-repeating timers are being stopped when they become pending), but
765\&\f(CW\*(C`ev_TYPE_stop\*(C'\fR ensures that the watcher is neither active nor pending. If
766you want to free or reuse the memory used by the watcher it is therefore a
767good idea to always call its \f(CW\*(C`ev_TYPE_stop\*(C'\fR function.
768.IP "bool ev_is_active (ev_TYPE *watcher)" 4
769.IX Item "bool ev_is_active (ev_TYPE *watcher)"
770Returns a true value iff the watcher is active (i.e. it has been started
771and not yet been stopped). As long as a watcher is active you must not modify
772it.
773.IP "bool ev_is_pending (ev_TYPE *watcher)" 4
774.IX Item "bool ev_is_pending (ev_TYPE *watcher)"
775Returns a true value iff the watcher is pending, (i.e. it has outstanding
776events but its callback has not yet been invoked). As long as a watcher
777is pending (but not active) you must not call an init function on it (but
778\&\f(CW\*(C`ev_TYPE_set\*(C'\fR is safe) and you must make sure the watcher is available to
779libev (e.g. you cnanot \f(CW\*(C`free ()\*(C'\fR it).
780.IP "callback = ev_cb (ev_TYPE *watcher)" 4
781.IX Item "callback = ev_cb (ev_TYPE *watcher)"
782Returns the callback currently set on the watcher.
783.IP "ev_cb_set (ev_TYPE *watcher, callback)" 4
784.IX Item "ev_cb_set (ev_TYPE *watcher, callback)"
785Change the callback. You can change the callback at virtually any time
786(modulo threads).
714.Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0" 787.Sh "\s-1ASSOCIATING\s0 \s-1CUSTOM\s0 \s-1DATA\s0 \s-1WITH\s0 A \s-1WATCHER\s0"
715.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER" 788.IX Subsection "ASSOCIATING CUSTOM DATA WITH A WATCHER"
716Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change 789Each watcher has, by default, a member \f(CW\*(C`void *data\*(C'\fR that you can change
717and read at any time, libev will completely ignore it. This can be used 790and read at any time, libev will completely ignore it. This can be used
718to associate arbitrary data with your watcher. If you need more data and 791to associate arbitrary data with your watcher. If you need more data and
1159.IX Subsection "ev_prepare and ev_check - customise your event loop" 1232.IX Subsection "ev_prepare and ev_check - customise your event loop"
1160Prepare and check watchers are usually (but not always) used in tandem: 1233Prepare and check watchers are usually (but not always) used in tandem:
1161prepare watchers get invoked before the process blocks and check watchers 1234prepare watchers get invoked before the process blocks and check watchers
1162afterwards. 1235afterwards.
1163.PP 1236.PP
1164Their main purpose is to integrate other event mechanisms into libev. This 1237Their main purpose is to integrate other event mechanisms into libev and
1165could be used, for example, to track variable changes, implement your own 1238their use is somewhat advanced. This could be used, for example, to track
1166watchers, integrate net-snmp or a coroutine library and lots more. 1239variable changes, implement your own watchers, integrate net-snmp or a
1240coroutine library and lots more.
1167.PP 1241.PP
1168This is done by examining in each prepare call which file descriptors need 1242This is done by examining in each prepare call which file descriptors need
1169to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for 1243to be watched by the other library, registering \f(CW\*(C`ev_io\*(C'\fR watchers for
1170them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries 1244them and starting an \f(CW\*(C`ev_timer\*(C'\fR watcher for any timeouts (many libraries
1171provide just this functionality). Then, in the check watcher you check for 1245provide just this functionality). Then, in the check watcher you check for
1191Initialises and configures the prepare or check watcher \- they have no 1265Initialises and configures the prepare or check watcher \- they have no
1192parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR 1266parameters of any kind. There are \f(CW\*(C`ev_prepare_set\*(C'\fR and \f(CW\*(C`ev_check_set\*(C'\fR
1193macros, but using them is utterly, utterly and completely pointless. 1267macros, but using them is utterly, utterly and completely pointless.
1194.PP 1268.PP
1195Example: *TODO*. 1269Example: *TODO*.
1270.ie n .Sh """ev_embed"" \- when one backend isn't enough"
1271.el .Sh "\f(CWev_embed\fP \- when one backend isn't enough"
1272.IX Subsection "ev_embed - when one backend isn't enough"
1273This is a rather advanced watcher type that lets you embed one event loop
1274into another (currently only \f(CW\*(C`ev_io\*(C'\fR events are supported in the embedded
1275loop, other types of watchers might be handled in a delayed or incorrect
1276fashion and must not be used).
1277.PP
1278There are primarily two reasons you would want that: work around bugs and
1279prioritise I/O.
1280.PP
1281As an example for a bug workaround, the kqueue backend might only support
1282sockets on some platform, so it is unusable as generic backend, but you
1283still want to make use of it because you have many sockets and it scales
1284so nicely. In this case, you would create a kqueue-based loop and embed it
1285into your default loop (which might use e.g. poll). Overall operation will
1286be a bit slower because first libev has to poll and then call kevent, but
1287at least you can use both at what they are best.
1288.PP
1289As for prioritising I/O: rarely you have the case where some fds have
1290to be watched and handled very quickly (with low latency), and even
1291priorities and idle watchers might have too much overhead. In this case
1292you would put all the high priority stuff in one loop and all the rest in
1293a second one, and embed the second one in the first.
1294.PP
1295As long as the watcher is active, the callback will be invoked every time
1296there might be events pending in the embedded loop. The callback must then
1297call \f(CW\*(C`ev_embed_sweep (mainloop, watcher)\*(C'\fR to make a single sweep and invoke
1298their callbacks (you could also start an idle watcher to give the embedded
1299loop strictly lower priority for example). You can also set the callback
1300to \f(CW0\fR, in which case the embed watcher will automatically execute the
1301embedded loop sweep.
1302.PP
1303As long as the watcher is started it will automatically handle events. The
1304callback will be invoked whenever some events have been handled. You can
1305set the callback to \f(CW0\fR to avoid having to specify one if you are not
1306interested in that.
1307.PP
1308Also, there have not currently been made special provisions for forking:
1309when you fork, you not only have to call \f(CW\*(C`ev_loop_fork\*(C'\fR on both loops,
1310but you will also have to stop and restart any \f(CW\*(C`ev_embed\*(C'\fR watchers
1311yourself.
1312.PP
1313Unfortunately, not all backends are embeddable, only the ones returned by
1314\&\f(CW\*(C`ev_embeddable_backends\*(C'\fR are, which, unfortunately, does not include any
1315portable one.
1316.PP
1317So when you want to use this feature you will always have to be prepared
1318that you cannot get an embeddable loop. The recommended way to get around
1319this is to have a separate variables for your embeddable loop, try to
1320create it, and if that fails, use the normal loop for everything:
1321.PP
1322.Vb 3
1323\& struct ev_loop *loop_hi = ev_default_init (0);
1324\& struct ev_loop *loop_lo = 0;
1325\& struct ev_embed embed;
1326.Ve
1327.PP
1328.Vb 5
1329\& // see if there is a chance of getting one that works
1330\& // (remember that a flags value of 0 means autodetection)
1331\& loop_lo = ev_embeddable_backends () & ev_recommended_backends ()
1332\& ? ev_loop_new (ev_embeddable_backends () & ev_recommended_backends ())
1333\& : 0;
1334.Ve
1335.PP
1336.Vb 8
1337\& // if we got one, then embed it, otherwise default to loop_hi
1338\& if (loop_lo)
1339\& {
1340\& ev_embed_init (&embed, 0, loop_lo);
1341\& ev_embed_start (loop_hi, &embed);
1342\& }
1343\& else
1344\& loop_lo = loop_hi;
1345.Ve
1346.IP "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)" 4
1347.IX Item "ev_embed_init (ev_embed *, callback, struct ev_loop *embedded_loop)"
1348.PD 0
1349.IP "ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop)" 4
1350.IX Item "ev_embed_set (ev_embed *, callback, struct ev_loop *embedded_loop)"
1351.PD
1352Configures the watcher to embed the given loop, which must be
1353embeddable. If the callback is \f(CW0\fR, then \f(CW\*(C`ev_embed_sweep\*(C'\fR will be
1354invoked automatically, otherwise it is the responsibility of the callback
1355to invoke it (it will continue to be called until the sweep has been done,
1356if you do not want thta, you need to temporarily stop the embed watcher).
1357.IP "ev_embed_sweep (loop, ev_embed *)" 4
1358.IX Item "ev_embed_sweep (loop, ev_embed *)"
1359Make a single, non-blocking sweep over the embedded loop. This works
1360similarly to \f(CW\*(C`ev_loop (embedded_loop, EVLOOP_NONBLOCK)\*(C'\fR, but in the most
1361apropriate way for embedded loops.
1196.SH "OTHER FUNCTIONS" 1362.SH "OTHER FUNCTIONS"
1197.IX Header "OTHER FUNCTIONS" 1363.IX Header "OTHER FUNCTIONS"
1198There are some other functions of possible interest. Described. Here. Now. 1364There are some other functions of possible interest. Described. Here. Now.
1199.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4 1365.IP "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 4
1200.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)" 1366.IX Item "ev_once (loop, int fd, int events, ev_tstamp timeout, callback)"
1229.Ve 1395.Ve
1230.Sp 1396.Sp
1231.Vb 1 1397.Vb 1
1232\& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0); 1398\& ev_once (STDIN_FILENO, EV_READ, 10., stdin_ready, 0);
1233.Ve 1399.Ve
1234.IP "ev_feed_event (loop, watcher, int events)" 4 1400.IP "ev_feed_event (ev_loop *, watcher *, int revents)" 4
1235.IX Item "ev_feed_event (loop, watcher, int events)" 1401.IX Item "ev_feed_event (ev_loop *, watcher *, int revents)"
1236Feeds the given event set into the event loop, as if the specified event 1402Feeds the given event set into the event loop, as if the specified event
1237had happened for the specified watcher (which must be a pointer to an 1403had happened for the specified watcher (which must be a pointer to an
1238initialised but not necessarily started event watcher). 1404initialised but not necessarily started event watcher).
1239.IP "ev_feed_fd_event (loop, int fd, int revents)" 4 1405.IP "ev_feed_fd_event (ev_loop *, int fd, int revents)" 4
1240.IX Item "ev_feed_fd_event (loop, int fd, int revents)" 1406.IX Item "ev_feed_fd_event (ev_loop *, int fd, int revents)"
1241Feed an event on the given fd, as if a file descriptor backend detected 1407Feed an event on the given fd, as if a file descriptor backend detected
1242the given events it. 1408the given events it.
1243.IP "ev_feed_signal_event (loop, int signum)" 4 1409.IP "ev_feed_signal_event (ev_loop *loop, int signum)" 4
1244.IX Item "ev_feed_signal_event (loop, int signum)" 1410.IX Item "ev_feed_signal_event (ev_loop *loop, int signum)"
1245Feed an event as if the given signal occured (loop must be the default loop!). 1411Feed an event as if the given signal occured (\f(CW\*(C`loop\*(C'\fR must be the default
1412loop!).
1246.SH "LIBEVENT EMULATION" 1413.SH "LIBEVENT EMULATION"
1247.IX Header "LIBEVENT EMULATION" 1414.IX Header "LIBEVENT EMULATION"
1248Libev offers a compatibility emulation layer for libevent. It cannot 1415Libev offers a compatibility emulation layer for libevent. It cannot
1249emulate the internals of libevent, so here are some usage hints: 1416emulate the internals of libevent, so here are some usage hints:
1250.IP "* Use it by including <event.h>, as usual." 4 1417.IP "* Use it by including <event.h>, as usual." 4

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines