!308 fix: 修复pthread_create行为与posix不一致问题

Merge pull request !308 from zhushengle/pthread
This commit is contained in:
openharmony_ci 2021-09-15 08:07:55 +00:00 committed by Gitee
commit 4700ba6973
1 changed files with 66 additions and 26 deletions

View File

@ -34,6 +34,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <securec.h>
#include <limits.h>
#include "los_config.h"
#include "los_task.h"
@ -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)) {
if (!attr) {
(VOID)pthread_attr_init(&attrTmp);
threadAttr = &attrTmp;
}
if (threadAttr->detachstate == PTHREAD_CREATE_DETACHED) {
return ENOTSUP;
}
if (threadAttr->stackaddr_set != 0) {
return ENOTSUP;
}
if (threadAttr->stacksize < PTHREAD_STACK_MIN) {
return EINVAL;
}
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;
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;
}
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;
}