2016-12-09 22:13:28 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* sched/task/task_cancelpt.c
|
|
|
|
*
|
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
|
2016-12-09 22:13:28 +08:00
|
|
|
*
|
2021-02-08 23:33:58 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-12-09 22:13: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.
|
2016-12-09 22:13:28 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Cancellation Points.
|
|
|
|
*
|
|
|
|
* Cancellation points shall occur when a thread is executing the following
|
|
|
|
* functions:
|
|
|
|
*
|
|
|
|
* accept() mq_timedsend() putpmsg() sigtimedwait()
|
|
|
|
* aio_suspend() msgrcv() pwrite() sigwait()
|
|
|
|
* clock_nanosleep() msgsnd() read() sigwaitinfo()
|
|
|
|
* close() msync() readv() sleep()
|
|
|
|
* connect() nanosleep() recv() system()
|
|
|
|
* creat() open() recvfrom() tcdrain()
|
|
|
|
* fcntl() pause() recvmsg() usleep()
|
|
|
|
* fdatasync() poll() select() wait()
|
|
|
|
* fsync() pread() sem_timedwait() waitid()
|
|
|
|
* getmsg() pselect() sem_wait() waitpid()
|
|
|
|
* getpmsg() pthread_cond_timedwait() send() write()
|
|
|
|
* lockf() pthread_cond_wait() sendmsg() writev()
|
|
|
|
* mq_receive() pthread_join() sendto()
|
|
|
|
* mq_send() pthread_testcancel() sigpause()
|
|
|
|
* mq_timedreceive() putmsg() sigsuspend()
|
|
|
|
*
|
|
|
|
* Each of the above function must call enter_cancellation_point() on entry
|
2020-03-08 20:51:33 +08:00
|
|
|
* in order to establish the cancellation point and
|
|
|
|
* leave_cancellation_point() on exit. These functions are described below.
|
2016-12-09 22:13:28 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sched.h>
|
2021-05-18 14:59:14 +08:00
|
|
|
#include <assert.h>
|
2016-12-09 23:44:23 +08:00
|
|
|
#include <errno.h>
|
2016-12-09 22:13:28 +08:00
|
|
|
|
2016-12-09 23:44:23 +08:00
|
|
|
#include <nuttx/irq.h>
|
2016-12-10 23:08:26 +08:00
|
|
|
#include <nuttx/cancelpt.h>
|
2023-11-10 18:56:10 +08:00
|
|
|
#include <nuttx/tls.h>
|
2016-12-09 22:13:28 +08:00
|
|
|
|
2016-12-09 23:44:23 +08:00
|
|
|
#include "sched/sched.h"
|
|
|
|
#include "semaphore/semaphore.h"
|
2017-10-12 22:55:19 +08:00
|
|
|
#include "signal/signal.h"
|
2016-12-09 23:44:23 +08:00
|
|
|
#include "mqueue/mqueue.h"
|
2016-12-09 22:13:28 +08:00
|
|
|
#include "task/task.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
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-05 03:42:51 +08:00
|
|
|
* Name: nxnotify_cancellation
|
2016-12-09 22:13:28 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called by task_delete() or pthread_cancel() if the cancellation occurs
|
|
|
|
* while we the thread is within the cancellation point. This logic
|
|
|
|
* behaves much like sending a signal: It will cause waiting threads
|
|
|
|
* to wake up and terminated with ECANCELED. A call to
|
2020-02-23 16:50:23 +08:00
|
|
|
* leave_cancellation_point() would then follow, causing the thread to
|
2016-12-09 22:13:28 +08:00
|
|
|
* exit.
|
|
|
|
*
|
2020-06-07 00:41:47 +08:00
|
|
|
* Returned Value:
|
|
|
|
* Indicate whether the notification delivery to the target
|
|
|
|
*
|
2016-12-09 22:13:28 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-06-07 00:41:47 +08:00
|
|
|
bool nxnotify_cancellation(FAR struct tcb_s *tcb)
|
2016-12-09 22:13:28 +08:00
|
|
|
{
|
2023-11-21 20:56:36 +08:00
|
|
|
FAR struct tls_info_s *tls = nxsched_get_tls(tcb);
|
2016-12-09 23:44:23 +08:00
|
|
|
irqstate_t flags;
|
2023-02-16 16:13:27 +08:00
|
|
|
bool ret = false;
|
2016-12-09 23:44:23 +08:00
|
|
|
|
|
|
|
/* We need perform the following operations from within a critical section
|
|
|
|
* because it can compete with interrupt level activity.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* We only notify the cancellation if (1) the thread has not disabled
|
2020-02-23 16:50:23 +08:00
|
|
|
* cancellation, (2) the thread uses the deferred cancellation mode,
|
2016-12-09 23:44:23 +08:00
|
|
|
* (3) the thread is waiting within a cancellation point.
|
|
|
|
*/
|
|
|
|
|
2020-06-07 00:41:47 +08:00
|
|
|
/* Check to see if this task has the non-cancelable bit set. */
|
|
|
|
|
2023-11-10 18:56:10 +08:00
|
|
|
if ((tcb->flags & TCB_FLAG_FORCED_CANCEL) == 0 &&
|
|
|
|
(tls->tl_cpstate & CANCEL_FLAG_NONCANCELABLE) != 0)
|
2016-12-09 23:44:23 +08:00
|
|
|
{
|
2020-06-07 00:41:47 +08:00
|
|
|
/* Then we cannot cancel the thread now. Here is how this is
|
|
|
|
* supposed to work:
|
|
|
|
*
|
|
|
|
* "When cancellability is disabled, all cancels are held pending
|
|
|
|
* in the target thread until the thread changes the cancellability.
|
|
|
|
* When cancellability is deferred, all cancels are held pending in
|
|
|
|
* the target thread until the thread changes the cancellability,
|
|
|
|
* calls a function which is a cancellation point or calls
|
|
|
|
* pthread_testcancel(), thus creating a cancellation point. When
|
|
|
|
* cancellability is asynchronous, all cancels are acted upon
|
|
|
|
* immediately, interrupting the thread with its processing."
|
2016-12-09 23:44:23 +08:00
|
|
|
*/
|
|
|
|
|
2023-11-10 18:56:10 +08:00
|
|
|
tls->tl_cpstate |= CANCEL_FLAG_CANCEL_PENDING;
|
2020-06-07 00:41:47 +08:00
|
|
|
leave_critical_section(flags);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* Check if this task supports deferred cancellation */
|
2016-12-09 23:44:23 +08:00
|
|
|
|
2023-11-10 18:56:10 +08:00
|
|
|
if ((tls->tl_cpstate & CANCEL_FLAG_CANCEL_ASYNC) == 0)
|
2020-06-07 00:41:47 +08:00
|
|
|
{
|
2023-02-16 16:13:27 +08:00
|
|
|
/* Then we cannot cancel the task asynchronously. */
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
|
|
|
|
/* Mark the cancellation as pending. */
|
2016-12-09 23:44:23 +08:00
|
|
|
|
2023-11-10 18:56:10 +08:00
|
|
|
tls->tl_cpstate |= CANCEL_FLAG_CANCEL_PENDING;
|
2017-10-12 22:55:19 +08:00
|
|
|
|
2020-06-07 00:41:47 +08:00
|
|
|
/* If the task is waiting at a cancellation point, then notify of the
|
|
|
|
* cancellation thereby waking the task up with an ECANCELED error.
|
2017-10-12 22:55:19 +08:00
|
|
|
*/
|
|
|
|
|
2023-11-10 18:56:10 +08:00
|
|
|
if (tls->tl_cpcount > 0)
|
2016-12-09 23:44:23 +08:00
|
|
|
{
|
2020-06-07 00:41:47 +08:00
|
|
|
/* If the thread is blocked waiting for a semaphore, then the
|
|
|
|
* thread must be unblocked to handle the cancellation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (tcb->task_state == TSTATE_WAIT_SEM)
|
|
|
|
{
|
|
|
|
nxsem_wait_irq(tcb, ECANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the thread is blocked waiting on a signal, then the
|
|
|
|
* thread must be unblocked to handle the cancellation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (tcb->task_state == TSTATE_WAIT_SIG)
|
|
|
|
{
|
|
|
|
nxsig_wait_irq(tcb, ECANCELED);
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:20:31 +08:00
|
|
|
#if !defined(CONFIG_DISABLE_MQUEUE) || !defined(CONFIG_DISABLE_MQUEUE_SYSV)
|
2020-06-07 00:41:47 +08:00
|
|
|
/* If the thread is blocked waiting on a message queue, then
|
|
|
|
* the thread must be unblocked to handle the cancellation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (tcb->task_state == TSTATE_WAIT_MQNOTEMPTY ||
|
2022-03-18 22:21:41 +08:00
|
|
|
tcb->task_state == TSTATE_WAIT_MQNOTFULL)
|
2020-06-07 00:41:47 +08:00
|
|
|
{
|
|
|
|
nxmq_wait_irq(tcb, ECANCELED);
|
|
|
|
}
|
2016-12-09 23:44:23 +08:00
|
|
|
#endif
|
2020-06-07 00:41:47 +08:00
|
|
|
}
|
|
|
|
|
2023-02-16 16:13:27 +08:00
|
|
|
#ifdef HAVE_GROUP_MEMBERS
|
|
|
|
else if (tcb->group && (tcb->group->tg_flags & GROUP_FLAG_EXITING))
|
|
|
|
{
|
|
|
|
/* Exit in progress, do asynchronous cancel instead */
|
|
|
|
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-09 23:44:23 +08:00
|
|
|
}
|
2020-06-07 00:41:47 +08:00
|
|
|
#endif
|
2016-12-09 23:44:23 +08:00
|
|
|
|
|
|
|
leave_critical_section(flags);
|
2023-02-16 16:13:27 +08:00
|
|
|
return ret;
|
2016-12-09 22:13:28 +08:00
|
|
|
}
|