Filename | /home/hejohns/perl5/lib/perl5/x86_64-linux-gnu-thread-multi/IO/Select.pm |
Statements | Executed 8 statements in 779µs |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 8µs | 10µs | BEGIN@9 | IO::Select::
1 | 1 | 1 | 5µs | 24µs | BEGIN@10 | IO::Select::
0 | 0 | 0 | 0s | 0s | _fileno | IO::Select::
0 | 0 | 0 | 0s | 0s | _max | IO::Select::
0 | 0 | 0 | 0s | 0s | _update | IO::Select::
0 | 0 | 0 | 0s | 0s | add | IO::Select::
0 | 0 | 0 | 0s | 0s | as_string | IO::Select::
0 | 0 | 0 | 0s | 0s | bits | IO::Select::
0 | 0 | 0 | 0s | 0s | can_read | IO::Select::
0 | 0 | 0 | 0s | 0s | can_write | IO::Select::
0 | 0 | 0 | 0s | 0s | count | IO::Select::
0 | 0 | 0 | 0s | 0s | exists | IO::Select::
0 | 0 | 0 | 0s | 0s | handles | IO::Select::
0 | 0 | 0 | 0s | 0s | has_error | IO::Select::
0 | 0 | 0 | 0s | 0s | has_exception | IO::Select::
0 | 0 | 0 | 0s | 0s | new | IO::Select::
0 | 0 | 0 | 0s | 0s | remove | IO::Select::
0 | 0 | 0 | 0s | 0s | select | IO::Select::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | # IO::Select.pm | ||||
2 | # | ||||
3 | # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. | ||||
4 | # This program is free software; you can redistribute it and/or | ||||
5 | # modify it under the same terms as Perl itself. | ||||
6 | |||||
7 | package IO::Select; | ||||
8 | |||||
9 | 2 | 18µs | 2 | 11µs | # spent 10µs (8+1) within IO::Select::BEGIN@9 which was called:
# once (8µs+1µs) by IPC::Cmd::BEGIN@5 at line 9 # spent 10µs making 1 call to IO::Select::BEGIN@9
# spent 1µs making 1 call to strict::import |
10 | 2 | 750µs | 2 | 43µs | # spent 24µs (5+19) within IO::Select::BEGIN@10 which was called:
# once (5µs+19µs) by IPC::Cmd::BEGIN@5 at line 10 # spent 24µs making 1 call to IO::Select::BEGIN@10
# spent 19µs making 1 call to warnings::register::import |
11 | 1 | 300ns | require Exporter; | ||
12 | |||||
13 | 1 | 200ns | our $VERSION = "1.49"; | ||
14 | |||||
15 | 1 | 7µs | our @ISA = qw(Exporter); # This is only so we can do version checking | ||
16 | |||||
17 | sub VEC_BITS () {0} | ||||
18 | sub FD_COUNT () {1} | ||||
19 | sub FIRST_FD () {2} | ||||
20 | |||||
21 | sub new | ||||
22 | { | ||||
23 | my $self = shift; | ||||
24 | my $type = ref($self) || $self; | ||||
25 | |||||
26 | my $vec = bless [undef,0], $type; | ||||
27 | |||||
28 | $vec->add(@_) | ||||
29 | if @_; | ||||
30 | |||||
31 | $vec; | ||||
32 | } | ||||
33 | |||||
34 | sub add | ||||
35 | { | ||||
36 | shift->_update('add', @_); | ||||
37 | } | ||||
38 | |||||
39 | |||||
40 | sub remove | ||||
41 | { | ||||
42 | shift->_update('remove', @_); | ||||
43 | } | ||||
44 | |||||
45 | |||||
46 | sub exists | ||||
47 | { | ||||
48 | my $vec = shift; | ||||
49 | my $fno = $vec->_fileno(shift); | ||||
50 | return undef unless defined $fno; | ||||
51 | $vec->[$fno + FIRST_FD]; | ||||
52 | } | ||||
53 | |||||
54 | |||||
55 | sub _fileno | ||||
56 | { | ||||
57 | my($self, $f) = @_; | ||||
58 | return unless defined $f; | ||||
59 | $f = $f->[0] if ref($f) eq 'ARRAY'; | ||||
60 | if($f =~ /^[0-9]+$/) { # plain file number | ||||
61 | return $f; | ||||
62 | } | ||||
63 | elsif(defined(my $fd = fileno($f))) { | ||||
64 | return $fd; | ||||
65 | } | ||||
66 | else { | ||||
67 | # Neither a plain file number nor an opened filehandle; but maybe it was | ||||
68 | # previously registered and has since been closed. ->remove still wants to | ||||
69 | # know what fileno it had | ||||
70 | foreach my $i ( FIRST_FD .. $#$self ) { | ||||
71 | return $i - FIRST_FD if defined $self->[$i] && $self->[$i] == $f; | ||||
72 | } | ||||
73 | return undef; | ||||
74 | } | ||||
75 | } | ||||
76 | |||||
77 | sub _update | ||||
78 | { | ||||
79 | my $vec = shift; | ||||
80 | my $add = shift eq 'add'; | ||||
81 | |||||
82 | my $bits = $vec->[VEC_BITS]; | ||||
83 | $bits = '' unless defined $bits; | ||||
84 | |||||
85 | my $count = 0; | ||||
86 | my $f; | ||||
87 | foreach $f (@_) | ||||
88 | { | ||||
89 | my $fn = $vec->_fileno($f); | ||||
90 | if ($add) { | ||||
91 | next unless defined $fn; | ||||
92 | my $i = $fn + FIRST_FD; | ||||
93 | if (defined $vec->[$i]) { | ||||
94 | $vec->[$i] = $f; # if array rest might be different, so we update | ||||
95 | next; | ||||
96 | } | ||||
97 | $vec->[FD_COUNT]++; | ||||
98 | vec($bits, $fn, 1) = 1; | ||||
99 | $vec->[$i] = $f; | ||||
100 | } else { # remove | ||||
101 | if ( ! defined $fn ) { # remove if fileno undef'd | ||||
102 | $fn = 0; | ||||
103 | for my $fe (@{$vec}[FIRST_FD .. $#$vec]) { | ||||
104 | if (defined($fe) && $fe == $f) { | ||||
105 | $vec->[FD_COUNT]--; | ||||
106 | $fe = undef; | ||||
107 | vec($bits, $fn, 1) = 0; | ||||
108 | last; | ||||
109 | } | ||||
110 | ++$fn; | ||||
111 | } | ||||
112 | } | ||||
113 | else { | ||||
114 | my $i = $fn + FIRST_FD; | ||||
115 | next unless defined $vec->[$i]; | ||||
116 | $vec->[FD_COUNT]--; | ||||
117 | vec($bits, $fn, 1) = 0; | ||||
118 | $vec->[$i] = undef; | ||||
119 | } | ||||
120 | } | ||||
121 | $count++; | ||||
122 | } | ||||
123 | $vec->[VEC_BITS] = $vec->[FD_COUNT] ? $bits : undef; | ||||
124 | $count; | ||||
125 | } | ||||
126 | |||||
127 | sub can_read | ||||
128 | { | ||||
129 | my $vec = shift; | ||||
130 | my $timeout = shift; | ||||
131 | my $r = $vec->[VEC_BITS]; | ||||
132 | |||||
133 | defined($r) && (select($r,undef,undef,$timeout) > 0) | ||||
134 | ? handles($vec, $r) | ||||
135 | : (); | ||||
136 | } | ||||
137 | |||||
138 | sub can_write | ||||
139 | { | ||||
140 | my $vec = shift; | ||||
141 | my $timeout = shift; | ||||
142 | my $w = $vec->[VEC_BITS]; | ||||
143 | |||||
144 | defined($w) && (select(undef,$w,undef,$timeout) > 0) | ||||
145 | ? handles($vec, $w) | ||||
146 | : (); | ||||
147 | } | ||||
148 | |||||
149 | sub has_exception | ||||
150 | { | ||||
151 | my $vec = shift; | ||||
152 | my $timeout = shift; | ||||
153 | my $e = $vec->[VEC_BITS]; | ||||
154 | |||||
155 | defined($e) && (select(undef,undef,$e,$timeout) > 0) | ||||
156 | ? handles($vec, $e) | ||||
157 | : (); | ||||
158 | } | ||||
159 | |||||
160 | sub has_error | ||||
161 | { | ||||
162 | warnings::warn("Call to deprecated method 'has_error', use 'has_exception'") | ||||
163 | if warnings::enabled(); | ||||
164 | goto &has_exception; | ||||
165 | } | ||||
166 | |||||
167 | sub count | ||||
168 | { | ||||
169 | my $vec = shift; | ||||
170 | $vec->[FD_COUNT]; | ||||
171 | } | ||||
172 | |||||
173 | sub bits | ||||
174 | { | ||||
175 | my $vec = shift; | ||||
176 | $vec->[VEC_BITS]; | ||||
177 | } | ||||
178 | |||||
179 | sub as_string # for debugging | ||||
180 | { | ||||
181 | my $vec = shift; | ||||
182 | my $str = ref($vec) . ": "; | ||||
183 | my $bits = $vec->bits; | ||||
184 | my $count = $vec->count; | ||||
185 | $str .= defined($bits) ? unpack("b*", $bits) : "undef"; | ||||
186 | $str .= " $count"; | ||||
187 | my @handles = @$vec; | ||||
188 | splice(@handles, 0, FIRST_FD); | ||||
189 | for (@handles) { | ||||
190 | $str .= " " . (defined($_) ? "$_" : "-"); | ||||
191 | } | ||||
192 | $str; | ||||
193 | } | ||||
194 | |||||
195 | sub _max | ||||
196 | { | ||||
197 | my($a,$b,$c) = @_; | ||||
198 | $a > $b | ||||
199 | ? $a > $c | ||||
200 | ? $a | ||||
201 | : $c | ||||
202 | : $b > $c | ||||
203 | ? $b | ||||
204 | : $c; | ||||
205 | } | ||||
206 | |||||
207 | sub select | ||||
208 | { | ||||
209 | shift | ||||
210 | if defined $_[0] && !ref($_[0]); | ||||
211 | |||||
212 | my($r,$w,$e,$t) = @_; | ||||
213 | my @result = (); | ||||
214 | |||||
215 | my $rb = defined $r ? $r->[VEC_BITS] : undef; | ||||
216 | my $wb = defined $w ? $w->[VEC_BITS] : undef; | ||||
217 | my $eb = defined $e ? $e->[VEC_BITS] : undef; | ||||
218 | |||||
219 | if(select($rb,$wb,$eb,$t) > 0) | ||||
220 | { | ||||
221 | my @r = (); | ||||
222 | my @w = (); | ||||
223 | my @e = (); | ||||
224 | my $i = _max(defined $r ? scalar(@$r)-1 : 0, | ||||
225 | defined $w ? scalar(@$w)-1 : 0, | ||||
226 | defined $e ? scalar(@$e)-1 : 0); | ||||
227 | |||||
228 | for( ; $i >= FIRST_FD ; $i--) | ||||
229 | { | ||||
230 | my $j = $i - FIRST_FD; | ||||
231 | push(@r, $r->[$i]) | ||||
232 | if defined $rb && defined $r->[$i] && vec($rb, $j, 1); | ||||
233 | push(@w, $w->[$i]) | ||||
234 | if defined $wb && defined $w->[$i] && vec($wb, $j, 1); | ||||
235 | push(@e, $e->[$i]) | ||||
236 | if defined $eb && defined $e->[$i] && vec($eb, $j, 1); | ||||
237 | } | ||||
238 | |||||
239 | @result = (\@r, \@w, \@e); | ||||
240 | } | ||||
241 | @result; | ||||
242 | } | ||||
243 | |||||
244 | |||||
245 | sub handles | ||||
246 | { | ||||
247 | my $vec = shift; | ||||
248 | my $bits = shift; | ||||
249 | my @h = (); | ||||
250 | my $i; | ||||
251 | my $max = scalar(@$vec) - 1; | ||||
252 | |||||
253 | for ($i = FIRST_FD; $i <= $max; $i++) | ||||
254 | { | ||||
255 | next unless defined $vec->[$i]; | ||||
256 | push(@h, $vec->[$i]) | ||||
257 | if !defined($bits) || vec($bits, $i - FIRST_FD, 1); | ||||
258 | } | ||||
259 | |||||
260 | @h; | ||||
261 | } | ||||
262 | |||||
263 | 1 | 3µs | 1; | ||
264 | __END__ |