From 1ddddccb81f0ffe14abf83f3318a5a51d10212c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Fri, 7 Oct 2016 17:07:37 +0200 Subject: [PATCH] logger: move thread start/stop logic into LogWriterFile --- src/modules/logger/log_writer.cpp | 18 +++++++++--------- src/modules/logger/log_writer.h | 8 +------- src/modules/logger/log_writer_file.cpp | 14 ++++++++++++-- src/modules/logger/log_writer_file.h | 4 ++-- src/modules/logger/logger.cpp | 20 +------------------- 5 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/modules/logger/log_writer.cpp b/src/modules/logger/log_writer.cpp index ad1c942314..376575ad66 100644 --- a/src/modules/logger/log_writer.cpp +++ b/src/modules/logger/log_writer.cpp @@ -54,8 +54,17 @@ bool LogWriter::init() { if (_log_writer_file) { if (!_log_writer_file->init()) { + PX4_ERR("alloc failed"); return false; } + + int ret = _log_writer_file->thread_start(); + + if (ret) { + PX4_ERR("failed to create writer thread (%i)", ret); + return false; + } + } 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() { if (_log_writer_file) { diff --git a/src/modules/logger/log_writer.h b/src/modules/logger/log_writer.h index 98ee7b356a..f250cc0bec 100644 --- a/src/modules/logger/log_writer.h +++ b/src/modules/logger/log_writer.h @@ -60,13 +60,7 @@ public: Backend backend() const { return _backend; } - /** - * 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); - + /** stop all running threads and wait for them to exit */ void thread_stop(); void start_log_file(const char *filename); diff --git a/src/modules/logger/log_writer_file.cpp b/src/modules/logger/log_writer_file.cpp index 1180f4fbd3..c04b55c273 100644 --- a/src/modules/logger/log_writer_file.cpp +++ b/src/modules/logger/log_writer_file.cpp @@ -108,7 +108,7 @@ void LogWriterFile::stop_log() notify(); } -int LogWriterFile::thread_start(pthread_t &thread) +int LogWriterFile::thread_start() { pthread_attr_t 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)); - 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); return ret; @@ -131,6 +131,16 @@ void LogWriterFile::thread_stop() // this will terminate the main loop of the writer thread _exit_thread = true; _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) diff --git a/src/modules/logger/log_writer_file.h b/src/modules/logger/log_writer_file.h index 970c04d52e..a67fa6368d 100644 --- a/src/modules/logger/log_writer_file.h +++ b/src/modules/logger/log_writer_file.h @@ -58,10 +58,9 @@ public: /** * 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); + int thread_start(); void thread_stop(); @@ -144,6 +143,7 @@ private: pthread_cond_t _cv; perf_counter_t _perf_write; perf_counter_t _perf_fsync; + pthread_t _thread = 0; }; } diff --git a/src/modules/logger/logger.cpp b/src/modules/logger/logger.cpp index 2f42a2071b..96f50d2f7e 100644 --- a/src/modules/logger/logger.cpp +++ b/src/modules/logger/logger.cpp @@ -635,16 +635,7 @@ void Logger::run() if (!_writer.init()) { - PX4_ERR("init of writer failed (alloc failed)"); - return; - } - - pthread_t writer_thread; - - int ret = _writer.thread_start(writer_thread); - - if (ret) { - PX4_ERR("failed to create writer thread (%i)", ret); + PX4_ERR("writer init failed"); return; } @@ -814,15 +805,6 @@ void Logger::run() // stop the writer thread _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 for (LoggerSubscription &sub : _subscriptions) { for (uint8_t instance = 0; instance < ORB_MULTI_MAX_INSTANCES; instance++) {