mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-04-14 10:07:39 +08:00
log_writer_file: protect access to _should_run, use px4::atomicbool for _exit_thread
This commit is contained in:
parent
0053aeec97
commit
ebbe08bc86
@ -287,7 +287,9 @@ int LogWriterFile::hardfault_store_filename(const char *log_file)
|
||||
|
||||
void LogWriterFile::stop_log(LogType type)
|
||||
{
|
||||
lock();
|
||||
_buffers[(int)type]._should_run = false;
|
||||
unlock();
|
||||
notify();
|
||||
}
|
||||
|
||||
@ -312,8 +314,10 @@ int LogWriterFile::thread_start()
|
||||
void LogWriterFile::thread_stop()
|
||||
{
|
||||
// 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;
|
||||
unlock();
|
||||
|
||||
notify();
|
||||
|
||||
@ -335,10 +339,10 @@ void *LogWriterFile::run_helper(void *context)
|
||||
|
||||
void LogWriterFile::run()
|
||||
{
|
||||
while (!_exit_thread) {
|
||||
while (!_exit_thread.load()) {
|
||||
// Outer endless loop
|
||||
// Wait for _should_run flag
|
||||
while (!_exit_thread) {
|
||||
while (!_exit_thread.load()) {
|
||||
bool start = false;
|
||||
pthread_mutex_lock(&_mtx);
|
||||
pthread_cond_wait(&_cv, &_mtx);
|
||||
@ -350,7 +354,7 @@ void LogWriterFile::run()
|
||||
}
|
||||
}
|
||||
|
||||
if (_exit_thread) {
|
||||
if (_exit_thread.load()) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@ -34,6 +34,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <px4_platform_common/defines.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <stdint.h>
|
||||
#include <pthread.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
@ -205,8 +206,8 @@ private:
|
||||
|
||||
LogFileBuffer _buffers[(int)LogType::Count];
|
||||
|
||||
bool _exit_thread = false;
|
||||
bool _need_reliable_transfer = false;
|
||||
px4::atomic_bool _exit_thread{false};
|
||||
bool _need_reliable_transfer{false};
|
||||
pthread_mutex_t _mtx;
|
||||
pthread_cond_t _cv;
|
||||
pthread_t _thread = 0;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user