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

Comparing libev/ev.pod (file contents):
Revision 1.388 by root, Tue Dec 20 04:08:35 2011 UTC vs.
Revision 1.401 by root, Wed Apr 18 06:06:04 2012 UTC

247the current system, you would need to look at C<ev_embeddable_backends () 247the current system, you would need to look at C<ev_embeddable_backends ()
248& ev_supported_backends ()>, likewise for recommended ones. 248& ev_supported_backends ()>, likewise for recommended ones.
249 249
250See the description of C<ev_embed> watchers for more info. 250See the description of C<ev_embed> watchers for more info.
251 251
252=item ev_set_allocator (void *(*cb)(void *ptr, long size)) 252=item ev_set_allocator (void *(*cb)(void *ptr, long size) throw ())
253 253
254Sets the allocation function to use (the prototype is similar - the 254Sets the allocation function to use (the prototype is similar - the
255semantics are identical to the C<realloc> C89/SuS/POSIX function). It is 255semantics are identical to the C<realloc> C89/SuS/POSIX function). It is
256used to allocate and free memory (no surprises here). If it returns zero 256used to allocate and free memory (no surprises here). If it returns zero
257when memory needs to be allocated (C<size != 0>), the library might abort 257when memory needs to be allocated (C<size != 0>), the library might abort
283 } 283 }
284 284
285 ... 285 ...
286 ev_set_allocator (persistent_realloc); 286 ev_set_allocator (persistent_realloc);
287 287
288=item ev_set_syserr_cb (void (*cb)(const char *msg)) 288=item ev_set_syserr_cb (void (*cb)(const char *msg) throw ())
289 289
290Set the callback function to call on a retryable system call error (such 290Set the callback function to call on a retryable system call error (such
291as failed select, poll, epoll_wait). The message is a printable string 291as failed select, poll, epoll_wait). The message is a printable string
292indicating the system call or subsystem causing the problem. If this 292indicating the system call or subsystem causing the problem. If this
293callback is set, then libev will expect it to remedy the situation, no 293callback is set, then libev will expect it to remedy the situation, no
567 567
568It scales in the same way as the epoll backend, but the interface to the 568It scales in the same way as the epoll backend, but the interface to the
569kernel is more efficient (which says nothing about its actual speed, of 569kernel is more efficient (which says nothing about its actual speed, of
570course). While stopping, setting and starting an I/O watcher does never 570course). While stopping, setting and starting an I/O watcher does never
571cause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to 571cause an extra system call as with C<EVBACKEND_EPOLL>, it still adds up to
572two event changes per incident. Support for C<fork ()> is very bad (but 572two event changes per incident. Support for C<fork ()> is very bad (you
573sane, unlike epoll) and it drops fds silently in similarly hard-to-detect 573might have to leak fd's on fork, but it's more sane than epoll) and it
574cases 574drops fds silently in similarly hard-to-detect cases
575 575
576This backend usually performs well under most conditions. 576This backend usually performs well under most conditions.
577 577
578While nominally embeddable in other event loops, this doesn't work 578While nominally embeddable in other event loops, this doesn't work
579everywhere, so you might need to test for this. And since it is broken 579everywhere, so you might need to test for this. And since it is broken
792without a previous call to C<ev_suspend>. 792without a previous call to C<ev_suspend>.
793 793
794Calling C<ev_suspend>/C<ev_resume> has the side effect of updating the 794Calling C<ev_suspend>/C<ev_resume> has the side effect of updating the
795event loop time (see C<ev_now_update>). 795event loop time (see C<ev_now_update>).
796 796
797=item ev_run (loop, int flags) 797=item bool ev_run (loop, int flags)
798 798
799Finally, this is it, the event handler. This function usually is called 799Finally, this is it, the event handler. This function usually is called
800after you have initialised all your watchers and you want to start 800after you have initialised all your watchers and you want to start
801handling events. It will ask the operating system for any new events, call 801handling events. It will ask the operating system for any new events, call
802the watcher callbacks, an then repeat the whole process indefinitely: This 802the watcher callbacks, and then repeat the whole process indefinitely: This
803is why event loops are called I<loops>. 803is why event loops are called I<loops>.
804 804
805If the flags argument is specified as C<0>, it will keep handling events 805If the flags argument is specified as C<0>, it will keep handling events
806until either no event watchers are active anymore or C<ev_break> was 806until either no event watchers are active anymore or C<ev_break> was
807called. 807called.
808
809The return value is false if there are no more active watchers (which
810usually means "all jobs done" or "deadlock"), and true in all other cases
811(which usually means " you should call C<ev_run> again").
808 812
809Please note that an explicit C<ev_break> is usually better than 813Please note that an explicit C<ev_break> is usually better than
810relying on all watchers to be stopped when deciding when a program has 814relying on all watchers to be stopped when deciding when a program has
811finished (especially in interactive programs), but having a program 815finished (especially in interactive programs), but having a program
812that automatically loops as long as it has to and no longer by virtue 816that automatically loops as long as it has to and no longer by virtue
813of relying on its watchers stopping correctly, that is truly a thing of 817of relying on its watchers stopping correctly, that is truly a thing of
814beauty. 818beauty.
815 819
816This function is also I<mostly> exception-safe - you can break out of 820This function is I<mostly> exception-safe - you can break out of a
817a C<ev_run> call by calling C<longjmp> in a callback, throwing a C++ 821C<ev_run> call by calling C<longjmp> in a callback, throwing a C++
818exception and so on. This does not decrement the C<ev_depth> value, nor 822exception and so on. This does not decrement the C<ev_depth> value, nor
819will it clear any outstanding C<EVBREAK_ONE> breaks. 823will it clear any outstanding C<EVBREAK_ONE> breaks.
820 824
821A flags value of C<EVRUN_NOWAIT> will look for new events, will handle 825A flags value of C<EVRUN_NOWAIT> will look for new events, will handle
822those events and any already outstanding ones, but will not wait and 826those events and any already outstanding ones, but will not wait and
1012invoke the actual watchers inside another context (another thread etc.). 1016invoke the actual watchers inside another context (another thread etc.).
1013 1017
1014If you want to reset the callback, use C<ev_invoke_pending> as new 1018If you want to reset the callback, use C<ev_invoke_pending> as new
1015callback. 1019callback.
1016 1020
1017=item ev_set_loop_release_cb (loop, void (*release)(EV_P), void (*acquire)(EV_P)) 1021=item ev_set_loop_release_cb (loop, void (*release)(EV_P) throw (), void (*acquire)(EV_P) throw ())
1018 1022
1019Sometimes you want to share the same loop between multiple threads. This 1023Sometimes you want to share the same loop between multiple threads. This
1020can be done relatively simply by putting mutex_lock/unlock calls around 1024can be done relatively simply by putting mutex_lock/unlock calls around
1021each call to a libev function. 1025each call to a libev function.
1022 1026
1878 // timeout occurred, take action 1882 // timeout occurred, take action
1879 } 1883 }
1880 else 1884 else
1881 { 1885 {
1882 // callback was invoked, but there was some recent 1886 // callback was invoked, but there was some recent
1883 // activity. simply restart the timer to time out 1887 // activity. simply restart the timer to time out
1884 // after "after" seconds, which is the earliest time 1888 // after "after" seconds, which is the earliest time
1885 // the timeout can occur. 1889 // the timeout can occur.
1886 ev_timer_set (w, after, 0.); 1890 ev_timer_set (w, after, 0.);
1887 ev_timer_start (EV_A_ w); 1891 ev_timer_start (EV_A_ w);
1888 } 1892 }
2108keep up with the timer (because it takes longer than those 10 seconds to 2112keep up with the timer (because it takes longer than those 10 seconds to
2109do stuff) the timer will not fire more than once per event loop iteration. 2113do stuff) the timer will not fire more than once per event loop iteration.
2110 2114
2111=item ev_timer_again (loop, ev_timer *) 2115=item ev_timer_again (loop, ev_timer *)
2112 2116
2113This will act as if the timer timed out and restarts it again if it is 2117This will act as if the timer timed out, and restarts it again if it is
2114repeating. The exact semantics are: 2118repeating. It basically works like calling C<ev_timer_stop>, updating the
2119timeout to the C<repeat> value and calling C<ev_timer_start>.
2115 2120
2121The exact semantics are as in the following rules, all of which will be
2122applied to the watcher:
2123
2124=over 4
2125
2116If the timer is pending, its pending status is cleared. 2126=item If the timer is pending, the pending status is always cleared.
2117 2127
2118If the timer is started but non-repeating, stop it (as if it timed out). 2128=item If the timer is started but non-repeating, stop it (as if it timed
2129out, without invoking it).
2119 2130
2120If the timer is repeating, either start it if necessary (with the 2131=item If the timer is repeating, make the C<repeat> value the new timeout
2121C<repeat> value), or reset the running timer to the C<repeat> value. 2132and start the timer, if necessary.
2133
2134=back
2122 2135
2123This sounds a bit complicated, see L<Be smart about timeouts>, above, for a 2136This sounds a bit complicated, see L<Be smart about timeouts>, above, for a
2124usage example. 2137usage example.
2125 2138
2126=item ev_tstamp ev_timer_remaining (loop, ev_timer *) 2139=item ev_tstamp ev_timer_remaining (loop, ev_timer *)
3633 int exit_main_loop = 0; 3646 int exit_main_loop = 0;
3634 3647
3635 while (!exit_main_loop) 3648 while (!exit_main_loop)
3636 ev_run (EV_DEFAULT_ EVRUN_ONCE); 3649 ev_run (EV_DEFAULT_ EVRUN_ONCE);
3637 3650
3638 // in a model watcher 3651 // in a modal watcher
3639 int exit_nested_loop = 0; 3652 int exit_nested_loop = 0;
3640 3653
3641 while (!exit_nested_loop) 3654 while (!exit_nested_loop)
3642 ev_run (EV_A_ EVRUN_ONCE); 3655 ev_run (EV_A_ EVRUN_ONCE);
3643 3656
3823 switch_to (libev_coro); 3836 switch_to (libev_coro);
3824 } 3837 }
3825 3838
3826That basically suspends the coroutine inside C<wait_for_event> and 3839That basically suspends the coroutine inside C<wait_for_event> and
3827continues the libev coroutine, which, when appropriate, switches back to 3840continues the libev coroutine, which, when appropriate, switches back to
3828this or any other coroutine. I am sure if you sue this your own :) 3841this or any other coroutine.
3829 3842
3830You can do similar tricks if you have, say, threads with an event queue - 3843You can do similar tricks if you have, say, threads with an event queue -
3831instead of storing a coroutine, you store the queue object and instead of 3844instead of storing a coroutine, you store the queue object and instead of
3832switching to a coroutine, you push the watcher onto the queue and notify 3845switching to a coroutine, you push the watcher onto the queue and notify
3833any waiters. 3846any waiters.
3883to use the libev header file and library. 3896to use the libev header file and library.
3884 3897
3885=back 3898=back
3886 3899
3887=head1 C++ SUPPORT 3900=head1 C++ SUPPORT
3901
3902=head2 C API
3903
3904The normal C API should work fine when used from C++: both ev.h and the
3905libev sources can be compiled as C++. Therefore, code that uses the C API
3906will work fine.
3907
3908Proper exception specifications might have to be added to callbacks passed
3909to libev: exceptions may be thrown only from watcher callbacks, all
3910other callbacks (allocator, syserr, loop acquire/release and periodioc
3911reschedule callbacks) must not throw exceptions, and might need a C<throw
3912()> specification. If you have code that needs to be compiled as both C
3913and C++ you can use the C<EV_THROW> macro for this:
3914
3915 static void
3916 fatal_error (const char *msg) EV_THROW
3917 {
3918 perror (msg);
3919 abort ();
3920 }
3921
3922 ...
3923 ev_set_syserr_cb (fatal_error);
3924
3925The only API functions that can currently throw exceptions are C<ev_run>,
3926C<ev_inoke> and C<ev_invoke_pending>.
3927
3928Throwing exceptions in watcher callbacks is only supported if libev itself
3929is compiled with a C++ compiler or your C and C++ environments allow
3930throwing exceptions through C libraries (most do).
3931
3932=head2 C++ API
3888 3933
3889Libev comes with some simplistic wrapper classes for C++ that mainly allow 3934Libev comes with some simplistic wrapper classes for C++ that mainly allow
3890you to use some convenience methods to start/stop watchers and also change 3935you to use some convenience methods to start/stop watchers and also change
3891the callback model to a model using method callbacks on objects. 3936the callback model to a model using method callbacks on objects.
3892 3937
3908with C<operator ()> can be used as callbacks. Other types should be easy 3953with C<operator ()> can be used as callbacks. Other types should be easy
3909to add as long as they only need one additional pointer for context. If 3954to add as long as they only need one additional pointer for context. If
3910you need support for other types of functors please contact the author 3955you need support for other types of functors please contact the author
3911(preferably after implementing it). 3956(preferably after implementing it).
3912 3957
3958For all this to work, your C++ compiler either has to use the same calling
3959conventions as your C compiler (for static member functions), or you have
3960to embed libev and compile libev itself as C++.
3961
3913Here is a list of things available in the C<ev> namespace: 3962Here is a list of things available in the C<ev> namespace:
3914 3963
3915=over 4 3964=over 4
3916 3965
3917=item C<ev::READ>, C<ev::WRITE> etc. 3966=item C<ev::READ>, C<ev::WRITE> etc.
3926=item C<ev::io>, C<ev::timer>, C<ev::periodic>, C<ev::idle>, C<ev::sig> etc. 3975=item C<ev::io>, C<ev::timer>, C<ev::periodic>, C<ev::idle>, C<ev::sig> etc.
3927 3976
3928For each C<ev_TYPE> watcher in F<ev.h> there is a corresponding class of 3977For each C<ev_TYPE> watcher in F<ev.h> there is a corresponding class of
3929the same name in the C<ev> namespace, with the exception of C<ev_signal> 3978the same name in the C<ev> namespace, with the exception of C<ev_signal>
3930which is called C<ev::sig> to avoid clashes with the C<signal> macro 3979which is called C<ev::sig> to avoid clashes with the C<signal> macro
3931defines by many implementations. 3980defined by many implementations.
3932 3981
3933All of those classes have these methods: 3982All of those classes have these methods:
3934 3983
3935=over 4 3984=over 4
3936 3985
4495If defined to be C<1>, libev will compile in support for the Linux inotify 4544If defined to be C<1>, libev will compile in support for the Linux inotify
4496interface to speed up C<ev_stat> watchers. Its actual availability will 4545interface to speed up C<ev_stat> watchers. Its actual availability will
4497be detected at runtime. If undefined, it will be enabled if the headers 4546be detected at runtime. If undefined, it will be enabled if the headers
4498indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. 4547indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled.
4499 4548
4549=item EV_NO_SMP
4550
4551If defined to be C<1>, libev will assume that memory is always coherent
4552between threads, that is, threads can be used, but threads never run on
4553different cpus (or different cpu cores). This reduces dependencies
4554and makes libev faster.
4555
4556=item EV_NO_THREADS
4557
4558If defined to be C<1>, libev will assume that it will never be called
4559from different threads, which is a stronger assumption than C<EV_NO_SMP>,
4560above. This reduces dependencies and makes libev faster.
4561
4500=item EV_ATOMIC_T 4562=item EV_ATOMIC_T
4501 4563
4502Libev requires an integer type (suitable for storing C<0> or C<1>) whose 4564Libev requires an integer type (suitable for storing C<0> or C<1>) whose
4503access is atomic and serialised with respect to other threads or signal 4565access is atomic and serialised with respect to other threads or signal
4504contexts. No such type is easily found in the C language, so you can 4566contexts. No such type is easily found in the C language, so you can
4590 #define EV_USE_POLL 1 4652 #define EV_USE_POLL 1
4591 #define EV_CHILD_ENABLE 1 4653 #define EV_CHILD_ENABLE 1
4592 #define EV_ASYNC_ENABLE 1 4654 #define EV_ASYNC_ENABLE 1
4593 4655
4594The actual value is a bitset, it can be a combination of the following 4656The actual value is a bitset, it can be a combination of the following
4595values: 4657values (by default, all of these are enabled):
4596 4658
4597=over 4 4659=over 4
4598 4660
4599=item C<1> - faster/larger code 4661=item C<1> - faster/larger code
4600 4662
4604code size by roughly 30% on amd64). 4666code size by roughly 30% on amd64).
4605 4667
4606When optimising for size, use of compiler flags such as C<-Os> with 4668When optimising for size, use of compiler flags such as C<-Os> with
4607gcc is recommended, as well as C<-DNDEBUG>, as libev contains a number of 4669gcc is recommended, as well as C<-DNDEBUG>, as libev contains a number of
4608assertions. 4670assertions.
4671
4672The default is off when C<__OPTIMIZE_SIZE__> is defined by your compiler
4673(e.g. gcc with C<-Os>).
4609 4674
4610=item C<2> - faster/larger data structures 4675=item C<2> - faster/larger data structures
4611 4676
4612Replaces the small 2-heap for timer management by a faster 4-heap, larger 4677Replaces the small 2-heap for timer management by a faster 4-heap, larger
4613hash table sizes and so on. This will usually further increase code size 4678hash table sizes and so on. This will usually further increase code size
4614and can additionally have an effect on the size of data structures at 4679and can additionally have an effect on the size of data structures at
4615runtime. 4680runtime.
4681
4682The default is off when C<__OPTIMIZE_SIZE__> is defined by your compiler
4683(e.g. gcc with C<-Os>).
4616 4684
4617=item C<4> - full API configuration 4685=item C<4> - full API configuration
4618 4686
4619This enables priorities (sets C<EV_MAXPRI>=2 and C<EV_MINPRI>=-2), and 4687This enables priorities (sets C<EV_MAXPRI>=2 and C<EV_MINPRI>=-2), and
4620enables multiplicity (C<EV_MULTIPLICITY>=1). 4688enables multiplicity (C<EV_MULTIPLICITY>=1).
4662when you embed libev, only want to use libev functions in a single file, 4730when you embed libev, only want to use libev functions in a single file,
4663and do not want its identifiers to be visible. 4731and do not want its identifiers to be visible.
4664 4732
4665To use this, define C<EV_API_STATIC> and include F<ev.c> in the file that 4733To use this, define C<EV_API_STATIC> and include F<ev.c> in the file that
4666wants to use libev. 4734wants to use libev.
4735
4736This option only works when libev is compiled with a C compiler, as C++
4737doesn't support the required declaration syntax.
4667 4738
4668=item EV_AVOID_STDIO 4739=item EV_AVOID_STDIO
4669 4740
4670If this is set to C<1> at compiletime, then libev will avoid using stdio 4741If this is set to C<1> at compiletime, then libev will avoid using stdio
4671functions (printf, scanf, perror etc.). This will increase the code size 4742functions (printf, scanf, perror etc.). This will increase the code size

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines