From 28844b54ddafa0456439877022c6a5a9bb2994d4 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Thu, 28 Jun 2018 21:13:41 +0100 Subject: [PATCH 1/4] Possible fix for #164. --- lib/src/common/logger_file.cc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/src/common/logger_file.cc b/lib/src/common/logger_file.cc index abbea8f93..999a73077 100644 --- a/lib/src/common/logger_file.cc +++ b/lib/src/common/logger_file.cc @@ -45,11 +45,15 @@ logger_file::~logger_file() { not_done = false; if(inited) { log(new std::string("Closing log\n")); + pthread_mutex_lock(&mutex); + pthread_cond_signal(¬_empty); // wakeup thread and let it terminate + pthread_mutex_unlock(&mutex); wait_thread_finish(); flush(); if (logfile) { fclose(logfile); } + pthread_mutex_destroy(&mutex); } } @@ -84,6 +88,10 @@ void logger_file::run_thread() { pthread_mutex_lock(&mutex); while(buffer.empty()) { pthread_cond_wait(¬_empty, &mutex); + if(not_done == false) // Thread done. Messages in buffer will be handled in flush. + { + return; + } } str_ptr s = buffer.front(); pthread_cond_signal(¬_full); From 58823b16110da71d706ce918bbb6d342d745bdc4 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Thu, 28 Jun 2018 21:22:37 +0100 Subject: [PATCH 2/4] Destroying condition variable. --- lib/src/common/logger_file.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/src/common/logger_file.cc b/lib/src/common/logger_file.cc index 999a73077..e8c60dcdb 100644 --- a/lib/src/common/logger_file.cc +++ b/lib/src/common/logger_file.cc @@ -54,6 +54,8 @@ logger_file::~logger_file() { fclose(logfile); } pthread_mutex_destroy(&mutex); + pthread_cond_destroy(¬_empty); + pthread_cond_destroy(¬_full); } } From 4deb2510711134a0fb40cb4295cc39b942f43dff Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Tue, 3 Jul 2018 13:09:43 +0200 Subject: [PATCH 3/4] remove duplicate run-state variable in logger_file --- lib/include/srslte/common/logger_file.h | 3 +-- lib/src/common/logger_file.cc | 20 ++++++++------------ 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/include/srslte/common/logger_file.h b/lib/include/srslte/common/logger_file.h index f0bd5a15b..f4bfd4582 100644 --- a/lib/include/srslte/common/logger_file.h +++ b/lib/include/srslte/common/logger_file.h @@ -64,8 +64,7 @@ private: int64_t max_length; int64_t cur_length; FILE* logfile; - bool inited; - bool not_done; + bool is_running; std::string filename; pthread_cond_t not_empty; pthread_cond_t not_full; diff --git a/lib/src/common/logger_file.cc b/lib/src/common/logger_file.cc index e8c60dcdb..12fcbb3ba 100644 --- a/lib/src/common/logger_file.cc +++ b/lib/src/common/logger_file.cc @@ -34,18 +34,17 @@ using namespace std; namespace srslte{ logger_file::logger_file() - :inited(false) - ,logfile(NULL) - ,not_done(true) + :logfile(NULL) + ,is_running(false) ,cur_length(0) ,max_length(0) {} logger_file::~logger_file() { - not_done = false; - if(inited) { + if(is_running) { log(new std::string("Closing log\n")); pthread_mutex_lock(&mutex); + is_running = false; pthread_cond_signal(¬_empty); // wakeup thread and let it terminate pthread_mutex_unlock(&mutex); wait_thread_finish(); @@ -67,11 +66,11 @@ void logger_file::init(std::string file, int max_length_) { name_idx = 0; filename = file; logfile = fopen(filename.c_str(), "w"); - if(logfile==NULL) { + if(logfile == NULL) { printf("Error: could not create log file, no messages will be logged!\n"); } + is_running = true; start(-2); - inited = true; } void logger_file::log(const char *msg) { @@ -86,14 +85,11 @@ void logger_file::log(str_ptr msg) { } void logger_file::run_thread() { - while(not_done) { + while(is_running) { pthread_mutex_lock(&mutex); while(buffer.empty()) { pthread_cond_wait(¬_empty, &mutex); - if(not_done == false) // Thread done. Messages in buffer will be handled in flush. - { - return; - } + if(!is_running) return; // Thread done. Messages in buffer will be handled in flush. } str_ptr s = buffer.front(); pthread_cond_signal(¬_full); From 87fd218c72fd249728f8513228ee3c303e42de19 Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Tue, 3 Jul 2018 13:17:48 +0200 Subject: [PATCH 4/4] remove unused cond variable in logger --- lib/include/srslte/common/logger_file.h | 1 - lib/src/common/logger_file.cc | 3 --- 2 files changed, 4 deletions(-) diff --git a/lib/include/srslte/common/logger_file.h b/lib/include/srslte/common/logger_file.h index f4bfd4582..956753f7a 100644 --- a/lib/include/srslte/common/logger_file.h +++ b/lib/include/srslte/common/logger_file.h @@ -67,7 +67,6 @@ private: bool is_running; std::string filename; pthread_cond_t not_empty; - pthread_cond_t not_full; pthread_mutex_t mutex; pthread_t thread; std::deque buffer; diff --git a/lib/src/common/logger_file.cc b/lib/src/common/logger_file.cc index 12fcbb3ba..d89dfac0c 100644 --- a/lib/src/common/logger_file.cc +++ b/lib/src/common/logger_file.cc @@ -54,14 +54,12 @@ logger_file::~logger_file() { } pthread_mutex_destroy(&mutex); pthread_cond_destroy(¬_empty); - pthread_cond_destroy(¬_full); } } void logger_file::init(std::string file, int max_length_) { pthread_mutex_init(&mutex, NULL); pthread_cond_init(¬_empty, NULL); - pthread_cond_init(¬_full, NULL); max_length = (int64_t)max_length_*1024; name_idx = 0; filename = file; @@ -92,7 +90,6 @@ void logger_file::run_thread() { if(!is_running) return; // Thread done. Messages in buffer will be handled in flush. } str_ptr s = buffer.front(); - pthread_cond_signal(¬_full); int n = 0; if(logfile) n = fprintf(logfile, "%s", s->c_str());