revert to using std::thread. experimenting
This commit is contained in:
parent
37042f6366
commit
ebbd955abd
2
Makefile
2
Makefile
|
@ -11,7 +11,7 @@ dylibbundler:
|
|||
$(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/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:
|
||||
rm -f *.o
|
||||
|
|
|
@ -246,7 +246,6 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
|
|||
exit(1);
|
||||
}
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
|
||||
// FIXME - hackish
|
||||
|
@ -273,7 +272,6 @@ void Dependency::fixFileThatDependsOnMe(std::string file_to_fix)
|
|||
exit(1);
|
||||
}
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ void changeLibPathsOnFile(std::string 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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
|
||||
// });
|
||||
}
|
||||
|
||||
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
|
||||
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)
|
||||
{
|
||||
if(dep.mergeIfSameAs(deps[i]))
|
||||
return;
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
// pthread_exit(NULL);
|
||||
// });
|
||||
|
||||
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)
|
||||
{
|
||||
std::cerr << output << std::endl;
|
||||
std::cerr << "Cannot find file " << filename << " to read its dependencies" << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
|
@ -217,8 +223,10 @@ void collectDependencies(std::string filename)
|
|||
std::cout << "."; fflush(stdout);
|
||||
|
||||
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)
|
||||
{
|
||||
std::cout << "."; fflush(stdout);
|
||||
|
@ -235,8 +243,8 @@ void collectDependencies(std::string filename)
|
|||
|
||||
addDependency(dep_path);
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
// pthread_exit(NULL);
|
||||
// });
|
||||
}
|
||||
|
||||
void collectSubDependencies()
|
||||
|
@ -248,8 +256,10 @@ void collectSubDependencies()
|
|||
while(true)
|
||||
{
|
||||
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)
|
||||
{
|
||||
std::cout << "."; fflush(stdout);
|
||||
|
@ -262,9 +272,10 @@ void collectSubDependencies()
|
|||
collectDependencies(original_path, lines);
|
||||
|
||||
const int line_amount = lines.size();
|
||||
for(int j=0; j<line_amount; j++)
|
||||
parallel_for(line_amount, [&](int s, int e)
|
||||
{
|
||||
int s = 0;
|
||||
int e = line_amount;
|
||||
// parallel_for(line_amount, [&](int s, int e)
|
||||
// {
|
||||
for(int j=s; j<e; ++j)
|
||||
{
|
||||
if(lines[j][0] != '\t')
|
||||
|
@ -279,11 +290,9 @@ void collectSubDependencies()
|
|||
|
||||
addDependency(dep_path);
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
// });
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
// });
|
||||
|
||||
if(deps.size() == dep_amount)
|
||||
break; // no more dependencies were added on this iteration, stop searching
|
||||
|
@ -338,9 +347,7 @@ void doneWithDeps_go()
|
|||
const int dep_amount = deps.size();
|
||||
// print info to user
|
||||
for(int n=0; n<dep_amount; n++)
|
||||
{
|
||||
deps[n].print();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// copy files if requested by user
|
||||
|
@ -357,7 +364,6 @@ void doneWithDeps_go()
|
|||
changeLibPathsOnFile(deps[i].getInstallPath());
|
||||
fixRpathsOnFile(deps[i].getOriginalPath(), deps[i].getInstallPath());
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -369,7 +375,7 @@ void doneWithDeps_go()
|
|||
changeLibPathsOnFile(Settings::fileToFix(i));
|
||||
fixRpathsOnFile(Settings::fileToFix(i), Settings::fileToFix(i));
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
#include <thread>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
#include <pthread.h>
|
||||
// #include <pthread.h>
|
||||
|
||||
typedef void *(*THREADFUNCPTR)(void *);
|
||||
// typedef void *(*THREADFUNCPTR)(void *);
|
||||
|
||||
/**
|
||||
* @param nb_elements size of your for loop
|
||||
|
@ -17,73 +17,74 @@ typedef void *(*THREADFUNCPTR)(void *);
|
|||
* @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,
|
||||
std::function<void (int start, int end)> functor,
|
||||
bool use_threads = true)
|
||||
{
|
||||
unsigned nb_threads_hint = std::thread::hardware_concurrency();
|
||||
unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint);
|
||||
// unsigned nb_threads_hint = std::thread::hardware_concurrency();
|
||||
// unsigned nb_threads = nb_threads_hint == 0 ? 8 : (nb_threads_hint);
|
||||
|
||||
unsigned nb_threads = 8;
|
||||
|
||||
unsigned batch_size = 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_attr_t attr;
|
||||
// pthread_t threads[nb_threads];
|
||||
// pthread_attr_t attr;
|
||||
int rc;
|
||||
int i;
|
||||
void *status;
|
||||
|
||||
// Initialize and set thread joinable
|
||||
pthread_attr_init(&attr);
|
||||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||
// pthread_attr_init(&attr);
|
||||
// pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
||||
|
||||
if (use_threads) {
|
||||
// Multithread execution
|
||||
// for(unsigned i = 0; i < nb_threads; ++i)
|
||||
for (i = 0; i < nb_threads; ++i) {
|
||||
for(unsigned i = 0; i < nb_threads; ++i) {
|
||||
// for (i = 0; i < nb_threads; ++i) {
|
||||
int start = i * batch_size;
|
||||
// my_threads[i] = std::thread(functor, start, start+batch_size);
|
||||
rc = pthread_create(&threads[i], &attr, (THREADFUNCPTR)&functor, (void *)i);
|
||||
if (rc) {
|
||||
std::cout << "Error:unable to create thread," << rc << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
std::cout << "pthread created" << std::endl;
|
||||
my_threads[i] = std::thread(functor, start, start+batch_size);
|
||||
// rc = pthread_create(&threads[i], &attr, (THREADFUNCPTR)&functor, (void *)i);
|
||||
// if (rc) {
|
||||
// std::cout << "Error:unable to create thread," << rc << std::endl;
|
||||
// exit(-1);
|
||||
// }
|
||||
// std::cout << "pthread created" << std::endl;
|
||||
}
|
||||
|
||||
// free attribute and wait for the other threads
|
||||
pthread_attr_destroy(&attr);
|
||||
for (i = 0; i < nb_threads; ++i) {
|
||||
rc = pthread_join(threads[i], &status);
|
||||
if (rc) {
|
||||
std::cout << "Error:unable to join," << rc << std::endl;
|
||||
exit(-1);
|
||||
}
|
||||
std::cout << "Main: completed thread id :" << i ;
|
||||
std::cout << " exiting with status :" << status << std::endl;
|
||||
}
|
||||
// pthread_attr_destroy(&attr);
|
||||
// for (i = 0; i < nb_threads; ++i) {
|
||||
// rc = pthread_join(threads[i], &status);
|
||||
// if (rc) {
|
||||
// std::cout << "Error:unable to join," << rc << std::endl;
|
||||
// exit(-1);
|
||||
// }
|
||||
// std::cout << "Main: completed thread id :" << i ;
|
||||
// std::cout << " exiting with status :" << status << std::endl;
|
||||
// }
|
||||
|
||||
std::cout << "Main: program exiting." << std::endl;
|
||||
pthread_exit(NULL);
|
||||
// std::cout << "Main: program exiting." << std::endl;
|
||||
// pthread_exit(NULL);
|
||||
}
|
||||
else {
|
||||
// Single thread execution (for easy debugging)
|
||||
for (unsigned i = 0; i < nb_threads; ++i){
|
||||
int start = i * batch_size;
|
||||
functor( start, start+batch_size );
|
||||
functor(start, start+batch_size);
|
||||
}
|
||||
}
|
||||
|
||||
// Deform the elements left
|
||||
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
|
||||
// if(use_threads)
|
||||
// std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join));
|
||||
if(use_threads)
|
||||
std::for_each(my_threads.begin(), my_threads.end(), std::mem_fn(&std::thread::join));
|
||||
}
|
||||
|
|
10
src/main.cpp
10
src/main.cpp
|
@ -154,13 +154,13 @@ int main (int argc, char * const argv[])
|
|||
std::cout << "* Collecting dependencies"; fflush(stdout);
|
||||
|
||||
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)
|
||||
collectDependencies(Settings::fileToFix(i));
|
||||
pthread_exit(NULL);
|
||||
});
|
||||
|
||||
// });
|
||||
|
||||
collectSubDependencies();
|
||||
doneWithDeps_go();
|
||||
|
|
Loading…
Reference in New Issue