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

Comparing libev/ev.pod (file contents):
Revision 1.175 by root, Mon Sep 8 16:36:14 2008 UTC vs.
Revision 1.179 by root, Sat Sep 13 19:14:21 2008 UTC

359writing a server, you should C<accept ()> in a loop to accept as many 359writing a server, you should C<accept ()> in a loop to accept as many
360connections as possible during one iteration. You might also want to have 360connections as possible during one iteration. You might also want to have
361a look at C<ev_set_io_collect_interval ()> to increase the amount of 361a look at C<ev_set_io_collect_interval ()> to increase the amount of
362readiness notifications you get per iteration. 362readiness notifications you get per iteration.
363 363
364This backend maps C<EV_READ> to the C<readfds> set and C<EV_WRITE> to the
365C<writefds> set (and to work around Microsoft Windows bugs, also onto the
366C<exceptfds> set on that platform).
367
364=item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows) 368=item C<EVBACKEND_POLL> (value 2, poll backend, available everywhere except on windows)
365 369
366And this is your standard poll(2) backend. It's more complicated 370And this is your standard poll(2) backend. It's more complicated
367than select, but handles sparse fds better and has no artificial 371than select, but handles sparse fds better and has no artificial
368limit on the number of fds you can use (except it will slow down 372limit on the number of fds you can use (except it will slow down
369considerably with a lot of inactive fds). It scales similarly to select, 373considerably with a lot of inactive fds). It scales similarly to select,
370i.e. O(total_fds). See the entry for C<EVBACKEND_SELECT>, above, for 374i.e. O(total_fds). See the entry for C<EVBACKEND_SELECT>, above, for
371performance tips. 375performance tips.
376
377This backend maps C<EV_READ> to C<POLLIN | POLLERR | POLLHUP>, and
378C<EV_WRITE> to C<POLLOUT | POLLERR | POLLHUP>.
372 379
373=item C<EVBACKEND_EPOLL> (value 4, Linux) 380=item C<EVBACKEND_EPOLL> (value 4, Linux)
374 381
375For few fds, this backend is a bit little slower than poll and select, 382For few fds, this backend is a bit little slower than poll and select,
376but it scales phenomenally better. While poll and select usually scale 383but it scales phenomenally better. While poll and select usually scale
395keep at least one watcher active per fd at all times. 402keep at least one watcher active per fd at all times.
396 403
397While nominally embeddable in other event loops, this feature is broken in 404While nominally embeddable in other event loops, this feature is broken in
398all kernel versions tested so far. 405all kernel versions tested so far.
399 406
407This backend maps C<EV_READ> and C<EV_WRITE> in the same way as
408C<EVBACKEND_POLL>.
409
400=item C<EVBACKEND_KQUEUE> (value 8, most BSD clones) 410=item C<EVBACKEND_KQUEUE> (value 8, most BSD clones)
401 411
402Kqueue deserves special mention, as at the time of this writing, it 412Kqueue deserves special mention, as at the time of this writing, it
403was broken on all BSDs except NetBSD (usually it doesn't work reliably 413was broken on all BSDs except NetBSD (usually it doesn't work reliably
404with anything but sockets and pipes, except on Darwin, where of course 414with anything but sockets and pipes, except on Darwin, where of course
425almost everywhere, you should only use it when you have a lot of sockets 435almost everywhere, you should only use it when you have a lot of sockets
426(for which it usually works), by embedding it into another event loop 436(for which it usually works), by embedding it into another event loop
427(e.g. C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>) and using it only for 437(e.g. C<EVBACKEND_SELECT> or C<EVBACKEND_POLL>) and using it only for
428sockets. 438sockets.
429 439
440This backend maps C<EV_READ> into an C<EVFILT_READ> kevent with
441C<NOTE_EOF>, and C<EV_WRITE> into an C<EVFILT_WRITE> kevent with
442C<NOTE_EOF>.
443
430=item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8) 444=item C<EVBACKEND_DEVPOLL> (value 16, Solaris 8)
431 445
432This is not implemented yet (and might never be, unless you send me an 446This is not implemented yet (and might never be, unless you send me an
433implementation). According to reports, C</dev/poll> only supports sockets 447implementation). According to reports, C</dev/poll> only supports sockets
434and is not embeddable, which would limit the usefulness of this backend 448and is not embeddable, which would limit the usefulness of this backend
449might perform better. 463might perform better.
450 464
451On the positive side, ignoring the spurious readiness notifications, this 465On the positive side, ignoring the spurious readiness notifications, this
452backend actually performed to specification in all tests and is fully 466backend actually performed to specification in all tests and is fully
453embeddable, which is a rare feat among the OS-specific backends. 467embeddable, which is a rare feat among the OS-specific backends.
468
469This backend maps C<EV_READ> and C<EV_WRITE> in the same way as
470C<EVBACKEND_POLL>.
454 471
455=item C<EVBACKEND_ALL> 472=item C<EVBACKEND_ALL>
456 473
457Try all backends (even potentially broken ones that wouldn't be tried 474Try all backends (even potentially broken ones that wouldn't be tried
458with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as 475with C<EVFLAG_AUTO>). Since this is a mask, you can do stuff such as
572Returns the current "event loop time", which is the time the event loop 589Returns the current "event loop time", which is the time the event loop
573received events and started processing them. This timestamp does not 590received events and started processing them. This timestamp does not
574change as long as callbacks are being processed, and this is also the base 591change as long as callbacks are being processed, and this is also the base
575time used for relative timers. You can treat it as the timestamp of the 592time used for relative timers. You can treat it as the timestamp of the
576event occurring (or more correctly, libev finding out about it). 593event occurring (or more correctly, libev finding out about it).
594
595=item ev_now_update (loop)
596
597Establishes the current time by querying the kernel, updating the time
598returned by C<ev_now ()> in the progress. This is a costly operation and
599is usually done automatically within C<ev_loop ()>.
600
601This function is rarely useful, but when some event callback runs for a
602very long time without entering the event loop, updating libev's idea of
603the current time is a good idea.
604
605See also "The special problem of time updates" in the C<ev_timer> section.
577 606
578=item ev_loop (loop, int flags) 607=item ev_loop (loop, int flags)
579 608
580Finally, this is it, the event handler. This function usually is called 609Finally, this is it, the event handler. This function usually is called
581after you initialised all your watchers and you want to start handling 610after you initialised all your watchers and you want to start handling
994 { 1023 {
995 struct ev_io io; 1024 struct ev_io io;
996 int otherfd; 1025 int otherfd;
997 void *somedata; 1026 void *somedata;
998 struct whatever *mostinteresting; 1027 struct whatever *mostinteresting;
999 } 1028 };
1029
1030 ...
1031 struct my_io w;
1032 ev_io_init (&w.io, my_cb, fd, EV_READ);
1000 1033
1001And since your callback will be called with a pointer to the watcher, you 1034And since your callback will be called with a pointer to the watcher, you
1002can cast it back to your own type: 1035can cast it back to your own type:
1003 1036
1004 static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents) 1037 static void my_cb (struct ev_loop *loop, struct ev_io *w_, int revents)
1008 } 1041 }
1009 1042
1010More interesting and less C-conformant ways of casting your callback type 1043More interesting and less C-conformant ways of casting your callback type
1011instead have been omitted. 1044instead have been omitted.
1012 1045
1013Another common scenario is having some data structure with multiple 1046Another common scenario is to use some data structure with multiple
1014watchers: 1047embedded watchers:
1015 1048
1016 struct my_biggy 1049 struct my_biggy
1017 { 1050 {
1018 int some_data; 1051 int some_data;
1019 ev_timer t1; 1052 ev_timer t1;
1020 ev_timer t2; 1053 ev_timer t2;
1021 } 1054 }
1022 1055
1023In this case getting the pointer to C<my_biggy> is a bit more complicated, 1056In this case getting the pointer to C<my_biggy> is a bit more
1024you need to use C<offsetof>: 1057complicated: Either you store the address of your C<my_biggy> struct
1058in the C<data> member of the watcher, or you need to use some pointer
1059arithmetic using C<offsetof> inside your watchers:
1025 1060
1026 #include <stddef.h> 1061 #include <stddef.h>
1027 1062
1028 static void 1063 static void
1029 t1_cb (EV_P_ struct ev_timer *w, int revents) 1064 t1_cb (EV_P_ struct ev_timer *w, int revents)
1203but if multiple timers become ready during the same loop iteration then 1238but if multiple timers become ready during the same loop iteration then
1204order of execution is undefined. 1239order of execution is undefined.
1205 1240
1206=head3 The special problem of time updates 1241=head3 The special problem of time updates
1207 1242
1208Requesting the current time is a costly operation (it usually takes at 1243Establishing the current time is a costly operation (it usually takes at
1209least two syscalls): EV therefore updates it's idea of the current time 1244least two system calls): EV therefore updates its idea of the current
1210only before and after C<ev_loop> polls for new events, which causes the 1245time only before and after C<ev_loop> polls for new events, which causes
1211difference between C<ev_now ()> and C<ev_time ()>. 1246a growing difference between C<ev_now ()> and C<ev_time ()> when handling
1247lots of events.
1212 1248
1213The relative timeouts are calculated relative to the C<ev_now ()> 1249The relative timeouts are calculated relative to the C<ev_now ()>
1214time. This is usually the right thing as this timestamp refers to the time 1250time. This is usually the right thing as this timestamp refers to the time
1215of the event triggering whatever timeout you are modifying/starting. If 1251of the event triggering whatever timeout you are modifying/starting. If
1216you suspect event processing to be delayed and you I<need> to base the 1252you suspect event processing to be delayed and you I<need> to base the
1217timeout on the current time, use something like this to adjust for this: 1253timeout on the current time, use something like this to adjust for this:
1218 1254
1219 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.); 1255 ev_timer_set (&timer, after + ev_now () - ev_time (), 0.);
1256
1257If the event loop is suspended for a long time, you can also force an
1258update of the time returned by C<ev_now ()> by calling C<ev_now_update
1259()>.
1220 1260
1221=head3 Watcher-Specific Functions and Data Members 1261=head3 Watcher-Specific Functions and Data Members
1222 1262
1223=over 4 1263=over 4
1224 1264

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines