Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Don't spawn a separate thread if there is only one input file #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 77 additions & 75 deletions src/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
// Threading doesn't work in windows yet.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better location for this comment might be up by the #ifdef _WIN32 directive. It could also be that use_threads is false because the user is processing one file on a system which does, in fact, support 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());
Expand Down