From 47304e72541ae63be9d8503cec799b1f6bec2d51 Mon Sep 17 00:00:00 2001 From: anjiahao Date: Fri, 1 Apr 2022 11:53:02 +0800 Subject: [PATCH] sched/pthread:need check pthread is DETACHED pthread_join need check thread is DETACHED, Whether to wait according to the result.And, if a thread is DETACHED,it will not set a new attr. Signed-off-by: anjiahao --- sched/pthread/pthread_detach.c | 15 +++++++++------ sched/pthread/pthread_join.c | 7 +++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/sched/pthread/pthread_detach.c b/sched/pthread/pthread_detach.c index 6b68a3968e..aa53973873 100644 --- a/sched/pthread/pthread_detach.c +++ b/sched/pthread/pthread_detach.c @@ -65,7 +65,7 @@ int pthread_detach(pthread_t thread) FAR struct tcb_s *rtcb = this_task(); FAR struct task_group_s *group = rtcb->group; FAR struct join_s *pjoin; - int ret; + int ret = OK; sinfo("Thread=%d group=%p\n", thread, group); DEBUGASSERT(group); @@ -96,12 +96,15 @@ int pthread_detach(pthread_t thread) * thread exits */ - pjoin->detached = true; + if (pjoin->detached) + { + ret = EINVAL; + } + else + { + pjoin->detached = true; + } } - - /* Either case is successful */ - - ret = OK; } pthread_sem_give(&group->tg_joinsem); diff --git a/sched/pthread/pthread_join.c b/sched/pthread/pthread_join.c index f200461b3e..86be6f488c 100644 --- a/sched/pthread/pthread_join.c +++ b/sched/pthread/pthread_join.c @@ -138,6 +138,13 @@ int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value) } else { + if (pjoin->detached) + { + pthread_sem_give(&group->tg_joinsem); + leave_cancellation_point(); + return EINVAL; + } + /* NOTE: sched_lock() is not enough for SMP * because another CPU would continue the pthread and exit * sequences so need to protect it with a critical section