pthread.c 6.4 KB
Newer Older
L
likailong 已提交
1
/*
M
mamingshuai 已提交
2 3
 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
L
likailong 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

M
mamingshuai 已提交
32
#include "pthread.h"
L
likailong 已提交
33
#include <errno.h>
34
#include <stdlib.h>
L
likailong 已提交
35 36
#include <unistd.h>
#include <securec.h>
37
#include "los_config.h"
L
likailong 已提交
38 39 40 41
#include "los_task.h"

#define PTHREAD_NAMELEN 16

42 43 44 45 46
typedef struct {
    void *(*startRoutine)(void *);
    void *param;
    char name[PTHREAD_NAMELEN];
}PthreadData;
L
likailong 已提交
47

48
static void *PthreadEntry(UINT32 param)
L
likailong 已提交
49
{
50 51 52 53 54 55
    PthreadData *pthreadData = (PthreadData *)(UINTPTR)param;
    void *(*startRoutine)(void *) = pthreadData->startRoutine;
    void *ret = startRoutine(pthreadData->param);
    free(pthreadData);
    return ret;
}
L
likailong 已提交
56

57 58 59 60
static inline int IsPthread(pthread_t thread)
{
    return ((UINT32)thread <= LOSCFG_BASE_CORE_TSK_LIMIT) &&
           (OS_TCB_FROM_TID((UINT32)thread)->taskEntry == PthreadEntry);
L
likailong 已提交
61 62 63 64 65 66
}

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                   void *(*startRoutine)(void *), void *arg)
{
    TSK_INIT_PARAM_S taskInitParam = {0};
67
    PthreadData *pthreadData = NULL;
L
likailong 已提交
68 69 70 71 72 73
    UINT32 taskID;

    if ((thread == NULL) || (startRoutine == NULL)) {
        return EINVAL;
    }

C
Caoruihong 已提交
74 75
    taskInitParam.usTaskPrio = LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO;
    taskInitParam.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE;
L
likailong 已提交
76 77 78 79
    if (attr) {
        if (attr->detachstate == PTHREAD_CREATE_DETACHED) {
            return ENOTSUP;
        }
C
Caoruihong 已提交
80 81 82 83 84 85
        if (attr->stackaddr_set) {
            return ENOTSUP;
        }
        if (attr->stacksize_set) {
            taskInitParam.uwStackSize = attr->stacksize;
        }
L
likailong 已提交
86 87 88
        taskInitParam.usTaskPrio = (UINT16)attr->schedparam.sched_priority;
    }

89 90
    pthreadData = (PthreadData *)malloc(sizeof(PthreadData));
    if (pthreadData == NULL) {
L
likailong 已提交
91 92 93
        return ENOMEM;
    }

M
mamingshuai 已提交
94 95
    pthreadData->startRoutine  = startRoutine;
    pthreadData->param         = arg;
96 97 98
    taskInitParam.pcName       = pthreadData->name;
    taskInitParam.pfnTaskEntry = PthreadEntry;
    taskInitParam.uwArg   = (UINT32)(UINTPTR)pthreadData;
L
likailong 已提交
99 100

    if (LOS_TaskCreate(&taskID, &taskInitParam) != LOS_OK) {
101
        free(pthreadData);
L
likailong 已提交
102 103 104 105 106 107 108
        return EINVAL;
    }

    /* set pthread default name */
    (void)sprintf_s(taskInitParam.pcName, PTHREAD_NAMELEN, "pthread%u", taskID);

    *thread = (pthread_t)taskID;
109
    return 0;
L
likailong 已提交
110 111 112 113
}

int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
{
114 115
    if ((param == NULL) || (param->sched_priority < OS_TASK_PRIORITY_HIGHEST) ||
        (param->sched_priority >= OS_TASK_PRIORITY_LOWEST) || !IsPthread(thread)) {
L
likailong 已提交
116 117 118 119 120 121 122 123 124 125 126 127
        return EINVAL;
    }

    /* Only support SCHED_RR policy now */
    if (policy != SCHED_RR) {
        return ENOTSUP;
    }

    if (LOS_TaskPriSet((UINT32)thread, (UINT16)param->sched_priority) != LOS_OK) {
        return EINVAL;
    }

128
    return 0;
L
likailong 已提交
129 130 131 132 133 134
}

int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
{
    UINT32 prio;

135
    if ((policy == NULL) || (param == NULL) || !IsPthread(thread)) {
L
likailong 已提交
136 137 138 139 140 141 142 143 144 145
        return EINVAL;
    }

    prio = LOS_TaskPriGet((UINT32)thread);
    if (prio == OS_INVALID) {
        return EINVAL;
    }

    *policy = SCHED_RR;
    param->sched_priority = prio;
146
    return 0;
L
likailong 已提交
147 148 149 150 151 152 153 154 155
}

pthread_t pthread_self(void)
{
    return (pthread_t)LOS_CurTaskIDGet();
}

int pthread_cancel(pthread_t thread)
{
156 157 158
    if (!IsPthread(thread)) {
        return EINVAL;
    }
L
likailong 已提交
159

160
    return ENOSYS;
L
likailong 已提交
161 162 163 164 165 166
}

int pthread_join(pthread_t thread, void **retval)
{
    UINT32 taskStatus;

167 168 169 170
    if (!IsPthread(thread)) {
        return EINVAL;
    }

L
likailong 已提交
171 172 173 174 175 176 177 178 179 180
    if (retval) {
        /* retrieve thread exit code is not supported currently */
        return ENOTSUP;
    }

    if (thread == pthread_self()) {
        return EDEADLK;
    }

    while (LOS_TaskStatusGet((UINT32)thread, &taskStatus) == LOS_OK) {
M
mamingshuai 已提交
181
        (void)LOS_TaskDelay(10); /* 10: Waiting for the end of thread execution. */
L
likailong 已提交
182 183 184 185 186 187 188
    }

    return 0;
}

int pthread_detach(pthread_t thread)
{
189 190 191 192
    if (!IsPthread(thread)) {
        return EINVAL;
    }

L
likailong 已提交
193 194 195 196 197 198
    return ENOSYS;
}

void pthread_exit(void *retVal)
{
    (void)retVal;
199 200
    LosTaskCB *tcb = OS_TCB_FROM_TID(LOS_CurTaskIDGet());
    free((PthreadData *)(UINTPTR)tcb->arg);
L
likailong 已提交
201 202 203 204 205 206
    (void)LOS_TaskDelete(LOS_CurTaskIDGet());
}

int pthread_setname_np(pthread_t thread, const char *name)
{
    char *taskName = LOS_TaskNameGet((UINT32)thread);
207
    if (taskName == NULL || !IsPthread(thread)) {
L
likailong 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
        return EINVAL;
    }
    if (strnlen(name, PTHREAD_NAMELEN) >= PTHREAD_NAMELEN) {
        return ERANGE;
    }
    (void)strcpy_s(taskName, PTHREAD_NAMELEN, name);
    return 0;
}

int pthread_getname_np(pthread_t thread, char *buf, size_t buflen)
{
    int ret;

    const char *name = LOS_TaskNameGet((UINT32)thread);
222
    if (name == NULL || !IsPthread(thread)) {
L
likailong 已提交
223 224 225 226 227 228 229 230 231 232
        return EINVAL;
    }
    if (buflen > strlen(name)) {
        ret = strcpy_s(buf, buflen, name);
        if (ret == 0) {
            return 0;
        }
    }
    return ERANGE;
}