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)
{
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;
}