Browse Source

logger: move thread start/stop logic into LogWriterFile

sbg
Beat Küng 8 years ago committed by Lorenz Meier
parent
commit
1ddddccb81
  1. 18
      src/modules/logger/log_writer.cpp
  2. 8
      src/modules/logger/log_writer.h
  3. 14
      src/modules/logger/log_writer_file.cpp
  4. 4
      src/modules/logger/log_writer_file.h
  5. 20
      src/modules/logger/logger.cpp

18
src/modules/logger/log_writer.cpp

@ -54,8 +54,17 @@ bool LogWriter::init() @@ -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() @@ -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) {

8
src/modules/logger/log_writer.h

@ -60,13 +60,7 @@ public: @@ -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);

14
src/modules/logger/log_writer_file.cpp

@ -108,7 +108,7 @@ void LogWriterFile::stop_log() @@ -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) @@ -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() @@ -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)

4
src/modules/logger/log_writer_file.h

@ -58,10 +58,9 @@ public: @@ -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: @@ -144,6 +143,7 @@ private:
pthread_cond_t _cv;
perf_counter_t _perf_write;
perf_counter_t _perf_fsync;
pthread_t _thread = 0;
};
}

20
src/modules/logger/logger.cpp

@ -635,16 +635,7 @@ void Logger::run() @@ -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() @@ -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++) {

Loading…
Cancel
Save