Skip to content

Commit

Permalink
fixed perl event loop
Browse files Browse the repository at this point in the history
fixed event::queue initialization
event.pm and perl loops are tested
  • Loading branch information
Uri Guttman committed Sep 10, 2009
1 parent 4536f65 commit 4932dd9
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
18 changes: 8 additions & 10 deletions lib/Stem/Event.pm
Expand Up @@ -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 ;

}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
20 changes: 8 additions & 12 deletions lib/Stem/Event/Queue.pm
Expand Up @@ -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 <<DIE ;
Expand All @@ -64,7 +63,6 @@ DIE

#print fileno( $queue_read ), " FILENO\n" ;

$self = bless {} ;

$queue_read->blocking( 0 ) ;
$queue_read_event = Stem::Event::Read->new(
Expand All @@ -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();
Expand Down
9 changes: 8 additions & 1 deletion lib/Stem/Event/Signal.pm
Expand Up @@ -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
} ;
Expand All @@ -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 ;
}

Expand Down
9 changes: 4 additions & 5 deletions t/event/event_test.pl
Expand Up @@ -11,8 +11,7 @@ BEGIN

use strict ;

#use Test::More tests => 29 ;
use Test::More tests => 24 ;
use Test::More tests => 27 ;

use Symbol ;

Expand All @@ -27,9 +26,9 @@ BEGIN

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 () ;
Expand Down

0 comments on commit 4932dd9

Please sign in to comment.