| 1 | package POE::Loop::EV; | ||
| 2 | |||
| 3 | # EV.pm (libev) event loop bridge | ||
| 4 | # $Id: EV.pm 27 2008-01-29 19:42:57Z andyg $ | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | our $VERSION = '0.06'; | ||
| 9 | |||
| 10 | # Everything plugs into POE::Kernel. | ||
| 11 | package # hide me from PAUSE | ||
| 12 | POE::Kernel; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | |||
| 16 | use EV; | ||
| 17 | use POE::Kernel; | ||
| 18 | |||
| 19 | # Loop debugging | ||
| 20 | sub EV_DEBUG () { $ENV{POE_EV_DEBUG} || 0 } | ||
| 21 | |||
| 22 | # Global EV timer object | ||
| 23 | my $_watcher_timer; | ||
| 24 | |||
| 25 | # Global SIGCHLD watcher | ||
| 26 | my $_child_watcher; | ||
| 27 | |||
| 28 | # Global list of EV signal objects | ||
| 29 | my %signal_events; | ||
| 30 | |||
| 31 | # Global list of EV filehandle objects, indexed by fd number | ||
| 32 | my @fileno_watcher; | ||
| 33 | |||
| 34 | my $DIE_MESSAGE; | ||
| 35 | |||
| 36 | ############################################################################ | ||
| 37 | # Initialization, Finalization, and the Loop itself | ||
| 38 | ############################################################################ | ||
| 39 | |||
| 40 | sub loop_initialize { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | if ( EV_DEBUG ) { | ||
| 44 | my $methods = { | ||
| 45 | EV::BACKEND_SELECT() => 'select', | ||
| 46 | EV::BACKEND_POLL() => 'poll', | ||
| 47 | EV::BACKEND_EPOLL() => 'epoll', | ||
| 48 | EV::BACKEND_KQUEUE() => 'kqueue', | ||
| 49 | EV::BACKEND_DEVPOLL() => 'devpoll', | ||
| 50 | EV::BACKEND_PORT() => 'port', | ||
| 51 | }; | ||
| 52 | |||
| 53 | warn "loop_initialize, EV is using method: " . $methods->{ EV::backend() } . "\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | # Set up the global timer object | ||
| 57 | $_watcher_timer = EV::periodic_ns( 0, 0, 0, \&_loop_timer_callback ); | ||
| 58 | |||
| 59 | # Set up the callback for SIGCHLD | ||
| 60 | $_child_watcher = EV::child( 0, 0, \&_child_callback ); | ||
| 61 | |||
| 62 | $EV::DIED = \&_die_handler; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Timer callback to dispatch events. | ||
| 66 | my $last_time = time(); | ||
| 67 | sub _loop_timer_callback { | ||
| 68 | my $self = $poe_kernel; | ||
| 69 | |||
| 70 | EV_DEBUG && warn "_loop_timer_callback, at " . time() . "\n"; | ||
| 71 | |||
| 72 | if (TRACE_STATISTICS) { | ||
| 73 | $self->_data_stat_add('idle_seconds', time() - $last_time); | ||
| 74 | } | ||
| 75 | |||
| 76 | $self->_data_ev_dispatch_due(); | ||
| 77 | $self->_test_if_kernel_is_idle(); | ||
| 78 | |||
| 79 | # Transferring control back to EV; this is idle time. | ||
| 80 | $last_time = time() if TRACE_STATISTICS; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub loop_finalize { | ||
| 84 | EV_DEBUG && warn "loop_finalize\n"; | ||
| 85 | |||
| 86 | foreach my $fd ( 0 .. $#fileno_watcher ) { | ||
| 87 | next unless defined $fileno_watcher[ $fd ]; | ||
| 88 | foreach my $mode ( EV::READ, EV::WRITE ) { | ||
| 89 | if ( defined $fileno_watcher[ $fd ]->[ $mode ] ) { | ||
| 90 | POE::Kernel::_warn( | ||
| 91 | "Mode $mode watcher for fileno $fd is defined during loop finalize" | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | loop_ignore_all_signals(); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub loop_attach_uidestroy { | ||
| 101 | # does nothing, no UI | ||
| 102 | } | ||
| 103 | |||
| 104 | sub loop_do_timeslice { | ||
| 105 | # does nothing | ||
| 106 | } | ||
| 107 | |||
| 108 | sub loop_run { | ||
| 109 | EV_DEBUG && warn "loop_run\n"; | ||
| 110 | |||
| 111 | EV::loop(); | ||
| 112 | |||
| 113 | if ( defined $DIE_MESSAGE ) { | ||
| 114 | my $message = $DIE_MESSAGE; | ||
| 115 | undef $DIE_MESSAGE; | ||
| 116 | die $message; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | sub loop_halt { | ||
| 121 | EV_DEBUG && warn "loop_halt\n"; | ||
| 122 | |||
| 123 | $_watcher_timer->stop(); | ||
| 124 | undef $_watcher_timer; | ||
| 125 | |||
| 126 | EV::unloop(); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub _die_handler { | ||
| 130 | EV_DEBUG && warn "_die_handler( $@ )\n"; | ||
| 131 | |||
| 132 | # EV doesn't let you rethrow an error here, so we have | ||
| 133 | # to stop the loop and get the error later | ||
| 134 | $DIE_MESSAGE = $@; | ||
| 135 | |||
| 136 | # This will cause the EV::loop call in loop_run to return, | ||
| 137 | # and cause the process to die. | ||
| 138 | EV::unloop(); | ||
| 139 | } | ||
| 140 | |||
| 141 | ############################################################################ | ||
| 142 | # Signal Handling | ||
| 143 | ############################################################################ | ||
| 144 | |||
| 145 | sub loop_watch_signal { | ||
| 146 | my ( $self, $signame ) = @_; | ||
| 147 | |||
| 148 | # Child process has stopped. | ||
| 149 | # XXX: libev always sets a SIGCHLD handler | ||
| 150 | if ( $signame eq 'CHLD' or $signame eq 'CLD' ) { | ||
| 151 | $_child_watcher->start(); | ||
| 152 | |||
| 153 | return; | ||
| 154 | } | ||
| 155 | |||
| 156 | EV_DEBUG && warn "loop_watch_signal( $signame )\n"; | ||
| 157 | |||
| 158 | $signal_events{ $signame } ||= EV::signal( | ||
| 159 | $signame, | ||
| 160 | sub { | ||
| 161 | if ( TRACE_SIGNALS ) { | ||
| 162 | my $pipelike = $signame eq 'PIPE' ? 'PIPE-like' : 'generic'; | ||
| 163 | POE::Kernel::_warn "<sg> Enqueuing $pipelike SIG$signame event"; | ||
| 164 | } | ||
| 165 | |||
| 166 | EV_DEBUG && warn "_loop_signal_callback( $signame )\n"; | ||
| 167 | |||
| 168 | $poe_kernel->_data_ev_enqueue( | ||
| 169 | $poe_kernel, $poe_kernel, EN_SIGNAL, ET_SIGNAL, [ $signame ], | ||
| 170 | __FILE__, __LINE__, undef, time() | ||
| 171 | ); | ||
| 172 | }, | ||
| 173 | ); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub loop_ignore_signal { | ||
| 177 | my ( $self, $signame ) = @_; | ||
| 178 | |||
| 179 | if ( $signame eq 'CHLD' or $signame eq 'CLD' ) { | ||
| 180 | if ( $_child_watcher ) { | ||
| 181 | $_child_watcher->stop(); | ||
| 182 | } | ||
| 183 | |||
| 184 | return; | ||
| 185 | } | ||
| 186 | |||
| 187 | if ( defined $signal_events{ $signame } ) { | ||
| 188 | $signal_events{ $signame }->stop(); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | sub loop_ignore_all_signals { | ||
| 193 | my $self = shift; | ||
| 194 | |||
| 195 | map { $_->stop() if defined $_ } values %signal_events; | ||
| 196 | |||
| 197 | %signal_events = (); | ||
| 198 | |||
| 199 | if ( $_child_watcher ) { | ||
| 200 | $_child_watcher->stop(); | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | sub _child_callback { | ||
| 205 | my $w = shift; | ||
| 206 | |||
| 207 | my $pid = $w->rpid; | ||
| 208 | my $status = $w->rstatus; | ||
| 209 | |||
| 210 | EV_DEBUG && warn "_child_callback( $pid, $status )\n"; | ||
| 211 | |||
| 212 | if ( TRACE_SIGNALS ) { | ||
| 213 | POE::Kernel::_warn("<sg> POE::Kernel detected SIGCHLD (pid=$pid; exit=$status)"); | ||
| 214 | } | ||
| 215 | |||
| 216 | # Check for explicit SIGCHLD watchers, and enqueue explicit | ||
| 217 | # events for them. | ||
| 218 | if ( exists $poe_kernel->[KR_PIDS]->{$pid} ) { | ||
| 219 | my @sessions_to_clear; | ||
| 220 | while ( my ($ses_key, $ses_rec) = each %{ $poe_kernel->[KR_PIDS]->{$pid} } ) { | ||
| 221 | $poe_kernel->_data_ev_enqueue( | ||
| 222 | $ses_rec->[0], $poe_kernel, $ses_rec->[1], ET_SIGCLD, | ||
| 223 | [ 'CHLD', $pid, $status ], | ||
| 224 | __FILE__, __LINE__, undef, time(), | ||
| 225 | ); | ||
| 226 | push @sessions_to_clear, $ses_rec->[0]; | ||
| 227 | } | ||
| 228 | $poe_kernel->_data_sig_pid_ignore($_, $pid) foreach @sessions_to_clear; | ||
| 229 | } | ||
| 230 | |||
| 231 | $poe_kernel->_data_ev_enqueue( | ||
| 232 | $poe_kernel, $poe_kernel, EN_SIGNAL, ET_SIGNAL, | ||
| 233 | [ 'CHLD', $pid, $status ], | ||
| 234 | __FILE__, __LINE__, undef, time() | ||
| 235 | ); | ||
| 236 | } | ||
| 237 | |||
| 238 | ############################################################################ | ||
| 239 | # Timer code | ||
| 240 | ############################################################################ | ||
| 241 | |||
| 242 | sub loop_resume_time_watcher { | ||
| 243 | my ( $self, $next_time ) = @_; | ||
| 244 | ( $_watcher_timer and $next_time ) or return; | ||
| 245 | |||
| 246 | EV_DEBUG && warn "loop_resume_time_watcher( $next_time, in " . ( $next_time - time() ) . " )\n"; | ||
| 247 | |||
| 248 | $_watcher_timer->set($next_time); | ||
| 249 | $_watcher_timer->start(); | ||
| 250 | } | ||
| 251 | |||
| 252 | sub loop_reset_time_watcher { | ||
| 253 | my ( $self, $next_time ) = @_; | ||
| 254 | ( $_watcher_timer and $next_time ) or return; | ||
| 255 | |||
| 256 | EV_DEBUG && warn "loop_reset_time_watcher( $next_time, in " . ( $next_time - time() ) . " )\n"; | ||
| 257 | |||
| 258 | $_watcher_timer->set($next_time); | ||
| 259 | $_watcher_timer->start(); | ||
| 260 | } | ||
| 261 | |||
| 262 | sub loop_pause_time_watcher { | ||
| 263 | $_watcher_timer or return; | ||
| 264 | |||
| 265 | EV_DEBUG && warn "loop_pause_time_watcher()\n"; | ||
| 266 | |||
| 267 | $_watcher_timer->stop(); | ||
| 268 | } | ||
| 269 | |||
| 270 | ############################################################################ | ||
| 271 | # Filehandle code | ||
| 272 | ############################################################################ | ||
| 273 | |||
| 274 | # helper function, not a method | ||
| 275 | sub _mode_to_ev { | ||
| 276 | return EV::READ if $_[0] == MODE_RD; | ||
| 277 | return EV::WRITE if $_[0] == MODE_WR; | ||
| 278 | |||
| 279 | confess "POE::Loop::EV does not support MODE_EX" | ||
| 280 | if $_[0] == MODE_EX; | ||
| 281 | |||
| 282 | confess "Unknown mode $_[0]"; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub loop_watch_filehandle { | ||
| 286 | my ( $self, $handle, $mode ) = @_; | ||
| 287 | |||
| 288 | my $fileno = fileno($handle); | ||
| 289 | my $watcher = $fileno_watcher[ $fileno ]->[ $mode ]; | ||
| 290 | |||
| 291 | if ( defined $watcher ) { | ||
| 292 | $watcher->stop(); | ||
| 293 | undef $fileno_watcher[ $fileno ]->[ $mode ]; | ||
| 294 | } | ||
| 295 | |||
| 296 | EV_DEBUG && warn "loop_watch_filehandle( $handle ($fileno), $mode )\n"; | ||
| 297 | |||
| 298 | $fileno_watcher[ $fileno ]->[ $mode ] = EV::io( | ||
| 299 | $fileno, | ||
| 300 | _mode_to_ev($mode), | ||
| 301 | \&_loop_filehandle_callback, | ||
| 302 | ); | ||
| 303 | } | ||
| 304 | |||
| 305 | sub loop_ignore_filehandle { | ||
| 306 | my ( $self, $handle, $mode ) = @_; | ||
| 307 | |||
| 308 | my $fileno = fileno($handle); | ||
| 309 | my $watcher = $fileno_watcher[ $fileno ]->[ $mode ]; | ||
| 310 | |||
| 311 | return if !defined $watcher; | ||
| 312 | |||
| 313 | EV_DEBUG && warn "loop_ignore_filehandle( $handle ($fileno), $mode )\n"; | ||
| 314 | |||
| 315 | $watcher->stop(); | ||
| 316 | |||
| 317 | undef $fileno_watcher[ $fileno ]->[ $mode ]; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub loop_pause_filehandle { | ||
| 321 | my ( $self, $handle, $mode ) = @_; | ||
| 322 | |||
| 323 | my $fileno = fileno($handle); | ||
| 324 | |||
| 325 | $fileno_watcher[ $fileno ]->[ $mode ]->stop(); | ||
| 326 | |||
| 327 | EV_DEBUG && warn "loop_pause_filehandle( $handle ($fileno), $mode )\n"; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub loop_resume_filehandle { | ||
| 331 | my ( $self, $handle, $mode ) = @_; | ||
| 332 | |||
| 333 | my $fileno = fileno($handle); | ||
| 334 | |||
| 335 | $fileno_watcher[ $fileno ]->[ $mode ]->start(); | ||
| 336 | |||
| 337 | EV_DEBUG && warn "loop_resume_filehandle( $handle ($fileno), $mode )\n"; | ||
| 338 | } | ||
| 339 | |||
| 340 | sub _loop_filehandle_callback { | ||
| 341 | my ( $watcher, $ev_mode ) = @_; | ||
| 342 | |||
| 343 | EV_DEBUG && warn "_loop_filehandle_callback( " . $watcher->fh . ", $ev_mode )\n"; | ||
| 344 | |||
| 345 | my $mode = ( $ev_mode == EV::READ ) | ||
| 346 | ? MODE_RD | ||
| 347 | : ( $ev_mode == EV::WRITE ) | ||
| 348 | ? MODE_WR | ||
| 349 | : confess "Invalid mode occured in POE::Loop::EV IO callback: $ev_mode"; | ||
| 350 | |||
| 351 | # ->fh is actually the fileno, since that's what we called EV::io with | ||
| 352 | $poe_kernel->_data_handle_enqueue_ready( $mode, $watcher->fh ); | ||
| 353 | |||
| 354 | $poe_kernel->_test_if_kernel_is_idle(); | ||
| 355 | } | ||
| 356 | |||
| 357 | 1; | ||
| 358 | |||
| 359 | __END__ |