mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-24 07:09:48 +08:00
logger: move thread start/stop logic into LogWriterFile
This commit is contained in:
@@ -54,8 +54,17 @@ bool LogWriter::init()
|
||||
{
|
||||
if (_log_writer_file) {
|
||||
if (!_log_writer_file->init()) {
|
||||
PX4_ERR("alloc failed");
|
||||
return false;
|
||||
}
|
||||
|
||||
int ret = _log_writer_file->thread_start();
|
||||
|
||||
if (ret) {
|
||||
PX4_ERR("failed to create writer thread (%i)", ret);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -102,15 +111,6 @@ void LogWriter::stop_log_file()
|
||||
}
|
||||
}
|
||||
|
||||
int LogWriter::thread_start(pthread_t &thread)
|
||||
{
|
||||
if (_log_writer_file) {
|
||||
return _log_writer_file->thread_start(thread);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void LogWriter::thread_stop()
|
||||
{
|
||||
if (_log_writer_file) {
|
||||
|
||||
@@ -60,13 +60,7 @@ public:
|
||||
|
||||
Backend backend() const { return _backend; }
|
||||
|
||||
/**
|
||||
* start the thread
|
||||
* @param thread will be set to the created thread on success
|
||||
* @return 0 on success, error number otherwise (@see pthread_create)
|
||||
*/
|
||||
int thread_start(pthread_t &thread);
|
||||
|
||||
/** stop all running threads and wait for them to exit */
|
||||
void thread_stop();
|
||||
|
||||
void start_log_file(const char *filename);
|
||||
|
||||
@@ -108,7 +108,7 @@ void LogWriterFile::stop_log()
|
||||
notify();
|
||||
}
|
||||
|
||||
int LogWriterFile::thread_start(pthread_t &thread)
|
||||
int LogWriterFile::thread_start()
|
||||
{
|
||||
pthread_attr_t thr_attr;
|
||||
pthread_attr_init(&thr_attr);
|
||||
@@ -120,7 +120,7 @@ int LogWriterFile::thread_start(pthread_t &thread)
|
||||
|
||||
pthread_attr_setstacksize(&thr_attr, PX4_STACK_ADJUSTED(1024));
|
||||
|
||||
int ret = pthread_create(&thread, &thr_attr, &LogWriterFile::run_helper, this);
|
||||
int ret = pthread_create(&_thread, &thr_attr, &LogWriterFile::run_helper, this);
|
||||
pthread_attr_destroy(&thr_attr);
|
||||
|
||||
return ret;
|
||||
@@ -131,6 +131,16 @@ void LogWriterFile::thread_stop()
|
||||
// this will terminate the main loop of the writer thread
|
||||
_exit_thread = true;
|
||||
_should_run = false;
|
||||
|
||||
notify();
|
||||
|
||||
// wait for thread to complete
|
||||
int ret = pthread_join(_thread, NULL);
|
||||
|
||||
if (ret) {
|
||||
PX4_WARN("join failed: %d", ret);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void *LogWriterFile::run_helper(void *context)
|
||||
|
||||
@@ -58,10 +58,9 @@ public:
|
||||
|
||||
/**
|
||||
* start the thread
|
||||
* @param thread will be set to the created thread on success
|
||||
* @return 0 on success, error number otherwise (@see pthread_create)
|
||||
*/
|
||||
int thread_start(pthread_t &thread);
|
||||
int thread_start();
|
||||
|
||||
void thread_stop();
|
||||
|
||||
@@ -144,6 +143,7 @@ private:
|
||||
pthread_cond_t _cv;
|
||||
perf_counter_t _perf_write;
|
||||
perf_counter_t _perf_fsync;
|
||||
pthread_t _thread = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -635,16 +635,7 @@ void Logger::run()
|
||||
|
||||
|
||||
if (!_writer.init()) {
|
||||
PX4_ERR("init of writer failed (alloc failed)");
|
||||
return;
|
||||
}
|
||||
|
||||
pthread_t writer_thread;
|
||||
|
||||
int ret = _writer.thread_start(writer_thread);
|
||||
|
||||
if (ret) {
|
||||
PX4_ERR("failed to create writer thread (%i)", ret);
|
||||
PX4_ERR("writer init failed");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -814,15 +805,6 @@ void Logger::run()
|
||||
// stop the writer thread
|
||||
_writer.thread_stop();
|
||||
|
||||
_writer.notify();
|
||||
|
||||
// wait for thread to complete
|
||||
ret = pthread_join(writer_thread, NULL);
|
||||
|
||||
if (ret) {
|
||||
PX4_WARN("join failed: %d", ret);
|
||||
}
|
||||
|
||||
//unsubscribe
|
||||
for (LoggerSubscription &sub : _subscriptions) {
|
||||
for (uint8_t instance = 0; instance < ORB_MULTI_MAX_INSTANCES; instance++) {
|
||||
|
||||
Reference in New Issue
Block a user