sched/pthread_create:fix bug,delete data_sem

if a pthread set attr is detach,and when call pthread_create,
new thread exit quikly,new thread's tcb be free,then pthread_create
use new thread's tcb will crash.
Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao 2022-04-11 17:54:04 +08:00 committed by Xiang Xiao
parent a5c64adbe4
commit a191d9bc3c
2 changed files with 15 additions and 47 deletions

View File

@ -51,7 +51,6 @@ struct join_s
{ {
FAR struct join_s *next; /* Implements link list */ FAR struct join_s *next; /* Implements link list */
uint8_t crefs; /* Reference count */ uint8_t crefs; /* Reference count */
bool started; /* true: pthread started. */
bool detached; /* true: pthread_detached'ed */ bool detached; /* true: pthread_detached'ed */
bool terminated; /* true: detach'ed+exit'ed */ bool terminated; /* true: detach'ed+exit'ed */
pthread_t thread; /* Includes pid */ pthread_t thread; /* Includes pid */

View File

@ -151,21 +151,9 @@ static inline void pthread_addjoininfo(FAR struct task_group_s *group,
static void pthread_start(void) static void pthread_start(void)
{ {
FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)this_task(); FAR struct pthread_tcb_s *ptcb = (FAR struct pthread_tcb_s *)this_task();
FAR struct task_group_s *group = ptcb->cmn.group;
FAR struct join_s *pjoin = (FAR struct join_s *)ptcb->joininfo; FAR struct join_s *pjoin = (FAR struct join_s *)ptcb->joininfo;
DEBUGASSERT(group != NULL && pjoin != NULL); DEBUGASSERT(pjoin != NULL);
/* Successfully spawned, add the pjoin to our data set. */
pthread_sem_take(&group->tg_joinsem, NULL, false);
pthread_addjoininfo(group, pjoin);
pthread_sem_give(&group->tg_joinsem);
/* Report to the spawner that we successfully started. */
pjoin->started = true;
pthread_sem_give(&pjoin->data_sem);
/* The priority of this thread may have been boosted to avoid priority /* The priority of this thread may have been boosted to avoid priority
* inversion problems. If that is the case, then drop to the correct * inversion problems. If that is the case, then drop to the correct
@ -529,37 +517,28 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
pid = ptcb->cmn.pid; pid = ptcb->cmn.pid;
pjoin->thread = (pthread_t)pid; pjoin->thread = (pthread_t)pid;
/* Initialize the semaphores in the join structure to zero. */ /* Initialize the semaphore in the join structure to zero. */
ret = nxsem_init(&pjoin->data_sem, 0, 0); ret = nxsem_init(&pjoin->exit_sem, 0, 0);
if (ret == OK)
{
ret = nxsem_init(&pjoin->exit_sem, 0, 0);
}
if (ret < 0) if (ret < 0)
{ {
ret = -ret; ret = -ret;
} }
/* Thse semaphores are used for signaling and, hence, should not have /* Thse semaphore are used for signaling and, hence, should not have
* priority inheritance enabled. * priority inheritance enabled.
*/ */
if (ret == OK) if (ret == OK)
{ {
ret = nxsem_set_protocol(&pjoin->data_sem, SEM_PRIO_NONE); ret = nxsem_set_protocol(&pjoin->exit_sem, SEM_PRIO_NONE);
}
if (ret == OK) if (ret < 0)
{ {
ret = nxsem_set_protocol(&pjoin->exit_sem, SEM_PRIO_NONE); ret = -ret;
} }
if (ret < 0)
{
ret = -ret;
}
}
/* If the priority of the new pthread is lower than the priority of the /* If the priority of the new pthread is lower than the priority of the
* parent thread, then starting the pthread could result in both the * parent thread, then starting the pthread could result in both the
@ -590,14 +569,11 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
sched_lock(); sched_lock();
if (ret == OK) if (ret == OK)
{ {
pthread_sem_take(&ptcb->cmn.group->tg_joinsem, NULL, false);
pthread_addjoininfo(ptcb->cmn.group, pjoin);
pthread_sem_give(&ptcb->cmn.group->tg_joinsem);
nxtask_activate((FAR struct tcb_s *)ptcb); nxtask_activate((FAR struct tcb_s *)ptcb);
/* Wait for the task to actually get running and to register
* its join structure.
*/
pthread_sem_take(&pjoin->data_sem, NULL, false);
/* Return the thread information to the caller */ /* Return the thread information to the caller */
if (thread) if (thread)
@ -605,19 +581,12 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
*thread = (pthread_t)pid; *thread = (pthread_t)pid;
} }
if (!pjoin->started)
{
ret = EINVAL;
}
sched_unlock(); sched_unlock();
nxsem_destroy(&pjoin->data_sem);
} }
else else
{ {
sched_unlock(); sched_unlock();
dq_rem((FAR dq_entry_t *)ptcb, (FAR dq_queue_t *)&g_inactivetasks); dq_rem((FAR dq_entry_t *)ptcb, (FAR dq_queue_t *)&g_inactivetasks);
nxsem_destroy(&pjoin->data_sem);
nxsem_destroy(&pjoin->exit_sem); nxsem_destroy(&pjoin->exit_sem);
errcode = EIO; errcode = EIO;