2016-08-07 16:25:30 +02:00
|
|
|
/****************************************************************************
|
2023-11-10 11:56:10 +01:00
|
|
|
* libs/libc/sched/task_setcancelstate.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01: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
|
|
|
*
|
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 <sched.h>
|
2023-11-10 11:56:10 +01:00
|
|
|
#include <assert.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <errno.h>
|
2023-11-10 11:56:10 +01:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
2016-12-09 15:13:28 +01:00
|
|
|
|
2023-11-10 11:56:10 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
|
|
|
#include <nuttx/tls.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
|
|
|
{
|
2023-11-10 11:56:10 +01:00
|
|
|
FAR struct tls_info_s *tls = tls_get_info();
|
2007-02-18 00:21:28 +01:00
|
|
|
int ret = OK;
|
|
|
|
|
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
|
|
|
{
|
2023-11-10 11:56:10 +01:00
|
|
|
if ((tls->tl_cpstate & CANCEL_FLAG_NONCANCELABLE) != 0)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
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
|
|
|
|
2023-11-10 11:56:10 +01:00
|
|
|
tls->tl_cpstate &= ~CANCEL_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
|
|
|
|
2023-11-10 11:56:10 +01:00
|
|
|
if ((tls->tl_cpstate & CANCEL_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? */
|
|
|
|
|
2023-11-10 11:56:10 +01:00
|
|
|
if ((tls->tl_cpstate & CANCEL_FLAG_CANCEL_ASYNC) != 0)
|
2016-12-09 14:23:00 +01:00
|
|
|
#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
|
|
|
|
2023-11-10 11:56:10 +01:00
|
|
|
tls->tl_cpstate &= ~CANCEL_FLAG_CANCEL_PENDING;
|
2016-12-10 22:16:46 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
2023-11-10 11:56:10 +01:00
|
|
|
pthread_exit(PTHREAD_CANCELED);
|
|
|
|
#else
|
|
|
|
exit(EXIT_FAILURE);
|
2016-12-10 22:16:46 +01:00
|
|
|
#endif
|
2016-12-10 23:06:14 +01:00
|
|
|
}
|
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 */
|
|
|
|
|
2023-11-10 11:56:10 +01:00
|
|
|
tls->tl_cpstate |= CANCEL_FLAG_NONCANCELABLE;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-10 22:16:46 +01:00
|
|
|
set_errno(EINVAL);
|
|
|
|
ret = ERROR;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|