Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: echonest/echoprint-codegen
...
head fork: jhurliman/echoprint-codegen
compare: master
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Showing with 76 additions and 74 deletions.
  1. +76 −74 src/main.cxx
View
150 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;i<count;i++) {
- codegen_response_t* response = codegen_file((char*)files[i].c_str(), start_offset, duration, i);
- char *output = make_json_string(response);
- print_json_to_screen(output, count, i+1);
- if (response->codegen) {
- 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;i<num_threads;i++) {
- 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(done<count) {
- // Check which threads are done
+ if (!use_threads) {
+ for(int i=0;i<count;i++) {
+ codegen_response_t* response = codegen_file((char*)files[i].c_str(), start_offset, duration, i);
+ char *output = make_json_string(response);
+ print_json_to_screen(output, count, i+1);
+ if (response->codegen) {
+ 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;i<num_threads;i++) {
- if (parm[i]->done) {
- 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(done<count) {
+ // Check which threads are done
+ for(int i=0;i<num_threads;i++) {
+ if (parm[i]->done) {
+ 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<num_threads;i++) {
- free(parm[i]);
+ // Clean up threads
+ for(int i=0;i<num_threads;i++) {
+ free(parm[i]);
+ }
+ free(t);
+ free(parm);
+ free(attr);
+ return 0;
}
- free(t);
- free(parm);
- free(attr);
- return 0;
-
-#endif // _WIN32
}
catch(std::runtime_error& ex) {
fprintf(stderr, "%s\n", ex.what());

No commit comments for this range

Something went wrong with that request. Please try again.