Acme-Sort-Sleep
view release on metacpan or search on metacpan
local/lib/perl5/IO/Async/Loop/Select.pm view on Meta::CPAN
sub new
{
my $class = shift;
my $self = $class->__new( @_ );
$self->{rvec} = '';
$self->{wvec} = '';
$self->{evec} = '';
$self->{avec} = ''; # Bitvector of handles always to claim are ready
return $self;
}
=head1 METHODS
=cut
=head2 pre_select
$loop->pre_select( \$readvec, \$writevec, \$exceptvec, \$timeout )
This method prepares the bitvectors for a C<select> call, setting the bits
that the Loop is interested in. It will also adjust the C<$timeout> value if
appropriate, reducing it if the next event timeout the Loop requires is sooner
than the current value.
=over 8
=item \$readvec
=item \$writevec
=item \$exceptvec
Scalar references to the reading, writing and exception bitvectors
=item \$timeout
Scalar reference to the timeout value
=back
=cut
sub pre_select
{
my $self = shift;
my ( $readref, $writeref, $exceptref, $timeref ) = @_;
# BITWISE operations
$$readref |= $self->{rvec};
$$writeref |= $self->{wvec};
$$exceptref |= $self->{evec};
$self->_adjust_timeout( $timeref );
$$timeref = 0 if FAKE_ISREG_READY and length $self->{avec};
# Round up to nearest millisecond
if( $$timeref ) {
my $mils = $$timeref * 1000;
my $fraction = $mils - int $mils;
$$timeref += ( 1 - $fraction ) / 1000 if $fraction;
}
return;
}
=head2 post_select
$loop->post_select( $readvec, $writevec, $exceptvec )
This method checks the returned bitvectors from a C<select> call, and calls
any of the callbacks that are appropriate.
=over 8
=item $readvec
=item $writevec
=item $exceptvec
Scalars containing the read-ready, write-ready and exception bitvectors
=back
=cut
sub post_select
{
my $self = shift;
my ( $readvec, $writevec, $exceptvec ) = @_;
my $iowatches = $self->{iowatches};
my $count = 0;
alarm( IO::Async::Loop->WATCHDOG_INTERVAL ) if WATCHDOG_ENABLE;
foreach my $fd ( keys %$iowatches ) {
my $watch = $iowatches->{$fd} or next;
my $fileno = $watch->[0]->fileno;
if( vec( $readvec, $fileno, 1 ) or
FAKE_ISREG_READY and vec( $self->{avec}, $fileno, 1 ) and vec( $self->{rvec}, $fileno, 1 ) ) {
$count++, $watch->[1]->() if defined $watch->[1];
}
if( vec( $writevec, $fileno, 1 ) or
SELECT_CONNECT_EVEC and vec( $exceptvec, $fileno, 1 ) or
FAKE_ISREG_READY and vec( $self->{avec}, $fileno, 1 ) and vec( $self->{wvec}, $fileno, 1 ) ) {
$count++, $watch->[2]->() if defined $watch->[2];
}
}
# Since we have no way to know if the timeout occured, we'll have to
# attempt to fire any waiting timeout events anyway
( run in 0.789 second using v1.01-cache-2.11-cpan-39bf76dae61 )