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

Comparing libev/ev.pod (file contents):
Revision 1.289 by root, Tue Mar 16 17:11:49 2010 UTC vs.
Revision 1.298 by sf-exg, Sat Jul 31 23:00:11 2010 UTC

75While this document tries to be as complete as possible in documenting 75While this document tries to be as complete as possible in documenting
76libev, its usage and the rationale behind its design, it is not a tutorial 76libev, its usage and the rationale behind its design, it is not a tutorial
77on event-based programming, nor will it introduce event-based programming 77on event-based programming, nor will it introduce event-based programming
78with libev. 78with libev.
79 79
80Familarity with event based programming techniques in general is assumed 80Familiarity with event based programming techniques in general is assumed
81throughout this document. 81throughout this document.
82 82
83=head1 ABOUT LIBEV 83=head1 ABOUT LIBEV
84 84
85Libev is an event loop: you register interest in certain events (such as a 85Libev is an event loop: you register interest in certain events (such as a
124this argument. 124this argument.
125 125
126=head2 TIME REPRESENTATION 126=head2 TIME REPRESENTATION
127 127
128Libev represents time as a single floating point number, representing 128Libev represents time as a single floating point number, representing
129the (fractional) number of seconds since the (POSIX) epoch (somewhere 129the (fractional) number of seconds since the (POSIX) epoch (in practise
130near the beginning of 1970, details are complicated, don't ask). This 130somewhere near the beginning of 1970, details are complicated, don't
131type is called C<ev_tstamp>, which is what you should use too. It usually 131ask). This type is called C<ev_tstamp>, which is what you should use
132aliases to the C<double> type in C. When you need to do any calculations 132too. It usually aliases to the C<double> type in C. When you need to do
133on it, you should treat it as some floating point value. Unlike the name 133any calculations on it, you should treat it as some floating point value.
134
134component C<stamp> might indicate, it is also used for time differences 135Unlike the name component C<stamp> might indicate, it is also used for
135throughout libev. 136time differences (e.g. delays) throughout libev.
136 137
137=head1 ERROR HANDLING 138=head1 ERROR HANDLING
138 139
139Libev knows three classes of errors: operating system errors, usage errors 140Libev knows three classes of errors: operating system errors, usage errors
140and internal errors (bugs). 141and internal errors (bugs).
191as this indicates an incompatible change. Minor versions are usually 192as this indicates an incompatible change. Minor versions are usually
192compatible to older versions, so a larger minor version alone is usually 193compatible to older versions, so a larger minor version alone is usually
193not a problem. 194not a problem.
194 195
195Example: Make sure we haven't accidentally been linked against the wrong 196Example: Make sure we haven't accidentally been linked against the wrong
196version. 197version (note, however, that this will not detect ABI mismatches :).
197 198
198 assert (("libev version mismatch", 199 assert (("libev version mismatch",
199 ev_version_major () == EV_VERSION_MAJOR 200 ev_version_major () == EV_VERSION_MAJOR
200 && ev_version_minor () >= EV_VERSION_MINOR)); 201 && ev_version_minor () >= EV_VERSION_MINOR));
201 202
345useful to try out specific backends to test their performance, or to work 346useful to try out specific backends to test their performance, or to work
346around bugs. 347around bugs.
347 348
348=item C<EVFLAG_FORKCHECK> 349=item C<EVFLAG_FORKCHECK>
349 350
350Instead of calling C<ev_default_fork> or C<ev_loop_fork> manually after 351Instead of calling C<ev_loop_fork> manually after a fork, you can also
351a fork, you can also make libev check for a fork in each iteration by 352make libev check for a fork in each iteration by enabling this flag.
352enabling this flag.
353 353
354This works by calling C<getpid ()> on every iteration of the loop, 354This works by calling C<getpid ()> on every iteration of the loop,
355and thus this might slow down your event loop if you do a lot of loop 355and thus this might slow down your event loop if you do a lot of loop
356iterations and little real work, but is usually not noticeable (on my 356iterations and little real work, but is usually not noticeable (on my
357GNU/Linux system for example, C<getpid> is actually a simple 5-insn sequence 357GNU/Linux system for example, C<getpid> is actually a simple 5-insn sequence
567 ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE); 567 ev_default_loop (ev_recommended_backends () | EVBACKEND_KQUEUE);
568 568
569=item struct ev_loop *ev_loop_new (unsigned int flags) 569=item struct ev_loop *ev_loop_new (unsigned int flags)
570 570
571Similar to C<ev_default_loop>, but always creates a new event loop that is 571Similar to C<ev_default_loop>, but always creates a new event loop that is
572always distinct from the default loop. Unlike the default loop, it cannot 572always distinct from the default loop.
573handle signal and child watchers, and attempts to do so will be greeted by
574undefined behaviour (or a failed assertion if assertions are enabled).
575 573
576Note that this function I<is> thread-safe, and the recommended way to use 574Note that this function I<is> thread-safe, and one common way to use
577libev with threads is indeed to create one loop per thread, and using the 575libev with threads is indeed to create one loop per thread, and using the
578default loop in the "main" or "initial" thread. 576default loop in the "main" or "initial" thread.
579 577
580Example: Try to create a event loop that uses epoll and nothing else. 578Example: Try to create a event loop that uses epoll and nothing else.
581 579
583 if (!epoller) 581 if (!epoller)
584 fatal ("no epoll found here, maybe it hides under your chair"); 582 fatal ("no epoll found here, maybe it hides under your chair");
585 583
586=item ev_default_destroy () 584=item ev_default_destroy ()
587 585
588Destroys the default loop again (frees all memory and kernel state 586Destroys the default loop (frees all memory and kernel state etc.). None
589etc.). None of the active event watchers will be stopped in the normal 587of the active event watchers will be stopped in the normal sense, so
590sense, so e.g. C<ev_is_active> might still return true. It is your 588e.g. C<ev_is_active> might still return true. It is your responsibility to
591responsibility to either stop all watchers cleanly yourself I<before> 589either stop all watchers cleanly yourself I<before> calling this function,
592calling this function, or cope with the fact afterwards (which is usually 590or cope with the fact afterwards (which is usually the easiest thing, you
593the easiest thing, you can just ignore the watchers and/or C<free ()> them 591can just ignore the watchers and/or C<free ()> them for example).
594for example).
595 592
596Note that certain global state, such as signal state (and installed signal 593Note that certain global state, such as signal state (and installed signal
597handlers), will not be freed by this function, and related watchers (such 594handlers), will not be freed by this function, and related watchers (such
598as signal and child watchers) would need to be stopped manually. 595as signal and child watchers) would need to be stopped manually.
599 596
614name, you can call it anytime, but it makes most sense after forking, in 611name, you can call it anytime, but it makes most sense after forking, in
615the child process (or both child and parent, but that again makes little 612the child process (or both child and parent, but that again makes little
616sense). You I<must> call it in the child before using any of the libev 613sense). You I<must> call it in the child before using any of the libev
617functions, and it will only take effect at the next C<ev_loop> iteration. 614functions, and it will only take effect at the next C<ev_loop> iteration.
618 615
616Again, you I<have> to call it on I<any> loop that you want to re-use after
617a fork, I<even if you do not plan to use the loop in the parent>. This is
618because some kernel interfaces *cough* I<kqueue> *cough* do funny things
619during fork.
620
619On the other hand, you only need to call this function in the child 621On the other hand, you only need to call this function in the child
620process if and only if you want to use the event library in the child. If 622process if and only if you want to use the event loop in the child. If you
621you just fork+exec, you don't have to call it at all. 623just fork+exec or create a new loop in the child, you don't have to call
624it at all.
622 625
623The function itself is quite fast and it's usually not a problem to call 626The function itself is quite fast and it's usually not a problem to call
624it just in case after a fork. To make this easy, the function will fit in 627it just in case after a fork. To make this easy, the function will fit in
625quite nicely into a call to C<pthread_atfork>: 628quite nicely into a call to C<pthread_atfork>:
626 629
628 631
629=item ev_loop_fork (loop) 632=item ev_loop_fork (loop)
630 633
631Like C<ev_default_fork>, but acts on an event loop created by 634Like C<ev_default_fork>, but acts on an event loop created by
632C<ev_loop_new>. Yes, you have to call this on every allocated event loop 635C<ev_loop_new>. Yes, you have to call this on every allocated event loop
633after fork that you want to re-use in the child, and how you do this is 636after fork that you want to re-use in the child, and how you keep track of
634entirely your own problem. 637them is entirely your own problem.
635 638
636=item int ev_is_default_loop (loop) 639=item int ev_is_default_loop (loop)
637 640
638Returns true when the given loop is, in fact, the default loop, and false 641Returns true when the given loop is, in fact, the default loop, and false
639otherwise. 642otherwise.
640 643
641=item unsigned int ev_loop_count (loop) 644=item unsigned int ev_iteration (loop)
642 645
643Returns the count of loop iterations for the loop, which is identical to 646Returns the current iteration count for the loop, which is identical to
644the number of times libev did poll for new events. It starts at C<0> and 647the number of times libev did poll for new events. It starts at C<0> and
645happily wraps around with enough iterations. 648happily wraps around with enough iterations.
646 649
647This value can sometimes be useful as a generation counter of sorts (it 650This value can sometimes be useful as a generation counter of sorts (it
648"ticks" the number of loop iterations), as it roughly corresponds with 651"ticks" the number of loop iterations), as it roughly corresponds with
649C<ev_prepare> and C<ev_check> calls. 652C<ev_prepare> and C<ev_check> calls - and is incremented between the
653prepare and check phases.
650 654
651=item unsigned int ev_loop_depth (loop) 655=item unsigned int ev_depth (loop)
652 656
653Returns the number of times C<ev_loop> was entered minus the number of 657Returns the number of times C<ev_loop> was entered minus the number of
654times C<ev_loop> was exited, in other words, the recursion depth. 658times C<ev_loop> was exited, in other words, the recursion depth.
655 659
656Outside C<ev_loop>, this number is zero. In a callback, this number is 660Outside C<ev_loop>, this number is zero. In a callback, this number is
657C<1>, unless C<ev_loop> was invoked recursively (or from another thread), 661C<1>, unless C<ev_loop> was invoked recursively (or from another thread),
658in which case it is higher. 662in which case it is higher.
659 663
660Leaving C<ev_loop> abnormally (setjmp/longjmp, cancelling the thread 664Leaving C<ev_loop> abnormally (setjmp/longjmp, cancelling the thread
661etc.), doesn't count as exit. 665etc.), doesn't count as "exit" - consider this as a hint to avoid such
666ungentleman behaviour unless it's really convenient.
662 667
663=item unsigned int ev_backend (loop) 668=item unsigned int ev_backend (loop)
664 669
665Returns one of the C<EVBACKEND_*> flags indicating the event backend in 670Returns one of the C<EVBACKEND_*> flags indicating the event backend in
666use. 671use.
700C<ev_resume> directly afterwards to resume timer processing. 705C<ev_resume> directly afterwards to resume timer processing.
701 706
702Effectively, all C<ev_timer> watchers will be delayed by the time spend 707Effectively, all C<ev_timer> watchers will be delayed by the time spend
703between C<ev_suspend> and C<ev_resume>, and all C<ev_periodic> watchers 708between C<ev_suspend> and C<ev_resume>, and all C<ev_periodic> watchers
704will be rescheduled (that is, they will lose any events that would have 709will be rescheduled (that is, they will lose any events that would have
705occured while suspended). 710occurred while suspended).
706 711
707After calling C<ev_suspend> you B<must not> call I<any> function on the 712After calling C<ev_suspend> you B<must not> call I<any> function on the
708given loop other than C<ev_resume>, and you B<must not> call C<ev_resume> 713given loop other than C<ev_resume>, and you B<must not> call C<ev_resume>
709without a previous call to C<ev_suspend>. 714without a previous call to C<ev_suspend>.
710 715
787C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or 792C<EVUNLOOP_ONE>, which will make the innermost C<ev_loop> call return, or
788C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return. 793C<EVUNLOOP_ALL>, which will make all nested C<ev_loop> calls return.
789 794
790This "unloop state" will be cleared when entering C<ev_loop> again. 795This "unloop state" will be cleared when entering C<ev_loop> again.
791 796
792It is safe to call C<ev_unloop> from otuside any C<ev_loop> calls. 797It is safe to call C<ev_unloop> from outside any C<ev_loop> calls.
793 798
794=item ev_ref (loop) 799=item ev_ref (loop)
795 800
796=item ev_unref (loop) 801=item ev_unref (loop)
797 802
867usually doesn't make much sense to set it to a lower value than C<0.01>, 872usually doesn't make much sense to set it to a lower value than C<0.01>,
868as this approaches the timing granularity of most systems. Note that if 873as this approaches the timing granularity of most systems. Note that if
869you do transactions with the outside world and you can't increase the 874you do transactions with the outside world and you can't increase the
870parallelity, then this setting will limit your transaction rate (if you 875parallelity, then this setting will limit your transaction rate (if you
871need to poll once per transaction and the I/O collect interval is 0.01, 876need to poll once per transaction and the I/O collect interval is 0.01,
872then you can't do more than 100 transations per second). 877then you can't do more than 100 transactions per second).
873 878
874Setting the I<timeout collect interval> can improve the opportunity for 879Setting the I<timeout collect interval> can improve the opportunity for
875saving power, as the program will "bundle" timer callback invocations that 880saving power, as the program will "bundle" timer callback invocations that
876are "near" in time together, by delaying some, thus reducing the number of 881are "near" in time together, by delaying some, thus reducing the number of
877times the process sleeps and wakes up again. Another useful technique to 882times the process sleeps and wakes up again. Another useful technique to
1375 1380
1376For example, to emulate how many other event libraries handle priorities, 1381For example, to emulate how many other event libraries handle priorities,
1377you can associate an C<ev_idle> watcher to each such watcher, and in 1382you can associate an C<ev_idle> watcher to each such watcher, and in
1378the normal watcher callback, you just start the idle watcher. The real 1383the normal watcher callback, you just start the idle watcher. The real
1379processing is done in the idle watcher callback. This causes libev to 1384processing is done in the idle watcher callback. This causes libev to
1380continously poll and process kernel event data for the watcher, but when 1385continuously poll and process kernel event data for the watcher, but when
1381the lock-out case is known to be rare (which in turn is rare :), this is 1386the lock-out case is known to be rare (which in turn is rare :), this is
1382workable. 1387workable.
1383 1388
1384Usually, however, the lock-out model implemented that way will perform 1389Usually, however, the lock-out model implemented that way will perform
1385miserably under the type of load it was designed to handle. In that case, 1390miserably under the type of load it was designed to handle. In that case,
1399 { 1404 {
1400 // stop the I/O watcher, we received the event, but 1405 // stop the I/O watcher, we received the event, but
1401 // are not yet ready to handle it. 1406 // are not yet ready to handle it.
1402 ev_io_stop (EV_A_ w); 1407 ev_io_stop (EV_A_ w);
1403 1408
1404 // start the idle watcher to ahndle the actual event. 1409 // start the idle watcher to handle the actual event.
1405 // it will not be executed as long as other watchers 1410 // it will not be executed as long as other watchers
1406 // with the default priority are receiving events. 1411 // with the default priority are receiving events.
1407 ev_idle_start (EV_A_ &idle); 1412 ev_idle_start (EV_A_ &idle);
1408 } 1413 }
1409 1414
1463 1468
1464If you cannot use non-blocking mode, then force the use of a 1469If you cannot use non-blocking mode, then force the use of a
1465known-to-be-good backend (at the time of this writing, this includes only 1470known-to-be-good backend (at the time of this writing, this includes only
1466C<EVBACKEND_SELECT> and C<EVBACKEND_POLL>). The same applies to file 1471C<EVBACKEND_SELECT> and C<EVBACKEND_POLL>). The same applies to file
1467descriptors for which non-blocking operation makes no sense (such as 1472descriptors for which non-blocking operation makes no sense (such as
1468files) - libev doesn't guarentee any specific behaviour in that case. 1473files) - libev doesn't guarantee any specific behaviour in that case.
1469 1474
1470Another thing you have to watch out for is that it is quite easy to 1475Another thing you have to watch out for is that it is quite easy to
1471receive "spurious" readiness notifications, that is your callback might 1476receive "spurious" readiness notifications, that is your callback might
1472be called with C<EV_READ> but a subsequent C<read>(2) will actually block 1477be called with C<EV_READ> but a subsequent C<read>(2) will actually block
1473because there is no data. Not only are some backends known to create a 1478because there is no data. Not only are some backends known to create a
1541somewhere, as that would have given you a big clue). 1546somewhere, as that would have given you a big clue).
1542 1547
1543=head3 The special problem of accept()ing when you can't 1548=head3 The special problem of accept()ing when you can't
1544 1549
1545Many implementations of the POSIX C<accept> function (for example, 1550Many implementations of the POSIX C<accept> function (for example,
1546found in port-2004 Linux) have the peculiar behaviour of not removing a 1551found in post-2004 Linux) have the peculiar behaviour of not removing a
1547connection from the pending queue in all error cases. 1552connection from the pending queue in all error cases.
1548 1553
1549For example, larger servers often run out of file descriptors (because 1554For example, larger servers often run out of file descriptors (because
1550of resource limits), causing C<accept> to fail with C<ENFILE> but not 1555of resource limits), causing C<accept> to fail with C<ENFILE> but not
1551rejecting the connection, leading to libev signalling readiness on 1556rejecting the connection, leading to libev signalling readiness on
1732 ev_tstamp timeout = last_activity + 60.; 1737 ev_tstamp timeout = last_activity + 60.;
1733 1738
1734 // if last_activity + 60. is older than now, we did time out 1739 // if last_activity + 60. is older than now, we did time out
1735 if (timeout < now) 1740 if (timeout < now)
1736 { 1741 {
1737 // timeout occured, take action 1742 // timeout occurred, take action
1738 } 1743 }
1739 else 1744 else
1740 { 1745 {
1741 // callback was invoked, but there was some activity, re-arm 1746 // callback was invoked, but there was some activity, re-arm
1742 // the watcher to fire in last_activity + 60, which is 1747 // the watcher to fire in last_activity + 60, which is
1769 callback (loop, timer, EV_TIMER); 1774 callback (loop, timer, EV_TIMER);
1770 1775
1771And when there is some activity, simply store the current time in 1776And when there is some activity, simply store the current time in
1772C<last_activity>, no libev calls at all: 1777C<last_activity>, no libev calls at all:
1773 1778
1774 last_actiivty = ev_now (loop); 1779 last_activity = ev_now (loop);
1775 1780
1776This technique is slightly more complex, but in most cases where the 1781This technique is slightly more complex, but in most cases where the
1777time-out is unlikely to be triggered, much more efficient. 1782time-out is unlikely to be triggered, much more efficient.
1778 1783
1779Changing the timeout is trivial as well (if it isn't hard-coded in the 1784Changing the timeout is trivial as well (if it isn't hard-coded in the
3337 ev::io iow; 3342 ev::io iow;
3338 iow.set <myclass, &myclass::io_cb> (&obj); 3343 iow.set <myclass, &myclass::io_cb> (&obj);
3339 3344
3340=item w->set (object *) 3345=item w->set (object *)
3341 3346
3342This is an B<experimental> feature that might go away in a future version.
3343
3344This is a variation of a method callback - leaving out the method to call 3347This is a variation of a method callback - leaving out the method to call
3345will default the method to C<operator ()>, which makes it possible to use 3348will default the method to C<operator ()>, which makes it possible to use
3346functor objects without having to manually specify the C<operator ()> all 3349functor objects without having to manually specify the C<operator ()> all
3347the time. Incidentally, you can then also leave out the template argument 3350the time. Incidentally, you can then also leave out the template argument
3348list. 3351list.
3660define before including (or compiling) any of its files. The default in 3663define before including (or compiling) any of its files. The default in
3661the absence of autoconf is documented for every option. 3664the absence of autoconf is documented for every option.
3662 3665
3663Symbols marked with "(h)" do not change the ABI, and can have different 3666Symbols marked with "(h)" do not change the ABI, and can have different
3664values when compiling libev vs. including F<ev.h>, so it is permissible 3667values when compiling libev vs. including F<ev.h>, so it is permissible
3665to redefine them before including F<ev.h> without breakign compatibility 3668to redefine them before including F<ev.h> without breaking compatibility
3666to a compiled library. All other symbols change the ABI, which means all 3669to a compiled library. All other symbols change the ABI, which means all
3667users of libev and the libev code itself must be compiled with compatible 3670users of libev and the libev code itself must be compiled with compatible
3668settings. 3671settings.
3669 3672
3670=over 4 3673=over 4
3983 3986
3984The highest supported signal number, +1 (or, the number of 3987The highest supported signal number, +1 (or, the number of
3985signals): Normally, libev tries to deduce the maximum number of signals 3988signals): Normally, libev tries to deduce the maximum number of signals
3986automatically, but sometimes this fails, in which case it can be 3989automatically, but sometimes this fails, in which case it can be
3987specified. Also, using a lower number than detected (C<32> should be 3990specified. Also, using a lower number than detected (C<32> should be
3988good for about any system in existance) can save some memory, as libev 3991good for about any system in existence) can save some memory, as libev
3989statically allocates some 12-24 bytes per signal number. 3992statically allocates some 12-24 bytes per signal number.
3990 3993
3991=item EV_PID_HASHSIZE 3994=item EV_PID_HASHSIZE
3992 3995
3993C<ev_child> watchers use a small hash table to distribute workload by 3996C<ev_child> watchers use a small hash table to distribute workload by
4621involves iterating over all running async watchers or all signal numbers. 4624involves iterating over all running async watchers or all signal numbers.
4622 4625
4623=back 4626=back
4624 4627
4625 4628
4626=head1 PORTING FROM 3.X TO 4.X 4629=head1 PORTING FROM LIBEV 3.X TO 4.X
4627 4630
4628The major version 4 introduced some minor incompatible changes to the API. 4631The major version 4 introduced some minor incompatible changes to the API.
4629 4632
4633At the moment, the C<ev.h> header file tries to implement superficial
4634compatibility, so most programs should still compile. Those might be
4635removed in later versions of libev, so better update early than late.
4636
4630=over 4 4637=over 4
4631 4638
4639=item C<ev_loop_count> renamed to C<ev_iteration>
4640
4641=item C<ev_loop_depth> renamed to C<ev_depth>
4642
4643=item C<ev_loop_verify> renamed to C<ev_verify>
4644
4645Most functions working on C<struct ev_loop> objects don't have an
4646C<ev_loop_> prefix, so it was removed. Note that C<ev_loop_fork> is
4647still called C<ev_loop_fork> because it would otherwise clash with the
4648C<ev_fork> typedef.
4649
4632=item C<EV_TIMEOUT> replaced by C<EV_TIMER> in C<revents> 4650=item C<EV_TIMEOUT> renamed to C<EV_TIMER> in C<revents>
4633 4651
4634This is a simple rename - all other watcher types use their name 4652This is a simple rename - all other watcher types use their name
4635as revents flag, and now C<ev_timer> does, too. 4653as revents flag, and now C<ev_timer> does, too.
4636 4654
4637Both C<EV_TIMER> and C<EV_TIMEOUT> symbols were present in 3.x versions 4655Both C<EV_TIMER> and C<EV_TIMEOUT> symbols were present in 3.x versions
4638and continue to be present for the forseeable future, so this is mostly a 4656and continue to be present for the foreseeable future, so this is mostly a
4639documentation change. 4657documentation change.
4640 4658
4641=item C<EV_MINIMAL> mechanism replaced by C<EV_FEATURES> 4659=item C<EV_MINIMAL> mechanism replaced by C<EV_FEATURES>
4642 4660
4643The preprocessor symbol C<EV_MINIMAL> has been replaced by a different 4661The preprocessor symbol C<EV_MINIMAL> has been replaced by a different

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines