2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:21:48 +02:00
|
|
|
* sched/pthread/pthread_cancel.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
|
|
|
*
|
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 19:39:29 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <errno.h>
|
2009-12-14 19:39:29 +01:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2016-12-09 19:01:18 +01:00
|
|
|
#include "task/task.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
|
|
|
* Public Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
int pthread_cancel(pthread_t thread)
|
|
|
|
{
|
2020-06-03 20:38:39 +02:00
|
|
|
FAR struct tcb_s *tcb;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* First, make sure that the handle references a valid thread */
|
|
|
|
|
2016-12-10 21:39:19 +01:00
|
|
|
if (thread == 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2017-05-11 21:15:31 +02:00
|
|
|
/* pid == 0 is the IDLE task (in a single CPU configuration). Callers
|
|
|
|
* cannot cancel the IDLE task.
|
2007-02-18 00:21:28 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
return ESRCH;
|
|
|
|
}
|
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
tcb = nxsched_get_tcb((pid_t)thread);
|
2016-12-08 16:27:13 +01:00
|
|
|
if (tcb == NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2013-02-03 17:43:58 +01:00
|
|
|
/* The pid does not correspond to any known thread. The thread
|
|
|
|
* has probably already exited.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
return ESRCH;
|
|
|
|
}
|
|
|
|
|
2016-12-10 21:39:19 +01:00
|
|
|
/* Only pthreads should use this interface */
|
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
DEBUGASSERT((tcb->flags & TCB_FLAG_TTYPE_MASK) ==
|
2020-05-09 17:05:29 +02:00
|
|
|
TCB_FLAG_TTYPE_PTHREAD);
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2020-06-06 18:41:47 +02:00
|
|
|
/* Notify the target if the non-cancelable or deferred cancellation set */
|
2016-12-09 19:01:18 +01:00
|
|
|
|
2020-06-06 18:41:47 +02:00
|
|
|
if (nxnotify_cancellation(tcb))
|
2016-12-09 19:01:18 +01:00
|
|
|
{
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, perform the asyncrhonous cancellation */
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Check to see if the ID refers to ourselves.. this would be the
|
|
|
|
* same as pthread_exit(PTHREAD_CANCELED).
|
|
|
|
*/
|
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
if (tcb == this_task())
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
|
2016-12-08 16:27:13 +01:00
|
|
|
#ifdef CONFIG_PTHREAD_CLEANUP
|
2016-12-08 17:24:40 +01:00
|
|
|
/* Perform any stack pthread clean-up callbacks.
|
|
|
|
*
|
|
|
|
* REVISIT: In this case, the clean-up callback will execute on the
|
|
|
|
* thread of the caller of pthread cancel, not on the thread of
|
2017-04-10 22:56:23 +02:00
|
|
|
* the thread-to-be-canceled. This is a problem when deferred
|
|
|
|
* cancellation is not supported because, for example, the clean-up
|
|
|
|
* function will be unable to unlock its own mutexes.
|
2016-12-08 17:24:40 +01:00
|
|
|
*/
|
2016-12-08 16:27:13 +01:00
|
|
|
|
2020-06-03 20:38:39 +02:00
|
|
|
pthread_cleanup_popall(tcb);
|
2016-12-08 16:27:13 +01:00
|
|
|
#endif
|
|
|
|
|
2007-02-18 00:21:28 +01:00
|
|
|
/* Complete pending join operations */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
pthread_completejoin((pid_t)thread, PTHREAD_CANCELED);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-04-10 17:51:03 +02:00
|
|
|
#ifndef CONFIG_PTHREAD_MUTEX_UNSAFE
|
|
|
|
/* Recover any mutexes still held by the canceled thread */
|
|
|
|
|
|
|
|
pthread_mutex_inconsistent(tcb);
|
|
|
|
#endif
|
|
|
|
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
/* Then let nxtask_terminate do the real work */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
This commit renames all internal OS functions defined under sched/task so that they begin with the prefix. For example, nxtask_exit() vs. task_exit().
Squashed commit of the following:
Trivial, cosmetic
sched/, arch/, and include: Rename task_vforkstart() as nxtask_vforkstart()
sched/, arch/, and include: Rename task_vforkabort() as nxtask_vforkabort()
sched/, arch/, and include: Rename task_vforksetup() as nxtask_vfork_setup()
sched/: Rename notify_cancellation() as nxnotify_cancellation()
sched/: Rename task_recover() to nxtask_recover()
sched/task, sched/pthread/, Documentation/: Rename task_argsetup() and task_terminate() to nxtask_argsetup() and nxtask_terminate(), respectively.
sched/task: Rename task_schedsetup() to nxtask_schedsetup()
sched/ (plus some binfmt/, include/, and arch/): Rename task_start() and task_starthook() to nxtask_start() and nxtask_starthook().
arch/ and sched/: Rename task_exit() and task_exithook() to nxtask_exit() and nxtask_exithook(), respectively.
sched/task: Rename all internal, static, functions to begin with the nx prefix.
2019-02-04 20:42:51 +01:00
|
|
|
return nxtask_terminate((pid_t)thread, false);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|