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

This commit is contained in:
Beat Küng
2022-05-11 10:06:35 -04:00
committed by Daniel Agar
parent 0053aeec97
commit ebbe08bc86
2 changed files with 11 additions and 6 deletions
+8 -4
View File
@@ -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;
} }
+3 -2
View File
@@ -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;