From: Uri Guttman Date: Thu, 10 Sep 2009 06:31:45 +0000 (-0400) Subject: fixed perl event loop X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4932dd97955faf49e7777420bb2fb196db7cb92a;p=urisagit%2FStem.git fixed perl event loop fixed event::queue initialization event.pm and perl loops are tested --- diff --git a/lib/Stem/Event.pm b/lib/Stem/Event.pm index bd80857..9c2caef 100644 --- a/lib/Stem/Event.pm +++ b/lib/Stem/Event.pm @@ -71,7 +71,7 @@ sub init_loop { $loop_class->_init_loop() ; -Stem::Event::Queue::_init_queue() if defined &Stem::Event::Queue::_init_queue ; +#Stem::Event::Queue::_init_queue() if defined &Stem::Event::Queue::_init_queue ; } @@ -316,9 +316,7 @@ sub _get_loop_class { package Stem::Event::Plain ; -BEGIN { - @Stem::Event::Plain::ISA = qw( Stem::Event ) ; -} +our @ISA = qw( Stem::Event ) ; =head2 Stem::Event::Plain::new @@ -365,7 +363,7 @@ sub new { my $self = Stem::Class::parse_args( $attr_spec_plain, @_ ) ; return $self unless ref $self ; - my $err = $self->_core_event_build( 'plain' ) ; + my $err = $self->_build_core_event( 'plain' ) ; return $err if $err ; return $self ; @@ -375,7 +373,7 @@ sub new { package Stem::Event::Signal ; -BEGIN { our @ISA = qw( Stem::Event ) } ; +our @ISA = qw( Stem::Event ) ; =head2 Stem::Event::Signal::new @@ -462,7 +460,7 @@ sub new { package Stem::Event::Timer ; -BEGIN { our @ISA = qw( Stem::Event ) } ; +our @ISA = qw( Stem::Event ) ; =head2 Stem::Event::Timer::new @@ -652,7 +650,7 @@ sub timer_triggered { package Stem::Event::IO ; -BEGIN { our @ISA = qw( Stem::Event ) } ; +our @ISA = qw( Stem::Event ) ; sub init_io_timeout { @@ -726,7 +724,7 @@ sub timed_out { package Stem::Event::Read ; -BEGIN { our @ISA = qw( Stem::Event::IO ) } +our @ISA = qw( Stem::Event::IO ) ; =head2 Stem::Event::Read::new @@ -825,7 +823,7 @@ sub new { package Stem::Event::Write ; -BEGIN { our @ISA = qw( Stem::Event::IO ) } ; +our @ISA = qw( Stem::Event::IO ) ; =head2 Stem::Event::Write::new diff --git a/lib/Stem/Event/Queue.pm b/lib/Stem/Event/Queue.pm index e6549d9..53db4fa 100644 --- a/lib/Stem/Event/Queue.pm +++ b/lib/Stem/Event/Queue.pm @@ -48,14 +48,13 @@ use warnings ; use Socket; use IO::Handle ; -use base 'Exporter' ; -our @EXPORT = qw( &mark_not_empty ) ; +my( $self, $queue_read, $queue_write, $queue_read_event, $queue_has_event ) ; -my( $queue_read, $queue_write, $queue_read_event ) ; +sub _init_event_queue { -my $self ; + return if $self ; -sub _init_queue { + $self = bless {} ; socketpair( $queue_read, $queue_write, AF_UNIX, SOCK_STREAM, PF_UNSPEC ) || die <blocking( 0 ) ; $queue_read_event = Stem::Event::Read->new( @@ -78,27 +76,25 @@ DIE } -my $queue_is_marked ; - -sub mark_not_empty { +sub queue_has_event { my( $always_mark ) = @_ ; # don't mark the queue if it is already marked and we aren't forced # the signal queue always marks the queue - return if $queue_is_marked && !$always_mark ; + return if $queue_has_event && !$always_mark ; syswrite( $queue_write, 'x' ) ; - $queue_is_marked = 1 ; + $queue_has_event = 1 ; } sub readable { sysread( $queue_read, my $buf, 10 ) ; - $queue_is_marked = 0 ; + $queue_has_event = 0 ; # Stem::Event::Plain::process_queue(); Stem::Event::Signal::process_signal_queue(); diff --git a/lib/Stem/Event/Signal.pm b/lib/Stem/Event/Signal.pm index c9d723e..95e1023 100644 --- a/lib/Stem/Event/Signal.pm +++ b/lib/Stem/Event/Signal.pm @@ -32,7 +32,8 @@ sub _build { $SIG{ $signal } = $cached_handlers{$signal} ||= sub { - mark_not_empty() ; + Stem::Event::Queue::queue_has_event() ; + #print "HIT $signal\n"; push @signal_queue, $signal } ; @@ -42,6 +43,12 @@ sub _build { $signal2event{$signal} = $self ; #print "$signal = $SIG{ $signal }\n" ; + +# make sure the event queue is set up so we can handle signals in the +# event loop + + Stem::Event::Queue::_init_event_queue() ; + return ; } diff --git a/t/event/event_test.pl b/t/event/event_test.pl index 11227d1..673b79b 100644 --- a/t/event/event_test.pl +++ b/t/event/event_test.pl @@ -11,8 +11,7 @@ BEGIN { use strict ; -#use Test::More tests => 29 ; -use Test::More tests => 24 ; +use Test::More tests => 27 ; use Symbol ; @@ -27,9 +26,9 @@ exit ; sub test_events { -# test_null_events() ; -# test_plain_events () ; -# test_signal_events () ; + test_null_events() ; + test_plain_events () ; + test_signal_events () ; test_hard_timer_events () ; test_soft_timer_events () ; test_io_events () ;