log_writer_file: do not call close() with mutex held

Generally not an issue, but if close() takes long, or even busy-loops due
to an underlying bug in the OS, it will block the main thread too.
This commit is contained in:
Beat Küng
2023-02-21 11:32:30 -05:00
committed by Daniel Agar
parent 5676cc32bc
commit 015ba62727
2 changed files with 18 additions and 4 deletions
+16 -4
View File
@@ -443,13 +443,19 @@ void LogWriterFile::run()
if (!buffer._should_run && written == static_cast<int>(available) && !is_part) {
/* Stop only when all data written */
pthread_mutex_unlock(&_mtx);
buffer.close_file();
pthread_mutex_lock(&_mtx);
buffer.reset();
}
} else {
PX4_ERR("write failed (%i)", errno);
buffer._should_run = false;
pthread_mutex_unlock(&_mtx);
buffer.close_file();
pthread_mutex_lock(&_mtx);
buffer.reset();
}
} else if (call_fsync && buffer._should_run) {
@@ -458,7 +464,10 @@ void LogWriterFile::run()
pthread_mutex_lock(&_mtx);
} else if (available == 0 && !buffer._should_run) {
pthread_mutex_unlock(&_mtx);
buffer.close_file();
pthread_mutex_lock(&_mtx);
buffer.reset();
}
/* if split into 2 parts, write the second part immediately as well */
@@ -687,12 +696,8 @@ ssize_t LogWriterFile::LogFileBuffer::write_to_file(const void *buffer, size_t s
void LogWriterFile::LogFileBuffer::close_file()
{
_head = 0;
_count = 0;
if (_fd >= 0) {
int res = close(_fd);
_fd = -1;
if (res) {
PX4_WARN("closing log file failed (%i)", errno);
@@ -703,5 +708,12 @@ void LogWriterFile::LogFileBuffer::close_file()
}
}
void LogWriterFile::LogFileBuffer::reset()
{
_head = 0;
_count = 0;
_fd = -1;
}
}
}
+2
View File
@@ -171,6 +171,8 @@ private:
void close_file();
void reset();
size_t get_read_ptr(void **ptr, bool *is_part);
/**