From 1cbcdb5e48b59c1cc264d24f2e284746212940f4 Mon Sep 17 00:00:00 2001 From: Lizongdi <1210855344@qq.com> Date: Tue, 17 Nov 2026 15:57:56 +0800 Subject: [PATCH] =?UTF-8?q?motor=20=E3=80=90=E7=BC=96=E8=AF=91=E9=80=9A?= =?UTF-8?q?=E8=BF=87=E3=80=91=E5=A2=9E=E5=8A=A0=E5=A4=9A=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=88=E5=85=B1=E7=94=A8=E5=A4=96=E8=AE=BE?= =?UTF-8?q?=E8=BF=98=E6=98=AF=E4=B8=8D=E8=83=BD=E8=B7=A8=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- motor/motor_manager.c | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/motor/motor_manager.c b/motor/motor_manager.c index 0312055..f386f04 100644 --- a/motor/motor_manager.c +++ b/motor/motor_manager.c @@ -18,6 +18,7 @@ ******************************************************************************/ #include "motor_manager.h" +#include "rd_thread.h" /*----------------------------------------------* * 外部变量说明 * @@ -39,6 +40,7 @@ * 模块级变量 * *----------------------------------------------*/ static rd_slist_t g_stMotorList; // 电机链表头 +static rd_mutex_t g_stMotorLock = RD_MUTEX_INITIALIZER; // 电机链表互斥锁(保护g_stMotorList的增删查) /*----------------------------------------------* * 常量定义 * @@ -70,6 +72,7 @@ static rd_slist_t g_stMotorList; // 电机链表头 *****************************************************************************/ void MotorMgr_Init(void) { + (void)Rd_MutexInit(&g_stMotorLock); rd_slist_init(&g_stMotorList); } @@ -91,7 +94,7 @@ void MotorMgr_Init(void) 修改内容 : 新生成函数 *****************************************************************************/ -Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, +Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, TComCtrl *ptCAN, const Motor_Protocol_t *pstProtocol, void *pstPrivData) @@ -101,6 +104,10 @@ Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, return NULL; } + Motor_Instance_t *pstMotor = NULL; + + Rd_MutexLock(&g_stMotorLock, __func__, NULL); + // 检查是否已存在相同ID的电机 rd_slist_t *pos; rd_slist_t *n; @@ -109,15 +116,15 @@ Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, Motor_Instance_t *pstCheck = rd_slist_entry(pos, Motor_Instance_t, node); if (pstCheck->m_stConfig.m_ucMotorID == pstConfig->m_ucMotorID) { - return NULL; // ID重复 + goto END; // ID重复 } } // 分配内存 - Motor_Instance_t *pstMotor = (Motor_Instance_t *)RD_CALLOC(1, sizeof(Motor_Instance_t)); + pstMotor = (Motor_Instance_t *)RD_CALLOC(1, sizeof(Motor_Instance_t)); if (NULL == pstMotor) { - return NULL; + goto END; } // 初始化链表节点 @@ -142,6 +149,8 @@ Motor_Instance_t* MotorMgr_Create(const Motor_Config_t *pstConfig, // 添加到链表尾部 rd_slist_append(&g_stMotorList, &pstMotor->node); +END: + Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); return pstMotor; } @@ -167,8 +176,11 @@ int MotorMgr_Destroy(Motor_Instance_t *pstMotor) return RD_NULL; } - // 从链表中移除 + // 从链表中移除(节点摘除后即可释放,FREE放在锁外减少持锁时间) + Rd_MutexLock(&g_stMotorLock, __func__, NULL); rd_slist_remove(&g_stMotorList, &pstMotor->node); + Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); + RD_FREE(pstMotor); return RD_SUCCESS; @@ -196,6 +208,9 @@ Motor_Instance_t* MotorMgr_FindByName(const char *pszName) return NULL; } + Motor_Instance_t *pstResult = NULL; + + Rd_MutexLock(&g_stMotorLock, __func__, NULL); rd_slist_t *pos; rd_slist_t *n; rd_slist_for_each(pos, n, &g_stMotorList) @@ -203,10 +218,13 @@ Motor_Instance_t* MotorMgr_FindByName(const char *pszName) Motor_Instance_t *pstMotor = rd_slist_entry(pos, Motor_Instance_t, node); if (RD_STRCMP(pstMotor->m_szName, pszName) == 0) { - return pstMotor; + pstResult = pstMotor; + break; } } - return NULL; + Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); + + return pstResult; } /***************************************************************************** @@ -226,6 +244,9 @@ Motor_Instance_t* MotorMgr_FindByName(const char *pszName) *****************************************************************************/ Motor_Instance_t* MotorMgr_FindByID(uint8_t ucMotorID) { + Motor_Instance_t *pstResult = NULL; + + Rd_MutexLock(&g_stMotorLock, __func__, NULL); rd_slist_t *pos; rd_slist_t *n; rd_slist_for_each(pos, n, &g_stMotorList) @@ -233,10 +254,13 @@ Motor_Instance_t* MotorMgr_FindByID(uint8_t ucMotorID) Motor_Instance_t *pstMotor = rd_slist_entry(pos, Motor_Instance_t, node); if (pstMotor->m_stConfig.m_ucMotorID == ucMotorID) { - return pstMotor; + pstResult = pstMotor; + break; } } - return NULL; + Rd_MutexUnlock(&g_stMotorLock, __func__, NULL); + + return pstResult; } /*****************************************************************************