Browse Source

log_writer_file: protect access to _should_run, use px4::atomicbool for _exit_thread

v1.13.0-BW
Beat Küng 3 years ago committed by Daniel Agar
parent
commit
9e91ca8294
No known key found for this signature in database
GPG Key ID: FD3CBA98017A69DE
  1. 12
      src/modules/logger/log_writer_file.cpp
  2. 5
      src/modules/logger/log_writer_file.h

12
src/modules/logger/log_writer_file.cpp

@ -287,7 +287,9 @@ int LogWriterFile::hardfault_store_filename(const char *log_file)
void LogWriterFile::stop_log(LogType type) void LogWriterFile::stop_log(LogType type)
{ {
lock();
_buffers[(int)type]._should_run = false; _buffers[(int)type]._should_run = false;
unlock();
notify(); notify();
} }
@ -312,8 +314,10 @@ int LogWriterFile::thread_start()
void LogWriterFile::thread_stop() 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; lock();
_exit_thread.store(true);
_buffers[0]._should_run = _buffers[1]._should_run = false; _buffers[0]._should_run = _buffers[1]._should_run = false;
unlock();
notify(); notify();
@ -335,10 +339,10 @@ void *LogWriterFile::run_helper(void *context)
void LogWriterFile::run() void LogWriterFile::run()
{ {
while (!_exit_thread) { while (!_exit_thread.load()) {
// Outer endless loop // Outer endless loop
// Wait for _should_run flag // Wait for _should_run flag
while (!_exit_thread) { while (!_exit_thread.load()) {
bool start = false; bool start = false;
pthread_mutex_lock(&_mtx); pthread_mutex_lock(&_mtx);
pthread_cond_wait(&_cv, &_mtx); pthread_cond_wait(&_cv, &_mtx);
@ -350,7 +354,7 @@ void LogWriterFile::run()
} }
} }
if (_exit_thread) { if (_exit_thread.load()) {
break; break;
} }

5
src/modules/logger/log_writer_file.h

@ -34,6 +34,7 @@
#pragma once #pragma once
#include <px4_platform_common/defines.h> #include <px4_platform_common/defines.h>
#include <px4_platform_common/atomic.h>
#include <stdint.h> #include <stdint.h>
#include <pthread.h> #include <pthread.h>
#include <drivers/drv_hrt.h> #include <drivers/drv_hrt.h>
@ -205,8 +206,8 @@ private:
LogFileBuffer _buffers[(int)LogType::Count]; LogFileBuffer _buffers[(int)LogType::Count];
bool _exit_thread = false; px4::atomic_bool _exit_thread{false};
bool _need_reliable_transfer = false; bool _need_reliable_transfer{false};
pthread_mutex_t _mtx; pthread_mutex_t _mtx;
pthread_cond_t _cv; pthread_cond_t _cv;
pthread_t _thread = 0; pthread_t _thread = 0;

Loading…
Cancel
Save