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

Comparing libev/ev.pod (file contents):
Revision 1.177 by root, Mon Sep 8 17:27:42 2008 UTC vs.
Revision 1.181 by root, Fri Sep 19 03:47:50 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
1006 { 1023 {
1007 struct ev_io io; 1024 struct ev_io io;
1008 int otherfd; 1025 int otherfd;
1009 void *somedata; 1026 void *somedata;
1010 struct whatever *mostinteresting; 1027 struct whatever *mostinteresting;
1011 } 1028 };
1029
1030 ...
1031 struct my_io w;
1032 ev_io_init (&w.io, my_cb, fd, EV_READ);
1012 1033
1013And 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
1014can cast it back to your own type: 1035can cast it back to your own type:
1015 1036
1016 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)
1020 } 1041 }
1021 1042
1022More interesting and less C-conformant ways of casting your callback type 1043More interesting and less C-conformant ways of casting your callback type
1023instead have been omitted. 1044instead have been omitted.
1024 1045
1025Another common scenario is having some data structure with multiple 1046Another common scenario is to use some data structure with multiple
1026watchers: 1047embedded watchers:
1027 1048
1028 struct my_biggy 1049 struct my_biggy
1029 { 1050 {
1030 int some_data; 1051 int some_data;
1031 ev_timer t1; 1052 ev_timer t1;
1032 ev_timer t2; 1053 ev_timer t2;
1033 } 1054 }
1034 1055
1035In 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
1036you 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:
1037 1060
1038 #include <stddef.h> 1061 #include <stddef.h>
1039 1062
1040 static void 1063 static void
1041 t1_cb (EV_P_ struct ev_timer *w, int revents) 1064 t1_cb (EV_P_ struct ev_timer *w, int revents)
3217 3240
3218=head1 THREADS AND COROUTINES 3241=head1 THREADS AND COROUTINES
3219 3242
3220=head2 THREADS 3243=head2 THREADS
3221 3244
3222Libev itself is completely thread-safe, but it uses no locking. This 3245Libev itself is thread-safe (unless the opposite is specifically
3246documented for a function), but it uses no locking itself. This means that
3223means that you can use as many loops as you want in parallel, as long as 3247you can use as many loops as you want in parallel, as long as only one
3224only one thread ever calls into one libev function with the same loop 3248thread ever calls into one libev function with the same loop parameter:
3225parameter. 3249libev guarentees that different event loops share no data structures that
3250need locking.
3226 3251
3227Or put differently: calls with different loop parameters can be done in 3252Or to put it differently: calls with different loop parameters can be done
3228parallel from multiple threads, calls with the same loop parameter must be 3253concurrently from multiple threads, calls with the same loop parameter
3229done serially (but can be done from different threads, as long as only one 3254must be done serially (but can be done from different threads, as long as
3230thread ever is inside a call at any point in time, e.g. by using a mutex 3255only one thread ever is inside a call at any point in time, e.g. by using
3231per loop). 3256a mutex per loop).
3257
3258Specifically to support threads (and signal handlers), libev implements
3259so-called C<ev_async> watchers, which allow some limited form of
3260concurrency on the same event loop.
3232 3261
3233If you want to know which design (one loop, locking, or multiple loops 3262If you want to know which design (one loop, locking, or multiple loops
3234without or something else still) is best for your problem, then I cannot 3263without or something else still) is best for your problem, then I cannot
3235help you. I can give some generic advice however: 3264help you. I can give some generic advice however:
3236 3265
3254better than you currently do :-) 3283better than you currently do :-)
3255 3284
3256=item * often you need to talk to some other thread which blocks in the 3285=item * often you need to talk to some other thread which blocks in the
3257event loop - C<ev_async> watchers can be used to wake them up from other 3286event loop - C<ev_async> watchers can be used to wake them up from other
3258threads safely (or from signal contexts...). 3287threads safely (or from signal contexts...).
3288
3289=item * some watcher types are only supported in the default loop - use
3290C<ev_async> watchers to tell your other loops about any such events.
3259 3291
3260=back 3292=back
3261 3293
3262=head2 COROUTINES 3294=head2 COROUTINES
3263 3295
3266coroutines (e.g. you can call C<ev_loop> on the same loop from two 3298coroutines (e.g. you can call C<ev_loop> on the same loop from two
3267different coroutines and switch freely between both coroutines running the 3299different coroutines and switch freely between both coroutines running the
3268loop, as long as you don't confuse yourself). The only exception is that 3300loop, as long as you don't confuse yourself). The only exception is that
3269you must not do this from C<ev_periodic> reschedule callbacks. 3301you must not do this from C<ev_periodic> reschedule callbacks.
3270 3302
3271Care has been invested into making sure that libev does not keep local 3303Care has been taken to ensure that libev does not keep local state inside
3272state inside C<ev_loop>, and other calls do not usually allow coroutine 3304C<ev_loop>, and other calls do not usually allow coroutine switches.
3273switches.
3274 3305
3275 3306
3276=head1 COMPLEXITIES 3307=head1 COMPLEXITIES
3277 3308
3278In this section the complexities of (many of) the algorithms used inside 3309In this section the complexities of (many of) the algorithms used inside

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines