Fixed concurrency issues

This commit is contained in:
Alessandro Ranellucci 2014-06-13 19:23:51 +02:00
parent b9de65e174
commit d896ad090b
2 changed files with 54 additions and 24 deletions

View file

@ -84,6 +84,9 @@ use constant INFILL_OVERLAP_OVER_SPACING => 0.45;
use constant EXTERNAL_INFILL_MARGIN => 3;
use constant INSET_OVERLAP_TOLERANCE => 0.2;
# keep track of threads we created
my @threads = ();
sub parallelize {
my %params = @_;
@ -93,7 +96,13 @@ sub parallelize {
$q->enqueue(@items, (map undef, 1..$params{threads}));
my $thread_cb = sub {
# ignore threads created by our parent
@threads = ();
# execute thread callback
$params{thread_cb}->($q);
# cleanup before terminating thread
Slic3r::thread_cleanup();
# This explicit exit avoids an untrappable
@ -111,7 +120,10 @@ sub parallelize {
$params{collect_cb} ||= sub {};
@_ = ();
foreach my $th (map threads->create($thread_cb), 1..$params{threads}) {
my @my_threads = map threads->create($thread_cb), 1..$params{threads};
push @threads, map $_->tid, @my_threads;
foreach my $th (@my_threads) {
$params{collect_cb}->($th->join);
}
} else {
@ -131,6 +143,8 @@ sub parallelize {
# object in a thread, make sure the main thread still holds a
# reference so that it won't be destroyed in thread.
sub thread_cleanup {
return if !$Slic3r::have_threads;
# prevent destruction of shared objects
no warnings 'redefine';
*Slic3r::Config::DESTROY = sub {};
@ -162,6 +176,10 @@ sub thread_cleanup {
*Slic3r::Surface::DESTROY = sub {};
*Slic3r::Surface::Collection::DESTROY = sub {};
*Slic3r::TriangleMesh::DESTROY = sub {};
# detach any running thread created in the current one
$_->detach for grep defined($_), map threads->object($_), @threads;
return undef; # this prevents a "Scalars leaked" warning
}