@@ -31,23 +31,14 @@ ps_move_tracker<DiagnosticsEnabled>::ps_move_tracker()
3131 m_vc_attr.red_gain = 1 .0f ;
3232 m_vc_attr.green_gain = 1 .0f ;
3333 m_vc_attr.blue_gain = 1 .0f ;
34-
35- init_workers ();
3634}
3735
3836template <bool DiagnosticsEnabled>
3937ps_move_tracker<DiagnosticsEnabled>::~ps_move_tracker ()
4038{
4139 for (u32 index = 0 ; index < CELL_GEM_MAX_NUM ; index++)
4240 {
43- if (auto & worker = m_workers[index])
44- {
45- auto & thread = *worker;
46- thread = thread_state::aborting;
47- m_wake_up_workers[index].release (1 );
48- m_wake_up_workers[index].notify_one ();
49- thread ();
50- }
41+ join_worker (index);
5142 }
5243}
5344
@@ -139,52 +130,61 @@ void ps_move_tracker<DiagnosticsEnabled>::set_saturation_threshold(u32 index, u1
139130}
140131
141132template <bool DiagnosticsEnabled>
142- void ps_move_tracker<DiagnosticsEnabled>::init_workers( )
133+ void ps_move_tracker<DiagnosticsEnabled>::init_worker( u32 index )
143134{
144- for (u32 index = 0 ; index < CELL_GEM_MAX_NUM ; index++)
135+ auto & worker = ::at32 (m_workers, index);
136+ if (worker) return ;
137+
138+ worker = std::make_unique<named_thread<std::function<void ()>>>(fmt::format (" PS Move Worker %d" , index), [this , index]()
145139 {
146- if (m_workers[index] )
140+ while ( thread_ctrl::state () != thread_state::aborting )
147141 {
148- continue ;
149- }
142+ // Wait for work
143+ m_wake_up_workers[index].wait (0 );
144+ m_wake_up_workers[index].release (0 );
150145
151- m_workers[index] = std::make_unique<named_thread<std::function<void ()>>>(fmt::format (" PS Move Worker %d" , index), [this , index]()
152- {
153- while (thread_ctrl::state () != thread_state::aborting)
146+ if (thread_ctrl::state () == thread_state::aborting)
154147 {
155- // Notify that all work is done
156- m_workers_finished[index].release (1 );
157- m_workers_finished[index].notify_one ();
158-
159- // Wait for work
160- m_wake_up_workers[index].wait (0 );
161- m_wake_up_workers[index].release (0 );
162-
163- if (thread_ctrl::state () == thread_state::aborting)
164- {
165- break ;
166- }
167-
168- // Find contours
169- ps_move_info& info = m_info[index];
170- ps_move_info new_info = info;
171- process_contours (new_info, index);
172-
173- if (new_info.valid )
174- {
175- info = std::move (new_info);
176- }
177- else
178- {
179- info.valid = false ;
180- }
148+ break ;
181149 }
182150
183- // Notify one last time that all work is done
151+ // Find contours
152+ ps_move_info& info = m_info[index];
153+ ps_move_info new_info = info;
154+ process_contours (new_info, index);
155+
156+ if (new_info.valid )
157+ {
158+ info = std::move (new_info);
159+ }
160+ else
161+ {
162+ info.valid = false ;
163+ }
164+
165+ // Notify that all work is done
184166 m_workers_finished[index].release (1 );
185167 m_workers_finished[index].notify_one ();
186- });
187- }
168+ }
169+
170+ // Notify one last time that all work is done
171+ m_workers_finished[index].release (1 );
172+ m_workers_finished[index].notify_one ();
173+ });
174+ }
175+
176+ template <bool DiagnosticsEnabled>
177+ void ps_move_tracker<DiagnosticsEnabled>::join_worker(u32 index)
178+ {
179+ auto & worker = ::at32 (m_workers, index);
180+ if (!worker) return ;
181+
182+ auto & thread = *worker;
183+ thread = thread_state::aborting;
184+ m_wake_up_workers[index].release (1 );
185+ m_wake_up_workers[index].notify_one ();
186+ thread ();
187+ worker.reset ();
188188}
189189
190190template <bool DiagnosticsEnabled>
@@ -204,10 +204,12 @@ void ps_move_tracker<DiagnosticsEnabled>::process_image()
204204
205205 if (config.active )
206206 {
207+ init_worker (index);
207208 active_devices.push_back (index);
208209 }
209210 else
210211 {
212+ join_worker (index);
211213 ps_move_info& info = m_info[index];
212214 info.valid = false ;
213215 m_fail_count[index] = 0 ;
0 commit comments