/usr/share/perl5/POE/Loop/IO_Poll.pm is in libpoe-perl 2:1.3650-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | # IO::Poll event loop bridge for POE::Kernel. The theory is that this
# will be faster for large scale applications. This file is
# contributed by Matt Sergeant (baud).
# Empty package to appease perl.
package POE::Loop::IO_Poll;
use vars qw($VERSION);
$VERSION = '1.365'; # NOTE - Should be #.### (three decimal places)
# Include common signal handling.
use POE::Loop::PerlSignals;
# Everything plugs into POE::Kernel;
package POE::Kernel;
=for poe_tests
sub skip_tests {
return "IO::Poll is not 100% compatible with $^O" if (
$^O eq "MSWin32" and not $ENV{POE_DANTIC}
);
return "IO::Poll tests require the IO::Poll module" if (
do { eval "use IO::Poll"; $@ }
);
}
=cut
use strict;
# Be sure we're using a contemporary version of IO::Poll. There were
# issues with certain versions of IO::Poll prior to 0.05. The latest
# version is 0.01, however.
use IO::Poll 0.01;
# Hand off to POE::Loop::Select if we're running under ActivePerl.
BEGIN {
if ($^O eq "MSWin32" and not $ENV{POE_DANTIC}) {
warn "IO::Poll is defective on $^O. Falling back to IO::Select.\n";
require POE::Loop::Select;
POE::Loop::Select->import();
die "not really dying";
}
}
use Errno qw(EINPROGRESS EWOULDBLOCK EINTR);
use IO::Poll qw(
POLLRDNORM POLLWRNORM POLLRDBAND POLLERR POLLHUP POLLNVAL
);
# Many systems' IO::Poll don't define POLLRDNORM.
# Usually upgrading IO::Poll helps.
BEGIN {
my $x = eval { POLLRDNORM };
if ($@ or not defined $x) {
warn(
"Your IO::Poll doesn't define POLLRDNORM. Falling back to IO::Select.\n"
);
require POE::Loop::Select;
POE::Loop::Select->import();
die "not really dying";
}
}
my %poll_fd_masks;
# Allow $^T to change without affecting our internals.
my $start_time = monotime();
#------------------------------------------------------------------------------
# Loop construction and destruction.
sub loop_initialize {
my $self = shift;
%poll_fd_masks = ();
}
sub loop_finalize {
my $self = shift;
$self->loop_ignore_all_signals();
}
#------------------------------------------------------------------------------
# Signal handler maintenance functions.
sub loop_attach_uidestroy {
# does nothing
}
#------------------------------------------------------------------------------
# Maintain time watchers. For this loop, we simply save the next
# event time in a scalar. loop_do_timeslice() will use the saved
# value. A "paused" time watcher is just a timeout for some future
# time.
my $_next_event_time = monotime();
sub loop_resume_time_watcher {
$_next_event_time = $_[1];
}
sub loop_reset_time_watcher {
$_next_event_time = $_[1];
}
sub loop_pause_time_watcher {
$_next_event_time = monotime() + 3600;
}
# A static function; not some object method.
sub mode_to_poll {
return POLLRDNORM if $_[0] == MODE_RD;
return POLLWRNORM if $_[0] == MODE_WR;
return POLLRDBAND if $_[0] == MODE_EX;
croak "unknown I/O mode $_[0]";
}
#------------------------------------------------------------------------------
# Maintain filehandle watchers.
sub loop_watch_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
my $type = mode_to_poll($mode);
my $current = $poll_fd_masks{$fileno} || 0;
my $new = $current | $type;
if (TRACE_FILES) {
POE::Kernel::_warn(
sprintf(
"<fh> Watch $fileno: " .
"Current mask: 0x%02X - including 0x%02X = 0x%02X\n",
$current, $type, $new
)
);
}
$poll_fd_masks{$fileno} = $new;
}
sub loop_ignore_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
my $type = mode_to_poll($mode);
my $current = $poll_fd_masks{$fileno} || 0;
my $new = $current & ~$type;
if (TRACE_FILES) {
POE::Kernel::_warn(
sprintf(
"<fh> Ignore $fileno: " .
": Current mask: 0x%02X - removing 0x%02X = 0x%02X\n",
$current, $type, $new
)
);
}
if ($new) {
$poll_fd_masks{$fileno} = $new;
}
else {
delete $poll_fd_masks{$fileno};
}
}
sub loop_pause_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
my $type = mode_to_poll($mode);
my $current = $poll_fd_masks{$fileno} || 0;
my $new = $current & ~$type;
if (TRACE_FILES) {
POE::Kernel::_warn(
sprintf(
"<fh> Pause $fileno: " .
": Current mask: 0x%02X - removing 0x%02X = 0x%02X\n",
$current, $type, $new
)
);
}
if ($new) {
$poll_fd_masks{$fileno} = $new;
}
else {
delete $poll_fd_masks{$fileno};
}
}
sub loop_resume_filehandle {
my ($self, $handle, $mode) = @_;
my $fileno = fileno($handle);
my $type = mode_to_poll($mode);
my $current = $poll_fd_masks{$fileno} || 0;
my $new = $current | $type;
if (TRACE_FILES) {
POE::Kernel::_warn(
sprintf(
"<fh> Resume $fileno: " .
"Current mask: 0x%02X - including 0x%02X = 0x%02X\n",
$current, $type, $new
)
);
}
$poll_fd_masks{$fileno} = $new;
}
#------------------------------------------------------------------------------
# The event loop itself.
sub loop_do_timeslice {
my $self = shift;
# Check for a hung kernel.
$self->_test_if_kernel_is_idle();
# Set the poll timeout based on current queue conditions. If there
# are FIFO events, then the poll timeout is zero and move on.
# Otherwise set the poll timeout until the next pending event, if
# there are any. If nothing is waiting, set the timeout for some
# constant number of seconds.
my $timeout = $_next_event_time;
my $now = monotime();
if (defined $timeout) {
$timeout -= $now;
$timeout = 0 if $timeout < 0;
}
else {
die "shouldn't happen" if ASSERT_DATA;
$timeout = 3600;
}
if (TRACE_EVENTS) {
POE::Kernel::_warn(
'<ev> Kernel::run() iterating. ' .
sprintf(
"now(%.4f) timeout(%.4f) then(%.4f)\n",
$now-$start_time, $timeout, ($now-$start_time)+$timeout
)
);
}
if (TRACE_FILES) {
foreach (sort { $a<=>$b} keys %poll_fd_masks) {
my @types;
push @types, "plain-file" if -f;
push @types, "directory" if -d;
push @types, "symlink" if -l;
push @types, "pipe" if -p;
push @types, "socket" if -S;
push @types, "block-special" if -b;
push @types, "character-special" if -c;
push @types, "tty" if -t;
my @modes;
my $flags = $poll_fd_masks{$_};
push @modes, 'r' if $flags & (POLLRDNORM | POLLHUP | POLLERR);
push @modes, 'w' if $flags & (POLLWRNORM | POLLHUP | POLLERR);
push @modes, 'x' if $flags & (POLLRDBAND | POLLHUP | POLLERR);
POE::Kernel::_warn(
"<fh> file descriptor $_ = modes(@modes) types(@types)\n"
);
}
}
# Avoid looking at filehandles if we don't need to.
# TODO The added code to make this sleep is non-optimal. There is a
# way to do this in fewer tests.
if (scalar keys %poll_fd_masks) {
# There are filehandles to poll, so do so.
# Check filehandles, or wait for a period of time to elapse.
my $hits = IO::Poll::_poll($timeout * 1000, my @results = %poll_fd_masks);
if (ASSERT_FILES) {
if ($hits < 0) {
POE::Kernel::_trap("<fh> poll returned $hits (error): $!")
unless ( ($! == EINPROGRESS) or
($! == EWOULDBLOCK) or
($! == EINTR) or
($! == 0) # SIGNAL_PIPE strangeness
);
}
}
if (TRACE_FILES) {
if ($hits > 0) {
POE::Kernel::_warn "<fh> poll hits = $hits\n";
}
elsif ($hits == 0) {
POE::Kernel::_warn "<fh> poll timed out...\n";
}
}
# If poll has seen filehandle activity, then gather up the
# active filehandles and synchronously dispatch events to the
# appropriate handlers.
if ($hits > 0) {
# This is where they're gathered.
my (@rd_ready, @wr_ready, @ex_ready);
my %poll_fd_results = @results;
while (my ($fd, $got_mask) = each %poll_fd_results) {
next unless $got_mask;
my $watch_mask = $poll_fd_masks{$fd};
if (
$watch_mask & POLLRDNORM and
$got_mask & (POLLRDNORM | POLLHUP | POLLERR | POLLNVAL)
) {
if (TRACE_FILES) {
POE::Kernel::_warn "<fh> enqueuing read for fileno $fd";
}
push @rd_ready, $fd;
}
if (
$watch_mask & POLLWRNORM and
$got_mask & (POLLWRNORM | POLLHUP | POLLERR | POLLNVAL)
) {
if (TRACE_FILES) {
POE::Kernel::_warn "<fh> enqueuing write for fileno $fd";
}
push @wr_ready, $fd;
}
if (
$watch_mask & POLLRDBAND and
$got_mask & (POLLRDBAND | POLLHUP | POLLERR | POLLNVAL)
) {
if (TRACE_FILES) {
POE::Kernel::_warn "<fh> enqueuing expedite for fileno $fd";
}
push @ex_ready, $fd;
}
}
@rd_ready and $self->_data_handle_enqueue_ready(MODE_RD, @rd_ready);
@wr_ready and $self->_data_handle_enqueue_ready(MODE_WR, @wr_ready);
@ex_ready and $self->_data_handle_enqueue_ready(MODE_EX, @ex_ready);
}
}
elsif ($timeout) {
# No filehandles to poll on. Try to sleep instead. Use sleep()
# itself on MSWin32. Use a dummy four-argument select() everywhere
# else.
if ($^O eq 'MSWin32') {
sleep($timeout);
}
else {
CORE::select(undef, undef, undef, $timeout);
}
}
# Dispatch whatever events are due.
$self->_data_ev_dispatch_due();
}
### Run for as long as there are sessions to service.
sub loop_run {
my $self = shift;
while ($self->_data_ses_count()) {
$self->loop_do_timeslice();
}
}
sub loop_halt {
# does nothing
}
1;
__END__
=head1 NAME
POE::Loop::IO_Poll - a bridge that allows POE to be driven by IO::Poll
=head1 SYNOPSIS
See L<POE::Loop>.
=head1 DESCRIPTION
POE::Loop::IO_Poll implements the interface documented in L<POE::Loop>.
Therefore it has no documentation of its own. Please see L<POE::Loop>
for more details.
=head1 SEE ALSO
L<POE>, L<POE::Loop>, L<IO::Poll>, L<POE::Loop::PerlSignals>
=head1 AUTHORS & LICENSING
Please see L<POE> for more information about authors, contributors,
and POE's licensing.
=cut
# rocco // vim: ts=2 sw=2 expandtab
# TODO - Edit.
|