2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* pthread_join.c
|
|
|
|
*
|
2011-12-26 17:24:43 +01:00
|
|
|
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
|
|
|
* 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
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include "os_internal.h"
|
|
|
|
#include "pthread_internal.h"
|
|
|
|
|
2008-01-30 22:59:12 +01:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Definitions
|
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
|
|
|
* Private Type Declarations
|
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
|
|
|
* Global Variables
|
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
|
|
|
* Private Variables
|
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
|
|
|
* Private 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
|
|
|
/****************************************************************************
|
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
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Function: pthread_join
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* A thread can await termination of another thread and
|
|
|
|
* retrieve the return value of the thread.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* thread
|
|
|
|
* pexit_value
|
|
|
|
*
|
|
|
|
* Return Value:
|
|
|
|
* 0 if successful. Otherwise, one of the following error codes:
|
2007-03-30 15:11:19 +02:00
|
|
|
*
|
|
|
|
* EINVAL The value specified by thread does not refer to a
|
|
|
|
* joinable thread.
|
|
|
|
* ESRCH No thread could be found corresponding to that
|
|
|
|
* specified by the 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
|
|
|
{
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR join_t *pjoin;
|
2007-02-18 00:21:28 +01:00
|
|
|
int ret;
|
|
|
|
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("thread=%d\n", thread);
|
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
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)pthread_takesemaphore(&g_join_semaphore);
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
* was detached and has exitted.
|
|
|
|
*/
|
|
|
|
|
2007-02-20 23:39:56 +01:00
|
|
|
pjoin = pthread_findjoininfo((pid_t)thread);
|
2007-02-18 00:21:28 +01:00
|
|
|
if (!pjoin)
|
|
|
|
{
|
|
|
|
/* Determine what kind of error to return */
|
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
FAR _TCB *tcb = sched_gettcb((pthread_t)thread);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("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) */
|
|
|
|
|
|
|
|
if (!tcb)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)pthread_givesemaphore(&g_join_semaphore);
|
|
|
|
}
|
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
|
|
|
{
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("Thread has terminated\n");
|
2007-03-30 15:11:19 +02:00
|
|
|
|
|
|
|
/* Get the thread exit value from the terminated thread. */
|
|
|
|
|
|
|
|
if (pexit_value)
|
|
|
|
{
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("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
|
|
|
|
{
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("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
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
(void)pthread_givesemaphore(&g_join_semaphore);
|
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
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
(void)pthread_takesemaphore(&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)
|
|
|
|
{
|
|
|
|
*pexit_value = pjoin->exit_value;
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("exit_value=0x%p\n", pjoin->exit_value);
|
2007-03-30 15:11:19 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
/* Post the thread's data semaphore so that the exitting thread
|
|
|
|
* will know that we have received the data.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
(void)pthread_givesemaphore(&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
|
|
|
|
2007-03-30 15:11:19 +02:00
|
|
|
(void)pthread_takesemaphore(&g_join_semaphore);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2007-03-30 15:11:19 +02:00
|
|
|
(void)pthread_destroyjoin(pjoin);
|
2007-03-30 02:49:11 +02:00
|
|
|
}
|
|
|
|
(void)pthread_givesemaphore(&g_join_semaphore);
|
2007-03-30 15:11:19 +02:00
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
2007-11-23 23:32:52 +01:00
|
|
|
sdbg("Returning %d\n", ret);
|
2007-02-18 00:21:28 +01:00
|
|
|
return ret;
|
|
|
|
}
|
2007-02-27 22:17:21 +01:00
|
|
|
|