2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/pthread/pthread_completejoin.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <sys/types.h>
|
2009-12-14 22:15:18 +01:00
|
|
|
#include <stdbool.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2009-12-14 22:15:18 +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
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Private Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_notifywaiters
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* Notify all other threads waiting in phread join for this thread's
|
|
|
|
* exit data. This must be done by the child at child thread
|
|
|
|
* destruction time.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
static bool pthread_notifywaiters(FAR struct join_s *pjoin)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
int ntasks_waiting;
|
|
|
|
int status;
|
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sinfo("pjoin=0x%p\n", pjoin);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Are any tasks waiting for our exit value? */
|
|
|
|
|
2020-05-17 15:56:21 +02:00
|
|
|
status = nxsem_get_value(&pjoin->exit_sem, &ntasks_waiting);
|
2007-02-18 00:21:28 +01:00
|
|
|
if (status == OK && ntasks_waiting < 0)
|
|
|
|
{
|
|
|
|
/* Set the data semaphore so that this thread will be
|
|
|
|
* awakened when all waiting tasks receive the data
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_init(&pjoin->data_sem, 0, (ntasks_waiting + 1));
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Post the semaphore to restart each thread that is waiting
|
|
|
|
* on the semaphore
|
|
|
|
*/
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2017-05-29 15:05:06 +02:00
|
|
|
status = pthread_sem_give(&pjoin->exit_sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
if (status == OK)
|
|
|
|
{
|
2020-05-17 15:56:21 +02:00
|
|
|
status = nxsem_get_value(&pjoin->exit_sem, &ntasks_waiting);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ntasks_waiting < 0 && status == OK);
|
|
|
|
|
|
|
|
/* Now wait for all these restarted tasks to obtain the return
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_take(&pjoin->data_sem, NULL, false);
|
2009-12-14 22:15:18 +01:00
|
|
|
return true;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
return false;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2013-02-03 17:43:58 +01:00
|
|
|
* Name: pthread_removejoininfo
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Remove a join structure from the local data set.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2013-02-03 17:43:58 +01:00
|
|
|
* pid
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2013-02-03 17:43:58 +01:00
|
|
|
* None.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller has provided protection from re-entrancy.
|
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2013-02-03 17:43:58 +01:00
|
|
|
|
|
|
|
static void pthread_removejoininfo(FAR struct task_group_s *group,
|
|
|
|
pid_t pid)
|
|
|
|
{
|
|
|
|
FAR struct join_s *prev;
|
|
|
|
FAR struct join_s *join;
|
|
|
|
|
|
|
|
/* Find the entry with the matching pid */
|
|
|
|
|
|
|
|
for (prev = NULL, join = group->tg_joinhead;
|
|
|
|
(join && (pid_t)join->thread != pid);
|
|
|
|
prev = join, join = join->next);
|
|
|
|
|
|
|
|
/* Remove it from the data set. */
|
|
|
|
|
|
|
|
/* First check if this is the entry at the head of the list. */
|
|
|
|
|
|
|
|
if (join)
|
|
|
|
{
|
|
|
|
if (!prev)
|
|
|
|
{
|
|
|
|
/* Check if this is the only entry in the list */
|
|
|
|
|
|
|
|
if (!join->next)
|
|
|
|
{
|
|
|
|
group->tg_joinhead = NULL;
|
|
|
|
group->tg_jointail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, remove it from the head of the list */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
group->tg_joinhead = join->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is not at the head of the list, check if it is at the tail. */
|
|
|
|
|
|
|
|
else if (!join->next)
|
|
|
|
{
|
|
|
|
group->tg_jointail = prev;
|
|
|
|
prev->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No, remove it from the middle of the list. */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prev->next = join->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_completejoin
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* A thread has been terminated -- either by returning, calling
|
|
|
|
* pthread_exit(), or through pthread_cancel(). In any event, we must
|
|
|
|
* complete any pending join events.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2007-02-18 00:21:28 +01:00
|
|
|
* exit_value
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2012-07-14 21:30:31 +02:00
|
|
|
* OK unless there is no join information associated with the pid.
|
|
|
|
* This could happen, for example, if a task started with task_create()
|
|
|
|
* calls pthread_exit().
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-02-27 22:17:21 +01:00
|
|
|
int pthread_completejoin(pid_t pid, FAR void *exit_value)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-02-03 17:43:58 +01:00
|
|
|
FAR struct task_group_s *group = task_getgroup(pid);
|
|
|
|
FAR struct join_s *pjoin;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
sinfo("pid=%d exit_value=%p group=%p\n", pid, exit_value, group);
|
2013-02-03 17:43:58 +01:00
|
|
|
DEBUGASSERT(group);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* First, find thread's structure in the private data set. */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_take(&group->tg_joinsem, NULL, false);
|
2013-02-03 17:43:58 +01:00
|
|
|
pjoin = pthread_findjoininfo(group, pid);
|
2007-02-18 00:21:28 +01:00
|
|
|
if (!pjoin)
|
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
serr("ERROR: Could not find join info, pid=%d\n", pid);
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&group->tg_joinsem);
|
2007-02-18 00:21:28 +01:00
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-14 22:15:18 +01:00
|
|
|
bool waiters;
|
2007-03-30 02:49:11 +02:00
|
|
|
|
|
|
|
/* Save the return exit value in the thread structure. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-12-14 22:15:18 +01:00
|
|
|
pjoin->terminated = true;
|
2007-03-30 02:49:11 +02:00
|
|
|
pjoin->exit_value = exit_value;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
/* Notify waiters of the availability of the exit value */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
waiters = pthread_notifywaiters(pjoin);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
/* If there are no waiters and if the thread is marked as detached.
|
|
|
|
* then discard the join information now. Otherwise, the pthread
|
|
|
|
* join logic will call pthread_destroyjoin() when all of the threads
|
|
|
|
* have sampled the exit value.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
if (!waiters && pjoin->detached)
|
|
|
|
{
|
2017-08-15 01:19:27 +02:00
|
|
|
pthread_destroyjoin(group, pjoin);
|
2007-03-30 02:49:11 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
/* Giving the following semaphore will allow the waiters
|
|
|
|
* to call pthread_destroyjoin.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_sem_give(&group->tg_joinsem);
|
2007-03-30 02:49:11 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
return OK;
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: pthread_destroyjoin
|
2007-03-30 02:49:11 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This is called from pthread_completejoin if the join info was
|
|
|
|
* detached or from pthread_join when the last waiting thread has
|
|
|
|
* received the thread exit info.
|
2007-03-30 02:49:11 +02:00
|
|
|
*
|
2012-07-14 21:30:31 +02:00
|
|
|
* Or it may never be called if the join info was never detached or if
|
|
|
|
* no thread ever calls pthread_join. In case, there is a memory leak!
|
2007-03-30 02:49:11 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2013-02-03 17:43:58 +01:00
|
|
|
* The caller holds tg_joinsem
|
2007-03-30 02:49:11 +02:00
|
|
|
*
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-03-30 02:49:11 +02:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
void pthread_destroyjoin(FAR struct task_group_s *group,
|
|
|
|
FAR struct join_s *pjoin)
|
2007-03-30 02:49:11 +02:00
|
|
|
{
|
2016-06-12 00:42:42 +02:00
|
|
|
sinfo("pjoin=0x%p\n", pjoin);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
/* Remove the join info from the set of joins */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2013-02-03 17:43:58 +01:00
|
|
|
pthread_removejoininfo(group, (pid_t)pjoin->thread);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
/* Destroy its semaphores */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
nxsem_destroy(&pjoin->data_sem);
|
|
|
|
nxsem_destroy(&pjoin->exit_sem);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-30 02:49:11 +02:00
|
|
|
/* And deallocate the pjoin structure */
|
|
|
|
|
2020-04-07 16:31:47 +02:00
|
|
|
kmm_free(pjoin);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|