Skip to content

Commit

Permalink
EV does not generate circular references
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 9, 2015
1 parent a78a582 commit 67b2377
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
5 changes: 2 additions & 3 deletions lib/Mojo/IOLoop.pm
Expand Up @@ -173,10 +173,9 @@ sub _accepting {
}

sub _id {
my $self = shift;
my $id;
do { $id = md5_sum('c' . steady_time . rand 999) }
while $self->{connections}{$id} || $self->{acceptors}{$id};
do { $id = md5_sum 'c' . steady_time . rand 999 }
while $_[0]->{connections}{$id} || $_[0]->{acceptors}{$id};
return $id;
}

Expand Down
10 changes: 3 additions & 7 deletions lib/Mojo/Reactor/EV.pm
Expand Up @@ -2,7 +2,6 @@ package Mojo::Reactor::EV;
use Mojo::Base 'Mojo::Reactor::Poll';

use EV 4.0;
use Scalar::Util 'weaken';

my $EV;

Expand Down Expand Up @@ -39,7 +38,6 @@ sub watch {
if ($mode == 0) { delete $io->{watcher} }
elsif (my $w = $io->{watcher}) { $w->events($mode) }
else {
weaken $self;
$io->{watcher} = EV::io($fd, $mode, sub { $self->_io($fd, @_) });
}

Expand All @@ -58,14 +56,12 @@ sub _timer {
my ($self, $recurring, $after, $cb) = @_;
$after ||= 0.0001 if $recurring;

my $id = $self->SUPER::_timer(0, 0, $cb);
my $id = $self->_id;
EV::now_update() if $after > 0;
weaken $self;
$self->{timers}{$id}{watcher} = EV::timer(
$after => $after => sub {
my $timer = $self->{timers}{$id};
delete delete($self->{timers}{$id})->{watcher} unless $recurring;
$self->_sandbox("Timer $id", $timer->{cb});
delete $self->{timers}{$id} unless $recurring;
$self->_sandbox("Timer $id", $cb);
}
);

Expand Down
13 changes: 7 additions & 6 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -110,6 +110,12 @@ sub watch {
return $self;
}

sub _id {
my $id;
do { $id = md5_sum 't' . steady_time . rand 999 } while $_[0]->{timers}{$id};
return $id;
}

sub _poll { shift->{poll} ||= IO::Poll->new }

sub _sandbox {
Expand All @@ -119,14 +125,9 @@ sub _sandbox {

sub _timer {
my ($self, $recurring, $after, $cb) = @_;

my $timers = $self->{timers} //= {};
my $id;
do { $id = md5_sum('t' . steady_time . rand 999) } while $timers->{$id};
my $timer = $timers->{$id}
my $timer = $self->{timers}{my $id = $self->_id}
= {cb => $cb, after => $after, time => steady_time + $after};
$timer->{recurring} = $after if $recurring;

return $id;
}

Expand Down

0 comments on commit 67b2377

Please sign in to comment.