2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2016-12-10 22:16:46 +01:00
|
|
|
* sched/task/task_setcancelstate.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2016-12-10 22:16:46 +01:00
|
|
|
* Copyright (C) 2007, 2008, 2016 Gregory Nutt. All rights reserved.
|
2012-07-14 21:30:31 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2008-01-30 22:59:12 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-09 15:13:28 +01:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2016-12-10 23:06:14 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
2016-12-09 15:13:28 +01:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2016-12-10 22:16:46 +01:00
|
|
|
#include "task/task.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2016-12-10 22:16:46 +01:00
|
|
|
* Name: task_setcancelstate
|
2016-12-09 14:23:00 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2016-12-10 22:16:46 +01:00
|
|
|
* The task_setcancelstate() function atomically both sets the calling
|
2020-02-23 09:50:23 +01:00
|
|
|
* task's cancellability state to the indicated state and returns the
|
|
|
|
* previous cancellability state at the location referenced by oldstate.
|
2016-12-10 22:16:46 +01:00
|
|
|
* Legal values for state are TASK_CANCEL_ENABLE and TASK_CANCEL_DISABLE.
|
|
|
|
*
|
2020-02-23 09:50:23 +01:00
|
|
|
* The cancellability state and type of any newly created tasks are
|
2016-12-10 22:16:46 +01:00
|
|
|
* TASK_CANCEL_ENABLE and TASK_CANCEL_DEFERRED respectively.
|
2016-12-09 14:23:00 +01:00
|
|
|
*
|
2016-12-10 22:16:46 +01:00
|
|
|
* Input Parameters:
|
|
|
|
* state - the new cancellability state, either TASK_CANCEL_ENABLE or
|
|
|
|
* TASK_CANCEL_DISABLE
|
|
|
|
* oldstate - The location to return the old cancellability state.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; ERROR is returned on any failure with the
|
|
|
|
* errno value set appropriately.
|
2016-12-09 14:23:00 +01:00
|
|
|
*
|
2016-08-07 16:25:30 +02:00
|
|
|
****************************************************************************/
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2016-12-10 22:16:46 +01:00
|
|
|
int task_setcancelstate(int state, FAR int *oldstate)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-02-07 00:44:41 +01:00
|
|
|
FAR struct tcb_s *tcb = this_task();
|
2007-02-18 00:21:28 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Suppress context changes for a bit so that the flags are stable. (the
|
|
|
|
* flags should not change in interrupt handling).
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
|
2020-02-23 09:50:23 +01:00
|
|
|
/* Return the current state if so requested */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-10 14:40:48 +01:00
|
|
|
if (oldstate != NULL)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
if ((tcb->flags & TCB_FLAG_NONCANCELABLE) != 0)
|
|
|
|
{
|
2016-12-10 23:06:14 +01:00
|
|
|
*oldstate = TASK_CANCEL_DISABLE;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-10 23:06:14 +01:00
|
|
|
*oldstate = TASK_CANCEL_ENABLE;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the new cancellation state */
|
|
|
|
|
2016-12-10 23:06:14 +01:00
|
|
|
if (state == TASK_CANCEL_ENABLE)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-12-09 14:23:00 +01:00
|
|
|
/* Clear the non-cancelable flag */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-09 14:23:00 +01:00
|
|
|
tcb->flags &= ~TCB_FLAG_NONCANCELABLE;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-09 14:23:00 +01:00
|
|
|
/* Check if a cancellation was pending */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2016-12-09 14:23:00 +01:00
|
|
|
if ((tcb->flags & TCB_FLAG_CANCEL_PENDING) != 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2016-12-09 14:23:00 +01:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* If we are using deferred cancellation? */
|
|
|
|
|
|
|
|
if ((tcb->flags & TCB_FLAG_CANCEL_DEFERRED) != 0)
|
|
|
|
{
|
|
|
|
/* Yes.. If we are within a cancellation point, then
|
|
|
|
* notify of the cancellation.
|
|
|
|
*/
|
|
|
|
|
2016-12-09 16:44:23 +01:00
|
|
|
if (tcb->cpcount > 0)
|
2016-12-09 14:23:00 +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
|
|
|
nxnotify_cancellation(tcb);
|
2016-12-09 14:23:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2016-12-10 23:06:14 +01:00
|
|
|
{
|
|
|
|
/* No.. We are using asynchronous cancellation. If the
|
|
|
|
* cancellation was pending in this case, then just exit.
|
|
|
|
*/
|
2016-12-09 14:23:00 +01:00
|
|
|
|
2016-12-10 23:06:14 +01:00
|
|
|
tcb->flags &= ~TCB_FLAG_CANCEL_PENDING;
|
2016-12-10 22:16:46 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2016-12-10 23:06:14 +01:00
|
|
|
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
|
|
|
|
{
|
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
}
|
|
|
|
else
|
2016-12-10 22:16:46 +01:00
|
|
|
#endif
|
2016-12-10 23:06:14 +01:00
|
|
|
{
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
}
|
2016-12-10 23:06:14 +01:00
|
|
|
else if (state == TASK_CANCEL_DISABLE)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
|
|
|
/* Set the non-cancelable state */
|
|
|
|
|
|
|
|
tcb->flags |= TCB_FLAG_NONCANCELABLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-10 22:16:46 +01:00
|
|
|
set_errno(EINVAL);
|
|
|
|
ret = ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|