@@ -210,70 +210,39 @@ BackupFileInfos buildFileInfosForBackupEntries(const BackupEntries & backup_entr
210210 BackupFileInfos infos;
211211 infos.resize (backup_entries.size ());
212212
213- size_t num_active_jobs = 0 ;
214- std::mutex mutex;
215- std::condition_variable event;
216- std::exception_ptr exception;
213+ std::atomic_bool failed = false ;
217214
218- auto thread_group = CurrentThread::getGroup ();
219215 LoggerPtr log = getLogger (" FileInfosFromBackupEntries" );
220216
217+ ThreadPoolCallbackRunnerLocal<void > runner (thread_pool, " BackupWorker" );
221218 for (size_t i = 0 ; i != backup_entries.size (); ++i)
222219 {
223- {
224- std::lock_guard lock{mutex};
225- if (exception)
226- break ;
227- ++num_active_jobs;
228- }
220+ if (failed)
221+ break ;
229222
230- auto job = [&mutex, &num_active_jobs, &event, &exception, & infos, &backup_entries, &read_settings, &base_backup, &thread_group, & process_list_element, i, log]()
223+ runner ([& infos, &backup_entries, &read_settings, &base_backup, &process_list_element, i, log, &failed ]()
231224 {
232- SCOPE_EXIT_SAFE ({
233- std::lock_guard lock{mutex};
234- if (!--num_active_jobs)
235- event.notify_all ();
236- CurrentThread::detachFromGroupIfNotDetached ();
237- });
238-
225+ if (failed)
226+ return ;
239227 try
240228 {
241229 const auto & name = backup_entries[i].first ;
242230 const auto & entry = backup_entries[i].second ;
243231
244- if (thread_group)
245- CurrentThread::attachToGroup (thread_group);
246-
247- setThreadName (" BackupWorker" );
248-
249- {
250- std::lock_guard lock{mutex};
251- if (exception)
252- return ;
253- }
254-
255232 if (process_list_element)
256233 process_list_element->checkTimeLimit ();
257234
258235 infos[i] = buildFileInfoForBackupEntry (name, entry, base_backup, read_settings, log);
259236 }
260237 catch (...)
261238 {
262- std::lock_guard lock{mutex};
263- if (!exception)
264- exception = std::current_exception ();
239+ failed = true ;
240+ throw ;
265241 }
266- };
267-
268- thread_pool.scheduleOrThrowOnError (job);
242+ });
269243 }
270244
271- {
272- std::unique_lock lock{mutex};
273- event.wait (lock, [&] { return !num_active_jobs; });
274- if (exception)
275- std::rethrow_exception (exception);
276- }
245+ runner.waitForAllToFinishAndRethrowFirstError ();
277246
278247 return infos;
279248}
0 commit comments