Fight deadlocks

This commit is contained in:
Alessandro Ranellucci 2014-12-22 19:47:39 +01:00
parent bb907fb405
commit 6a939eb250
2 changed files with 13 additions and 10 deletions

View File

@ -99,9 +99,9 @@ sub spawn_thread {
my $thread = threads->create(sub { my $thread = threads->create(sub {
@my_threads = (); @my_threads = ();
Slic3r::debugf "Starting thread %d (parent: %d)...\n", threads->tid, $parent_tid; printf "Starting thread %d (parent: %d)...\n", threads->tid, $parent_tid;
local $SIG{'KILL'} = sub { local $SIG{'KILL'} = sub {
Slic3r::debugf "Exiting thread %d...\n", threads->tid; printf "Exiting thread %d...\n", threads->tid;
$parallel_sema->up if $parallel_sema; $parallel_sema->up if $parallel_sema;
kill_all_threads(); kill_all_threads();
Slic3r::thread_cleanup(); Slic3r::thread_cleanup();
@ -121,6 +121,7 @@ sub spawn_thread {
sub parallelize { sub parallelize {
my %params = @_; my %params = @_;
lock @threads;
if (!$params{disable} && $Slic3r::have_threads && $params{threads} > 1) { if (!$params{disable} && $Slic3r::have_threads && $params{threads} > 1) {
my @items = (ref $params{items} eq 'CODE') ? $params{items}->() : @{$params{items}}; my @items = (ref $params{items} eq 'CODE') ? $params{items}->() : @{$params{items}};
my $q = Thread::Queue->new; my $q = Thread::Queue->new;
@ -224,33 +225,35 @@ sub kill_all_threads {
if (threads->tid == 0) { if (threads->tid == 0) {
lock @threads; lock @threads;
foreach my $thread (get_running_threads(@threads)) { foreach my $thread (get_running_threads(@threads)) {
Slic3r::debugf "Thread %d killing %d...\n", threads->tid, $thread->tid; printf "Thread %d killing %d...\n", threads->tid, $thread->tid;
$thread->kill('KILL'); $thread->kill('KILL');
} }
# unlock semaphore before we block on wait # unlock semaphore before we block on wait
# otherwise we'd get a deadlock if threads were paused # otherwise we'd get a deadlock if threads were paused
resume_threads(); resume_all_threads();
} }
# in any thread we wait for our children # in any thread we wait for our children
foreach my $thread (get_running_threads(@my_threads)) { foreach my $thread (get_running_threads(@my_threads)) {
Slic3r::debugf " Thread %d waiting for %d...\n", threads->tid, $thread->tid; printf " Thread %d waiting for %d...\n", threads->tid, $thread->tid;
$thread->join; # block until threads are killed $thread->join; # block until threads are killed
Slic3r::debugf " Thread %d finished waiting for %d...\n", threads->tid, $thread->tid; printf " Thread %d finished waiting for %d...\n", threads->tid, $thread->tid;
} }
@my_threads = (); @my_threads = ();
} }
sub pause_threads { sub pause_all_threads {
return if $paused; return if $paused;
lock @threads;
$paused = 1; $paused = 1;
$pause_sema->down; $pause_sema->down;
$_->kill('STOP') for get_running_threads(@threads); $_->kill('STOP') for get_running_threads(@threads);
} }
sub resume_threads { sub resume_all_threads {
return unless $paused; return unless $paused;
lock @threads;
$paused = 0; $paused = 0;
$pause_sema->up; $pause_sema->up;
} }

View File

@ -933,7 +933,7 @@ sub pause_background_process {
my ($self) = @_; my ($self) = @_;
if ($self->{process_thread} || $self->{export_thread}) { if ($self->{process_thread} || $self->{export_thread}) {
Slic3r::pause_threads(); Slic3r::pause_all_threads();
} elsif (defined $self->{apply_config_timer} && $self->{apply_config_timer}->IsRunning) { } elsif (defined $self->{apply_config_timer} && $self->{apply_config_timer}->IsRunning) {
$self->{apply_config_timer}->Stop; $self->{apply_config_timer}->Stop;
} }
@ -943,7 +943,7 @@ sub resume_background_process {
my ($self) = @_; my ($self) = @_;
if ($self->{process_thread} || $self->{export_thread}) { if ($self->{process_thread} || $self->{export_thread}) {
Slic3r::resume_threads(); Slic3r::resume_all_threads();
} }
} }