From 2b1e5a7a130fbca1b86a787b54f474e183541d6d Mon Sep 17 00:00:00 2001 From: zhushengle Date: Tue, 14 Sep 2021 11:11:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dpthread=5Fcreate?= =?UTF-8?q?=E8=A1=8C=E4=B8=BA=E4=B8=8Eposix=E4=B8=8D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1.提升posix接口和LOS接口的兼容性 2.支持优先级继承属性 Close #I49W9F Signed-off-by: zhushengle Change-Id: I4d9df778a559f094dd3062e5e7030e3a03c884ec --- kal/posix/src/pthread.c | 92 +++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/kal/posix/src/pthread.c b/kal/posix/src/pthread.c index cd5d8702..d87d773e 100644 --- a/kal/posix/src/pthread.c +++ b/kal/posix/src/pthread.c @@ -34,6 +34,7 @@ #include #include #include +#include #include "los_config.h" #include "los_task.h" @@ -43,7 +44,7 @@ typedef struct { void *(*startRoutine)(void *); void *param; char name[PTHREAD_NAMELEN]; -}PthreadData; +} PthreadData; static void *PthreadEntry(UINT32 param) { @@ -56,34 +57,42 @@ static void *PthreadEntry(UINT32 param) static inline int IsPthread(pthread_t thread) { - return ((UINT32)thread <= LOSCFG_BASE_CORE_TSK_LIMIT) && - (OS_TCB_FROM_TID((UINT32)thread)->taskEntry == PthreadEntry); + return ((UINT32)thread <= LOSCFG_BASE_CORE_TSK_LIMIT); } -int pthread_create(pthread_t *thread, const pthread_attr_t *attr, - void *(*startRoutine)(void *), void *arg) +static int PthreadCreateAttrInit(const pthread_attr_t *attr, void *(*startRoutine)(void *), void *arg, + TSK_INIT_PARAM_S *taskInitParam) { - TSK_INIT_PARAM_S taskInitParam = {0}; + const pthread_attr_t *threadAttr = attr; + struct sched_param schedParam = { 0 }; PthreadData *pthreadData = NULL; - UINT32 taskID; + INT32 policy = 0; + pthread_attr_t attrTmp; + INT32 ret; - if ((thread == NULL) || (startRoutine == NULL)) { - return EINVAL; + if (!attr) { + (VOID)pthread_attr_init(&attrTmp); + threadAttr = &attrTmp; } - taskInitParam.usTaskPrio = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO; - taskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; - if (attr) { - if (attr->detachstate == PTHREAD_CREATE_DETACHED) { - return ENOTSUP; + if (threadAttr->detachstate == PTHREAD_CREATE_DETACHED) { + return ENOTSUP; + } + if (threadAttr->stackaddr_set != 0) { + return ENOTSUP; + } + if (threadAttr->stacksize < PTHREAD_STACK_MIN) { + return EINVAL; + } + taskInitParam->uwStackSize = threadAttr->stacksize; + if (threadAttr->inheritsched == PTHREAD_EXPLICIT_SCHED) { + taskInitParam->usTaskPrio = (UINT16)threadAttr->schedparam.sched_priority; + } else { + ret = pthread_getschedparam(pthread_self(), &policy, &schedParam); + if (ret != 0) { + return ret; } - if (attr->stackaddr_set) { - return ENOTSUP; - } - if (attr->stacksize_set) { - taskInitParam.uwStackSize = attr->stacksize; - } - taskInitParam.usTaskPrio = (UINT16)attr->schedparam.sched_priority; + taskInitParam->usTaskPrio = (UINT16)schedParam.sched_priority; } pthreadData = (PthreadData *)malloc(sizeof(PthreadData)); @@ -93,12 +102,31 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, pthreadData->startRoutine = startRoutine; pthreadData->param = arg; - taskInitParam.pcName = pthreadData->name; - taskInitParam.pfnTaskEntry = PthreadEntry; - taskInitParam.uwArg = (UINT32)(UINTPTR)pthreadData; + taskInitParam->pcName = pthreadData->name; + taskInitParam->pfnTaskEntry = PthreadEntry; + taskInitParam->uwArg = (UINT32)(UINTPTR)pthreadData; + + return 0; +} + +int pthread_create(pthread_t *thread, const pthread_attr_t *attr, + void *(*startRoutine)(void *), void *arg) +{ + TSK_INIT_PARAM_S taskInitParam = { 0 }; + UINT32 taskID; + UINT32 ret; + + if ((thread == NULL) || (startRoutine == NULL)) { + return EINVAL; + } + + ret = PthreadCreateAttrInit(attr, startRoutine, arg, &taskInitParam); + if (ret != 0) { + return ret; + } if (LOS_TaskCreateOnly(&taskID, &taskInitParam) != LOS_OK) { - free(pthreadData); + free((VOID *)(UINTPTR)taskInitParam.uwArg); return EINVAL; } @@ -205,14 +233,26 @@ void pthread_exit(void *retVal) int pthread_setname_np(pthread_t thread, const char *name) { + UINT32 intSave; + LosTaskCB *taskCB = NULL; char *taskName = LOS_TaskNameGet((UINT32)thread); if (taskName == NULL || !IsPthread(thread)) { return EINVAL; } + if (strnlen(name, PTHREAD_NAMELEN) >= PTHREAD_NAMELEN) { return ERANGE; } - (void)strcpy_s(taskName, PTHREAD_NAMELEN, name); + + taskCB = OS_TCB_FROM_TID((UINT32)thread); + intSave = LOS_IntLock(); + if (taskCB->taskEntry == PthreadEntry) { + (void)strcpy_s(taskName, PTHREAD_NAMELEN, name); + } else { + LOS_IntRestore(intSave); + return EINVAL; + } + LOS_IntRestore(intSave); return 0; }