From 87192cbdda9fa4f27d799dcfd8be6361cb15d228 Mon Sep 17 00:00:00 2001 From: John Hurliman Date: Tue, 21 Feb 2012 12:59:06 -0800 Subject: [PATCH 1/2] Don't spawn a separate thread if there is only one input file --- src/main.cxx | 152 ++++++++++++++++++++++++++------------------------- 1 file changed, 77 insertions(+), 75 deletions(-) diff --git a/src/main.cxx b/src/main.cxx index 8b2b389..d225ce2 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -249,91 +249,93 @@ int main(int argc, char** argv) { if(count == 0) throw std::runtime_error("No files given.\n"); - #ifdef _WIN32 - // Threading doesn't work in windows yet. - for(int i=0;icodegen) { - delete response->codegen; - } - free(response); - free(output); - } - return 0; - + bool use_threads = false; #else + bool use_threads = (count > 1); +#endif - // Figure out how many threads to use based on # of cores - int num_threads = getNumCores(); - if (num_threads > 8) num_threads = 8; - if (num_threads < 2) num_threads = 2; - if (num_threads > count) num_threads = count; - - // Setup threading - pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads); - thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads); - pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads); - - // Kick off the first N threads - int still_left = count-1-already; - for(int i=0;ifilename = (char*)files[still_left].c_str(); - parm[i]->start_offset = start_offset; - parm[i]->tag = still_left; - parm[i]->duration = duration; - parm[i]->done = 0; - still_left--; - pthread_attr_init(&attr[i]); - pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED); - // Kick off the thread - if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i])) - throw std::runtime_error("Problem creating thread\n"); - } - - int done = 0; - // Now wait for the threads to come back, and also kick off new ones - while(donecodegen) { + delete response->codegen; + } + free(response); + free(output); + } + return 0; + } else { + // Figure out how many threads to use based on # of cores + int num_threads = getNumCores(); + if (num_threads > 8) num_threads = 8; + if (num_threads < 2) num_threads = 2; + if (num_threads > count) num_threads = count; + + // Setup threading + pthread_t *t = (pthread_t*)malloc(sizeof(pthread_t)*num_threads); + thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads); + pthread_attr_t *attr = (pthread_attr_t*)malloc(sizeof(pthread_attr_t)*num_threads); + + // Kick off the first N threads + int still_left = count-1-already; for(int i=0;idone) { - parm[i]->done = 0; - done++; - codegen_response_t *response = (codegen_response_t*)parm[i]->response; - char *json = make_json_string(response); - print_json_to_screen(json, count, done); - if (response->codegen) { - delete response->codegen; - } - free(parm[i]->response); - free(json); - // More to do? Start a new one on this just finished thread - if(still_left >= 0) { - parm[i]->tag = still_left; - parm[i]->filename = (char*)files[still_left].c_str(); - still_left--; - int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]); - if(err) - throw std::runtime_error("Problem creating thread\n"); + parm[i] = (thread_parm_t *)malloc(sizeof(thread_parm_t)); + parm[i]->filename = (char*)files[still_left].c_str(); + parm[i]->start_offset = start_offset; + parm[i]->tag = still_left; + parm[i]->duration = duration; + parm[i]->done = 0; + still_left--; + pthread_attr_init(&attr[i]); + pthread_attr_setdetachstate(&attr[i], PTHREAD_CREATE_DETACHED); + // Kick off the thread + if (pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i])) + throw std::runtime_error("Problem creating thread\n"); + } + int done = 0; + // Now wait for the threads to come back, and also kick off new ones + while(donedone) { + parm[i]->done = 0; + done++; + codegen_response_t *response = (codegen_response_t*)parm[i]->response; + char *json = make_json_string(response); + print_json_to_screen(json, count, done); + if (response->codegen) { + delete response->codegen; + } + free(parm[i]->response); + free(json); + // More to do? Start a new one on this just finished thread + if(still_left >= 0) { + parm[i]->tag = still_left; + parm[i]->filename = (char*)files[still_left].c_str(); + still_left--; + int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]); + if(err) + throw std::runtime_error("Problem creating thread\n"); + + } } } } - } - // Clean up threads - for(int i=0;i Date: Tue, 28 Feb 2012 10:59:24 -0800 Subject: [PATCH 2/2] Moved the comment about threading not working in Windows yet --- src/main.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cxx b/src/main.cxx index d225ce2..2e3f14d 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -250,13 +250,13 @@ int main(int argc, char** argv) { if(count == 0) throw std::runtime_error("No files given.\n"); #ifdef _WIN32 + // Threading doesn't work in windows yet. bool use_threads = false; #else bool use_threads = (count > 1); #endif if (!use_threads) { - // Threading doesn't work in windows yet. for(int i=0;i