2013-06-26 23:18:30 +08:00
|
|
|
/*
|
2021-03-08 18:19:04 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-06-26 23:18:30 +08:00
|
|
|
*
|
2018-10-14 19:28:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-06-26 23:18:30 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
|
|
|
* Date Author Notes
|
2018-02-07 19:55:26 +08:00
|
|
|
* 2018-01-26 Bernard Fix pthread_detach issue for a none-joinable
|
|
|
|
* thread.
|
2019-02-07 23:50:49 +08:00
|
|
|
* 2019-02-07 Bernard Add _pthread_destroy to release pthread resource.
|
2022-05-10 21:51:30 +08:00
|
|
|
* 2022-05-10 xiangxistu Modify the recycle logic about resource of pthread.
|
2024-05-12 01:50:02 +08:00
|
|
|
* 2024-04-15 atwww Modify the recycle logic of TLS in function _pthread_data_destroy,
|
|
|
|
* make it safe for C++11's thread_local destructors.
|
2013-06-26 23:18:30 +08:00
|
|
|
*/
|
|
|
|
|
2019-05-13 09:19:44 +08:00
|
|
|
#include <rthw.h>
|
2013-01-08 22:40:58 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
2021-02-12 01:30:41 +08:00
|
|
|
#include <sys/time.h>
|
2013-01-08 22:40:58 +08:00
|
|
|
#include "pthread_internal.h"
|
|
|
|
|
2023-10-25 20:31:25 +08:00
|
|
|
RT_DEFINE_HW_SPINLOCK(pth_lock);
|
2019-05-12 17:56:11 +08:00
|
|
|
_pthread_data_t *pth_table[PTHREAD_NUM_MAX] = {NULL};
|
2021-12-17 15:34:17 +08:00
|
|
|
static int concurrency_level;
|
2019-05-12 17:56:11 +08:00
|
|
|
|
|
|
|
_pthread_data_t *_pthread_get_data(pthread_t thread)
|
|
|
|
{
|
|
|
|
_pthread_data_t *ptd;
|
|
|
|
|
|
|
|
if (thread >= PTHREAD_NUM_MAX) return NULL;
|
|
|
|
|
|
|
|
rt_hw_spin_lock(&pth_lock);
|
|
|
|
ptd = pth_table[thread];
|
|
|
|
rt_hw_spin_unlock(&pth_lock);
|
|
|
|
|
|
|
|
if (ptd && ptd->magic == PTHREAD_MAGIC) return ptd;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
rt_hw_spin_lock(&pth_lock);
|
|
|
|
for (index = 0; index < PTHREAD_NUM_MAX; index ++)
|
|
|
|
{
|
|
|
|
if (pth_table[index] == ptd) break;
|
|
|
|
}
|
|
|
|
rt_hw_spin_unlock(&pth_lock);
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_t _pthread_data_create(void)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
_pthread_data_t *ptd = NULL;
|
|
|
|
|
|
|
|
ptd = (_pthread_data_t*)rt_malloc(sizeof(_pthread_data_t));
|
|
|
|
if (!ptd) return PTHREAD_NUM_MAX;
|
|
|
|
|
2019-05-12 21:44:28 +08:00
|
|
|
memset(ptd, 0x0, sizeof(_pthread_data_t));
|
2019-05-12 17:56:11 +08:00
|
|
|
ptd->canceled = 0;
|
2021-09-17 14:57:42 +08:00
|
|
|
ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
|
2019-05-12 17:56:11 +08:00
|
|
|
ptd->canceltype = PTHREAD_CANCEL_DEFERRED;
|
|
|
|
ptd->magic = PTHREAD_MAGIC;
|
|
|
|
|
|
|
|
rt_hw_spin_lock(&pth_lock);
|
|
|
|
for (index = 0; index < PTHREAD_NUM_MAX; index ++)
|
|
|
|
{
|
|
|
|
if (pth_table[index] == NULL)
|
|
|
|
{
|
|
|
|
pth_table[index] = ptd;
|
2019-05-12 21:44:28 +08:00
|
|
|
break;
|
2019-05-12 17:56:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
rt_hw_spin_unlock(&pth_lock);
|
|
|
|
|
2019-05-13 09:19:44 +08:00
|
|
|
/* full of pthreads, clean magic and release ptd */
|
|
|
|
if (index == PTHREAD_NUM_MAX)
|
|
|
|
{
|
|
|
|
ptd->magic = 0x0;
|
|
|
|
rt_free(ptd);
|
|
|
|
}
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2024-05-12 01:50:02 +08:00
|
|
|
static inline void _destroy_item(int index, _pthread_data_t *ptd)
|
2019-05-12 17:56:11 +08:00
|
|
|
{
|
2021-09-03 11:38:48 +08:00
|
|
|
extern _pthread_key_data_t _thread_keys[PTHREAD_KEY_MAX];
|
2024-05-12 01:50:02 +08:00
|
|
|
void *data;
|
|
|
|
|
|
|
|
if (_thread_keys[index].is_used)
|
|
|
|
{
|
|
|
|
data = ptd->tls[index];
|
|
|
|
if (data && _thread_keys[index].destructor)
|
|
|
|
{
|
|
|
|
_thread_keys[index].destructor(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef RT_USING_CPLUSPLUS11
|
|
|
|
#define NOT_USE_CXX_TLS -1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void _pthread_data_destroy(_pthread_data_t *ptd)
|
|
|
|
{
|
2022-05-10 21:51:30 +08:00
|
|
|
pthread_t pth;
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
if (ptd)
|
|
|
|
{
|
2022-05-10 21:51:30 +08:00
|
|
|
/* if this thread create the local thread data,
|
|
|
|
* destruct thread local key
|
|
|
|
*/
|
2021-09-03 11:38:48 +08:00
|
|
|
if (ptd->tls != RT_NULL)
|
|
|
|
{
|
2024-05-12 01:50:02 +08:00
|
|
|
int index;
|
|
|
|
#ifdef RT_USING_CPLUSPLUS11
|
|
|
|
/* If C++11 is enabled and emutls is used,
|
|
|
|
* destructors of C++ object must be called safely.
|
|
|
|
*/
|
|
|
|
extern pthread_key_t emutls_get_pthread_key(void);
|
|
|
|
pthread_key_t emutls_pthread_key = emutls_get_pthread_key();
|
|
|
|
|
|
|
|
if (emutls_pthread_key != NOT_USE_CXX_TLS)
|
2021-09-03 11:38:48 +08:00
|
|
|
{
|
2024-05-12 01:50:02 +08:00
|
|
|
/* If execution reaches here, C++ 'thread_local' may be used.
|
|
|
|
* Destructors of c++ class object must be called before emutls_key_destructor.
|
|
|
|
*/
|
|
|
|
int start = ((emutls_pthread_key - 1 + PTHREAD_KEY_MAX) % PTHREAD_KEY_MAX);
|
|
|
|
int i = 0;
|
|
|
|
for (index = start; i < PTHREAD_KEY_MAX; index = (index - 1 + PTHREAD_KEY_MAX) % PTHREAD_KEY_MAX, i ++)
|
2021-09-03 11:38:48 +08:00
|
|
|
{
|
2024-05-12 01:50:02 +08:00
|
|
|
_destroy_item(index, ptd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* If only C TLS is used, that is, POSIX TLS or __Thread_local,
|
|
|
|
* just iterate the _thread_keys from index 0.
|
|
|
|
*/
|
|
|
|
for (index = 0; index < PTHREAD_KEY_MAX; index ++)
|
|
|
|
{
|
|
|
|
_destroy_item(index, ptd);
|
2021-09-03 11:38:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* release tls area */
|
|
|
|
rt_free(ptd->tls);
|
|
|
|
ptd->tls = RT_NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
pth = _pthread_data_get_pth(ptd);
|
2019-05-12 17:56:11 +08:00
|
|
|
/* remove from pthread table */
|
|
|
|
rt_hw_spin_lock(&pth_lock);
|
|
|
|
pth_table[pth] = NULL;
|
|
|
|
rt_hw_spin_unlock(&pth_lock);
|
|
|
|
|
|
|
|
/* delete joinable semaphore */
|
|
|
|
if (ptd->joinable_sem != RT_NULL)
|
|
|
|
{
|
2022-05-10 21:51:30 +08:00
|
|
|
rt_sem_delete(ptd->joinable_sem);
|
|
|
|
ptd->joinable_sem = RT_NULL;
|
2019-05-12 17:56:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clean magic */
|
|
|
|
ptd->magic = 0x0;
|
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* clear the "ptd->tid->pthread_data" */
|
|
|
|
ptd->tid->pthread_data = RT_NULL;
|
2022-05-10 21:51:30 +08:00
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
/* free ptd */
|
|
|
|
rt_free(ptd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
static void _pthread_cleanup(rt_thread_t tid)
|
2019-02-07 23:50:49 +08:00
|
|
|
{
|
2022-05-10 21:51:30 +08:00
|
|
|
/* clear cleanup function */
|
|
|
|
tid->cleanup = RT_NULL;
|
|
|
|
|
|
|
|
/* restore tid stack */
|
|
|
|
rt_free(tid->stack_addr);
|
2019-02-07 23:50:49 +08:00
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
/* restore tid control block */
|
|
|
|
rt_free(tid);
|
2019-02-07 23:50:49 +08:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
static void pthread_entry_stub(void *parameter)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2022-05-10 21:51:30 +08:00
|
|
|
void *value;
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
2019-05-12 17:56:11 +08:00
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
ptd = (_pthread_data_t *)parameter;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
/* execute pthread entry */
|
|
|
|
value = ptd->thread_entry(ptd->thread_parameter);
|
|
|
|
|
|
|
|
/* According to "detachstate" to whether or not to recycle resource immediately */
|
2013-06-26 23:18:30 +08:00
|
|
|
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
|
|
|
|
{
|
2022-05-10 21:51:30 +08:00
|
|
|
/* set value */
|
|
|
|
ptd->return_value = value;
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_sem_release(ptd->joinable_sem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* release pthread resource */
|
2022-05-10 21:51:30 +08:00
|
|
|
_pthread_data_destroy(ptd);
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
int pthread_create(pthread_t *pid,
|
2018-02-07 19:55:26 +08:00
|
|
|
const pthread_attr_t *attr,
|
|
|
|
void *(*start)(void *), void *parameter)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2019-05-12 17:56:11 +08:00
|
|
|
int ret = 0;
|
2013-06-26 23:18:30 +08:00
|
|
|
void *stack;
|
|
|
|
char name[RT_NAME_MAX];
|
|
|
|
static rt_uint16_t pthread_number = 0;
|
2019-05-12 17:56:11 +08:00
|
|
|
|
|
|
|
pthread_t pth_id;
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
|
|
|
|
2019-05-13 09:19:44 +08:00
|
|
|
/* pid shall be provided */
|
2019-05-12 17:56:11 +08:00
|
|
|
RT_ASSERT(pid != RT_NULL);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
/* allocate posix thread data */
|
2019-05-12 17:56:11 +08:00
|
|
|
pth_id = _pthread_data_create();
|
2021-03-08 18:19:04 +08:00
|
|
|
if (pth_id == PTHREAD_NUM_MAX)
|
2019-05-12 17:56:11 +08:00
|
|
|
{
|
|
|
|
ret = ENOMEM;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
/* get pthread data */
|
|
|
|
ptd = _pthread_get_data(pth_id);
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2021-03-31 09:22:47 +08:00
|
|
|
RT_ASSERT(ptd != RT_NULL);
|
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if (attr != RT_NULL)
|
2018-02-07 19:55:26 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
ptd->attr = *attr;
|
2018-02-07 19:55:26 +08:00
|
|
|
}
|
|
|
|
else
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
/* use default attribute */
|
|
|
|
pthread_attr_init(&ptd->attr);
|
|
|
|
}
|
|
|
|
|
2021-09-03 11:38:48 +08:00
|
|
|
if (ptd->attr.stacksize == 0)
|
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_snprintf(name, sizeof(name), "pth%02d", pthread_number ++);
|
|
|
|
|
|
|
|
/* pthread is a static thread object */
|
|
|
|
ptd->tid = (rt_thread_t) rt_malloc(sizeof(struct rt_thread));
|
|
|
|
if (ptd->tid == RT_NULL)
|
|
|
|
{
|
2019-05-12 17:56:11 +08:00
|
|
|
ret = ENOMEM;
|
|
|
|
goto __exit;
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
2019-10-10 22:42:14 +08:00
|
|
|
memset(ptd->tid, 0, sizeof(struct rt_thread));
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
|
|
|
|
{
|
|
|
|
ptd->joinable_sem = rt_sem_create(name, 0, RT_IPC_FLAG_FIFO);
|
|
|
|
if (ptd->joinable_sem == RT_NULL)
|
|
|
|
{
|
2019-05-12 17:56:11 +08:00
|
|
|
ret = ENOMEM;
|
|
|
|
goto __exit;
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2018-02-07 19:55:26 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
ptd->joinable_sem = RT_NULL;
|
2018-02-07 19:55:26 +08:00
|
|
|
}
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
/* set parameter */
|
|
|
|
ptd->thread_entry = start;
|
|
|
|
ptd->thread_parameter = parameter;
|
|
|
|
|
2019-10-10 22:42:14 +08:00
|
|
|
/* stack */
|
|
|
|
if (ptd->attr.stackaddr == 0)
|
|
|
|
{
|
|
|
|
stack = (void *)rt_malloc(ptd->attr.stacksize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
stack = (void *)(ptd->attr.stackaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack == RT_NULL)
|
|
|
|
{
|
|
|
|
ret = ENOMEM;
|
|
|
|
goto __exit;
|
|
|
|
}
|
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
/* initial this pthread to system */
|
2018-02-07 19:55:26 +08:00
|
|
|
if (rt_thread_init(ptd->tid, name, pthread_entry_stub, ptd,
|
2019-05-12 15:04:46 +08:00
|
|
|
stack, ptd->attr.stacksize,
|
2021-09-03 11:38:48 +08:00
|
|
|
ptd->attr.schedparam.sched_priority, 20) != RT_EOK)
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
2019-05-12 17:56:11 +08:00
|
|
|
ret = EINVAL;
|
|
|
|
goto __exit;
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set pthread id */
|
2019-05-12 17:56:11 +08:00
|
|
|
*pid = pth_id;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
/* set pthread cleanup function and ptd data */
|
2019-05-12 17:56:11 +08:00
|
|
|
ptd->tid->cleanup = _pthread_cleanup;
|
2022-08-08 11:16:17 +08:00
|
|
|
ptd->tid->pthread_data = (void *)ptd;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
/* start thread */
|
2019-05-12 17:56:11 +08:00
|
|
|
if (rt_thread_startup(ptd->tid) == RT_EOK)
|
2013-06-26 23:18:30 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* start thread failed */
|
|
|
|
rt_thread_detach(ptd->tid);
|
2019-05-12 17:56:11 +08:00
|
|
|
ret = EINVAL;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
__exit:
|
|
|
|
if (pth_id != PTHREAD_NUM_MAX)
|
2022-05-10 21:51:30 +08:00
|
|
|
{
|
|
|
|
_pthread_data_destroy(ptd);
|
|
|
|
}
|
2019-05-12 17:56:11 +08:00
|
|
|
return ret;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_create);
|
|
|
|
|
|
|
|
int pthread_detach(pthread_t thread)
|
|
|
|
{
|
2019-02-07 23:50:49 +08:00
|
|
|
int ret = 0;
|
|
|
|
_pthread_data_t *ptd = _pthread_get_data(thread);
|
2021-03-31 09:22:47 +08:00
|
|
|
if (ptd == RT_NULL)
|
|
|
|
{
|
|
|
|
/* invalid pthread id */
|
|
|
|
ret = EINVAL;
|
|
|
|
goto __exit;
|
|
|
|
}
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2018-02-07 19:55:26 +08:00
|
|
|
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
|
|
|
|
{
|
|
|
|
/* The implementation has detected that the value specified by thread does not refer
|
|
|
|
* to a joinable thread.
|
|
|
|
*/
|
2019-02-07 23:50:49 +08:00
|
|
|
ret = EINVAL;
|
|
|
|
goto __exit;
|
2018-02-07 19:55:26 +08:00
|
|
|
}
|
|
|
|
|
2024-06-16 15:42:37 +08:00
|
|
|
if ((RT_SCHED_CTX(ptd->tid).stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE)
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
2022-05-10 21:51:30 +08:00
|
|
|
/* destroy this pthread */
|
|
|
|
_pthread_data_destroy(ptd);
|
2019-02-07 23:50:49 +08:00
|
|
|
goto __exit;
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* change to detach state */
|
|
|
|
ptd->attr.detachstate = PTHREAD_CREATE_DETACHED;
|
|
|
|
|
|
|
|
/* detach joinable semaphore */
|
2019-02-07 23:50:49 +08:00
|
|
|
if (ptd->joinable_sem)
|
|
|
|
{
|
|
|
|
rt_sem_delete(ptd->joinable_sem);
|
|
|
|
ptd->joinable_sem = RT_NULL;
|
|
|
|
}
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
|
2019-02-07 23:50:49 +08:00
|
|
|
__exit:
|
|
|
|
return ret;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_detach);
|
|
|
|
|
2018-02-07 19:55:26 +08:00
|
|
|
int pthread_join(pthread_t thread, void **value_ptr)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2018-02-07 19:55:26 +08:00
|
|
|
_pthread_data_t *ptd;
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_err_t result;
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
ptd = _pthread_get_data(thread);
|
2021-03-31 09:22:47 +08:00
|
|
|
|
|
|
|
if (ptd == RT_NULL)
|
|
|
|
{
|
|
|
|
return EINVAL; /* invalid pthread id */
|
|
|
|
}
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
if (ptd && ptd->tid == rt_thread_self())
|
2013-06-26 23:18:30 +08:00
|
|
|
{
|
|
|
|
/* join self */
|
|
|
|
return EDEADLK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
|
2021-03-31 09:22:47 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
return EINVAL; /* join on a detached pthread */
|
2021-03-31 09:22:47 +08:00
|
|
|
}
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
|
|
|
|
if (result == RT_EOK)
|
|
|
|
{
|
|
|
|
/* get return value */
|
|
|
|
if (value_ptr != RT_NULL)
|
|
|
|
*value_ptr = ptd->return_value;
|
|
|
|
|
2019-02-07 23:50:49 +08:00
|
|
|
/* destroy this pthread */
|
2022-05-10 21:51:30 +08:00
|
|
|
_pthread_data_destroy(ptd);
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
else
|
2018-02-07 19:55:26 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
return ESRCH;
|
2018-02-07 19:55:26 +08:00
|
|
|
}
|
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_join);
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
pthread_t pthread_self (void)
|
|
|
|
{
|
|
|
|
rt_thread_t tid;
|
|
|
|
_pthread_data_t *ptd;
|
|
|
|
|
|
|
|
tid = rt_thread_self();
|
|
|
|
if (tid == NULL) return PTHREAD_NUM_MAX;
|
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* get pthread data from pthread_data of thread */
|
|
|
|
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
2019-05-12 17:56:11 +08:00
|
|
|
RT_ASSERT(ptd != RT_NULL);
|
|
|
|
|
|
|
|
return _pthread_data_get_pth(ptd);
|
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_self);
|
|
|
|
|
2021-12-17 15:34:17 +08:00
|
|
|
int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id)
|
|
|
|
{
|
|
|
|
if(_pthread_get_data(thread) == NULL)
|
|
|
|
{
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*clock_id = (clockid_t)rt_tick_get();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_getcpuclockid);
|
|
|
|
|
|
|
|
int pthread_getconcurrency(void)
|
|
|
|
{
|
|
|
|
return concurrency_level;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_getconcurrency);
|
|
|
|
|
|
|
|
int pthread_setconcurrency(int new_level)
|
|
|
|
{
|
|
|
|
concurrency_level = new_level;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_setconcurrency);
|
|
|
|
|
|
|
|
int pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
|
|
|
|
{
|
|
|
|
_pthread_data_t *ptd;
|
|
|
|
|
|
|
|
ptd = _pthread_get_data(thread);
|
|
|
|
pthread_attr_getschedpolicy(&ptd->attr, policy);
|
|
|
|
pthread_attr_getschedparam(&ptd->attr, param);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_getschedparam);
|
|
|
|
|
|
|
|
int pthread_setschedparam(pthread_t thread, int policy, const struct sched_param *param)
|
|
|
|
{
|
|
|
|
_pthread_data_t *ptd;
|
|
|
|
|
|
|
|
ptd = _pthread_get_data(thread);
|
|
|
|
pthread_attr_setschedpolicy(&ptd->attr, policy);
|
|
|
|
pthread_attr_setschedparam(&ptd->attr, param);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_setschedparam);
|
|
|
|
|
|
|
|
int pthread_setschedprio(pthread_t thread, int prio)
|
|
|
|
{
|
|
|
|
_pthread_data_t *ptd;
|
|
|
|
struct sched_param param;
|
|
|
|
|
|
|
|
ptd = _pthread_get_data(thread);
|
|
|
|
param.sched_priority = prio;
|
|
|
|
pthread_attr_setschedparam(&ptd->attr, ¶m);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_setschedprio);
|
|
|
|
|
2018-02-07 19:55:26 +08:00
|
|
|
void pthread_exit(void *value)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
|
|
|
_pthread_cleanup_t *cleanup;
|
2022-05-10 21:51:30 +08:00
|
|
|
rt_thread_t tid;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
if (rt_thread_self() == RT_NULL)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-05-12 17:56:11 +08:00
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* get pthread data from pthread_data of thread */
|
|
|
|
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
rt_enter_critical();
|
|
|
|
/* disable cancel */
|
|
|
|
ptd->cancelstate = PTHREAD_CANCEL_DISABLE;
|
|
|
|
/* set return value */
|
|
|
|
ptd->return_value = value;
|
|
|
|
rt_exit_critical();
|
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
/*
|
|
|
|
* When use pthread_exit to exit.
|
|
|
|
* invoke pushed cleanup
|
|
|
|
*/
|
2013-06-26 23:18:30 +08:00
|
|
|
while (ptd->cleanup != RT_NULL)
|
|
|
|
{
|
|
|
|
cleanup = ptd->cleanup;
|
|
|
|
ptd->cleanup = cleanup->next;
|
|
|
|
|
|
|
|
cleanup->cleanup_func(cleanup->parameter);
|
|
|
|
/* release this cleanup function */
|
|
|
|
rt_free(cleanup);
|
|
|
|
}
|
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
/* get the info aboult "tid" early */
|
|
|
|
tid = ptd->tid;
|
2018-02-07 19:55:26 +08:00
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
/* According to "detachstate" to whether or not to recycle resource immediately */
|
|
|
|
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
|
|
|
|
{
|
|
|
|
/* set value */
|
|
|
|
rt_sem_release(ptd->joinable_sem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* release pthread resource */
|
|
|
|
_pthread_data_destroy(ptd);
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:51:30 +08:00
|
|
|
/*
|
|
|
|
* second: detach thread.
|
|
|
|
* this thread will be removed from scheduler list
|
|
|
|
* and because there is a cleanup function in the
|
|
|
|
* thread (pthread_cleanup), it will move to defunct
|
|
|
|
* thread list and wait for handling in idle thread.
|
|
|
|
*/
|
|
|
|
rt_thread_detach(tid);
|
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
/* reschedule thread */
|
|
|
|
rt_schedule();
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_exit);
|
|
|
|
|
2018-02-07 19:55:26 +08:00
|
|
|
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
RT_ASSERT(once_control != RT_NULL);
|
|
|
|
RT_ASSERT(init_routine != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
rt_enter_critical();
|
|
|
|
if (!(*once_control))
|
|
|
|
{
|
|
|
|
/* call routine once */
|
|
|
|
*once_control = 1;
|
|
|
|
rt_exit_critical();
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
init_routine();
|
|
|
|
}
|
|
|
|
rt_exit_critical();
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_once);
|
|
|
|
|
|
|
|
int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
|
|
|
|
{
|
2014-03-11 16:05:14 +08:00
|
|
|
return EOPNOTSUPP;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_atfork);
|
|
|
|
|
|
|
|
int pthread_kill(pthread_t thread, int sig)
|
|
|
|
{
|
2017-10-15 22:41:59 +08:00
|
|
|
#ifdef RT_USING_SIGNALS
|
2019-05-12 17:56:11 +08:00
|
|
|
_pthread_data_t *ptd;
|
2021-09-03 11:38:48 +08:00
|
|
|
int ret;
|
2019-05-12 17:56:11 +08:00
|
|
|
|
|
|
|
ptd = _pthread_get_data(thread);
|
|
|
|
if (ptd)
|
|
|
|
{
|
2021-09-03 11:38:48 +08:00
|
|
|
ret = rt_thread_kill(ptd->tid, sig);
|
|
|
|
if (ret == -RT_EINVAL)
|
|
|
|
{
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2019-05-12 17:56:11 +08:00
|
|
|
}
|
|
|
|
|
2021-09-03 11:38:48 +08:00
|
|
|
return ESRCH;
|
2017-10-15 22:41:59 +08:00
|
|
|
#else
|
2018-02-07 19:55:26 +08:00
|
|
|
return ENOSYS;
|
2017-10-15 22:41:59 +08:00
|
|
|
#endif
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_kill);
|
|
|
|
|
2017-10-15 22:41:59 +08:00
|
|
|
#ifdef RT_USING_SIGNALS
|
|
|
|
int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
|
|
|
|
{
|
2018-02-07 19:55:26 +08:00
|
|
|
return sigprocmask(how, set, oset);
|
2017-10-15 22:41:59 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-01-08 22:40:58 +08:00
|
|
|
void pthread_cleanup_pop(int execute)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
|
|
|
_pthread_cleanup_t *cleanup;
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
if (rt_thread_self() == NULL) return;
|
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* get pthread data from pthread_data of thread */
|
|
|
|
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
2013-06-26 23:18:30 +08:00
|
|
|
RT_ASSERT(ptd != RT_NULL);
|
|
|
|
|
|
|
|
if (execute)
|
|
|
|
{
|
|
|
|
rt_enter_critical();
|
|
|
|
cleanup = ptd->cleanup;
|
|
|
|
if (cleanup)
|
|
|
|
ptd->cleanup = cleanup->next;
|
|
|
|
rt_exit_critical();
|
|
|
|
|
|
|
|
if (cleanup)
|
|
|
|
{
|
|
|
|
cleanup->cleanup_func(cleanup->parameter);
|
|
|
|
|
|
|
|
rt_free(cleanup);
|
|
|
|
}
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_cleanup_pop);
|
|
|
|
|
2018-02-07 19:55:26 +08:00
|
|
|
void pthread_cleanup_push(void (*routine)(void *), void *arg)
|
2013-01-08 22:40:58 +08:00
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
|
|
|
_pthread_cleanup_t *cleanup;
|
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
if (rt_thread_self() == NULL) return;
|
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* get pthread data from pthread_data of thread */
|
|
|
|
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
2013-06-26 23:18:30 +08:00
|
|
|
RT_ASSERT(ptd != RT_NULL);
|
|
|
|
|
|
|
|
cleanup = (_pthread_cleanup_t *)rt_malloc(sizeof(_pthread_cleanup_t));
|
|
|
|
if (cleanup != RT_NULL)
|
|
|
|
{
|
|
|
|
cleanup->cleanup_func = routine;
|
|
|
|
cleanup->parameter = arg;
|
|
|
|
|
|
|
|
rt_enter_critical();
|
|
|
|
cleanup->next = ptd->cleanup;
|
|
|
|
ptd->cleanup = cleanup;
|
|
|
|
rt_exit_critical();
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_cleanup_push);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* According to IEEE Std 1003.1, 2004 Edition , following pthreads
|
|
|
|
* interface support cancellation point:
|
|
|
|
* mq_receive()
|
|
|
|
* mq_send()
|
|
|
|
* mq_timedreceive()
|
|
|
|
* mq_timedsend()
|
|
|
|
* msgrcv()
|
|
|
|
* msgsnd()
|
|
|
|
* msync()
|
|
|
|
* pthread_cond_timedwait()
|
|
|
|
* pthread_cond_wait()
|
|
|
|
* pthread_join()
|
|
|
|
* pthread_testcancel()
|
|
|
|
* sem_timedwait()
|
|
|
|
* sem_wait()
|
|
|
|
*
|
|
|
|
* A cancellation point may also occur when a thread is
|
|
|
|
* executing the following functions:
|
|
|
|
* pthread_rwlock_rdlock()
|
|
|
|
* pthread_rwlock_timedrdlock()
|
|
|
|
* pthread_rwlock_timedwrlock()
|
|
|
|
* pthread_rwlock_wrlock()
|
|
|
|
*
|
|
|
|
* The pthread_cancel(), pthread_setcancelstate(), and pthread_setcanceltype()
|
|
|
|
* functions are defined to be async-cancel safe.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int pthread_setcancelstate(int state, int *oldstate)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
if (rt_thread_self() == NULL) return EINVAL;
|
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* get pthread data from pthread_data of thread */
|
|
|
|
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
2013-06-26 23:18:30 +08:00
|
|
|
RT_ASSERT(ptd != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if ((state == PTHREAD_CANCEL_ENABLE) || (state == PTHREAD_CANCEL_DISABLE))
|
|
|
|
{
|
|
|
|
if (oldstate)
|
|
|
|
*oldstate = ptd->cancelstate;
|
|
|
|
ptd->cancelstate = state;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return EINVAL;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_setcancelstate);
|
|
|
|
|
|
|
|
int pthread_setcanceltype(int type, int *oldtype)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
if (rt_thread_self() == NULL) return EINVAL;
|
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* get pthread data from pthread_data of thread */
|
|
|
|
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
2013-06-26 23:18:30 +08:00
|
|
|
RT_ASSERT(ptd != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2018-02-07 19:55:26 +08:00
|
|
|
if ((type != PTHREAD_CANCEL_DEFERRED) && (type != PTHREAD_CANCEL_ASYNCHRONOUS))
|
2013-06-26 23:18:30 +08:00
|
|
|
return EINVAL;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if (oldtype)
|
|
|
|
*oldtype = ptd->canceltype;
|
|
|
|
ptd->canceltype = type;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_setcanceltype);
|
|
|
|
|
|
|
|
void pthread_testcancel(void)
|
|
|
|
{
|
2018-02-07 19:55:26 +08:00
|
|
|
int cancel = 0;
|
|
|
|
_pthread_data_t *ptd;
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
if (rt_thread_self() == NULL) return;
|
|
|
|
|
2022-08-08 11:16:17 +08:00
|
|
|
/* get pthread data from pthread_data of thread */
|
|
|
|
ptd = (_pthread_data_t *)rt_thread_self()->pthread_data;
|
2013-06-26 23:18:30 +08:00
|
|
|
RT_ASSERT(ptd != RT_NULL);
|
2013-01-08 22:40:58 +08:00
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
|
|
|
|
cancel = ptd->canceled;
|
|
|
|
if (cancel)
|
2018-02-07 19:55:26 +08:00
|
|
|
pthread_exit((void *)PTHREAD_CANCELED);
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_testcancel);
|
|
|
|
|
|
|
|
int pthread_cancel(pthread_t thread)
|
|
|
|
{
|
2013-06-26 23:18:30 +08:00
|
|
|
_pthread_data_t *ptd;
|
2022-05-10 21:51:30 +08:00
|
|
|
_pthread_cleanup_t *cleanup;
|
|
|
|
rt_thread_t tid;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
|
|
|
/* get posix thread data */
|
|
|
|
ptd = _pthread_get_data(thread);
|
2021-03-31 09:22:47 +08:00
|
|
|
if (ptd == RT_NULL)
|
|
|
|
{
|
|
|
|
return EINVAL;
|
|
|
|
}
|
2022-05-10 21:51:30 +08:00
|
|
|
tid = ptd->tid;
|
2013-06-26 23:18:30 +08:00
|
|
|
|
2019-05-12 17:56:11 +08:00
|
|
|
/* cancel self */
|
|
|
|
if (ptd->tid == rt_thread_self())
|
|
|
|
return 0;
|
|
|
|
|
2013-06-26 23:18:30 +08:00
|
|
|
/* set canceled */
|
|
|
|
if (ptd->cancelstate == PTHREAD_CANCEL_ENABLE)
|
|
|
|
{
|
|
|
|
ptd->canceled = 1;
|
|
|
|
if (ptd->canceltype == PTHREAD_CANCEL_ASYNCHRONOUS)
|
|
|
|
{
|
|
|
|
/*
|
2022-05-10 21:51:30 +08:00
|
|
|
* When use pthread_cancel to exit.
|
|
|
|
* invoke pushed cleanup
|
|
|
|
*/
|
|
|
|
while (ptd->cleanup != RT_NULL)
|
|
|
|
{
|
|
|
|
cleanup = ptd->cleanup;
|
|
|
|
ptd->cleanup = cleanup->next;
|
|
|
|
|
|
|
|
cleanup->cleanup_func(cleanup->parameter);
|
|
|
|
/* release this cleanup function */
|
|
|
|
rt_free(cleanup);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* According to "detachstate" to whether or not to recycle resource immediately */
|
|
|
|
if (ptd->attr.detachstate == PTHREAD_CREATE_JOINABLE)
|
|
|
|
{
|
|
|
|
/* set value */
|
|
|
|
rt_sem_release(ptd->joinable_sem);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* release pthread resource */
|
|
|
|
_pthread_data_destroy(ptd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* second: detach thread.
|
2013-06-26 23:18:30 +08:00
|
|
|
* this thread will be removed from scheduler list
|
|
|
|
* and because there is a cleanup function in the
|
|
|
|
* thread (pthread_cleanup), it will move to defunct
|
|
|
|
* thread list and wait for handling in idle thread.
|
|
|
|
*/
|
2022-05-10 21:51:30 +08:00
|
|
|
rt_thread_detach(tid);
|
2013-06-26 23:18:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2013-01-08 22:40:58 +08:00
|
|
|
}
|
|
|
|
RTM_EXPORT(pthread_cancel);
|
2021-09-03 11:38:48 +08:00
|
|
|
|