From a191d9bc3c194cfdc52bfd7feec6b00539605826 Mon Sep 17 00:00:00 2001 From: anjiahao Date: Mon, 11 Apr 2022 17:54:04 +0800 Subject: [PATCH] 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 --- sched/pthread/pthread.h | 1 - sched/pthread/pthread_create.c | 61 +++++++++------------------------- 2 files changed, 15 insertions(+), 47 deletions(-) diff --git a/sched/pthread/pthread.h b/sched/pthread/pthread.h index 8a3317a5f7..b2db247d03 100644 --- a/sched/pthread/pthread.h +++ b/sched/pthread/pthread.h @@ -51,7 +51,6 @@ struct join_s { FAR struct join_s *next; /* Implements link list */ uint8_t crefs; /* Reference count */ - bool started; /* true: pthread started. */ bool detached; /* true: pthread_detached'ed */ bool terminated; /* true: detach'ed+exit'ed */ pthread_t thread; /* Includes pid */ diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index ea73f11b68..95bdbf97db 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -151,21 +151,9 @@ static inline void pthread_addjoininfo(FAR struct task_group_s *group, static void pthread_start(void) { 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; - DEBUGASSERT(group != NULL && 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); + DEBUGASSERT(pjoin != NULL); /* The priority of this thread may have been boosted to avoid priority * 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; 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); - if (ret == OK) - { - ret = nxsem_init(&pjoin->exit_sem, 0, 0); - } + ret = nxsem_init(&pjoin->exit_sem, 0, 0); if (ret < 0) { 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. */ - if (ret == OK) - { - ret = nxsem_set_protocol(&pjoin->data_sem, SEM_PRIO_NONE); + if (ret == OK) + { + ret = nxsem_set_protocol(&pjoin->exit_sem, SEM_PRIO_NONE); + } - if (ret == OK) - { - ret = nxsem_set_protocol(&pjoin->exit_sem, SEM_PRIO_NONE); - } - - if (ret < 0) - { - ret = -ret; - } - } + if (ret < 0) + { + ret = -ret; + } /* 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 @@ -590,14 +569,11 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, sched_lock(); 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); - /* 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 */ if (thread) @@ -605,19 +581,12 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, *thread = (pthread_t)pid; } - if (!pjoin->started) - { - ret = EINVAL; - } - sched_unlock(); - nxsem_destroy(&pjoin->data_sem); } else { sched_unlock(); dq_rem((FAR dq_entry_t *)ptcb, (FAR dq_queue_t *)&g_inactivetasks); - nxsem_destroy(&pjoin->data_sem); nxsem_destroy(&pjoin->exit_sem); errcode = EIO;