thread.cpp 2.3 KB
Newer Older
L
liukangcc 已提交
1 2 3 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-04-27     flybreak     the first version.
 */

#include <arm-tpl.h>
#include "tpl.h"
#include <cstdio>
#include <pthread.h>

extern "C" int __ARM_TPL_thread_create(__ARM_TPL_thread_t *__t,
                                       void *(*__func)(void *),
                                       void *__arg)
{
    int ret = 0;
    /* TODO memory leek */
    pthread_t *pid = (pthread_t *)rt_malloc(sizeof(pthread_t));
    if (pid == nullptr)
        return -1;
    ret = pthread_create(pid, RT_NULL, __func, __arg);
    if (ret == 0)
    {
        __t->data = (std::uintptr_t)pid;
        return 0;
    }
    return -1;
}

extern "C" int __ARM_TPL_thread_id_compare(__ARM_TPL_thread_id __tid1,
        __ARM_TPL_thread_id __tid2)
{
    if (__tid1 > __tid2)
        return 1;
    else if (__tid1 < __tid2)
        return -1;
    else
        return 0;
}

extern "C" __ARM_TPL_thread_id __ARM_TPL_thread_get_current_id()
{
    return (__ARM_TPL_thread_id)pthread_self();
}

extern "C" __ARM_TPL_thread_id __ARM_TPL_thread_get_id(
    const __ARM_TPL_thread_t *__t)
{
    return (__ARM_TPL_thread_id)((*(pthread_t *)__t->data));
}

extern "C" int __ARM_TPL_thread_join(__ARM_TPL_thread_t *__t)
{
    pthread_join((*(pthread_t *)__t->data), RT_NULL);
    return 0;
}

extern "C" int __ARM_TPL_thread_detach(__ARM_TPL_thread_t *__t)
{
    pthread_detach((*(pthread_t *)__t->data));
    return 0;
}

extern "C" void __ARM_TPL_thread_yield()
{
    rt_thread_yield();
}

extern "C" int __ARM_TPL_thread_nanosleep(const __ARM_TPL_timespec_t *__req,
        __ARM_TPL_timespec_t *__rem)
{
    return nanosleep(__req, rem);
}

extern "C" unsigned __ARM_TPL_thread_hw_concurrency()
{
    return 1;
}

extern "C" int __ARM_TPL_tls_create(__ARM_TPL_tls_key *__key,
                                    void (*__at_exit)(void *))
{
    pthread_key_t key; 
    
    if (pthread_key_create(&key, __at_exit) == 0)
    {
        *__key = key;
        return 0;
    }
    return -1;
}

extern "C" void *__ARM_TPL_tls_get(__ARM_TPL_tls_key __key)
{
    return pthread_getspecific(__key);
}

extern "C" int __ARM_TPL_tls_set(__ARM_TPL_tls_key __key, void *__p)
{
    if (pthread_setspecific(__key, (void*)__p) != 0)
    {
        return -1;
    }
    return 0;
}