revert to using std::thread. experimenting

This commit is contained in:
SCG82 2019-12-12 02:52:37 -08:00
parent 37042f6366
commit ebbd955abd
5 changed files with 76 additions and 71 deletions

View File

@ -11,7 +11,7 @@ dylibbundler:
$(CXX) $(CXXFLAGS) -c -I./src ./src/Dependency.cpp -o ./Dependency.o $(CXX) $(CXXFLAGS) -c -I./src ./src/Dependency.cpp -o ./Dependency.o
$(CXX) $(CXXFLAGS) -c -I./src ./src/main.cpp -o ./main.o $(CXX) $(CXXFLAGS) -c -I./src ./src/main.cpp -o ./main.o
$(CXX) $(CXXFLAGS) -c -I./src ./src/Utils.cpp -o ./Utils.o $(CXX) $(CXXFLAGS) -c -I./src ./src/Utils.cpp -o ./Utils.o
$(CXX) $(CXXFLAGS) -lpthread -o ./dylibbundler ./Settings.o ./DylibBundler.o ./Dependency.o ./main.o ./Utils.o $(CXX) $(CXXFLAGS) -o ./dylibbundler ./Settings.o ./DylibBundler.o ./Dependency.o ./main.o ./Utils.o
clean: clean:
rm -f *.o rm -f *.o

View File

@ -246,7 +246,6 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
exit(1); exit(1);
} }
} }
pthread_exit(NULL);
}); });
// FIXME - hackish // FIXME - hackish
@ -273,7 +272,6 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
exit(1); exit(1);
} }
} }
pthread_exit(NULL);
}); });
} }
} }

View File

