/usr/share/perl5/Padre/TaskHandle.pm is in padre 1.00+dfsg-3.
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 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | package Padre::TaskHandle;
use 5.008005;
use strict;
use warnings;
use threads;
use threads::shared;
use Scalar::Util ();
use Params::Util ();
use Storable ();
use Padre::Wx::Role::Conduit ();
use Padre::Logger;
our $VERSION = '1.00';
our $SEQUENCE = 0;
######################################################################
# Constructor and Accessors
sub new {
# TRACE( $_[0] ) if DEBUG;
bless {
hid => ++$SEQUENCE,
task => $_[1],
},
$_[0];
}
sub hid {
$_[0]->{hid};
}
sub task {
$_[0]->{task};
}
sub child {
$_[0]->{child};
}
sub class {
Scalar::Util::blessed( $_[0]->{task} );
}
sub has_owner {
!!$_[0]->{task}->{owner};
}
sub owner {
require Padre::Role::Task;
Padre::Role::Task->task_owner( $_[0]->{task}->{owner} );
}
sub worker {
my $self = shift;
$self->{worker} = shift if @_;
$self->{worker};
}
sub queue {
$_[0]->{queue};
}
sub start_time {
my $self = shift;
$self->{start_time} = $self->{idle_time} = shift if @_;
$self->{start_time};
}
sub idle_time {
my $self = shift;
$self->{idle_time} = shift if @_;
$self->{idle_time};
}
######################################################################
# Setup and teardown
# Called in the child thread to set the task and handle up for processing.
sub start {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
$self->{child} = 1;
$self->{queue} = shift;
$self->signal('STARTED');
}
# Signal the task has stopped
sub stop {
TRACE( $_[0] ) if DEBUG;
my $self = shift;
$self->{child} = undef;
$self->{queue} = undef;
$self->signal( 'STOPPED' => $self->{task} );
}
######################################################################
# Serialisation
sub as_array {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
my $task = $self->task;
return [
$self->hid,
Scalar::Util::blessed($task),
$task->as_string,
];
}
sub from_array {
# TRACE( $_[0] ) if DEBUG;
my $class = shift;
my $array = shift;
# Load the task class first so we can deserialize
TRACE("Loading $array->[1]") if DEBUG;
(my $source = $array->[1].".pm") =~ s{::}{/}g;
require $source;
return bless {
hid => $array->[0] + 0,
task => $array->[1]->from_string( $array->[2] ),
}, $class;
}
######################################################################
# Parent-Only Methods
sub prepare {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
my $task = $self->{task};
unless ( defined $task ) {
TRACE("Exception: task not defined") if DEBUG;
return !1;
}
my $rv = eval { $task->prepare; };
if ($@) {
TRACE("Exception in task during 'prepare': $@") if DEBUG;
return !1;
}
return !!$rv;
}
sub on_started {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
my $task = $self->{task};
# Does the task have an owner and can we call it
my $owner = $self->owner or return;
my $method = $task->on_run or return;
$owner->$method( $task => @_ );
return;
}
sub on_message {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
my $method = shift;
my $task = $self->{task};
unless ( $self->child ) {
# Special case for printing a simple message to the main window
# status bar, without needing to pollute the task classes.
if ( $method eq 'STATUS' ) {
return $self->on_status(@_);
}
# Special case for routing messages to the owner of a task
# rather than to the task itself.
if ( $method eq 'OWNER' ) {
require Padre::Role::Task;
my $owner = $self->owner or return;
my $method = $task->on_message or return;
$owner->$method( $task => @_ );
return;
}
}
# Does the method exist
unless ( $self->{task}->can($method) ) {
# A method name provided directly by the Task
# doesn't exist in the Task. Naughty Task!!!
# Lacking anything more sane to do, squelch it.
return;
}
# Pass the call down to the task and protect it from itself
local $@;
eval { $self->{task}->$method(@_); };
if ($@) {
# A method in the main thread blew up.
# Beyond catching it and preventing it killing
# Padre entirely, I'm not sure what else we can
# really do about it at this point.
return;
}
return;
}
sub on_status {
# TRACE( $_[1] ) if DEBUG;
my $self = shift;
# If we don't have an owner, use the general status bar
unless ( $self->has_owner ) {
require Padre::Current;
Padre::Current->main->status(@_);
return;
}
# If we have an owner that is within the main window show normally
my $owner = $self->owner or return;
my $method = $self->{task}->on_status;
return $owner->$method(@_) if $method;
# Pass status messages up to the main window status if possible
if ( $owner->isa('Padre::Wx::Role::Main') ) {
$owner->main->status(@_);
return;
}
# Nothing else to do
return;
}
sub on_stopped {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
# The first parameter is the updated Task object.
# Replace all content in the stored version with that from the
# event-provided version.
my $new = shift;
my $task = $self->{task};
%$task = %$new;
%$new = ();
# Execute the finish method in the updated Task object first, before
# the task owner is passed to the task owner (if any)
$self->finish;
# If the task has an owner it will get the finish method instead.
my $owner = $self->owner or return;
my $method = $self->{task}->on_finish;
local $@;
eval { $owner->$method( $self->{task} ); };
return;
}
sub finish {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
my $task = $self->{task};
my $rv = eval { $task->finish; };
if ($@) {
TRACE("Exception in task during 'finish': $@") if DEBUG;
return !1;
}
return !!$rv;
}
######################################################################
# Worker-Only Methods
sub run {
# TRACE( $_[0] ) if DEBUG;
my $self = shift;
my $task = $self->task;
# Create the inbox for the handle
local $self->{inbox} = [];
# Create a circular reference back from the task
# HACK: This is pretty damned evil, find a better way
local $task->{handle} = $self;
# Call the task's run method
eval { $task->run; };
if ($@) {
# Save the exception
TRACE("Exception in task during 'run': $@") if DEBUG;
$self->{exception} = $@;
return !1;
}
return 1;
}
# Poll the inbound queue and process them
sub poll {
my $self = shift;
my $inbox = $self->{inbox} or return;
my $queue = $self->{queue} or return;
# Fetch from the queue until we run out of messages or get a cancel
while ( my $item = $queue->dequeue1_nb ) {
# Handle a valid parent -> task message
if ( $item->[0] eq 'message' ) {
my $message = Storable::thaw( $item->[1] );
push @$inbox, $message;
next;
}
# Handle aborting the task
if ( $item->[0] eq 'cancel' ) {
$self->{cancelled} = 1;
delete $self->{queue};
next;
}
die "Unknown or unexpected message type '$item->[0]'";
}
return;
}
# Block until we have an inbox message or have been cancelled
sub wait {
my $self = shift;
my $inbox = $self->{inbox} or return;
my $queue = $self->{queue} or return;
# If something is in our inbox we don't need to wait
return if @$inbox;
# Fetch the next message from the queue, blocking if needed
my $item = $queue->dequeue1;
# Handle a valid parent -> task message
if ( $item->[0] eq 'message' ) {
my $message = Storable::thaw( $item->[1] );
push @$inbox, $message;
return;
}
# Handle aborting the task
if ( $item->[0] eq 'cancel' ) {
$self->{cancelled} = 1;
delete $self->{queue};
return;
}
die "Unknown or unexpected message type '$item->[0]'";
}
sub cancel {
$_[0]->{cancelled} = 1;
}
# Has this task been cancelled by the parent?
sub cancelled {
my $self = shift;
# Shortcut if we can to avoid queue locking
return 1 if $self->{cancelled};
# Poll for new input
$self->poll;
# Check again now we have polled for new messages
return !!$self->{cancelled};
}
# Fetch the next message from our inbox
sub inbox {
my $self = shift;
my $inbox = $self->{inbox} or return undef;
# Shortcut if we can to avoid queue locking
return shift @$inbox if @$inbox;
# Poll for new messages
$self->poll;
# Check again now we have polled for new messages
return shift @$inbox;
}
######################################################################
# Bidirectional Communication
sub signal {
# TRACE( $_[0] ) if DEBUG;
Padre::Wx::Role::Conduit->signal( [ shift->hid => @_ ] );
}
sub tell_parent {
TRACE( $_[0] ) if DEBUG;
shift->signal( PARENT => @_ );
}
sub tell_child {
TRACE( $_[0] ) if DEBUG;
my $self = shift;
if ( $self->child ) {
# Add the message directly to the inbox
my $inbox = $self->{inbox} or next;
push @$inbox, [@_];
} else {
$self->worker->send_message(@_);
}
return 1;
}
sub tell_owner {
# TRACE( $_[0] ) if DEBUG;
shift->signal( OWNER => @_ );
}
sub tell_status {
# TRACE( $_[0] ) if DEBUG;
shift->signal( STATUS => @_ ? @_ : '' );
}
1;
# Copyright 2008-2013 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.
|