blib/lib/IO/Ppoll.pm

filter: Statementstmt 93.5% 72 / 77 Branchbran 61.1% 11 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.7% 15 / 16 Podpod 100% 12 / 12 totaltotal 89.4% 110 / 123 SCAR 25.1
CC
12
Cov
87%
sigmask
6.0
mask
4.0
poll
2.0
CRAP 12.3
↑ Summary Build »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2008-2015 -- leonerd@leonerd.org.uk
5  
6 package IO::Ppoll;
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 our $VERSION = '0.12';
14  
15 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 our @EXPORT = qw(
17 POLLIN
18 POLLOUT
19 POLLERR
20 POLLHUP
21 POLLNVAL
22 );
23  
24 5 require POSIX;
25  
26 5 require XSLoader;
27 5 XSLoader::load( __PACKAGE__, $VERSION );
28  
29 =head1 NAME
30  
31 C<IO::Ppoll> - Object interface to Linux's C<ppoll()> call
32  
33 =head1 SYNOPSIS
34  
35 use IO::Ppoll qw( POLLIN POLLOUT );
36 use POSIX qw( sigprocmask SIG_BLOCK SIGHUP );
37  
38 my $ppoll = IO::Ppoll->new();
39 $ppoll->mask( $input_handle => POLLIN );
40 $ppoll->mask( $output_handle => POLLOUT );
41  
42 $SIG{HUP} = sub { print "SIGHUP happened\n"; };
43 sigprocmask( SIG_BLOCK, POSIX::SigSet->new( SIGHUP ), undef );
44  
45 # If a SIGHUP happens, it can only happen during this poll
46 $ppoll->poll( $timeout );
47  
48 $input_ev = $poll->events( $input_handle );
49  
50 =head1 DESCRIPTION
51  
52 C<IO::Ppoll> is a simple interface to Linux's C<ppoll()> system call. It
53 provides an interface that is drop-in compatible with L<IO::Poll>. The object
54 stores a signal mask that will be in effect during the actual C<ppoll()>
55 system call and has additional methods for manipulating the signal mask.
56  
57 The C<ppoll()> system call atomically switches the process's signal mask to
58 that provided by the call, waits identically to C<poll()>, then switches it
59 back again. This allows a program to safely wait on either file handle IO or
60 signals, without needing such tricks as a self-connected pipe or socket.
61  
62 The usual way in which this is used is to block the signals the application is
63 interested in during the normal running of code. Whenever the C<ppoll()> wait
64 is entered the process signal mask will be switched to that stored in the
65 object. If there are any pending signals, the Linux kernel will then deliver
66 them and make C<ppoll()> return -1 with C<errno> set to C<EINTR>. If no
67 signals are pending, it will wait as a normal C<poll()> would. This guarantees
68 the signals will only be delivered during the C<ppoll()> wait, when it would
69 be safe to do so.
70  
71 =cut
72  
73 =head1 CONSTRUCTOR
74  
75 =cut
76  
77 =head2 new
78  
79 $ppoll = IO::Ppoll->new()
80  
81 Returns a new instance of an C<IO::Ppoll> object. It will contain no file
82 handles and its signal mask will be empty.
83  
84 =cut
85  
86 sub new
87 {
88 4 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
89  
90 4 my $self = bless {
91 fds => "",
92 nfds => 0,
93 handles => [],
94 sigmask => POSIX::SigSet->new(),
95 }, $class;
96  
97 4 return $self;
98 }
99  
100 =head1 METHODS
101  
102 =cut
103  
104 =head2 mask
105  
106 $mask = $ppoll->mask( $handle )
107  
108 Returns the current mask bits for the given IO handle
109  
110 $ppoll->mask( $handle, $newmask )
111  
112 Sets the mask bits for the given IO handle. If C<$newmask> is 0, the handle
113 will be removed.
114  
115 =cut
116  
117 sub mask
118 {
119 9 ▶ my $self = shift;
Subroutine mask: called CC=4 SCAR=13.9
Pod documented
120 9 my ( $handle, $newmask ) = @_;
121  
122 9 my $fd = fileno $handle;
123 9 ▶ defined $fd or croak "Expected a filehandle";
Branch: 1 branch
BranchTrueFalseTotal
defined $fd or croak("Expected a filehandle") 0 9 9
124  
125 9 ▶ if( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 5 4 9
126 5 ▶ if( $newmask ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($newmask) { } 4 1 5
127 4 $self->{handles}->[$fd] = $handle;
128 4 mas_events( $self->{fds}, $self->{nfds}, $fd, 0, $newmask );
129 }
130 else {
131 1 delete $self->{handles}->[$fd];
132 1 del_events( $self->{fds}, $self->{nfds}, $fd );
133 }
134 }
135 else {
136 4 return get_events( $self->{fds}, $self->{nfds}, $fd );
137 }
138 }
139  
140 =head2 mask_add
141  
142 =head2 mask_del
143  
144 $ppoll->mask_add( $handle, $addmask )
145  
146 $ppoll->mask_del( $handle, $delmask )
147  
148 I<Since version 0.12.>
149  
150 Convenient shortcuts to setting or clearing one or more bits in the mask of a
151 handle. Equivalent, respectively, to the following lines
152  
153 $ppoll->mask( $handle, $ppoll->mask( $handle ) | $addmask )
154  
155 $ppoll->mask( $handle, $ppoll->mask( $handle ) & ~$delmask )
156  
157 Specifically note that C<$maskbits> contains bits to remove from the mask.
158  
159 =cut
160  
161 sub mask_add
162 {
163 1 ▶ my $self = shift;
Subroutine mask_add: called CC=2 SCAR=7.0
Pod documented
164 1 my ( $handle, $addbits ) = @_;
165  
166 1 my $fd = fileno $handle;
167 1 ▶ defined $fd or croak "Expected a filehandle";
Branch: 1 branch
BranchTrueFalseTotal
defined $fd or croak("Expected a filehandle") 0 1 1
168  
169 1 mas_events( $self->{fds}, $self->{nfds}, $fd, ~0, $addbits );
170 }
171  
172 sub mask_del
173 {
174 1 ▶ my $self = shift;
Subroutine mask_del: called CC=2 SCAR=7.0
Pod documented
175 1 my ( $handle, $delbits ) = @_;
176  
177 1 my $fd = fileno $handle;
178 1 ▶ defined $fd or croak "Expected a filehandle";
Branch: 1 branch
BranchTrueFalseTotal
defined $fd or croak("Expected a filehandle") 0 1 1
179  
180 1 mas_events( $self->{fds}, $self->{nfds}, $fd, ~$delbits, 0 );
181 }
182  
183 =head2 poll
184  
185 $ret = $ppoll->poll( $timeout )
186  
187 Call the C<ppoll()> system call. If C<$timeout> is not supplied then no
188 timeout value will be passed to the system call. Returns the result of the
189 system call, which is the number of filehandles that have non-zero events, 0
190 on timeout, or -1 if an error occurred (including being interrupted by a
191 signal). If -1 is returned, C<$!> will contain the error.
192  
193 =cut
194  
195 sub poll
196 {
197 3 ▶ my $self = shift;
Subroutine poll: called CC=2 SCAR=7.0
Pod documented
198 3 my ( $timeout ) = @_;
199  
200 # do_poll wants timeout in miliseconds
201 3 ▶ $timeout *= 1000 if defined $timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $timeout 3 0 3
202  
203 3 return do_poll( $self->{fds}, $self->{nfds}, $timeout, $self->{sigmask} );
204 }
205  
206 =head2 events
207  
208 $bits = $ppoll->events( $handle )
209  
210 Returns the event mask which represents the events that happened on the
211 filehandle during the last call to C<poll()>.
212  
213 =cut
214  
215 sub events
216 {
217 3 ▶ my $self = shift;
Subroutine events: called CC=2 SCAR=7.0
Pod documented
218 3 my ( $handle ) = @_;
219  
220 3 my $fd = fileno $handle;
221 3 ▶ defined $fd or croak "Expected a filehandle";
Branch: 1 branch
BranchTrueFalseTotal
defined $fd or croak("Expected a filehandle") 0 3 3
222  
223 3 return get_revents( $self->{fds}, $self->{nfds}, $fd );
224 }
225  
226 =head2 remove
227  
228 $ppoll->remove( $handle )
229  
230 Removes the handle from the list of file descriptors for the next poll.
231  
232 =cut
233  
234 sub remove
235 {
236 1 ▶ my $self = shift;
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
237 1 my ( $handle ) = @_;
238  
239 1 $self->mask( $handle, 0 );
240 }
241  
242 =head2 handles
243  
244 @handles = $ppoll->handles( $bits )
245  
246 Returns a list of handles. If C<$bits> is not given then all of the handles
247 will be returned. If C<$bits> is given then the list will only contain handles
248 which reported at least one of the bits specified during the last C<poll()>
249 call.
250  
251 =cut
252  
253 sub handles
254 {
255 4 ▶ my $self = shift;
Subroutine handles: called CC=2 SCAR=6.9
Pod documented
256 4 my ( $events ) = @_;
257  
258 4 my @fds;
259 4 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 3 4
260 1 @fds = get_fds_for( $self->{fds}, $self->{nfds}, $events );
261 }
262 else {
263 3 @fds = get_fds( $self->{fds}, $self->{nfds} );
264 }
265  
266 4 my $handle_map = $self->{handles};
267 4 return map { $handle_map->[$_] } @fds;
268 }
269  
270 =head2 sigmask
271  
272 $sigset = $ppoll->sigmask
273  
274 Returns the C<POSIX::SigSet> object in which the signal mask is stored. Since
275 this is a reference to the object the C<IO::Ppoll> object uses, any
276 modifications made to it will be reflected in the signal mask given to the
277 C<ppoll()> system call.
278  
279 $ppoll->sigmask( $newsigset )
280  
281 Sets the C<POSIX::SigSet> object in which the signal mask is stored. Usually
282 this is not required, as a new C<IO::Ppoll> is initialised with an empty set,
283 and the C<sigmask_add()> and C<sigmask_del()> methods can be used to modify
284 it.
285  
286 =cut
287  
288 sub sigmask
289 {
290 0 ▶ my $self = shift;
Subroutine sigmask: not called CC=2 SCAR=17.9
Pod documented
291 0 my ( $newmask ) = @_;
292  
293 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
294 0 $self->{sigmask} = $newmask;
295 }
296 else {
297 0 return $self->{sigmask};
298 }
299 }
300  
301 =head2 sigmask_add
302  
303 $ppoll->sigmask_add( @signals )
304  
305 Adds the given signals to the signal mask. These signals will be blocked
306 during the C<poll()> call.
307  
308 =cut
309  
310 sub sigmask_add
311 {
312 2 ▶ my $self = shift;
Subroutine sigmask_add: called CC=2 SCAR=6.9
Pod documented
313 2 my @signals = @_;
314  
315 2 $self->{sigmask}->addset( $_ ) foreach @signals;
316 }
317  
318 =head2 sigmask_del
319  
320 $ppoll->sigmask_del( @signals )
321  
322 Removes the given signals from the signal mask. These signals will not be
323 blocked during the C<poll()> call, and may be delivered while C<poll()> is
324 waiting.
325  
326 =cut
327  
328 sub sigmask_del
329 {
330 1 ▶ my $self = shift;
Subroutine sigmask_del: called CC=2 SCAR=6.9
Pod documented
331 1 my @signals = @_;
332  
333 1 $self->{sigmask}->delset( $_ ) foreach @signals;
334 }
335  
336 =head2 sigmask_ismember
337  
338 $present = $ppoll->sigmask_ismember( $signal )
339  
340 Tests if the given signal is present in the signal mask.
341  
342 =cut
343  
344 sub sigmask_ismember
345 {
346 3 ▶ my $self = shift;
Subroutine sigmask_ismember: called CC=1 SCAR=0.0
Pod documented
347 3 my ( $signal ) = @_;
348  
349 3 return $self->{sigmask}->ismember( $signal );
350 }
351  
352 =head1 SEE ALSO
353  
354 =over 4
355  
356 =item *
357  
358 L<IO::Poll> - Object interface to system poll call
359  
360 =item *
361  
362 C<ppoll(2)> - wait for some event on a file descriptor (Linux manpages)
363  
364 =item *
365  
366 L<IO::Async::Loop::IO_Ppoll> - a Loop using an IO::Ppoll object
367  
368 =back
369  
370 =head1 AUTHOR
371  
372 Paul Evans <leonerd@leonerd.org.uk>
373  
374 =cut
375  
376 5 0x55AA;
↑ Summary Build »