@ -52,7 +52,7 @@ void changeLibPathsOnFile(std::string file_to_fix)
{ {
deps[i].fixFileThatDependsOnMe(file_to_fix); deps[i].fixFileThatDependsOnMe(file_to_fix);
} }
pthread_exit(NULL);
}); });
} }
@ -152,8 +152,11 @@ void fixRpathsOnFile(const std::string& original_file, const std::string& file_t
rpaths_to_fix = found->second; rpaths_to_fix = found->second;
} }
parallel_for(rpaths_to_fix.size(), [&](int start, int end) int start = 0;
{ int end = rpaths_to_fix.size();
// parallel_for(rpaths_to_fix.size(), [&](int start, int end)
// {
for(int i=start; i<end; ++i) for(int i=start; i<end; ++i)
{ {
std::string command = std::string("install_name_tool -rpath ") + std::string command = std::string("install_name_tool -rpath ") +
@ -164,8 +167,8 @@ void fixRpathsOnFile(const std::string& original_file, const std::string& file_t
exit(1); exit(1);
} }
} }
pthread_exit(NULL);
}); // });
} }
void addDependency(std::string path) void addDependency(std::string path)
@ -174,15 +177,17 @@ void addDependency(std::string path)
// we need to check if this library was already added to avoid duplicates // we need to check if this library was already added to avoid duplicates
const int dep_amount = deps.size(); const int dep_amount = deps.size();
parallel_for(dep_amount, [&](int start, int end) int start = 0;
{ int end = dep_amount;
// parallel_for(dep_amount, [&](int start, int end)
// {
for(int i=start; i<end; ++i) for(int i=start; i<end; ++i)
{ {
if(dep.mergeIfSameAs(deps[i])) if(dep.mergeIfSameAs(deps[i]))
return; return;
} }
pthread_exit(NULL); // pthread_exit(NULL);
}); // });
if(!Settings::isPrefixBundled(dep.getPrefix())) return; if(!Settings::isPrefixBundled(dep.getPrefix())) return;
@ -200,6 +205,7 @@ void collectDependencies(std::string filename, std::vector<std::string>& lines)
if(output.find("can't open file")!=std::string::npos or output.find("No such file")!=std::string::npos or output.size()<1) if(output.find("can't open file")!=std::string::npos or output.find("No such file")!=std::string::npos or output.size()<1)
{ {
std::cerr << output << std::endl;
std::cerr << "Cannot find file " << filename << " to read its dependencies" << std::endl; std::cerr << "Cannot find file " << filename << " to read its dependencies" << std::endl;
exit(1); exit(1);
} }
@ -217,8 +223,10 @@ void collectDependencies(std::string filename)
std::cout << "."; fflush(stdout); std::cout << "."; fflush(stdout);
const int line_amount = lines.size(); const int line_amount = lines.size();
parallel_for(line_amount, [&](int start, int end) int start = 0;
{ int end = line_amount;
// parallel_for(line_amount, [&](int start, int end)
// {
for(int i=start; i<end; ++i) for(int i=start; i<end; ++i)
{ {
std::cout << "."; fflush(stdout); std::cout << "."; fflush(stdout);
@ -235,8 +243,8 @@ void collectDependencies(std::string filename)
addDependency(dep_path); addDependency(dep_path);
} }
pthread_exit(NULL); // pthread_exit(NULL);
}); // });
} }
void collectSubDependencies() void collectSubDependencies()
@ -248,8 +256,10 @@ void collectSubDependencies()
while(true) while(true)
{ {
dep_amount = deps.size(); dep_amount = deps.size();
parallel_for(dep_amount, [&](int start, int end) int start = 0;
{ int end = deps.size();
// parallel_for(dep_amount, [&](int start, int end)
// {
for(int i=start; i<end; ++i) for(int i=start; i<end; ++i)
{ {
std::cout << "."; fflush(stdout); std::cout << "."; fflush(stdout);
@ -262,9 +272,10 @@ void collectSubDependencies()
collectDependencies(original_path, lines); collectDependencies(original_path, lines);
const int line_amount = lines.size(); const int line_amount = lines.size();
for(int j=0; j<line_amount; j++) int s = 0;
parallel_for(line_amount, [&](int s, int e) int e = line_amount;
{ // parallel_for(line_amount, [&](int s, int e)
// {
for(int j=s; j<e; ++j) for(int j=s; j<e; ++j)
{ {
if(lines[j][0] != '\t') if(lines[j][0] != '\t')
@ -279,11 +290,9 @@ void collectSubDependencies()
addDependency(dep_path); addDependency(dep_path);
} }
pthread_exit(NULL); // });
});
} }
pthread_exit(NULL); // });
});
if(deps.size() == dep_amount) if(deps.size() == dep_amount)
break; // no more dependencies were added on this iteration, stop searching break; // no more dependencies were added on this iteration, stop searching
@ -338,9 +347,7 @@ void doneWithDeps_go()
const int dep_amount = deps.size(); const int dep_amount = deps.size();
// print info to user // print info to user
for(int n=0; n<dep_amount; n++) for(int n=0; n<dep_amount; n++)
{
deps[n].print(); deps[n].print();
}
std::cout << std::endl; std::cout << std::endl;
// copy files if requested by user // copy files if requested by user
@ -357,7 +364,6 @@ void doneWithDeps_go()
changeLibPathsOnFile(deps[i].getInstallPath()); changeLibPathsOnFile(deps[i].getInstallPath());
fixRpathsOnFile(deps[i].getOriginalPath(), deps[i].getInstallPath()); fixRpathsOnFile(deps[i].getOriginalPath(), deps[i].getInstallPath());
} }
pthread_exit(NULL);
}); });
} }
@ -369,7 +375,7 @@ void doneWithDeps_go()
changeLibPathsOnFile(Settings::fileToFix(i)); changeLibPathsOnFile(Settings::fileToFix(i));
fixRpathsOnFile(Settings::fileToFix(i), Settings::fileToFix(i)); fixRpathsOnFile(Settings::fileToFix(i), Settings::fileToFix(i));
} }
pthread_exit(NULL);
}); });
} }

View File

