logger: move thread start/stop logic into LogWriterFile

This commit is contained in:
Beat Küng
2016-10-07 17:07:37 +02:00
committed by Lorenz Meier
parent 78f19fccce
commit 1ddddccb81
5 changed files with 25 additions and 39 deletions
+9 -9
View File
@@ -54,8 +54,17 @@ bool LogWriter::init()
{ {
if (_log_writer_file) { if (_log_writer_file) {
if (!_log_writer_file->init()) { if (!_log_writer_file->init()) {
PX4_ERR("alloc failed");
return false; return false;
} }
int ret = _log_writer_file->thread_start();
if (ret) {
PX4_ERR("failed to create writer thread (%i)", ret);
return false;
}
} }
return true; return true;
@@ -102,15 +111,6 @@ void LogWriter::stop_log_file()
} }
} }
int LogWriter::thread_start(pthread_t &thread)
{
if (_log_writer_file) {
return _log_writer_file->thread_start(thread);
}
return 0;
}
void LogWriter::thread_stop() void LogWriter::thread_stop()
{ {
if (_log_writer_file) { if (_log_writer_file) {
+1 -7
View File
@@ -60,13 +60,7 @@ public:
Backend backend() const { return _backend; } Backend backend() const { return _backend; }
/** /** stop all running threads and wait for them to exit */
* start the thread
* @param thread will be set to the created thread on success
* @return 0 on success, error number otherwise (@see pthread_create)
*/
int thread_start(pthread_t &thread);
void thread_stop(); void thread_stop();
void start_log_file(const char *filename); void start_log_file(const char *filename);
+12 -2
View File
@@ -108,7 +108,7 @@ void LogWriterFile::stop_log()
notify(); notify();
} }
int LogWriterFile::thread_start(pthread_t &thread) int LogWriterFile::thread_start()
{ {
pthread_attr_t thr_attr; pthread_attr_t thr_attr;
pthread_attr_init(&thr_attr); pthread_attr_init(&thr_attr);
@@ -120,7 +120,7 @@ int LogWriterFile::thread_start(pthread_t &thread)
pthread_attr_setstacksize(&thr_attr, PX4_STACK_ADJUSTED(1024)); pthread_attr_setstacksize(&thr_attr, PX4_STACK_ADJUSTED(1024));
int ret = pthread_create(&thread, &thr_attr, &LogWriterFile::run_helper, this); int ret = pthread_create(&_thread, &thr_attr, &LogWriterFile::run_helper, this);
pthread_attr_destroy(&thr_attr); pthread_attr_destroy(&thr_attr);
return ret; return ret;
@@ -131,6 +131,16 @@ void LogWriterFile::thread_stop()
// this will terminate the main loop of the writer thread // this will terminate the main loop of the writer thread
_exit_thread = true; _exit_thread = true;
_should_run = false; _should_run = false;
notify();
// wait for thread to complete
int ret = pthread_join(_thread, NULL);
if (ret) {
PX4_WARN("join failed: %d", ret);
}
} }
void *LogWriterFile::run_helper(void *context) void *LogWriterFile::run_helper(void *context)
+2 -2
View File
@@ -58,10 +58,9 @@ public:
/** /**
* start the thread * start the thread
* @param thread will be set to the created thread on success
* @return 0 on success, error number otherwise (@see pthread_create) * @return 0 on success, error number otherwise (@see pthread_create)
*/ */
int thread_start(pthread_t &thread); int thread_start();
void thread_stop(); void thread_stop();
@@ -144,6 +143,7 @@ private:
pthread_cond_t _cv; pthread_cond_t _cv;
perf_counter_t _perf_write; perf_counter_t _perf_write;
perf_counter_t _perf_fsync; perf_counter_t _perf_fsync;
pthread_t _thread = 0;
}; };
} }
+1 -19
View File
@@ -635,16 +635,7 @@ void Logger::run()
if (!_writer.init()) { if (!_writer.init()) {
PX4_ERR("init of writer failed (alloc failed)"); PX4_ERR("writer init failed");
return;
}
pthread_t writer_thread;
int ret = _writer.thread_start(writer_thread);
if (ret) {
PX4_ERR("failed to create writer thread (%i)", ret);
return; return;
} }
@@ -814,15 +805,6 @@ void Logger::run()
// stop the writer thread // stop the writer thread
_writer.thread_stop(); _writer.thread_stop();
_writer.notify();
// wait for thread to complete
ret = pthread_join(writer_thread, NULL);
if (ret) {
PX4_WARN("join failed: %d", ret);
}
//unsubscribe //unsubscribe
for (LoggerSubscription &sub : _subscriptions) { for (LoggerSubscription &sub : _subscriptions) {
for (uint8_t instance = 0; instance < ORB_MULTI_MAX_INSTANCES; instance++) { for (uint8_t instance = 0; instance < ORB_MULTI_MAX_INSTANCES; instance++) {