mirror of
https://github.com/apache/nuttx.git
synced 2026-06-04 23:03:27 +08:00
Sempahores: Add logic to clean up after task_delete() or pthread_cancel() if the task happens to be waiting on a semaphore when it is cancelled
This commit is contained in:
@@ -34,7 +34,7 @@
|
|||||||
############################################################################
|
############################################################################
|
||||||
|
|
||||||
CSRCS += sem_destroy.c sem_wait.c sem_trywait.c sem_timedwait.c
|
CSRCS += sem_destroy.c sem_wait.c sem_trywait.c sem_timedwait.c
|
||||||
CSRCS += sem_post.c
|
CSRCS += sem_post.c sem_recover.c
|
||||||
|
|
||||||
ifneq ($(CONFIG_DISABLE_SIGNALS),y)
|
ifneq ($(CONFIG_DISABLE_SIGNALS),y)
|
||||||
CSRCS += sem_waitirq.c
|
CSRCS += sem_waitirq.c
|
||||||
|
|||||||
@@ -0,0 +1,148 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* sched/semaphore/sem_recover.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <nuttx/arch.h>
|
||||||
|
#include <nuttx/sched.h>
|
||||||
|
|
||||||
|
#include "semaphore/semaphore.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Type Declarations
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Global Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: sem_recover
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function is called from task_recover() when a task is deleted via
|
||||||
|
* task_delete() or via pthread_cancel(). It current only checks on the
|
||||||
|
* case where a task is waiting for semaphore at the time that is was
|
||||||
|
* killed.
|
||||||
|
*
|
||||||
|
* REVISIT: A more complete implementation would release counts on all
|
||||||
|
* semaphores held by the thread. That would, however, require some
|
||||||
|
* significant extension to the semaphore data structures because given
|
||||||
|
* only the task, there is not mechanism to traverse all of the semaphores
|
||||||
|
* with counts held by the task.
|
||||||
|
*
|
||||||
|
* Inputs:
|
||||||
|
* tcb - The TCB of the terminated task or thread
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* None.
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* This function is called from task deletion logic in a safe context.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void sem_recover(FAR struct tcb_s *tcb)
|
||||||
|
{
|
||||||
|
irqstate_t flags;
|
||||||
|
|
||||||
|
/* The task is being deleted. If it is waiting for a semphore, then
|
||||||
|
* increment the count on the semaphores. This logic is almost identical
|
||||||
|
* to what you see in sem_waitirq() except that no attempt is made to
|
||||||
|
* restart the exiting task.
|
||||||
|
*
|
||||||
|
* NOTE: In the case that the task is waiting we can assume: (1) That the
|
||||||
|
* task state is TSTATE_WAIT_SEM and (2) that the 'waitsem' in the TCB is
|
||||||
|
* non-null. If we get here via pthread_cancel() or via task_delete(),
|
||||||
|
* then the task state should be preserved; it will be altered in other
|
||||||
|
* cases but in those cases waitsem should be NULL anyway (but we do not
|
||||||
|
* enforce that here).
|
||||||
|
*/
|
||||||
|
|
||||||
|
flags = irqsave();
|
||||||
|
if (tcb->task_state == TSTATE_WAIT_SEM)
|
||||||
|
{
|
||||||
|
sem_t *sem = tcb->waitsem;
|
||||||
|
DEBUGASSERT(sem != NULL && sem->semcount < 0);
|
||||||
|
|
||||||
|
/* Restore the correct priority of all threads that hold references
|
||||||
|
* to this semaphore.
|
||||||
|
*/
|
||||||
|
|
||||||
|
sem_canceled(tcb, sem);
|
||||||
|
|
||||||
|
/* And increment the count on the semaphore. This releases the count
|
||||||
|
* that was taken by sem_post(). This count decremented the semaphore
|
||||||
|
* count to negative and caused the thread to be blocked in the first
|
||||||
|
* place.
|
||||||
|
*/
|
||||||
|
|
||||||
|
sem->semcount++;
|
||||||
|
|
||||||
|
/* Clear the semaphore to assure that it is not reused. But leave the
|
||||||
|
* state as TSTATE_WAIT_SEM. This is necessary because this is a
|
||||||
|
* necessary indication that the TCB still resides in the waiting-for-
|
||||||
|
* semaphore list.
|
||||||
|
*/
|
||||||
|
|
||||||
|
tcb->waitsem = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
irqrestore(flags);
|
||||||
|
}
|
||||||
@@ -85,6 +85,10 @@ void sem_initialize(void);
|
|||||||
void sem_waitirq(FAR struct tcb_s *wtcb, int errcode);
|
void sem_waitirq(FAR struct tcb_s *wtcb, int errcode);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Recover semaphore resources with a task or thread is destroyed */
|
||||||
|
|
||||||
|
void sem_recover(FAR struct tcb_s *tcb);
|
||||||
|
|
||||||
/* Special logic needed only by priority inheritance to manage collections of
|
/* Special logic needed only by priority inheritance to manage collections of
|
||||||
* holders of semaphores.
|
* holders of semaphores.
|
||||||
*/
|
*/
|
||||||
|
|||||||
+14
-21
@@ -43,11 +43,13 @@
|
|||||||
#include <nuttx/wdog.h>
|
#include <nuttx/wdog.h>
|
||||||
#include <nuttx/sched.h>
|
#include <nuttx/sched.h>
|
||||||
|
|
||||||
|
#include "semaphore/semaphore.h"
|
||||||
|
#include "wdog/wdog.h"
|
||||||
#include "mqueue/mqueue.h"
|
#include "mqueue/mqueue.h"
|
||||||
#include "task/task.h"
|
#include "task/task.h"
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@@ -78,9 +80,9 @@
|
|||||||
* Name: task_recover
|
* Name: task_recover
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* This function is called when a task is deleted via task_deleted or
|
* This function is called when a task is deleted via task_delete() or
|
||||||
* via pthread_cancel. I checks if the task was waiting for a message
|
* via pthread_cancel. I checks checks for semaphores, message queue, and
|
||||||
* queue event and adjusts counts appropriately.
|
* watchdog timer resources stranded in bad conditions.
|
||||||
*
|
*
|
||||||
* Inputs:
|
* Inputs:
|
||||||
* tcb - The TCB of the terminated task or thread
|
* tcb - The TCB of the terminated task or thread
|
||||||
@@ -95,28 +97,19 @@
|
|||||||
|
|
||||||
void task_recover(FAR struct tcb_s *tcb)
|
void task_recover(FAR struct tcb_s *tcb)
|
||||||
{
|
{
|
||||||
irqstate_t flags;
|
/* The task is being deleted. Cancel in pending timeout events. */
|
||||||
|
|
||||||
/* The task is being deleted. If it is waiting for any timed event, then
|
wd_recover(tcb);
|
||||||
* tcb->waitdog will be non-NULL. Cancel the watchdog now so that no
|
|
||||||
* events occur after the watchdog expires. Obviously there are lots of
|
/* If the thread holds semaphore counts or is waiting for a semaphore count,
|
||||||
* race conditions here so this will most certainly have to be revisited in
|
* then release the counts.
|
||||||
* the future.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
flags = irqsave();
|
sem_recover(tcb);
|
||||||
if (tcb->waitdog)
|
|
||||||
{
|
|
||||||
(void)wd_cancel(tcb->waitdog);
|
|
||||||
(void)wd_delete(tcb->waitdog);
|
|
||||||
tcb->waitdog = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
irqrestore(flags);
|
|
||||||
|
|
||||||
/* Handle cases where the thread was waiting for a message queue event */
|
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_MQUEUE
|
#ifndef CONFIG_DISABLE_MQUEUE
|
||||||
|
/* Handle cases where the thread was waiting for a message queue event */
|
||||||
|
|
||||||
mq_recover(tcb);
|
mq_recover(tcb);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
############################################################################
|
############################################################################
|
||||||
|
|
||||||
CSRCS += wd_initialize.c wd_create.c wd_start.c wd_cancel.c wd_delete.c
|
CSRCS += wd_initialize.c wd_create.c wd_start.c wd_cancel.c wd_delete.c
|
||||||
CSRCS += wd_gettime.c
|
CSRCS += wd_gettime.c wd_recover.c
|
||||||
|
|
||||||
# Include wdog build support
|
# Include wdog build support
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* sched/wdog/wdog_recover.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <nuttx/arch.h>
|
||||||
|
#include <nuttx/wdog.h>
|
||||||
|
#include <nuttx/sched.h>
|
||||||
|
|
||||||
|
#include "wdog/wdog.h"
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Pre-processor Definitions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Type Declarations
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Global Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Variables
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: wd_recover
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function is called from task_recover() when a task is deleted via
|
||||||
|
* task_delete() or via pthread_cancel(). It checks if the deleted task
|
||||||
|
* is waiting for a timed event and if so cancels the timeout
|
||||||
|
*
|
||||||
|
* Inputs:
|
||||||
|
* tcb - The TCB of the terminated task or thread
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* None.
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* This function is called from task deletion logic in a safe context.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void wd_recover(FAR struct tcb_s *tcb)
|
||||||
|
{
|
||||||
|
irqstate_t flags;
|
||||||
|
|
||||||
|
/* The task is being deleted. If it is waiting for any timed event, then
|
||||||
|
* tcb->waitdog will be non-NULL. Cancel the watchdog now so that no
|
||||||
|
* events occur after the watchdog expires. Obviously there are lots of
|
||||||
|
* race conditions here so this will most certainly have to be revisited in
|
||||||
|
* the future.
|
||||||
|
*/
|
||||||
|
|
||||||
|
flags = irqsave();
|
||||||
|
if (tcb->waitdog)
|
||||||
|
{
|
||||||
|
(void)wd_cancel(tcb->waitdog);
|
||||||
|
(void)wd_delete(tcb->waitdog);
|
||||||
|
tcb->waitdog = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
irqrestore(flags);
|
||||||
|
}
|
||||||
@@ -144,6 +144,28 @@ unsigned int wd_timer(int ticks);
|
|||||||
void wd_timer(void);
|
void wd_timer(void);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: wd_recover
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* This function is called from task_recover() when a task is deleted via
|
||||||
|
* task_delete() or via pthread_cancel(). It checks if the deleted task
|
||||||
|
* is waiting for a timed event and if so cancels the timeout
|
||||||
|
*
|
||||||
|
* Inputs:
|
||||||
|
* tcb - The TCB of the terminated task or thread
|
||||||
|
*
|
||||||
|
* Return Value:
|
||||||
|
* None.
|
||||||
|
*
|
||||||
|
* Assumptions:
|
||||||
|
* This function is called from task deletion logic in a safe context.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
struct tcb_s;
|
||||||
|
void wd_recover(FAR struct tcb_s *tcb);
|
||||||
|
|
||||||
#undef EXTERN
|
#undef EXTERN
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user