mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-03-23 18:13:49 +08:00
QURT: Fix task management bugs
Fix return value bug in px4_task_spawn_internal returning the arg-parsing loop variable instead of the task index. Add pthread_attr_destroy calls to prevent resource leaks on task creation failure, deletion, and exit. Fix race condition in px4_task_delete by unlocking the mutex before pthread_join and properly joining after pthread_cancel. Fix mutex unlock placement in px4_task_exit to only unlock when the mutex was acquired.
This commit is contained in:
committed by
Eric Katzfey
parent
24833f41e5
commit
76fbac4dee
@@ -1,6 +1,6 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (C) 2022 ModalAI, Inc. All rights reserved.
|
||||
* Copyright (C) 2022-2026 ModalAI, Inc. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@@ -244,6 +244,7 @@ static px4_task_t px4_task_spawn_internal(const char *name, int priority, px4_ma
|
||||
(void *) &taskmap[task_index]);
|
||||
|
||||
if (retcode != PX4_OK) {
|
||||
pthread_attr_destroy(&taskmap[task_index].attr);
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
PX4_ERR("Couldn't create pthread %s", name);
|
||||
return -1;
|
||||
@@ -258,7 +259,7 @@ static px4_task_t px4_task_spawn_internal(const char *name, int priority, px4_ma
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
return i;
|
||||
return task_index;
|
||||
}
|
||||
|
||||
px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size, px4_main_t entry,
|
||||
@@ -292,15 +293,25 @@ int px4_task_delete(px4_task_t id)
|
||||
|
||||
if (pthread_self() == pid) {
|
||||
pthread_join(pid, nullptr);
|
||||
pthread_attr_destroy(&taskmap[id].attr);
|
||||
taskmap[id].isused = false;
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
pthread_exit(nullptr);
|
||||
|
||||
} else {
|
||||
rv = pthread_cancel(pid);
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
if (rv == 0) {
|
||||
pthread_join(pid, nullptr);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
}
|
||||
|
||||
pthread_attr_destroy(&taskmap[id].attr);
|
||||
taskmap[id].isused = false;
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
return rv;
|
||||
}
|
||||
@@ -315,6 +326,7 @@ void px4_task_exit(int ret)
|
||||
for (i = 0; i < PX4_MAX_TASKS; ++i) {
|
||||
if (taskmap[i].tid == pid) {
|
||||
pthread_mutex_lock(&task_mutex);
|
||||
pthread_attr_destroy(&taskmap[i].attr);
|
||||
taskmap[i].isused = false;
|
||||
break;
|
||||
}
|
||||
@@ -325,10 +337,9 @@ void px4_task_exit(int ret)
|
||||
|
||||
} else {
|
||||
PX4_DEBUG("px4_task_exit: %s", taskmap[i].name);
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&task_mutex);
|
||||
|
||||
pthread_exit((void *)(unsigned long)ret);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user