enhance robustness for pthread

This commit is contained in:
jeshrz 2021-03-31 09:22:47 +08:00
parent 69c0156368
commit 7c999e1950
1 changed files with 20 additions and 1 deletions

View File

@ -209,6 +209,8 @@ int pthread_create(pthread_t *pid,
/* get pthread data */ /* get pthread data */
ptd = _pthread_get_data(pth_id); ptd = _pthread_get_data(pth_id);
RT_ASSERT(ptd != RT_NULL);
if (attr != RT_NULL) if (attr != RT_NULL)
{ {
ptd->attr = *attr; ptd->attr = *attr;
@ -299,6 +301,12 @@ int pthread_detach(pthread_t thread)
{ {
int ret = 0; int ret = 0;
_pthread_data_t *ptd = _pthread_get_data(thread); _pthread_data_t *ptd = _pthread_get_data(thread);
if (ptd == RT_NULL)
{
/* invalid pthread id */
ret = EINVAL;
goto __exit;
}
rt_enter_critical(); rt_enter_critical();
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED) if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
@ -360,6 +368,12 @@ int pthread_join(pthread_t thread, void **value_ptr)
rt_err_t result; rt_err_t result;
ptd = _pthread_get_data(thread); ptd = _pthread_get_data(thread);
if (ptd == RT_NULL)
{
return EINVAL; /* invalid pthread id */
}
if (ptd && ptd->tid == rt_thread_self()) if (ptd && ptd->tid == rt_thread_self())
{ {
/* join self */ /* join self */
@ -367,7 +381,9 @@ int pthread_join(pthread_t thread, void **value_ptr)
} }
if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED) if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED)
{
return EINVAL; /* join on a detached pthread */ return EINVAL; /* join on a detached pthread */
}
result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER); result = rt_sem_take(ptd->joinable_sem, RT_WAITING_FOREVER);
if (result == RT_EOK) if (result == RT_EOK)
@ -662,7 +678,10 @@ int pthread_cancel(pthread_t thread)
/* get posix thread data */ /* get posix thread data */
ptd = _pthread_get_data(thread); ptd = _pthread_get_data(thread);
RT_ASSERT(ptd != RT_NULL); if (ptd == RT_NULL)
{
return EINVAL;
}
/* cancel self */ /* cancel self */
if (ptd->tid == rt_thread_self()) if (ptd->tid == rt_thread_self())