blib/lib/IO/Epoll.pm

filter: Statementstmt 84.8% 118 / 139 Branchbran 62.5% 25 / 40 Conditioncond 63.6% 7 / 11 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 79.3% 23 / 29 Podpod 47.3% 9 / 19 totaltotal 75.5% 185 / 245 CC 30 SCAR 18.8
CC
30
Cov
79%
handles
26.3
mask
11.0
poll
5.0
CRAP 6.5
↑ Summary const-xs.inc »
1 package IO::Epoll;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 require Exporter;
9 3 ▶use AutoLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our @ISA = qw(Exporter);
13  
14 # Items to export into callers namespace by default. Note: do not export
15 # names by default without a very good reason. Use EXPORT_OK instead.
16 # Do not simply export all your public functions/methods/constants.
17  
18 # This allows declaration use IO::Epoll ':all';
19 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
20 # will save memory.
21 3 our %EXPORT_TAGS = ( 'default' => [ qw(
22 EPOLLERR
23 EPOLLET
24 EPOLLHUP
25 EPOLLIN
26 EPOLLMSG
27 EPOLLOUT
28 EPOLLPRI
29 EPOLLRDBAND
30 EPOLLRDNORM
31 EPOLLWRBAND
32 EPOLLWRNORM
33 EPOLL_CTL_ADD
34 EPOLL_CTL_DEL
35 EPOLL_CTL_MOD
36 epoll_create
37 epoll_ctl
38 epoll_wait
39 epoll_pwait
40 ) ],
41 'compat' => [ qw(
42 POLLIN
43 POLLOUT
44 POLLERR
45 POLLHUP
46 POLLNVAL
47 POLLPRI
48 POLLRDNORM
49 POLLWRNORM
50 POLLRDBAND
51 POLLWRBAND
52 ) ] );
53  
54 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'default'} },
55 3 @{ $EXPORT_TAGS{'compat'} } );
56  
57 3 our @EXPORT = qw(
58 EPOLLERR
59 EPOLLET
60 EPOLLHUP
61 EPOLLIN
62 EPOLLMSG
63 EPOLLOUT
64 EPOLLPRI
65 EPOLLRDBAND
66 EPOLLRDNORM
67 EPOLLWRBAND
68 EPOLLWRNORM
69 EPOLL_CTL_ADD
70 EPOLL_CTL_DEL
71 EPOLL_CTL_MOD
72 epoll_create
73 epoll_ctl
74 epoll_wait
75 epoll_pwait
76 );
77  
78 3 our $VERSION = '0.03';
79  
80 22 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=3 SCAR=11.1
81 # This AUTOLOAD is used to 'autoload' constants from the constant()
82 # XS function.
83  
84 22 my $constname;
85 22 our $AUTOLOAD;
86 22 ($constname = $AUTOLOAD) =~ s/.*:://;
87 22 ▶ croak "&IO::Epoll::constant not defined" if $constname eq 'constant';
Branch: 1 branch
BranchTrueFalseTotal
if $constname eq "constant" 0 22 22
88 22 my ($error, $val) = constant($constname);
89 0 ▶ if ($error) { croak $error; }
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 22 22
90 {
91 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 # Fixed between 5.005_53 and 5.005_61
93 #XXX if ($] >= 5.00561) {
94 #XXX *$AUTOLOAD = sub () { $val };
95 #XXX }
96 #XXX else {
97 22 ▶ *$AUTOLOAD = sub { $val };
Subroutine __ANON__: called CC=1 SCAR=0.0
98 #XXX }
99 }
100 22 goto &$AUTOLOAD;
101 }
102  
103 3 require XSLoader;
104 3 XSLoader::load('IO::Epoll', $VERSION);
105  
106 # Preloaded methods go here.
107  
108 # IO::Poll Compatibility API
109  
110 # [0] maps fd's to requested masks
111 # [1] maps fd's to returned masks
112 # [2] maps fd's to handles
113 # [3] is the epoll fd
114 # [4] is the signal mask, if used. If present will use epoll_pwait() instead of epoll_wait()
115  
116 sub new
117 2 ▶{
Subroutine new: called CC=3 SCAR=14.2
Pod undocumented
118 2 my $package = shift;
119 2 my $self = bless [ {}, {}, {}, undef, undef ] => $package;
120  
121 2 $self->[3] = epoll_create(15);
122 2 ▶ if ($self->[3] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3] < 0) 0 2 2
123 0 ▶ if ($! =~ /not implemented/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! =~ /not implemented/) { } 0 0 0
124 0 die "You need at least Linux 2.5.44 to use IO::Epoll";
125 }
126 else {
127 0 die "epoll_create: $!\n";
128 }
129 }
130 2 return $self;
131 }
132  
133 sub mask
134 9 ▶{
Subroutine mask: called CC=11 SCAR=24.0
Pod documented
135 9 my $self = shift;
136 9 my $io = shift;
137 9 my $fd = fileno $io;
138  
139 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 3 9
140 6 my $mask = shift;
141  
142 6 ▶ if ($mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mask) { } 3 3 6
143 3 my $combined_mask = $mask;
144 3 my $op = &EPOLL_CTL_ADD;
145 3 ▶ if ( exists $self->[0]{$fd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[0]{$fd}) 1 2 3
146 1 $combined_mask |= $_ foreach values %{ $self->[0]{$fd} };
147 1 $op = &EPOLL_CTL_MOD;
148 }
149 3 ▶ return if epoll_ctl($self->[3], $op, $fd, $combined_mask) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if epoll_ctl($self->[3], $op, $fd, $combined_mask) < 0 1 2 3
150 2 $self->[0]{$fd}{$io} = $mask;
151 2 $self->[2]{$io} = $io;
152 }
153 else {
154 3 delete $self->[0]{$fd}{$io};
155 3 delete $self->[2]{$io};
156  
157 3 my $op = &EPOLL_CTL_DEL;
158 3 my $combined_mask = 0;
159 3 ▶ if ( %{ $self->[0]{$fd} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$self->[0]{$fd};}) { } 1 2 3
160 1 $combined_mask |= $_ foreach values %{ $self->[0]{$fd} };
161 1 $op = &EPOLL_CTL_MOD;
162 }
163 else {
164 2 delete $self->[1]{$fd};
165 2 delete $self->[0]{$fd};
166 }
167 3 ▶ return if epoll_ctl($self->[3], $op, $fd, $combined_mask) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if epoll_ctl($self->[3], $op, $fd, $combined_mask) < 0 1 2 3
168 }
169 }
170  
171 7 ▶ return unless exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io} 3 4 7
Condition100%3/3
exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io}
!ll&&!rl&&r
124
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}
ABresult
0X0
100
111
172 4 return $self->[0]{$fd}{$io};
173 }
174  
175 sub poll
176 6 ▶{
Subroutine poll: called CC=5 SCAR=16.2
Pod documented
177 6 my ($self, $timeout) = @_;
178  
179 6 $self->[1] = {};
180  
181 # Set max events to half the number of descriptors, to a minumum of 10
182 6 my $maxevents = int ((values %{ $self->[0] }) / 2);
183 6 ▶ $maxevents = 10 if $maxevents < 10;
Branch: 1 branch
BranchTrueFalseTotal
if $maxevents < 10 6 0 6
184  
185 6 ▶ my $msec = defined $timeout ? $timeout * 1000 : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 6 0 6
186  
187 6 my $ret = epoll_pwait($self->[3], $maxevents, $msec, $self->[4]);
188 6 ▶ return -1 unless defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 1 5 6
189  
190 5 foreach my $event (@$ret) {
191 1 $self->[1]{$event->[0]} = $event->[1];
192 }
193 5 return scalar(@$ret);
194 }
195  
196 sub events
197 3 ▶{
Subroutine events: called CC=3 SCAR=11.0
Pod documented
198 3 my $self = shift;
199 3 my $io = shift;
200 3 my $fd = fileno $io;
201  
202 3 ▶ if ( exists $self->[1]{$fd} && exists $self->[0]{$fd}{$io} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io}) { } 2 1 3
Condition66%2/3
exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io}
!ll&&!rl&&r
102
MC/DC50%1/2
exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io}
exists $self->[1]{$fd} exists $self->[0]{$fd}{$io}
Truth table66%2/3
A and B
A= exists $self->[1]{$fd}B= exists $self->[0]{$fd}{$io}
ABresult
0X0
100
111
203 2 return $self->[1]{$fd} & ($self->[0]{$fd}{$io} |
204 &EPOLLHUP | &EPOLLERR );
205 } else {
206 1 return 0;
207 }
208 }
209  
210 sub remove
211 3 ▶{
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
212 3 my $self = shift;
213 3 my $io = shift;
214 3 $self->mask($io, 0);
215 }
216  
217 sub handles
218 3 ▶{
Subroutine handles: called CC=6 SCAR=32.7
Pod documented
219 3 my $self = shift;
220 3 ▶ return values %{ $self->[2] } unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 3 0 3
221  
222 0 ▶ my $events = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
223 0 my($fd, $ev, $io, $mask);
224 0 my @handles = ();
225  
226 0 ▶ while( ($fd, $ev) = each %{ $self->[1] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[1];}) 0 0 0
227 0 ▶ while ( ($io, $mask) = each %{ $self->[0]{$fd} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self[0]{$fd};}) 0 0 0
228 0 $mask |= &EPOLLHUP | &EPOLLERR; # must allow these
229 0 ▶ push @handles, $self->[2]{$io} if ($ev & $mask) & $events;
Branch: 1 branch
BranchTrueFalseTotal
if $ev & $mask & $events 0 0 0
230 }
231 }
232 0 return @handles;
233 }
234  
235 # Close the epoll handle when object destroyed
236 sub DESTROY
237 2 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
238 2 my $self = shift;
239  
240 2 POSIX::close($self->[3]);
241 }
242  
243 # IO::Ppoll API extension
244  
245 sub sigmask
246 6 ▶{
Subroutine sigmask: called CC=3 SCAR=11.8
Pod documented
247 6 my $self = shift;
248  
249 6 ▶ if( my ( $newmask ) = @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($newmask) = ) { } 0 6 6
250 0 $self->[4] = $newmask;
251 }
252 else {
253 6 ▶ $self->[4] ||= POSIX::SigSet->new();
Condition66%2/3
$self->[4] ||= "POSIX::SigSet"->new
l!l&&r!l&&!r
510
MC/DC0%0/2
$self->[4] ||= "POSIX::SigSet"->new
$self->[4] "POSIX::SigSet"->new
Truth table66%2/3
A ||= B
A= $self->[4]B= "POSIX::SigSet"->new
ABresult
1X1
011
000
254 6 return $self->[4];
255 }
256 }
257  
258 sub sigmask_add
259 2 ▶{
Subroutine sigmask_add: called CC=2 SCAR=6.9
Pod documented
260 2 my $self = shift;
261 2 my @signals = @_;
262  
263 2 my $sigmask = $self->sigmask;
264 2 $sigmask->addset( $_ ) foreach @signals;
265 }
266  
267 sub sigmask_del
268 1 ▶{
Subroutine sigmask_del: called CC=2 SCAR=6.9
Pod documented
269 1 my $self = shift;
270 1 my @signals = @_;
271  
272 1 my $sigmask = $self->sigmask;
273 1 $sigmask->delset( $_ ) foreach @signals;
274 }
275  
276 sub sigmask_ismember
277 3 ▶{
Subroutine sigmask_ismember: called CC=1 SCAR=0.0
Pod documented
278 3 my $self = shift;
279 3 my ( $signal ) = @_;
280  
281 3 return $self->sigmask->ismember( $signal );
282 }
283  
284 # IO::Poll compatibility constants
285  
286 sub POLLNVAL () { 0 };
287 1 ▶sub POLLIN () { &EPOLLIN };
Subroutine POLLIN: called CC=1 SCAR=0.0
Pod undocumented
288 3 ▶sub POLLOUT () { &EPOLLOUT };
Subroutine POLLOUT: called CC=1 SCAR=0.0
Pod undocumented
289 0 ▶sub POLLERR () { &EPOLLERR };
Subroutine POLLERR: not called CC=1 SCAR=6.9
Pod undocumented
290 0 ▶sub POLLHUP () { &EPOLLHUP };
Subroutine POLLHUP: not called CC=1 SCAR=6.9
Pod undocumented
291 2 ▶sub POLLPRI () { &EPOLLPRI };
Subroutine POLLPRI: called CC=1 SCAR=0.0
Pod undocumented
292 0 ▶sub POLLRDNORM () { &EPOLLRDNORM };
Subroutine POLLRDNORM: not called CC=1 SCAR=6.9
Pod undocumented
293 0 ▶sub POLLWRNORM () { &EPOLLWRNORM };
Subroutine POLLWRNORM: not called CC=1 SCAR=6.9
Pod undocumented
294 0 ▶sub POLLRDBAND () { &EPOLLRDBAND };
Subroutine POLLRDBAND: not called CC=1 SCAR=6.9
Pod undocumented
295 0 ▶sub POLLWRBAND () { &EPOLLWRBAND };
Subroutine POLLWRBAND: not called CC=1 SCAR=6.9
Pod undocumented
296  
297 # Autoload methods go after =cut, and are processed by the autosplit program.
298  
299 3 1;
300 __END__
↑ Summary const-xs.inc »