@ -2,9 +2,9 @@
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <pthread.h> // #include <pthread.h>
typedef void *(*THREADFUNCPTR)(void *); // typedef void *(*THREADFUNCPTR)(void *);
/** /**
* @param nb_elements size of your for loop * @param nb_elements size of your for loop
@ -17,73 +17,74 @@ typedef void *(*THREADFUNCPTR)(void *);
* @param use_threads : enable / disable threads. * @param use_threads : enable / disable threads.
* *
*/ */
// static void parallel_for(unsigned nb_elements,
// std::function<void (int start, int end)> functor,
// bool use_threads = true)
static void parallel_for(unsigned nb_elements, static void parallel_for(unsigned nb_elements,
std::function<void (int start, int end)> functor, std::function<void (int start, int end)> functor,
bool use_threads = true) bool use_threads = true)
{ {
unsigned nb_threads_hint = std::thread::hardware_concurrency(); // unsigned nb_threads_hint = std::thread::hardware_concurrency();
unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint); // unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint);
unsigned nb_threads = 8;
unsigned batch_size = nb_elements / nb_threads; unsigned batch_size = nb_elements / nb_threads;
unsigned batch_remainder = nb_elements % nb_threads; unsigned batch_remainder = nb_elements % nb_threads;
std::vector<std::thread> my_threads(nb_threads); // std::vector<std::thread> my_threads(nb_threads);
std::vector<std::thread> my_threads(nb_threads+1);
pthread_t threads[nb_threads]; // pthread_t threads[nb_threads];
pthread_attr_t attr; // pthread_attr_t attr;
int rc; int rc;
int i; int i;
void *status; void *status;
// Initialize and set thread joinable // Initialize and set thread joinable
pthread_attr_init(&attr); // pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (use_threads) { if (use_threads) {
// Multithread execution // Multithread execution
// for(unsigned i = 0; i < nb_threads; ++i) for(unsigned i = 0; i < nb_threads; ++i) {
for (i = 0; i < nb_threads; ++i) { // for (i = 0; i < nb_threads; ++i) {
int start = i * batch_size; int start = i * batch_size;
// my_threads[i] = std::thread(functor, start, start+batch_size); my_threads[i] = std::thread(functor, start, start+batch_size);
rc = pthread_create(&threads[i], &attr, (THREADFUNCPTR)&functor, (void *)i); // rc = pthread_create(&threads[i], &attr, (THREADFUNCPTR)&functor, (void *)i);
if (rc) { // if (rc) {
std::cout << "Error:unable to create thread," << rc << std::endl; // std::cout << "Error:unable to create thread," << rc << std::endl;
exit(-1); // exit(-1);
} // }
std::cout << "pthread created" << std::endl; // std::cout << "pthread created" << std::endl;
} }
// free attribute and wait for the other threads // free attribute and wait for the other threads
pthread_attr_destroy(&attr); // pthread_attr_destroy(&attr);
for (i = 0; i < nb_threads; ++i) { // for (i = 0; i < nb_threads; ++i) {
rc = pthread_join(threads[i], &status); // rc = pthread_join(threads[i], &status);
if (rc) { // if (rc) {
std::cout << "Error:unable to join," << rc << std::endl; // std::cout << "Error:unable to join," << rc << std::endl;
exit(-1); // exit(-1);
} // }
std::cout << "Main: completed thread id :" << i ; // std::cout << "Main: completed thread id :" << i ;
std::cout << " exiting with status :" << status << std::endl; // std::cout << " exiting with status :" << status << std::endl;
} // }
std::cout << "Main: program exiting." << std::endl; // std::cout << "Main: program exiting." << std::endl;
pthread_exit(NULL); // pthread_exit(NULL);
} }
else { else {
// Single thread execution (for easy debugging) // Single thread execution (for easy debugging)
for (unsigned i = 0; i < nb_threads; ++i){ for (unsigned i = 0; i < nb_threads; ++i){
int start = i * batch_size; int start = i * batch_size;
functor( start, start+batch_size ); functor(start, start+batch_size);
} }
} }
// Deform the elements left // Deform the elements left
int start = nb_threads * batch_size; int start = nb_threads * batch_size;
functor(start, start+batch_remainder); // functor(start, start+batch_remainder);
my_threads[nb_threads] = std::thread(functor, start, start+batch_remainder);
// Wait for the other thread to finish their task // Wait for the other thread to finish their task
// if(use_threads) if(use_threads)
// std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join)); std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join));
} }

View File

@ -154,13 +154,13 @@ int main (int argc, char * const argv[])
std::cout << "* Collecting dependencies"; fflush(stdout); std::cout << "* Collecting dependencies"; fflush(stdout);
const int amount = Settings::fileToFixAmount(); const int amount = Settings::fileToFixAmount();
parallel_for(amount, [&](int start, int end) int start = 0;
{ int end = amount;
// parallel_for(amount, [&](int start, int end)
// {
for(int i=start; i<end; ++i) for(int i=start; i<end; ++i)
collectDependencies(Settings::fileToFix(i)); collectDependencies(Settings::fileToFix(i));
pthread_exit(NULL); // });
});
collectSubDependencies(); collectSubDependencies();
doneWithDeps_go(); doneWithDeps_go();