| 1 | |||||||||||||||||||||||||||||
| 2 | # IO::Poll.pm | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. | ||||||||||||||||||||||||||||
| 5 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 6 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package IO::Poll; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 15 | 2 | our $VERSION = "1.55"; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | our @EXPORT = qw( POLLIN | |||||||||||||||||||||||||||
| 18 | POLLOUT | ||||||||||||||||||||||||||||
| 19 | POLLERR | ||||||||||||||||||||||||||||
| 20 | POLLHUP | ||||||||||||||||||||||||||||
| 21 | POLLNVAL | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 25 | POLLPRI | ||||||||||||||||||||||||||||
| 26 | POLLRDNORM | ||||||||||||||||||||||||||||
| 27 | POLLWRNORM | ||||||||||||||||||||||||||||
| 28 | POLLRDBAND | ||||||||||||||||||||||||||||
| 29 | POLLWRBAND | ||||||||||||||||||||||||||||
| 30 | POLLNORM | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # [0] maps fd's to requested masks | ||||||||||||||||||||||||||||
| 34 | # [1] maps fd's to returned masks | ||||||||||||||||||||||||||||
| 35 | # [2] maps fd's to handles | ||||||||||||||||||||||||||||
| 36 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | my $self = bless [{},{},{}], $class; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 2 | $self; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 9 | ▶ | sub mask { | ||||||||||||||||||||||||||
|
Subroutine
mask: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 46 | 9 | my $io = shift; | |||||||||||||||||||||||||||
| 47 | 9 | my $fd = fileno($io); | |||||||||||||||||||||||||||
| 48 | 9 | ▶ | return unless defined $fd; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 9 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 6 | my $mask = shift; | |||||||||||||||||||||||||||
| 51 | 6 | ▶ | if($mask) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 3 | $self->[0]{$fd}{$io} = $mask; # the error events are always returned | |||||||||||||||||||||||||||
| 53 | 3 | $self->[1]{$fd} = 0; # output mask | |||||||||||||||||||||||||||
| 54 | 3 | $self->[2]{$io} = $io; # remember handle | |||||||||||||||||||||||||||
| 55 | } else { | ||||||||||||||||||||||||||||
| 56 | 3 | delete $self->[0]{$fd}{$io}; | |||||||||||||||||||||||||||
| 57 | 3 | ▶ | unless(%{$self->[0]{$fd}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | # We no longer have any handles for this FD | ||||||||||||||||||||||||||||
| 59 | 2 | delete $self->[1]{$fd}; | |||||||||||||||||||||||||||
| 60 | 2 | delete $self->[0]{$fd}; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 3 | delete $self->[2]{$io}; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 9 | ▶ | return unless exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io}
MC/DC100%2/2
exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io}
exists $self->[0]{$fd}
exists $self->[0]{$fd}{$io}
Truth table100%3/3
A and B
A= exists $self->[0]{$fd}B= exists $self->[0]{$fd}{$io}
| |||||||||||||||||||||||||||||
| 67 | 5 | return $self->[0]{$fd}{$io}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 4 | ▶ | sub poll { | ||||||||||||||||||||||||||
|
Subroutine
poll: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 4 | my($self,$timeout) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 4 | $self->[1] = {}; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 4 | my($fd,$mask,$iom); | |||||||||||||||||||||||||||
| 77 | 4 | my @poll = (); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 4 | ▶ | while(($fd,$iom) = each %{$self->[0]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 2 | $mask = 0; | |||||||||||||||||||||||||||
| 81 | 2 | $mask |= $_ for values(%$iom); | |||||||||||||||||||||||||||
| 82 | 2 | push(@poll,$fd => $mask); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 4 | ▶ | my $ret = _poll(defined($timeout) ? $timeout * 1000 : -1,@poll); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | ▶ | return $ret | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | unless $ret > 0; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | ▶ | while(@poll) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | my($fd,$got) = splice(@poll,0,2); | |||||||||||||||||||||||||||
| 92 | 1 | ▶ | $self->[1]{$fd} = $got if $got; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | return $ret; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | ▶ | sub events { | ||||||||||||||||||||||||||
|
Subroutine
events: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 100 | 3 | my $io = shift; | |||||||||||||||||||||||||||
| 101 | 3 | my $fd = fileno($io); | |||||||||||||||||||||||||||
| 102 | exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io} | ||||||||||||||||||||||||||||
| 103 | 3 | ▶ | ? $self->[1]{$fd} & ($self->[0]{$fd}{$io}|POLLHUP|POLLERR|POLLNVAL) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 104 | : 0; | ||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 3 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | 3 | my $io = shift; | |||||||||||||||||||||||||||
| 110 | 3 | $self->mask($io,0); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 3 | ▶ | sub handles { | ||||||||||||||||||||||||||
|
Subroutine
handles: called CC=6 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||
| 114 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 3 | ▶ | return values %{$self->[2]} unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my $events = shift || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 118 | 0 | my($fd,$ev,$io,$mask); | |||||||||||||||||||||||||||
| 119 | 0 | my @handles = (); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | while(($fd,$ev) = each %{$self->[1]}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | while (($io,$mask) = each %{$self->[0]{$fd}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $mask |= POLLHUP|POLLERR|POLLNVAL; # must allow these | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | push @handles,$self->[2]{$io} if ($ev & $mask) & $events; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | return @handles; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | 1; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | __END__ | ||||||||||||||||||||||||||||