--- cvsroot/EV/EV.pm 2007/10/29 07:24:37 1.10 +++ cvsroot/EV/EV.pm 2007/10/31 21:34:45 1.17 @@ -4,51 +4,75 @@ =head1 SYNOPSIS - use EV; - - # TIMER - - my $w = EV::timer 2, 0, sub { - warn "is called after 2s"; - }; - - my $w = EV::timer 2, 1, sub { - warn "is called roughly every 2s (repeat = 1)"; - }; - - undef $w; # destroy event watcher again - - # IO - - my $w = EV::timer_abs 0, 60, sub { - warn "is called every minute, on the minute, exactly"; - }; - - my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { - my ($w, $events) = @_; # all callbacks get the watcher object and event mask - if ($events & EV::TIMEOUT) { - warn "nothing received on stdin for 10 seconds, retrying"; - } else { - warn "stdin is readable, you entered: ", ; - } - }; - $w->timeout (10); - - # MAINLOOP - EV::dispatch; # loop as long as watchers are active - EV::loop; # the same thing - EV::loop EV::LOOP_ONCE; - EV::loop EV::LOOP_ONSHOT; + use EV; + + # TIMER + + my $w = EV::timer 2, 0, sub { + warn "is called after 2s"; + }; + + my $w = EV::timer 2, 1, sub { + warn "is called roughly every 2s (repeat = 1)"; + }; + + undef $w; # destroy event watcher again + + my $w = EV::timer_abs 0, 60, sub { + warn "is called every minute, on the minute, exactly"; + }; + + # IO + + my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { + my ($w, $revents) = @_; # all callbacks get the watcher object and event mask + if ($revents & EV::TIMEOUT) { + warn "nothing received on stdin for 10 seconds, retrying"; + } else { + warn "stdin is readable, you entered: ", ; + } + }; + $w->timeout (10); + + my $w = EV::timed_io \*STDIN, EV::READ, 30, sub { + my ($w, $revents) = @_; + if ($revents & EV::TIMEOUT) { + warn "nothing entered within 30 seconds, bye bye.\n"; + $w->stop; + } else { + my $line = ; + warn "you entered something, you again have 30 seconds.\n"; + } + }; + + # SIGNALS + + my $w = EV::signal 'QUIT', sub { + warn "sigquit received\n"; + }; + + my $w = EV::signal 3, sub { + warn "sigquit received (this is GNU/Linux, right?)\n"; + }; + + # CHILD/PID STATUS CHANGES + + my $w = EV::child 666, sub { + my ($w, $revents, $status) = @_; + }; + + # MAINLOOP + EV::dispatch; # loop as long as watchers are active + EV::loop; # the same thing + EV::loop EV::LOOP_ONESHOT; # block until some events could be handles + EV::loop EV::LOOP_NONBLOCK; # check and handle some events, but do not wait =head1 DESCRIPTION -This module provides an interface to libevent -(L). You probably should acquaint -yourself with its documentation and source code to be able to use this -module fully. - -Please note thta this module disables the libevent EPOLL method by -default, see BUGS, below, if you need to enable it. +This module provides an interface to libev +(L). You probably should +acquaint yourself with its documentation and source code to be able to use +this module fully. =cut @@ -57,11 +81,21 @@ use strict; BEGIN { - our $VERSION = '0.02'; + our $VERSION = '0.03'; use XSLoader; XSLoader::load "EV", $VERSION; } +@EV::Io::ISA = "EV::Watcher"; +@EV::Time::ISA = "EV::Watcher"; +@EV::Timer::ISA = "EV::Time"; +@EV::Periodic::ISA = "EV::Time"; +@EV::Signal::ISA = "EV::Watcher"; +@EV::Idle::ISA = "EV::Watcher"; +@EV::Prepare::ISA = "EV::Watcher"; +@EV::Check::ISA = "EV::Watcher"; +@EV::Child::ISA = "EV::Watcher"; + =head1 BASIC INTERFACE =over 4 @@ -180,11 +214,21 @@ The C variant doesn't add/start the newly created watcher. -=item my $w = EV::signal $signum, $callback +=item my $w = EV::signal $signal, $callback -=item my $w = EV::signal_ns $signum, $callback +=item my $w = EV::signal_ns $signal, $callback -Call the callback when signal $signum is received. +Call the callback when $signal is received (the signal can be specified +by number or by name, just as with kill or %SIG). Signal watchers are +persistent no natter what. + +EV will grab the signal for the process (the kernel only allows one +component to receive signals) when you start a signal watcher, and +removes it again when you stop it. Pelr does the same when you add/remove +callbacks to %SIG, so watch out. + +Unfortunately, only one handler can be registered per signal. Screw +libevent. The C variant doesn't add/start the newly created watcher. @@ -252,6 +296,11 @@ =back +=head1 THREADS + +Threads are not supported by this in any way. Perl pseudo-threads is evil +and must die. + =head1 BUGS Lots. Libevent itself isn't well tested and rather buggy, and this module @@ -271,9 +320,7 @@ warn "EV: error in callback (ignoring): $@"; }; -our $NPRI = 4; -our $BASE = init; -priority_init $NPRI; +init; push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"];