Skip to content

Commit

Permalink
avoid floating point fun in reactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Feb 18, 2015
1 parent 9e637b3 commit 582dcd3
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/Mojo/Reactor/Poll.pm
Expand Up @@ -32,17 +32,17 @@ sub one_tick {
# Stop automatically if there is nothing to watch
return $self->stop unless keys %{$self->{timers}} || keys %{$self->{io}};

# Calculate ideal timeout based on timers
# Calculate ideal timeout based on timers and round up to next millisecond
my $min = min map { $_->{time} } values %{$self->{timers}};
my $timeout = defined $min ? ($min - steady_time) : 0.5;
$timeout = 0 if $timeout < 0;
my $timeout = defined $min ? $min - steady_time : 0.5;
$timeout = $timeout < 0 ? 0 : int($timeout * 1000) + 1;

# I/O
if (keys %{$self->{io}}) {
my @poll = map { $_ => $self->{io}{$_}{mode} } keys %{$self->{io}};

# This may break in the future, but is worth it for performance
if (IO::Poll::_poll($timeout * 1000, @poll) > 0) {
if (IO::Poll::_poll($timeout, @poll) > 0) {
while (my ($fd, $mode) = splice @poll, 0, 2) {

if ($mode & (POLLIN | POLLPRI | POLLNVAL | POLLHUP | POLLERR)) {
Expand All @@ -56,7 +56,7 @@ sub one_tick {
}

# Wait for timeout if poll can't be used
elsif ($timeout) { usleep $timeout * 1000000 }
elsif ($timeout) { usleep $timeout * 1000 }

# Timers (time should not change in between timers)
my $now = steady_time;
Expand Down

0 comments on commit 582dcd3

Please sign in to comment.