2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2016-08-07 16:25:30 +02:00
|
|
|
* sched/pthread/pthread_join.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2020-05-09 17:05:29 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
2021-05-18 08:59:14 +02:00
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2016-12-09 23:50:34 +01:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 22:06:42 +02:00
|
|
|
#include "group/group.h"
|
2014-08-08 20:55:02 +02:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_join
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* A thread can await termination of another thread and retrieve the
|
|
|
|
* return value of the thread.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2013-02-03 17:43:58 +01:00
|
|
|
* The caller's task/thread must belong to the same "task group" as the
|
|
|
|
* pthread is (or was) a member of. The thread may or may not still
|
|
|
|
* be running.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* thread
|
|
|
|
* pexit_value
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2007-02-18 00:21:28 +01:00
|
|
|
* 0 if successful. Otherwise, one of the following error codes:
|
2007-03-30 15:11:19 +02:00
|
|
|
*
|
2014-04-27 19:38:24 +02:00
|
|
|
* EINVAL The value specified by thread does not refer to joinable
|
2012-07-14 21:30:31 +02:00
|
|
|
* thread.
|
2014-04-27 19:38:24 +02:00
|
|
|
* ESRCH No thread could be found corresponding to that specified by the
|
2012-07-14 21:30:31 +02:00
|
|
|
* given thread ID.
|
|
|
|
* EDEADLK A deadlock was detected or the value of thread specifies the
|
|
|
|
* calling thread.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2008-01-30 22:59:12 +01:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
int pthread_join(pthread_t thread, FAR pthread_addr_t *pexit_value)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
2013-02-03 17:43:58 +01:00
|
|
|
FAR struct task_group_s *group = rtcb->group;
|
|
|
|
FAR struct join_s *pjoin;
|
2007-02-18 00:21:28 +01:00
|
|
|
int ret;
|
|
|
|
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("thread=%d group=%p\n", thread, group);
|
2013-02-03 17:43:58 +01:00
|
|
|
DEBUGASSERT(group);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
/* pthread_join() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2016-12-09 23:50:34 +01:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* First make sure that this is not an attempt to join to
|
|
|
|
* ourself.
|
|
|
|
*/
|
|
|
|
|
2023-07-07 11:41:46 +02:00
|
|
|
if ((pid_t)thread == nxsched_gettid())
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2007-02-18 00:21:28 +01:00
|
|
|
return EDEADLK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure no other task is mucking with the data structures
|
|
|
|
* while we are performing the following operations. NOTE:
|
|
|
|
* we can be also sure that pthread_exit() will not execute
|
|
|
|
* because it will also attempt to get this semaphore.
|
|
|
|
*/
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&group->tg_joinlock);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Find the join information associated with this thread.
|
|
|
|
* This can fail for one of three reasons: (1) There is no
|
|
|
|
* thread associated with 'thread,' (2) the thread is a task
|
|
|
|
* and does not have join information, or (3) the thread
|
2013-01-25 01:01:08 +01:00
|
|
|
* was detached and has exited.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
2023-05-22 20:26:59 +02:00
|
|
|
ret = pthread_findjoininfo(group, (pid_t)thread, &pjoin);
|
|
|
|
if (ret == OK)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2022-04-01 05:53:02 +02:00
|
|
|
if (pjoin->detached)
|
|
|
|
{
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&group->tg_joinlock);
|
2022-04-01 05:53:02 +02:00
|
|
|
leave_cancellation_point();
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-11-10 03:03:12 +01:00
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
irqstate_t flags = enter_critical_section();
|
|
|
|
#endif
|
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* We found the join info structure. Increment for the reference
|
|
|
|
* to the join structure that we have. This will keep things
|
|
|
|
* stable for we have to do
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
sched_lock();
|
|
|
|
pjoin->crefs++;
|
|
|
|
|
|
|
|
/* Check if the thread is still running. If not, then things are
|
|
|
|
* simpler. There are still race conditions to be concerned with.
|
|
|
|
* For example, there could be multiple threads executing in the
|
|
|
|
* 'else' block below when we enter!
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
if (pjoin->terminated)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Thread has terminated\n");
|
2007-03-30 15:11:19 +02:00
|
|
|
|
|
|
|
/* Get the thread exit value from the terminated thread. */
|
|
|
|
|
|
|
|
if (pexit_value)
|
|
|
|
{
|
2022-06-30 19:34:24 +02:00
|
|
|
sinfo("exit_value=%p\n", pjoin->exit_value);
|
2007-03-30 15:11:19 +02:00
|
|
|
*pexit_value = pjoin->exit_value;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2007-03-30 15:11:19 +02:00
|
|
|
else
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Thread is still running\n");
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* Relinquish the data set semaphore. Since pre-emption is
|
|
|
|
* disabled, we can be certain that no task has the
|
|
|
|
* opportunity to run between the time we relinquish the
|
|
|
|
* join semaphore and the time that we wait on the thread exit
|
|
|
|
* semaphore.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&group->tg_joinlock);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* Take the thread's thread exit semaphore. We will sleep here
|
|
|
|
* until the thread exits. We need to exercise caution because
|
|
|
|
* there could be multiple threads waiting here for the same
|
|
|
|
* pthread to exit.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-05-10 07:45:09 +02:00
|
|
|
nxsem_wait_uninterruptible(&pjoin->exit_sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* The thread has exited! Get the thread exit value */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
if (pexit_value)
|
|
|
|
{
|
2017-08-15 01:19:27 +02:00
|
|
|
*pexit_value = pjoin->exit_value;
|
2022-06-30 19:34:24 +02:00
|
|
|
sinfo("exit_value=%p\n", pjoin->exit_value);
|
2007-03-30 15:11:19 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-04-27 19:38:24 +02:00
|
|
|
/* Post the thread's data semaphore so that the exiting thread
|
2007-03-30 15:11:19 +02:00
|
|
|
* will know that we have received the data.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&pjoin->data_sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* Retake the join semaphore, we need to hold this when
|
|
|
|
* pthread_destroyjoin is called.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&group->tg_joinlock);
|
2007-03-30 15:11:19 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* Pre-emption is okay now. The logic still cannot be re-entered
|
|
|
|
* because we hold the join semaphore
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
|
2020-11-10 03:03:12 +01:00
|
|
|
#ifdef CONFIG_SMP
|
|
|
|
leave_critical_section(flags);
|
|
|
|
#endif
|
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* Release our reference to the join structure and, if the reference
|
|
|
|
* count decrements to zero, deallocate the join structure.
|
|
|
|
*/
|
2007-03-23 17:06:22 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
if (--pjoin->crefs <= 0)
|
|
|
|
{
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_destroyjoin(group, pjoin);
|
2007-03-30 02:49:11 +02:00
|
|
|
}
|
2007-03-30 15:11:19 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2023-05-22 20:26:59 +02:00
|
|
|
nxmutex_unlock(&group->tg_joinlock);
|
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("Returning %d\n", ret);
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|