2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2014-08-09 04:21:48 +08:00
|
|
|
* sched/pthread/pthread_detach.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08: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 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08: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 07:21:28 +08:00
|
|
|
*
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2009-12-15 03:30:18 +08:00
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
2009-12-15 05:15:18 +08:00
|
|
|
#include <stdbool.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
2013-02-04 00:43:58 +08:00
|
|
|
#include <assert.h>
|
2007-02-18 07:21:28 +08:00
|
|
|
#include <debug.h>
|
2009-12-15 03:30:18 +08:00
|
|
|
|
2014-08-09 07:53:55 +08:00
|
|
|
#include "sched/sched.h"
|
2014-08-09 04:06:42 +08:00
|
|
|
#include "group/group.h"
|
2014-08-09 02:55:02 +08:00
|
|
|
#include "pthread/pthread.h"
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Public Functions
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2015-10-03 07:43:18 +08:00
|
|
|
/****************************************************************************
|
2012-07-15 03:30:31 +08:00
|
|
|
* Name: pthread_detach
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-15 03:30:31 +08:00
|
|
|
* A thread object may be "detached" to specify that the return value
|
|
|
|
* and completion status will not be requested.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2013-02-04 00:43:58 +08: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 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2007-02-18 07:21:28 +08:00
|
|
|
* thread
|
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2007-02-18 07:21:28 +08:00
|
|
|
* 0 if successful. Otherwise, an error code.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
2015-10-03 07:43:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
int pthread_detach(pthread_t thread)
|
|
|
|
{
|
2016-02-07 07:44:41 +08:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
2013-02-04 00:43:58 +08:00
|
|
|
FAR struct task_group_s *group = rtcb->group;
|
|
|
|
FAR struct join_s *pjoin;
|
2022-04-01 11:53:02 +08:00
|
|
|
int ret = OK;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2016-06-12 06:42:42 +08:00
|
|
|
sinfo("Thread=%d group=%p\n", thread, group);
|
2013-02-04 00:43:58 +08:00
|
|
|
DEBUGASSERT(group);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
/* Find the entry associated with this pthread. */
|
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_lock(&group->tg_joinlock);
|
2023-05-23 02:26:59 +08:00
|
|
|
ret = pthread_findjoininfo(group, (pid_t)thread, &pjoin);
|
|
|
|
if (ret == OK)
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
|
|
|
/* Has the thread already terminated? */
|
|
|
|
|
|
|
|
if (pjoin->terminated)
|
|
|
|
{
|
|
|
|
/* YES.. just remove the thread entry. */
|
|
|
|
|
2013-02-04 00:43:58 +08:00
|
|
|
pthread_destroyjoin(group, pjoin);
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* NO.. Just mark the thread as detached. It
|
|
|
|
* will be removed and deallocated when the
|
|
|
|
* thread exits
|
|
|
|
*/
|
|
|
|
|
2022-04-01 11:53:02 +08:00
|
|
|
if (pjoin->detached)
|
|
|
|
{
|
|
|
|
ret = EINVAL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pjoin->detached = true;
|
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
}
|
2012-07-15 03:30:31 +08:00
|
|
|
|
2022-09-06 14:18:45 +08:00
|
|
|
nxmutex_unlock(&group->tg_joinlock);
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2016-06-12 06:42:42 +08:00
|
|
|
sinfo("Returning %d\n", ret);
|
2007-02-18 07:21:28 +08:00
|
|
|
return ret;
|
|
|
|
}
|