mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-26 03:03:34 +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
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* 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]);
|
(void *) &taskmap[task_index]);
|
||||||
|
|
||||||
if (retcode != PX4_OK) {
|
if (retcode != PX4_OK) {
|
||||||
|
pthread_attr_destroy(&taskmap[task_index].attr);
|
||||||
pthread_mutex_unlock(&task_mutex);
|
pthread_mutex_unlock(&task_mutex);
|
||||||
PX4_ERR("Couldn't create pthread %s", name);
|
PX4_ERR("Couldn't create pthread %s", name);
|
||||||
return -1;
|
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);
|
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,
|
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) {
|
if (pthread_self() == pid) {
|
||||||
pthread_join(pid, nullptr);
|
pthread_join(pid, nullptr);
|
||||||
|
pthread_attr_destroy(&taskmap[id].attr);
|
||||||
taskmap[id].isused = false;
|
taskmap[id].isused = false;
|
||||||
pthread_mutex_unlock(&task_mutex);
|
pthread_mutex_unlock(&task_mutex);
|
||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
rv = pthread_cancel(pid);
|
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;
|
taskmap[id].isused = false;
|
||||||
|
pthread_mutex_unlock(&task_mutex);
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
@@ -315,6 +326,7 @@ void px4_task_exit(int ret)
|
|||||||
for (i = 0; i < PX4_MAX_TASKS; ++i) {
|
for (i = 0; i < PX4_MAX_TASKS; ++i) {
|
||||||
if (taskmap[i].tid == pid) {
|
if (taskmap[i].tid == pid) {
|
||||||
pthread_mutex_lock(&task_mutex);
|
pthread_mutex_lock(&task_mutex);
|
||||||
|
pthread_attr_destroy(&taskmap[i].attr);
|
||||||
taskmap[i].isused = false;
|
taskmap[i].isused = false;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -325,10 +337,9 @@ void px4_task_exit(int ret)
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
PX4_DEBUG("px4_task_exit: %s", taskmap[i].name);
|
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);
|
pthread_exit((void *)(unsigned long)ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user