parallelize using libdispatch (Grand Central Dispatch)

This commit is contained in:
SCG82 2020-01-07 07:12:15 -08:00
parent dec128d769
commit a121fb7d8f
4 changed files with 29 additions and 6 deletions

View File

@ -24,4 +24,5 @@ add_executable(dylibbundler
src/Settings.h
src/Utils.cpp
src/Utils.h
src/ParallelForEach.h
)

View File

@ -6,8 +6,6 @@
#include <string>
#include <vector>
//#include "DylibBundler.h"
class DylibBundler;
class Dependency {

View File

@ -12,6 +12,7 @@
#include <linux/limits.h>
#endif
#include "ParallelForEach.h"
#include "Dependency.h"
#include "Utils.h"
@ -161,18 +162,18 @@ void DylibBundler::bundleDependencies()
// copy & fix up dependencies
if (bundleLibs()) {
createDestDir();
for (const auto& dep : deps) {
parallel_for_each(deps.begin(), deps.end(), [&](Dependency* dep) {
dep->CopyToBundle();
changeLibPathsOnFile(dep->InstallPath());
fixRpathsOnFile(dep->OriginalPath(), dep->InstallPath());
}
});
}
// fix up input files
const auto files = filesToFix();
for (const auto& file : files) {
parallel_for_each(files.begin(), files.end(), [&](const std::string& file) {
changeLibPathsOnFile(file);
fixRpathsOnFile(file, file);
}
});
}
void DylibBundler::bundleQtPlugins()

23
src/ParallelForEach.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef DYLIBBUNDLER_PARALLELFOREACH_H
#define DYLIBBUNDLER_PARALLELFOREACH_H
#include <iterator>
#include <dispatch/dispatch.h>
template<typename It, typename F>
inline void parallel_for_each(It a, It b, F&& f)
{
size_t count = std::distance(a,b);
using data_t = std::pair<It, F>;
data_t helper = data_t(a, std::forward<F>(f));
dispatch_apply_f(count,
dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0),
&helper,
[](void* ctx, size_t cnt) {
auto* d = static_cast<data_t*>(ctx);
auto elem_it = std::next(d->first, cnt);
(*d).second(*(elem_it));
});
}
#endif //DYLIBBUNDLER_PARALLELFOREACH_H