blib/lib/IO/SigGuard.pm

filter: Statementstmt 92.5% 37 / 40 Branchbran 45.8% 11 / 24 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 85.7% 6 / 7 Podpod 33.3% 1 / 3 totaltotal 67.4% 58 / 86 SCAR 31.1
CC
16
Cov
71%
syswrite
16.7
select
7.5
sysread
5.9
CRAP 22.4
↑ Summary
1 package IO::SigGuard;
2  
3 =encoding utf-8
4  
5 =head1 NAME
6  
7 IO::SigGuard - SA_RESTART in pure Perl
8  
9 =head1 SYNOPSIS
10  
11 IO::SigGuard::sysread( $fh, $buf, $size );
12 IO::SigGuard::sysread( $fh, $buf, $size, $offset );
13  
14 IO::SigGuard::syswrite( $fh, $buf );
15 IO::SigGuard::syswrite( $fh, $buf, $len );
16 IO::SigGuard::syswrite( $fh, $buf, $len, $offset );
17  
18 IO::SigGuard::select( $read, $write, $exc, $timeout );
19  
20 =head1 DESCRIPTION
21  
22 C<perldoc perlipc> describes how Perl versions from 5.8.0 onward disable
23 the OS’s SA_RESTART flag when installing Perl signal handlers.
24  
25 This module imitates that pattern in pure Perl: it does an automatic
26 restart when a signal interrupts an operation so you can avoid
27 the generally-useless EINTR error when using
28 C<sysread()>, C<syswrite()>, and C<select()>.
29  
30 For this to work, whatever signal handler you implement will need to break
31 out of this module, probably via either C<die()> or C<exit()>.
32  
33 =head1 ABOUT C<sysread()> and C<syswrite()>
34  
35 Other than that you’ll never see EINTR and that
36 there are no function prototypes used (i.e., you need parentheses on
37 all invocations), C<sysread()> and C<syswrite()>
38 work exactly the same as Perl’s equivalent built-ins.
39  
40 =head1 ABOUT C<select()>
41  
42 To handle EINTR, C<IO::SigGuard::select()> has to subtract the elapsed time
43 from the given timeout then repeat the internal C<select()>. Because
44 the C<select()> built-in’s C<$timeleft> return is not reliable across
45 all platforms, we have to compute the elapsed time ourselves. By default the
46 only means of doing this is the C<time()> built-in, which can only measure
47 individual seconds.
48  
49 This works, but there are two ways to make it more accurate:
50  
51 =over
52  
53 =item * Have L<Time::HiRes> loaded, and C<IO::SigGuard::select()> will use that
54 module rather than the C<time()> built-in.
55  
56 =item * Set C<$IO::SigGuard::TIME_CR> to a compatible code reference. This is
57 useful, e.g., if you have your own logic to do the equivalent of
58 L<Time::HiRes>—for example, in Linux you may prefer to call the C<gettimeofday>
59 system call directly from Perl to avoid L<Time::HiRes>’s XS overhead.
60  
61 =back
62  
63 In scalar contact, C<IO::SigGuard::select()> is a drop-in replacement
64 for Perl’s 4-argument built-in.
65  
66 In list context, there may be discrepancies re the C<$timeleft> value
67 that Perl returns from a call to C<select>. As per Perl’s documentation
68 this value is generally not reliable anyway, though, so that shouldn’t be a
69 big deal. In fact, on systems like MacOS where the built-in’s C<$timeleft>
70 is completely useless, IO::SigGuard’s return is actually B<better> since it
71 does provide at least a rough estimate of how much of the given timeout value
72 is left.
73  
74 See C<perlport> for portability notes for C<select>.
75  
76 =head1 TODO
77  
78 This pattern could probably be extended to C<send>, C<recv>, C<flock>, and
79 other system calls that can receive EINTR. If there’s a desire for that I’ll
80 consider adding it.
81  
82 =cut
83  
84 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 3 ▶use Errno ();
Subroutine BEGIN: called CC=1 SCAR=0.0
88  
89 3 our $VERSION = '0.03-TRIAL2';
90  
91 #Set this in lieu of using Time::HiRes or built-in time().
92 3 our $TIME_CR;
93  
94 #Just in case someone has an actual reason to do this.
95 3 our $YES_I_REALLY_MEAN_TO_WRITE_ZERO_BYTES;
96 3 our $TOLERATE_NONERROR_ZERO_WRITE;
97  
98 #As light as possible …
99  
100 3 my $read;
101  
102 sub sysread {
103 READ: {
104 680 ▶ $read = ( (@_ == 3) ? CORE::sysread( $_[0], $_[1], $_[2] ) : (@_ == 4) ? CORE::sysread( $_[0], $_[1], $_[2], $_[3] ) : die "Wrong args count! (@_)" ) or do {
Subroutine sysread: called CC=5 SCAR=17.8
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
$read = @_ == 3 ? CORE::sysread($_[0], $_[1], $_[2]) : (@_ == 4 ? CORE::sysread($_[0], $_[1], $_[2], $_[3]) : die("Wrong args count! (@_)")) or do { redo READ if $! == 4 } 5 680 685
@_ == 3 ? : 685 0 685
@_ == 4 ? : 0 0 0
105 5 ▶ redo READ if $! == Errno::EINTR();
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 5 0 5
106 };
107 }
108  
109 680 return $read;
110 }
111  
112 my $wrote;
113  
114 sub syswrite {
115 WRITE: {
116 683550 ▶ $wrote = ( (@_ == 2) ? CORE::syswrite( $_[0], $_[1] ) : (@_ == 3) ? CORE::syswrite( $_[0], $_[1], $_[2] ) : (@_ == 4) ? CORE::syswrite( $_[0], $_[1], $_[2], $_[3] ) : die "Wrong args count! (@_)" ) || do {
Subroutine syswrite: called CC=6 SCAR=28.1
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
@_ == 2 ? : 683550 0 683550
@_ == 3 ? : 0 0 0
@_ == 4 ? : 0 0 0
Condition33%1/3
(@_ == 2 ? CORE::syswrite($_[0], $_[1]) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2]) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2], $_[3]) : die("Wrong args count! (@_)")))) || do { redo WRITE if $! == 4 }
l!l&&r!l&&!r
68355000
MC/DC0%0/2
(@_ == 2 ? CORE::syswrite($_[0], $_[1]) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2]) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2], $_[3]) : die("Wrong args count! (@_)")))) || do { redo WRITE if $! == 4 }
(@_ == 2 ? CORE::syswrite($_[0], $_[1]) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2]) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2], $_[3]) : die("Wrong args count! (@_)")))) do { redo WRITE if $! == 4 }
Truth table33%1/3
A || B
A= (@_ == 2 ? CORE::syswrite($_[0], $_[1]) : (@_ == 3 ? CORE::syswrite($_[0], $_[1], $_[2]) : (@_ == 4 ? CORE::syswrite($_[0], $_[1], $_[2], $_[3]) : die("Wrong args count! (@_)"))))B= do { redo WRITE if $! == 4 }
ABresult
1X1
011
000
117  
118 #EINTR means the file pointer is unchanged.
119 0 ▶ redo WRITE if $! == Errno::EINTR();
Branch: 1 branch
BranchTrueFalseTotal
if $! == 4 0 0 0
120 };
121 }
122  
123 683550 return $wrote;
124 }
125  
126 3 my ($start, $last_loop_time, $os_error, $nfound, $timeleft, $timer_cr);
127  
128 #pre-5.16 didn’t have \&CORE::time.
129 0 ▶sub _time { time }
Subroutine _time: not called CC=1 SCAR=6.9
130  
131 sub select {
132 6 ▶ die( (caller 0)[3] . ' must have 4 arguments!' ) if @_ != 4;
Subroutine select: called CC=7 SCAR=20.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 6 6
133  
134 6 $os_error = $!;
135  
136 6 ▶ $timer_cr = $TIME_CR || Time::HiRes->can('time') || \&_time;
Condition40%2/5
$IO::SigGuard::TIME_CR || 'Time::HiRes'->can('time') || \&IO::SigGuard::_time
l!l
60
$IO::SigGuard::TIME_CR or "Time::HiRes"->can("time")
l!l&&r!l&&!r
060
MC/DC0%0/2
$IO::SigGuard::TIME_CR || 'Time::HiRes'->can('time') || \&IO::SigGuard::_time
$IO::SigGuard::TIME_CR "Time::HiRes"->can("time")
Truth table33%1/3
A or B
A= $IO::SigGuard::TIME_CRB= "Time::HiRes"->can("time")
ABresult
1X1
011
000
137  
138 6 $start = $timer_cr->();
139 6 $last_loop_time = $start;
140  
141 SELECT: {
142 6 ($nfound, $timeleft) = CORE::select( $_[0], $_[1], $_[2], $_[3] - $last_loop_time + $start );
143 26 ▶ if ($nfound == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nfound == -1) { } 20 6 26
144  
145 #Use of %! will autoload Errno.pm,
146 #which can affect the value of $!.
147 20 my $select_error = $!;
148  
149 20 ▶ if ($! == Errno::EINTR()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! == 4) 20 0 20
150 20 $last_loop_time = $timer_cr->();
151 20 redo SELECT;
152 }
153  
154 0 $! = $select_error;
155 }
156 else {
157  
158 #select() doesn’t set $! on success, so let’s not clobber what
159 #value was there before.
160 6 $! = $os_error;
161 }
162  
163 6 ▶ return wantarray ? ($nfound, $timeleft) : $nfound;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 3 6
164 }
165 }
166  
167 =head1 REPOSITORY
168  
169 L<https://github.com/FGasper/p5-IO-SigGuard>
170  
171 =head1 AUTHOR
172  
173 Felipe Gasper (FELIPE)
174  
175 … with special thanks to Mario Roy (MARIOROY) for extra testing
176 and a few fixes/improvements.
177  
178 =head1 COPYRIGHT
179  
180 Copyright 2017 by L<Gasper Software Consulting|http://gaspersoftware.com>
181  
182 =head1 LICENSE
183  
184 This distribution is released under the same license as Perl.
185  
186 =cut
187  
188 3 1;
↑ Summary