Filename | /usr/lib/perl/5.10/IO/Select.pm |
Statements | Executed 13 statements in 1.94ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 39µs | 50µs | BEGIN@9 | IO::Select::
1 | 1 | 1 | 22µs | 127µs | BEGIN@10 | IO::Select::
1 | 1 | 1 | 16µs | 119µs | BEGIN@11 | 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 | 3 | 61µs | 2 | 60µs | # spent 50µs (39+11) within IO::Select::BEGIN@9 which was called:
# once (39µs+11µs) by Net::LDAP::BEGIN@9 at line 9 # spent 50µs making 1 call to IO::Select::BEGIN@9
# spent 11µs making 1 call to strict::import |
10 | 3 | 51µs | 2 | 233µs | # spent 127µs (22+106) within IO::Select::BEGIN@10 which was called:
# once (22µs+106µs) by Net::LDAP::BEGIN@9 at line 10 # spent 127µs making 1 call to IO::Select::BEGIN@10
# spent 106µs making 1 call to warnings::register::import |
11 | 3 | 1.79ms | 2 | 222µs | # spent 119µs (16+103) within IO::Select::BEGIN@11 which was called:
# once (16µs+103µs) by Net::LDAP::BEGIN@9 at line 11 # spent 119µs making 1 call to IO::Select::BEGIN@11
# spent 103µs making 1 call to vars::import |
12 | 1 | 3µs | require Exporter; | ||
13 | |||||
14 | 1 | 2µs | $VERSION = "1.17"; | ||
15 | |||||
16 | 1 | 19µs | @ISA = qw(Exporter); # This is only so we can do version checking | ||
17 | |||||
18 | sub VEC_BITS () {0} | ||||
19 | sub FD_COUNT () {1} | ||||
20 | sub FIRST_FD () {2} | ||||
21 | |||||
22 | sub new | ||||
23 | { | ||||
24 | my $self = shift; | ||||
25 | my $type = ref($self) || $self; | ||||
26 | |||||
27 | my $vec = bless [undef,0], $type; | ||||
28 | |||||
29 | $vec->add(@_) | ||||
30 | if @_; | ||||
31 | |||||
32 | $vec; | ||||
33 | } | ||||
34 | |||||
35 | sub add | ||||
36 | { | ||||
37 | shift->_update('add', @_); | ||||
38 | } | ||||
39 | |||||
40 | sub remove | ||||
41 | { | ||||
42 | shift->_update('remove', @_); | ||||
43 | } | ||||
44 | |||||
45 | sub exists | ||||
46 | { | ||||
47 | my $vec = shift; | ||||
48 | my $fno = $vec->_fileno(shift); | ||||
49 | return undef unless defined $fno; | ||||
50 | $vec->[$fno + FIRST_FD]; | ||||
51 | } | ||||
52 | |||||
53 | sub _fileno | ||||
54 | { | ||||
55 | my($self, $f) = @_; | ||||
56 | return unless defined $f; | ||||
57 | $f = $f->[0] if ref($f) eq 'ARRAY'; | ||||
58 | ($f =~ /^\d+$/) ? $f : fileno($f); | ||||
59 | } | ||||
60 | |||||
61 | sub _update | ||||
62 | { | ||||
63 | my $vec = shift; | ||||
64 | my $add = shift eq 'add'; | ||||
65 | |||||
66 | my $bits = $vec->[VEC_BITS]; | ||||
67 | $bits = '' unless defined $bits; | ||||
68 | |||||
69 | my $count = 0; | ||||
70 | my $f; | ||||
71 | foreach $f (@_) | ||||
72 | { | ||||
73 | my $fn = $vec->_fileno($f); | ||||
74 | next unless defined $fn; | ||||
75 | my $i = $fn + FIRST_FD; | ||||
76 | if ($add) { | ||||
77 | if (defined $vec->[$i]) { | ||||
78 | $vec->[$i] = $f; # if array rest might be different, so we update | ||||
79 | next; | ||||
80 | } | ||||
81 | $vec->[FD_COUNT]++; | ||||
82 | vec($bits, $fn, 1) = 1; | ||||
83 | $vec->[$i] = $f; | ||||
84 | } else { # remove | ||||
85 | next unless defined $vec->[$i]; | ||||
86 | $vec->[FD_COUNT]--; | ||||
87 | vec($bits, $fn, 1) = 0; | ||||
88 | $vec->[$i] = undef; | ||||
89 | } | ||||
90 | $count++; | ||||
91 | } | ||||
92 | $vec->[VEC_BITS] = $vec->[FD_COUNT] ? $bits : undef; | ||||
93 | $count; | ||||
94 | } | ||||
95 | |||||
96 | sub can_read | ||||
97 | { | ||||
98 | my $vec = shift; | ||||
99 | my $timeout = shift; | ||||
100 | my $r = $vec->[VEC_BITS]; | ||||
101 | |||||
102 | defined($r) && (select($r,undef,undef,$timeout) > 0) | ||||
103 | ? handles($vec, $r) | ||||
104 | : (); | ||||
105 | } | ||||
106 | |||||
107 | sub can_write | ||||
108 | { | ||||
109 | my $vec = shift; | ||||
110 | my $timeout = shift; | ||||
111 | my $w = $vec->[VEC_BITS]; | ||||
112 | |||||
113 | defined($w) && (select(undef,$w,undef,$timeout) > 0) | ||||
114 | ? handles($vec, $w) | ||||
115 | : (); | ||||
116 | } | ||||
117 | |||||
118 | sub has_exception | ||||
119 | { | ||||
120 | my $vec = shift; | ||||
121 | my $timeout = shift; | ||||
122 | my $e = $vec->[VEC_BITS]; | ||||
123 | |||||
124 | defined($e) && (select(undef,undef,$e,$timeout) > 0) | ||||
125 | ? handles($vec, $e) | ||||
126 | : (); | ||||
127 | } | ||||
128 | |||||
129 | sub has_error | ||||
130 | { | ||||
131 | warnings::warn("Call to deprecated method 'has_error', use 'has_exception'") | ||||
132 | if warnings::enabled(); | ||||
133 | goto &has_exception; | ||||
134 | } | ||||
135 | |||||
136 | sub count | ||||
137 | { | ||||
138 | my $vec = shift; | ||||
139 | $vec->[FD_COUNT]; | ||||
140 | } | ||||
141 | |||||
142 | sub bits | ||||
143 | { | ||||
144 | my $vec = shift; | ||||
145 | $vec->[VEC_BITS]; | ||||
146 | } | ||||
147 | |||||
148 | sub as_string # for debugging | ||||
149 | { | ||||
150 | my $vec = shift; | ||||
151 | my $str = ref($vec) . ": "; | ||||
152 | my $bits = $vec->bits; | ||||
153 | my $count = $vec->count; | ||||
154 | $str .= defined($bits) ? unpack("b*", $bits) : "undef"; | ||||
155 | $str .= " $count"; | ||||
156 | my @handles = @$vec; | ||||
157 | splice(@handles, 0, FIRST_FD); | ||||
158 | for (@handles) { | ||||
159 | $str .= " " . (defined($_) ? "$_" : "-"); | ||||
160 | } | ||||
161 | $str; | ||||
162 | } | ||||
163 | |||||
164 | sub _max | ||||
165 | { | ||||
166 | my($a,$b,$c) = @_; | ||||
167 | $a > $b | ||||
168 | ? $a > $c | ||||
169 | ? $a | ||||
170 | : $c | ||||
171 | : $b > $c | ||||
172 | ? $b | ||||
173 | : $c; | ||||
174 | } | ||||
175 | |||||
176 | sub select | ||||
177 | { | ||||
178 | shift | ||||
179 | if defined $_[0] && !ref($_[0]); | ||||
180 | |||||
181 | my($r,$w,$e,$t) = @_; | ||||
182 | my @result = (); | ||||
183 | |||||
184 | my $rb = defined $r ? $r->[VEC_BITS] : undef; | ||||
185 | my $wb = defined $w ? $w->[VEC_BITS] : undef; | ||||
186 | my $eb = defined $e ? $e->[VEC_BITS] : undef; | ||||
187 | |||||
188 | if(select($rb,$wb,$eb,$t) > 0) | ||||
189 | { | ||||
190 | my @r = (); | ||||
191 | my @w = (); | ||||
192 | my @e = (); | ||||
193 | my $i = _max(defined $r ? scalar(@$r)-1 : 0, | ||||
194 | defined $w ? scalar(@$w)-1 : 0, | ||||
195 | defined $e ? scalar(@$e)-1 : 0); | ||||
196 | |||||
197 | for( ; $i >= FIRST_FD ; $i--) | ||||
198 | { | ||||
199 | my $j = $i - FIRST_FD; | ||||
200 | push(@r, $r->[$i]) | ||||
201 | if defined $rb && defined $r->[$i] && vec($rb, $j, 1); | ||||
202 | push(@w, $w->[$i]) | ||||
203 | if defined $wb && defined $w->[$i] && vec($wb, $j, 1); | ||||
204 | push(@e, $e->[$i]) | ||||
205 | if defined $eb && defined $e->[$i] && vec($eb, $j, 1); | ||||
206 | } | ||||
207 | |||||
208 | @result = (\@r, \@w, \@e); | ||||
209 | } | ||||
210 | @result; | ||||
211 | } | ||||
212 | |||||
213 | sub handles | ||||
214 | { | ||||
215 | my $vec = shift; | ||||
216 | my $bits = shift; | ||||
217 | my @h = (); | ||||
218 | my $i; | ||||
219 | my $max = scalar(@$vec) - 1; | ||||
220 | |||||
221 | for ($i = FIRST_FD; $i <= $max; $i++) | ||||
222 | { | ||||
223 | next unless defined $vec->[$i]; | ||||
224 | push(@h, $vec->[$i]) | ||||
225 | if !defined($bits) || vec($bits, $i - FIRST_FD, 1); | ||||
226 | } | ||||
227 | |||||
228 | @h; | ||||
229 | } | ||||
230 | |||||
231 | 1 | 9µs | 1; | ||
232 | __END__ |