lib/App/dupfind/Threaded/ThreadManagement.pm

filter: Statementstmt 76.9% 10 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 5 / 6 Podpod n/a totaltotal 78.9% 15 / 19 CC 2 SCAR 4.0
CC
2
Cov
77%
__ANON__
2.5
BEGIN
1.3
BEGIN
1.1
CRAP 1.5
« lib/App/dupfind/Guts/Algorithms.pm ↑ Summary blib/lib/App/dupfind/Threaded/MapReduce.pm »
1 # ABSTRACT: Thread management logic, abstracted safely away in its own namespace
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package App::dupfind::Threaded::ThreadManagement;
7 {
8 $App::dupfind::Threaded::ThreadManagement::VERSION = '0.140230'; # TRIAL
9 }
10  
11 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 BEGIN
14 3 ▶{
Subroutine BEGIN: called CC=1 SCAR=1.2
15 0 ▶ $SIG{TERM} = $SIG{INT} = sub { $_->kill( 'KILL' ) for threads->list };
Subroutine __ANON__: not called CC=2 SCAR=9.2
16 }
17  
18 0 ▶use threads;
Subroutine BEGIN: called CC=1 SCAR=2.6
19 use threads::shared;
20  
21 our $counter :shared = 0;
22 our $term_flag :shared = 0;
23 our $init_flag :shared = 0;
24 our $mapped = &share( {} );
25  
26 use Moo;
27  
28 use Thread::Queue;
29 use Time::HiRes 'usleep';
30  
31 use lib 'lib';
32  
33 extends 'App::dupfind';
34  
35 with 'App::dupfind::Threaded::Overrides';
36  
37 has work_queue => ( is => 'rw', default => sub { Thread::Queue->new } );
38  
39 before threads_progress => sub { require Term::ProgressBar };
40  
41  
42 sub mapped { $mapped }
43  
44 sub counter { $counter }
45  
46 sub reset_all
47 {
48 my $self = shift;
49  
50 $self->reset_queue;
51  
52 $self->clear_counter;
53  
54 $self->reset_mapped;
55  
56 $self->init_flag( 0 );
57  
58 $self->term_flag( 0 );
59 }
60  
61 sub reset_queue { shift->work_queue( Thread::Queue->new ) };
62  
63 sub clear_counter { lock $counter; $counter = 0; return $counter; }
64  
65 sub reset_mapped { $mapped = &share( {} ); $mapped; }
66  
67 sub increment_counter { lock $counter; return ++$counter; }
68  
69 sub term_flag
70 {
71 shift;
72  
73 if ( @_ ) { lock $term_flag; $term_flag = shift; }
74  
75 return $term_flag
76 }
77  
78 sub init_flag
79 {
80 shift;
81  
82 if ( @_ ) { lock $init_flag; $init_flag = shift; }
83  
84 return $init_flag
85 }
86  
87 sub push_mapped
88 {
89 my ( $self, $key, @vals ) = @_;
90  
91 lock $mapped;
92  
93 $mapped->{ $key } ||= &share( [] );
94  
95 push @{ $mapped->{ $key } }, @vals;
96  
97 return $mapped;
98 }
99  
100 sub delete_mapped
101 {
102 my ( $self, @keys ) = @_;
103  
104 lock $mapped;
105  
106 delete $mapped->{ $_ } for @keys;
107  
108 return $mapped;
109 }
110  
111 sub create_thread_pool
112 {
113 my ( $self, $map_code, $dup_count ) = @_;
114  
115 $self->init_flag( 1 );
116  
117 threads->create( threads_progress => $self => $dup_count )
118 if $self->opts->{progress};
119  
120 for ( 1 .. $self->opts->{threads} )
121 {
122 # $map coderef is responsible for calling $self->increment_counter!
123  
124 threads->create( $map_code );
125 }
126 }
127  
128 sub end_wait_thread_pool
129 {
130 my $self = shift;
131  
132 $self->term_flag( 1 );
133  
134 $self->work_queue->end;
135  
136 $_->join for threads->list;
137 }
138  
139 sub threads_progress
140 {
141 my ( $self, $task_item_count ) = @_;
142  
143 my $last_update = 0;
144  
145 my $threads_progress = Term::ProgressBar->new
146 (
147 {
148 name => ' ...PROGRESS',
149 count => $task_item_count,
150 remove => 1,
151 }
152 );
153  
154 while ( !$self->term_flag )
155 {
156 usleep 1000; # sleep for 1 millisecond
157  
158 $threads_progress->update( $self->counter )
159 if $self->counter > $last_update;
160  
161 last if $self->counter == $task_item_count;
162  
163 $last_update = $self->counter;
164 }
165  
166 $threads_progress->update( $task_item_count );
167 }
168  
169 __PACKAGE__->meta->make_immutable;
170  
171 1;
172  
173 __END__
« lib/App/dupfind/Guts/Algorithms.pm ↑ Summary blib/lib/App/dupfind/Threaded/MapReduce.pm »