diff --git a/src/Makefile b/src/Makefile index f87e424..023689d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -9,10 +9,11 @@ CXX=g++ CC=gcc #OPTFLAGS=-g -O0 OPTFLAGS=-O3 -DBOOST_UBLAS_NDEBUG -DNDEBUG +MYFLAGS=-DNO_THREADS BOOST_CFLAGS=-I/usr/local/include/boost-1_35 TAGLIB_CFLAGS=`taglib-config --cflags` TAGLIB_LIBS=`taglib-config --libs` -CXXFLAGS=-Wall $(BOOST_CFLAGS) $(TAGLIB_CFLAGS) -fPIC $(OPTFLAGS) +CXXFLAGS=-Wall $(BOOST_CFLAGS) $(TAGLIB_CFLAGS) -fPIC $(OPTFLAGS) $(MYFLAGS) CFLAGS=-Wall -fPIC $(OPTFLAGS) LDFLAGS=$(TAGLIB_LIBS) -lz -lpthread $(OPTFLAGS) LIBNAME=libcodegen.so diff --git a/src/main.cxx b/src/main.cxx index 8b2b389..b941ab4 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -167,6 +167,11 @@ void *threaded_codegen_file(void *parm) { return NULL; } +#ifdef NO_THREADS +// no difference. +#define nothread_codegen_file threaded_codegen_file +#endif + void print_json_to_screen(char* output, int count, int done) { // Print a json block depending on how many there are and where we are. if(done==1 && count>1) { @@ -186,23 +191,13 @@ char *make_json_string(codegen_response_t* response) { return response->error; } - // Get the ID3 tag information. - auto_ptr pMetadata(new Metadata(response->filename)); - // preamble + codelen char* output = (char*) malloc(sizeof(char)*(16384 + strlen(response->codegen->getCodeString().c_str()) )); - sprintf(output,"{\"metadata\":{\"artist\":\"%s\", \"release\":\"%s\", \"title\":\"%s\", \"genre\":\"%s\", \"bitrate\":%d," - "\"sample_rate\":%d, \"duration\":%d, \"filename\":\"%s\", \"samples_decoded\":%d, \"given_duration\":%d," + sprintf(output,"{\"metadata\":{\"artist\":\"\", \"release\":\"\", \"title\":\"\", \"genre\":\"\", \"bitrate\":0," + "\"sample_rate\":0, \"duration\":0, \"filename\":\"%s\", \"samples_decoded\":%d, \"given_duration\":%d," " \"start_offset\":%d, \"version\":%2.2f, \"codegen_time\":%2.6f, \"decode_time\":%2.6f}, \"code_count\":%d," " \"code\":\"%s\", \"tag\":%d}", - escape(pMetadata->Artist()).c_str(), - escape(pMetadata->Album()).c_str(), - escape(pMetadata->Title()).c_str(), - escape(pMetadata->Genre()).c_str(), - pMetadata->Bitrate(), - pMetadata->SampleRate(), - pMetadata->Seconds(), escape(response->filename).c_str(), response->numSamples, response->duration, @@ -272,10 +267,12 @@ int main(int argc, char** argv) { if (num_threads < 2) num_threads = 2; if (num_threads > count) num_threads = count; + thread_parm_t **parm = (thread_parm_t**)malloc(sizeof(thread_parm_t*)*num_threads); +#ifndef NO_THREADS // 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); +#endif // Kick off the first N threads int still_left = count-1-already; @@ -287,19 +284,27 @@ int main(int argc, char** argv) { parm[i]->duration = duration; parm[i]->done = 0; still_left--; +#ifndef NO_THREADS 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"); +#else + // run sequentially, one by one + nothread_codegen_file((void*)parm[i]); +#endif } int done = 0; // Now wait for the threads to come back, and also kick off new ones while(donedone) { + if (parm[i]->done) { // There is a race condition here in the threaded version. + // We get a coredump file every hour. parm[i]->done = 0; done++; codegen_response_t *response = (codegen_response_t*)parm[i]->response; @@ -315,10 +320,13 @@ int main(int argc, char** argv) { parm[i]->tag = still_left; parm[i]->filename = (char*)files[still_left].c_str(); still_left--; +#ifndef NO_THREADS int err= pthread_create(&t[i], &attr[i], threaded_codegen_file, (void*)parm[i]); if(err) throw std::runtime_error("Problem creating thread\n"); - +#else + nothread_codegen_file((void*)parm[i]); +#endif } } } @@ -328,9 +336,11 @@ int main(int argc, char** argv) { for(int i=0;i