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
|
|
|
*
|
2016-12-09 23:50:34 +01:00
|
|
|
* Copyright (C) 2007, 2008, 2011, 2013, 2016 Gregory Nutt. All rights reserved.
|
2011-12-26 17:24:43 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-30 22:59:12 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
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>
|
|
|
|
#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.
|
|
|
|
*/
|
|
|
|
|
2007-02-20 23:39:56 +01:00
|
|
|
if ((pid_t)thread == getpid())
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_take(&group->tg_joinsem, NULL, false);
|
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
|
|
|
*/
|
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
pjoin = pthread_findjoininfo(group, (pid_t)thread);
|
2017-10-10 16:43:10 +02:00
|
|
|
if (pjoin == NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* Determine what kind of error to return */
|
|
|
|
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *tcb = sched_gettcb((pthread_t)thread);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
swarn("WARNING: Could not find thread data\n");
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Case (1) or (3) -- we can't tell which. Assume (3) */
|
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
if (tcb == NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
ret = ESRCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The thread is still active but has no join info. In that
|
|
|
|
* case, it must be a task and not a pthread.
|
|
|
|
*/
|
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
else
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&group->tg_joinsem);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2007-03-30 15:11:19 +02:00
|
|
|
else
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
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)
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("exit_value=0x%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
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&group->tg_joinsem);
|
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
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_take(&pjoin->exit_sem, NULL, false);
|
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;
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("exit_value=0x%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
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_take(&group->tg_joinsem, NULL, false);
|
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();
|
|
|
|
|
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
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&group->tg_joinsem);
|
2007-02-18 00:21:28 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|