File Coverage

blib/lib/IO/Concurrent/Runner/Select.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 12 0.0
condition 0 2 0.0
subroutine 4 6 66.6
pod 0 2 0.0
total 16 65 24.6


line stmt bran cond sub pod time code
1             package IO::Concurrent::Runner::Select;
2 1     1   5 use strict;
  1         2  
  1         21  
3 1     1   4 use warnings;
  1         1  
  1         19  
4              
5 1     1   407 use IO::Select;
  1         1323  
  1         41  
6              
7 1     1   5 use Carp ();
  1         2  
  1         317  
8             $Carp::Internal{+__PACKAGE__}++;
9              
10             sub new {
11 0     0 0   my ($class, $contexts, $opts) = @_;
12 0   0       $opts ||= {};
13              
14 0           bless {
15             %$opts,
16             contexts => $contexts,
17             } => $class;
18             }
19              
20             sub run {
21 0     0 0   my $self = shift;
22              
23 0           my %context_map = map { $_->handler->fileno => $_ } @{ $self->{contexts} };
  0            
  0            
24              
25 0           my @aborted_contexts;
26 0           while (%context_map) {
27 0           my ($readable, $writable) = do {
28 0           my @readers = map { $_->handler } grep { $_->current_actor->is_waiting_for_readable } values %context_map;
  0            
  0            
29 0           my @writers = map { $_->handler } grep { $_->current_actor->is_waiting_for_writable } values %context_map;
  0            
  0            
30 0           IO::Select->select(
31             IO::Select->new(@readers),
32             IO::Select->new(@writers),
33             undef,
34             0
35             )
36             };
37              
38 0 0         if (defined $writable) {
39 0           my @writable_context = @context_map{map $_->fileno, @$writable};
40 0           for my $context (@writable_context) {
41 0           $context->current_actor->callback->($context);
42 0 0         push @aborted_contexts => $context if $context->error;
43 0 0         delete $context_map{$context->handler->fileno} if $context->is_completed;
44             }
45             }
46              
47 0 0         if (defined $readable) {
48 0           my @readable_context = @context_map{map $_->fileno, @$readable};
49 0           for my $context (@readable_context) {
50 0           $context->current_actor->callback->($context);
51 0 0         push @aborted_contexts => $context if $context->error;
52 0 0         delete $context_map{$context->handler->fileno} if $context->is_completed;
53             }
54             }
55             }
56              
57 0           for my $context (@aborted_contexts) {
58 0           $self->{on_error}->($context);
59             }
60             }
61              
62             1;
63             __END__