Refactored parallelization code. #154

This commit is contained in:
Alessandro Ranellucci 2012-04-09 13:53:13 +02:00
parent 68b48fa807
commit 64e669d893
2 changed files with 26 additions and 28 deletions

View File

@ -568,25 +568,24 @@ sub generate_support_material {
return @paths;
};
my %layer_paths = ();
if ($Config{useithreads} && $Slic3r::threads > 1 && eval "use threads; use Thread::Queue; 1") {
my $q = Thread::Queue->new;
$q->enqueue(keys %layers, (map undef, 1..$Slic3r::threads));
my $thread_cb = sub {
Slic3r::parallelize(
items => [ keys %layers ],
thread_cb => sub {
my $q = shift;
my $paths = {};
while (defined (my $layer_id = $q->dequeue)) {
$paths->{$layer_id} = [ $clip_pattern->($layers{$layer_id}) ];
}
return $paths;
};
foreach my $th (map threads->create($thread_cb), 1..$Slic3r::threads) {
my $paths = $th->join;
},
collect_cb => sub {
my $paths = shift;
$layer_paths{$_} = $paths->{$_} for keys %$paths;
}
} else {
$layer_paths{$_} = [ $clip_pattern->($layers{$_}) ] for keys %layers;
}
},
no_threads_cb => sub {
$layer_paths{$_} = [ $clip_pattern->($layers{$_}) ] for keys %layers;
},
);
foreach my $layer_id (keys %layer_paths) {
my $layer = $self->layers->[$layer_id];

View File

@ -94,30 +94,29 @@ sub go {
{
my $fill_maker = Slic3r::Fill->new('print' => $print);
if ($Config{useithreads} && $Slic3r::threads > 1 && eval "use threads; use Thread::Queue; 1") {
my $q = Thread::Queue->new;
$q->enqueue(0..($print->layer_count-1), (map undef, 1..$Slic3r::threads));
my $thread_cb = sub {
Slic3r::parallelize(
items => [ 0..($print->layer_count-1) ],
thread_cb => sub {
my $q = shift;
$Slic3r::Geometry::Clipper::clipper = Math::Clipper->new;
my $fills = {};
while (defined (my $layer_id = $q->dequeue)) {
$fills->{$layer_id} = [ $fill_maker->make_fill($print->layers->[$layer_id]) ];
}
return $fills;
};
foreach my $th (map threads->create($thread_cb), 1..$Slic3r::threads) {
my $fills = $th->join;
},
collect_cb => sub {
my $fills = shift;
foreach my $layer_id (keys %$fills) {
@{$print->layers->[$layer_id]->fills} = @{$fills->{$layer_id}};
}
}
} else {
foreach my $layer (@{$print->layers}) {
@{$layer->fills} = $fill_maker->make_fill($layer);
}
}
},
no_threads_cb => sub {
foreach my $layer (@{$print->layers}) {
@{$layer->fills} = $fill_maker->make_fill($layer);
}
},
);
}
# generate support material