ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.pm
(Generate patch)

Comparing EV/EV.pm (file contents):
Revision 1.73 by root, Fri Dec 21 05:10:01 2007 UTC vs.
Revision 1.85 by root, Sat Mar 8 16:00:07 2008 UTC

35 warn "sigquit received\n"; 35 warn "sigquit received\n";
36 }; 36 };
37 37
38 # CHILD/PID STATUS CHANGES 38 # CHILD/PID STATUS CHANGES
39 39
40 my $w = EV::child 666, sub { 40 my $w = EV::child 666, 0, sub {
41 my ($w, $revents) = @_; 41 my ($w, $revents) = @_;
42 my $status = $w->rstatus; 42 my $status = $w->rstatus;
43 }; 43 };
44 44
45 # STAT CHANGES 45 # STAT CHANGES
68package EV; 68package EV;
69 69
70use strict; 70use strict;
71 71
72BEGIN { 72BEGIN {
73 our $VERSION = '2.0'; 73 our $VERSION = '3.1';
74 use XSLoader; 74 use XSLoader;
75 XSLoader::load "EV", $VERSION; 75 XSLoader::load "EV", $VERSION;
76} 76}
77 77
78@EV::IO::ISA = 78@EV::IO::ISA =
84@EV::Idle::ISA = 84@EV::Idle::ISA =
85@EV::Prepare::ISA = 85@EV::Prepare::ISA =
86@EV::Check::ISA = 86@EV::Check::ISA =
87@EV::Embed::ISA = 87@EV::Embed::ISA =
88@EV::Fork::ISA = 88@EV::Fork::ISA =
89@EV::Async::ISA =
89 "EV::Watcher"; 90 "EV::Watcher";
90 91
91@EV::Loop::Default::ISA = "EV::Loop"; 92@EV::Loop::Default::ISA = "EV::Loop";
92 93
93=head1 EVENT LOOPS 94=head1 EVENT LOOPS
102selecting a working backend (which for example rules out kqueue on most 103selecting a working backend (which for example rules out kqueue on most
103BSDs). Modules should, unless they have "special needs" always use the 104BSDs). Modules should, unless they have "special needs" always use the
104default loop as this is fastest (perl-wise), best supported by other 105default loop as this is fastest (perl-wise), best supported by other
105modules (e.g. AnyEvent or Coro) and most portable event loop. 106modules (e.g. AnyEvent or Coro) and most portable event loop.
106 107
107For specific programs you cna create additional event loops dynamically. 108For specific programs you can create additional event loops dynamically.
108 109
109=over 4 110=over 4
110 111
111=item $loop = new EV::loop [$flags] 112=item $loop = new EV::loop [$flags]
112 113
126Must be called after a fork in the child, before entering or continuing 127Must be called after a fork in the child, before entering or continuing
127the event loop. An alternative is to use C<EV::FLAG_FORKCHECK> which calls 128the event loop. An alternative is to use C<EV::FLAG_FORKCHECK> which calls
128this fucntion automatically, at some performance loss (refer to the libev 129this fucntion automatically, at some performance loss (refer to the libev
129documentation). 130documentation).
130 131
132=item $loop = EV::default_loop [$flags]
133
134Return the default loop (which is a singleton object).
135
131=back 136=back
132 137
133 138
134=head1 BASIC INTERFACE 139=head1 BASIC INTERFACE
135 140
140Must contain a reference to a function that is called when a callback 145Must contain a reference to a function that is called when a callback
141throws an exception (with $@ containing the error). The default prints an 146throws an exception (with $@ containing the error). The default prints an
142informative message and continues. 147informative message and continues.
143 148
144If this callback throws an exception it will be silently ignored. 149If this callback throws an exception it will be silently ignored.
150
151=item $flags = EV::supported_backends
152
153=item $flags = EV::recommended_backends
154
155=item $flags = EV::embeddable_backends
156
157Returns the set (see C<EV::BACKEND_*> flags) of backends supported by this
158instance of EV, the set of recommended backends (supposed to be good) for
159this platform and the set of embeddable backends (see EMBED WATCHERS).
160
161=item EV::sleep $seconds
162
163Block the process for the given number of (fractional) seconds.
145 164
146=item $time = EV::time 165=item $time = EV::time
147 166
148Returns the current time in (fractional) seconds since the epoch. 167Returns the current time in (fractional) seconds since the epoch.
149 168
208If timeout is C<undef> or negative, then there will be no 227If timeout is C<undef> or negative, then there will be no
209timeout. Otherwise a EV::timer with this value will be started. 228timeout. Otherwise a EV::timer with this value will be started.
210 229
211When an error occurs or either the timeout or I/O watcher triggers, then 230When an error occurs or either the timeout or I/O watcher triggers, then
212the callback will be called with the received event set (in general 231the callback will be called with the received event set (in general
213you can expect it to be a combination of C<EV:ERROR>, C<EV::READ>, 232you can expect it to be a combination of C<EV::ERROR>, C<EV::READ>,
214C<EV::WRITE> and C<EV::TIMEOUT>). 233C<EV::WRITE> and C<EV::TIMEOUT>).
215 234
216EV::once doesn't return anything: the watchers stay active till either 235EV::once doesn't return anything: the watchers stay active till either
217of them triggers, then they will be stopped and freed, and the callback 236of them triggers, then they will be stopped and freed, and the callback
218invoked. 237invoked.
228=item EV::feed_signal_event ($signal) 247=item EV::feed_signal_event ($signal)
229 248
230Feed a signal event into EV. EV will react to this call as if the signal 249Feed a signal event into EV. EV will react to this call as if the signal
231specified by C<$signal> had occured. 250specified by C<$signal> had occured.
232 251
252=item EV::set_io_collect_interval $time
253
254=item $loop->set_io_collect_interval ($time)
255
256=item EV::set_timeout_collect_interval $time
257
258=item $loop->set_timeout_collect_interval ($time)
259
260These advanced functions set the minimum block interval when polling for I/O events and the minimum
261wait interval for timer events. See the libev documentation at
262L<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#FUNCTIONS_CONTROLLING_THE_EVENT_LOOP> for
263a more detailed discussion.
264
233=back 265=back
234 266
235 267
236=head1 WATCHER OBJECTS 268=head1 WATCHER OBJECTS
237 269
239event. For instance, if you want to wait for STDIN to become readable, you 271event. For instance, if you want to wait for STDIN to become readable, you
240would create an EV::io watcher for that: 272would create an EV::io watcher for that:
241 273
242 my $watcher = EV::io *STDIN, EV::READ, sub { 274 my $watcher = EV::io *STDIN, EV::READ, sub {
243 my ($watcher, $revents) = @_; 275 my ($watcher, $revents) = @_;
244 warn "yeah, STDIN should not be readable without blocking!\n" 276 warn "yeah, STDIN should now be readable without blocking!\n"
245 }; 277 };
246 278
247All watchers can be active (waiting for events) or inactive (paused). Only 279All watchers can be active (waiting for events) or inactive (paused). Only
248active watchers will have their callbacks invoked. All callbacks will be 280active watchers will have their callbacks invoked. All callbacks will be
249called with at least two arguments: the watcher and a bitmask of received 281called with at least two arguments: the watcher and a bitmask of received
333Feed some events on this watcher into EV. EV will react to this call as if 365Feed some events on this watcher into EV. EV will react to this call as if
334the watcher had received the given C<$revents> mask. 366the watcher had received the given C<$revents> mask.
335 367
336=item $revents = $w->clear_pending 368=item $revents = $w->clear_pending
337 369
338If the watcher is pending, this function returns clears its pending status 370If the watcher is pending, this function clears its pending status and
339and returns its C<$revents> bitset (as if its callback was invoked). If the 371returns its C<$revents> bitset (as if its callback was invoked). If the
340watcher isn't pending it does nothing and returns C<0>. 372watcher isn't pending it does nothing and returns C<0>.
341 373
342=item $previous_state = $w->keepalive ($bool) 374=item $previous_state = $w->keepalive ($bool)
343 375
344Normally, C<EV::loop> will return when there are no active watchers 376Normally, C<EV::loop> will return when there are no active watchers
345(which is a "deadlock" because no progress can be made anymore). This is 377(which is a "deadlock" because no progress can be made anymore). This is
346convinient because it allows you to start your watchers (and your jobs), 378convinient because it allows you to start your watchers (and your jobs),
347call C<EV::loop> once and when it returns you know that all your jobs are 379call C<EV::loop> once and when it returns you know that all your jobs are
348finished (or they forgot to register some watchers for their task :). 380finished (or they forgot to register some watchers for their task :).
349 381
350Sometimes, however, this gets in your way, for example when you the module 382Sometimes, however, this gets in your way, for example when the module
351that calls C<EV::loop> (usually the main program) is not the same module 383that calls C<EV::loop> (usually the main program) is not the same module
352as a long-living watcher (for example a DNS client module written by 384as a long-living watcher (for example a DNS client module written by
353somebody else even). Then you might want any outstanding requests to be 385somebody else even). Then you might want any outstanding requests to be
354handled, but you would not want to keep C<EV::loop> from returning just 386handled, but you would not want to keep C<EV::loop> from returning just
355because you happen to have this long-running UDP port watcher. 387because you happen to have this long-running UDP port watcher.
363Example: Register an I/O watcher for some UDP socket but do not keep the 395Example: Register an I/O watcher for some UDP socket but do not keep the
364event loop from running just because of that watcher. 396event loop from running just because of that watcher.
365 397
366 my $udp_socket = ... 398 my $udp_socket = ...
367 my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; 399 my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... };
368 $udp_watcher->keepalive (0); 400 $1000udp_watcher->keepalive (0);
401
402=item $loop = $w->loop
403
404Return the loop that this watcher is attached to.
369 405
370=back 406=back
371 407
372 408
373=head1 WATCHER TYPES 409=head1 WATCHER TYPES
380 416
381=item $w = EV::io $fileno_or_fh, $eventmask, $callback 417=item $w = EV::io $fileno_or_fh, $eventmask, $callback
382 418
383=item $w = EV::io_ns $fileno_or_fh, $eventmask, $callback 419=item $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
384 420
385=item $w = $loop->io 8$fileno_or_fh, $eventmask, $callback) 421=item $w = $loop->io ($fileno_or_fh, $eventmask, $callback)
386 422
387=item $w = $loop->io_ns ($fileno_or_fh, $eventmask, $callback) 423=item $w = $loop->io_ns ($fileno_or_fh, $eventmask, $callback)
388 424
389As long as the returned watcher object is alive, call the C<$callback> 425As long as the returned watcher object is alive, call the C<$callback>
390when at least one of events specified in C<$eventmask> occurs. 426when at least one of events specified in C<$eventmask> occurs.
498This time simply fires at the wallclock time C<$at> and doesn't repeat. It 534This time simply fires at the wallclock time C<$at> and doesn't repeat. It
499will not adjust when a time jump occurs, that is, if it is to be run 535will not adjust when a time jump occurs, that is, if it is to be run
500at January 1st 2011 then it will run when the system time reaches or 536at January 1st 2011 then it will run when the system time reaches or
501surpasses this time. 537surpasses this time.
502 538
503=item * non-repeating interval timer ($interval > 0, $reschedule_cb = 0) 539=item * repeating interval timer ($interval > 0, $reschedule_cb = 0)
504 540
505In this mode the watcher will always be scheduled to time out at the 541In this mode the watcher will always be scheduled to time out at the
506next C<$at + N * $interval> time (for some integer N) and then repeat, 542next C<$at + N * $interval> time (for some integer N) and then repeat,
507regardless of any time jumps. 543regardless of any time jumps.
508 544
609 645
610=head3 CHILD WATCHERS - watch out for process status changes 646=head3 CHILD WATCHERS - watch out for process status changes
611 647
612=over 4 648=over 4
613 649
614=item $w = EV::child $pid, $callback 650=item $w = EV::child $pid, $trace, $callback
615 651
616=item $w = EV::child_ns $pid, $callback 652=item $w = EV::child_ns $pid, $trace, $callback
617 653
618=item $w = $loop->child ($pid, $callback) 654=item $w = $loop->child ($pid, $trace, $callback)
619 655
620=item $w = $loop->child_ns ($pid, $callback) 656=item $w = $loop->child_ns ($pid, $trace, $callback)
621 657
622Call the callback when a status change for pid C<$pid> (or any pid if 658Call the callback when a status change for pid C<$pid> (or any pid
623C<$pid> is 0) has been received. More precisely: when the process receives 659if C<$pid> is 0) has been received (a status change happens when the
660process terminates or is killed, or, when trace is true, additionally when
661it is stopped or continued). More precisely: when the process receives
624a C<SIGCHLD>, EV will fetch the outstanding exit/wait status for all 662a C<SIGCHLD>, EV will fetch the outstanding exit/wait status for all
625changed/zombie children and call the callback. 663changed/zombie children and call the callback.
626 664
627It is valid (and fully supported) to install a child watcher after a child 665It is valid (and fully supported) to install a child watcher after a child
628has exited but before the event loop has started its next iteration (for 666has exited but before the event loop has started its next iteration (for
635You can have as many pid watchers per pid as you want, they will all be 673You can have as many pid watchers per pid as you want, they will all be
636called. 674called.
637 675
638The C<child_ns> variant doesn't start (activate) the newly created watcher. 676The C<child_ns> variant doesn't start (activate) the newly created watcher.
639 677
640=item $w->set ($pid) 678=item $w->set ($pid, $trace)
641 679
642Reconfigures the watcher, see the constructor above for details. Can be called at 680Reconfigures the watcher, see the constructor above for details. Can be called at
643any time. 681any time.
644 682
645=item $current_pid = $w->pid 683=item $current_pid = $w->pid
646
647=item $old_pid = $w->pid ($new_pid)
648 684
649Returns the previously set process id and optionally set a new one. 685Returns the previously set process id and optionally set a new one.
650 686
651=item $exit_status = $w->rstatus 687=item $exit_status = $w->rstatus
652 688
787 823
788=item $w = EV::prepare_ns $callback 824=item $w = EV::prepare_ns $callback
789 825
790=item $w = $loop->prepare ($callback) 826=item $w = $loop->prepare ($callback)
791 827
792=item $w = $loop->prepare_ns 8$callback) 828=item $w = $loop->prepare_ns ($callback)
793 829
794Call the callback just before the process would block. You can still 830Call the callback just before the process would block. You can still
795create/modify any watchers at this point. 831create/modify any watchers at this point.
796 832
797See the EV::check watcher, below, for explanations and an example. 833See the EV::check watcher, below, for explanations and an example.
886The C<fork_ns> variant doesn't start (activate) the newly created watcher. 922The C<fork_ns> variant doesn't start (activate) the newly created watcher.
887 923
888=back 924=back
889 925
890 926
927=head3 EMBED WATCHERS - when one backend isn't enough...
928
929This is a rather advanced watcher type that lets you embed one event loop
930into another (currently only IO events are supported in the embedded
931loop, other types of watchers might be handled in a delayed or incorrect
932fashion and must not be used).
933
934See the libev documentation at
935L<http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_embed_code_when_one_backend_>
936for more details.
937
938In short, this watcher is most useful on BSD systems without working
939kqueue to still be able to handle a large number of sockets:
940
941 my $socket_loop;
942
943 # check wether we use SELECT or POLL _and_ KQUEUE is supported
944 if (
945 (EV::backend & (EV::BACKEND_POLL | EV::BACKEND_SELECT))
946 && (EV::supported_backends & EV::embeddable_backends & EV::BACKEND_KQUEUE)
947 ) {
948 # use kqueue for sockets
949 $socket_loop = new EV::Loop EV::BACKEND_KQUEUE | EV::FLAG_NOENV;
950 }
951
952 # use the default loop otherwise
953 $socket_loop ||= EV::default_loop;
954
955=over 4
956
957=item $w = EV::embed $otherloop, $callback
958
959=item $w = EV::embed_ns $otherloop, $callback
960
961=item $w = $loop->embed ($otherloop, $callback)
962
963=item $w = $loop->embed_ns ($otherloop, $callback)
964
965Call the callback when the embedded event loop (C<$otherloop>) has any
966I/O activity. The C<$callback> should alwas be specified as C<undef> in
967this version of EV, which means the embedded event loop will be managed
968automatically.
969
970The C<embed_ns> variant doesn't start (activate) the newly created watcher.
971
972=back
973
974=head3 ASYNC WATCHERS - how to wake up another event loop
975
976Async watchers are provided by EV, but have little use in perl directly, as perl
977neither supports threads nor direct access to signal handlers or other
978contexts where they could be of value.
979
980It is, however, possible to use them from the XS level.
981
982Please see the libev documentation for further details.
983
984
891=head1 PERL SIGNALS 985=head1 PERL SIGNALS
892 986
893While Perl signal handling (C<%SIG>) is not affected by EV, the behaviour 987While Perl signal handling (C<%SIG>) is not affected by EV, the behaviour
894with EV is as the same as any other C library: Perl-signals will only be 988with EV is as the same as any other C library: Perl-signals will only be
895handled when Perl runs, which means your signal handler might be invoked 989handled when Perl runs, which means your signal handler might be invoked
902to be called on every event loop iteration by installing a C<EV::check> 996to be called on every event loop iteration by installing a C<EV::check>
903watcher: 997watcher:
904 998
905 my $async_check = EV::check sub { }; 999 my $async_check = EV::check sub { };
906 1000
907This ensures that perl shortly gets into control for a short time, and 1001This ensures that perl gets into control for a short time to handle any
908also ensures slower overall operation. 1002pending signals, and also ensures (slightly) slower overall operation.
909 1003
910=head1 THREADS 1004=head1 THREADS
911 1005
912Threads are not supported by this module in any way. Perl pseudo-threads 1006Threads are not supported by this module in any way. Perl pseudo-threads
913is evil stuff and must die. As soon as Perl gains real threads I will work 1007is evil stuff and must die. As soon as Perl gains real threads I will work

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines