* sched/pthread/pthread_cancel.c
*
* SPDX-License-Identifier: Apache-2.0
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/tls.h>
#include "sched/sched.h"
#include "task/task.h"
#include "pthread/pthread.h"
* Public Functions
****************************************************************************/
int pthread_cancel(pthread_t thread)
{
FAR struct tcb_s *tcb;
if (thread == IDLE_PROCESS_ID)
{
* cannot cancel the IDLE task.
*/
return ESRCH;
}
tcb = nxsched_get_tcb(thread);
if (tcb == NULL)
{
* has probably already exited.
*/
return ESRCH;
}
if ((atomic_read(&tcb->flags) & TCB_FLAG_EXIT_PROCESSING) != 0)
{
nxsched_put_tcb(tcb);
return ESRCH;
}
DEBUGASSERT((atomic_read(&tcb->flags) & TCB_FLAG_TTYPE_MASK) ==
TCB_FLAG_TTYPE_PTHREAD);
if (nxnotify_cancellation(tcb))
{
nxsched_put_tcb(tcb);
return OK;
}
* same as pthread_exit(PTHREAD_CANCELED).
*/
if (tcb == this_task())
{
nxsched_put_tcb(tcb);
pthread_exit(PTHREAD_CANCELED);
}
tls_cleanup_popall(tcb->stack_alloc_ptr);
nxsched_put_tcb(tcb);
pthread_completejoin(thread, PTHREAD_CANCELED);
return nxtask_terminate(thread);
}