!308 fix: 修复pthread_create行为与posix不一致问题
Merge pull request !308 from zhushengle/pthread
This commit is contained in:
commit
4700ba6973
|
@ -34,6 +34,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <securec.h>
|
#include <securec.h>
|
||||||
|
#include <limits.h>
|
||||||
#include "los_config.h"
|
#include "los_config.h"
|
||||||
#include "los_task.h"
|
#include "los_task.h"
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ typedef struct {
|
||||||
void *(*startRoutine)(void *);
|
void *(*startRoutine)(void *);
|
||||||
void *param;
|
void *param;
|
||||||
char name[PTHREAD_NAMELEN];
|
char name[PTHREAD_NAMELEN];
|
||||||
}PthreadData;
|
} PthreadData;
|
||||||
|
|
||||||
static void *PthreadEntry(UINT32 param)
|
static void *PthreadEntry(UINT32 param)
|
||||||
{
|
{
|
||||||
|
@ -56,34 +57,42 @@ static void *PthreadEntry(UINT32 param)
|
||||||
|
|
||||||
static inline int IsPthread(pthread_t thread)
|
static inline int IsPthread(pthread_t thread)
|
||||||
{
|
{
|
||||||
return ((UINT32)thread <= LOSCFG_BASE_CORE_TSK_LIMIT) &&
|
return ((UINT32)thread <= LOSCFG_BASE_CORE_TSK_LIMIT);
|
||||||
(OS_TCB_FROM_TID((UINT32)thread)->taskEntry == PthreadEntry);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
static int PthreadCreateAttrInit(const pthread_attr_t *attr, void *(*startRoutine)(void *), void *arg,
|
||||||
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;
|
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;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
taskInitParam->uwStackSize = threadAttr->stacksize;
|
||||||
taskInitParam.usTaskPrio = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;
|
if (threadAttr->inheritsched == PTHREAD_EXPLICIT_SCHED) {
|
||||||
taskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
|
taskInitParam->usTaskPrio = (UINT16)threadAttr->schedparam.sched_priority;
|
||||||
if (attr) {
|
} else {
|
||||||
if (attr->detachstate == PTHREAD_CREATE_DETACHED) {
|
ret = pthread_getschedparam(pthread_self(), &policy, &schedParam);
|
||||||
return ENOTSUP;
|
if (ret != 0) {
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
if (attr->stackaddr_set) {
|
taskInitParam->usTaskPrio = (UINT16)schedParam.sched_priority;
|
||||||
return ENOTSUP;
|
|
||||||
}
|
|
||||||
if (attr->stacksize_set) {
|
|
||||||
taskInitParam.uwStackSize = attr->stacksize;
|
|
||||||
}
|
|
||||||
taskInitParam.usTaskPrio = (UINT16)attr->schedparam.sched_priority;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pthreadData = (PthreadData *)malloc(sizeof(PthreadData));
|
pthreadData = (PthreadData *)malloc(sizeof(PthreadData));
|
||||||
|
@ -93,12 +102,31 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
||||||
|
|
||||||
pthreadData->startRoutine = startRoutine;
|
pthreadData->startRoutine = startRoutine;
|
||||||
pthreadData->param = arg;
|
pthreadData->param = arg;
|
||||||
taskInitParam.pcName = pthreadData->name;
|
taskInitParam->pcName = pthreadData->name;
|
||||||
taskInitParam.pfnTaskEntry = PthreadEntry;
|
taskInitParam->pfnTaskEntry = PthreadEntry;
|
||||||
taskInitParam.uwArg = (UINT32)(UINTPTR)pthreadData;
|
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) {
|
if (LOS_TaskCreateOnly(&taskID, &taskInitParam) != LOS_OK) {
|
||||||
free(pthreadData);
|
free((VOID *)(UINTPTR)taskInitParam.uwArg);
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -205,14 +233,26 @@ void pthread_exit(void *retVal)
|
||||||
|
|
||||||
int pthread_setname_np(pthread_t thread, const char *name)
|
int pthread_setname_np(pthread_t thread, const char *name)
|
||||||
{
|
{
|
||||||
|
UINT32 intSave;
|
||||||
|
LosTaskCB *taskCB = NULL;
|
||||||
char *taskName = LOS_TaskNameGet((UINT32)thread);
|
char *taskName = LOS_TaskNameGet((UINT32)thread);
|
||||||
if (taskName == NULL || !IsPthread(thread)) {
|
if (taskName == NULL || !IsPthread(thread)) {
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strnlen(name, PTHREAD_NAMELEN) >= PTHREAD_NAMELEN) {
|
if (strnlen(name, PTHREAD_NAMELEN) >= PTHREAD_NAMELEN) {
|
||||||
return ERANGE;
|
return ERANGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
taskCB = OS_TCB_FROM_TID((UINT32)thread);
|
||||||
|
intSave = LOS_IntLock();
|
||||||
|
if (taskCB->taskEntry == PthreadEntry) {
|
||||||
(void)strcpy_s(taskName, PTHREAD_NAMELEN, name);
|
(void)strcpy_s(taskName, PTHREAD_NAMELEN, name);
|
||||||
|
} else {
|
||||||
|
LOS_IntRestore(intSave);
|
||||||
|
return EINVAL;
|
||||||
|
}
|
||||||
|
LOS_IntRestore(intSave);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue