Fix an error when a task with open message queue descriptors is killed via task_delete(). Noted by Anton Gropyanov.

This commit is contained in:
Gregory Nutt 2016-04-11 11:14:18 -06:00
parent 2e49111a17
commit 3fba968bb0
9 changed files with 112 additions and 124 deletions

View File

@ -44,35 +44,71 @@
#include <assert.h>
#include <nuttx/kmalloc.h>
#include <nuttx/sched.h>
#include <nuttx/mqueue.h>
#include "inode/inode.h"
#include "mqueue/mqueue.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: mq_close_group
*
* Description:
* This function is used to indicate that all threads in the group are
* finished with the specified message queue mqdes. The mq_close_group()
* deallocates any system resources allocated by the system for use by
* this task for its message queue.
*
* Parameters:
* mqdes - Message queue descriptor.
* group - Group that has the open descriptor.
*
* Return Value:
* 0 (OK) if the message queue is closed successfully,
* otherwise, -1 (ERROR).
*
****************************************************************************/
int mq_close_group(mqd_t mqdes, FAR struct task_group_s *group)
{
FAR struct mqueue_inode_s *msgq;
FAR struct inode *inode;
DEBUGASSERT(mqdes != NULL && group != NULL);
/* Verify the inputs */
if (mqdes)
{
sched_lock();
/* Find the message queue associated with the message descriptor */
msgq = mqdes->msgq;
DEBUGASSERT(msgq && msgq->inode);
/* Close/free the message descriptor */
mq_desclose_group(mqdes, group);
/* Get the inode from the message queue structure */
inode = msgq->inode;
DEBUGASSERT(inode->u.i_mqueue == msgq);
/* Decrement the reference count on the inode, possibly freeing it */
mq_inode_release(inode);
sched_unlock();
}
return OK;
}
/****************************************************************************
* Name: mq_close
*
@ -103,38 +139,24 @@
int mq_close(mqd_t mqdes)
{
FAR struct mqueue_inode_s *msgq;
FAR struct inode *inode;
FAR struct tcb_s *rtcb = (FAR struct tcb_s *)sched_self();
int ret;
/* Verify the inputs */
/* Lock the scheduler to prevent any asynchrounous task delete operation
* (unlikely).
*/
if (mqdes)
{
sched_lock();
sched_lock();
/* Find the message queue associated with the message descriptor */
rtcb = (FAR struct tcb_s *)sched_self();
DEBUGASSERT(mqdes != NULL && rtcb != NULL && rtcb->group != NULL);
msgq = mqdes->msgq;
DEBUGASSERT(msgq && msgq->inode);
/* Close/free the message descriptor */
mq_desclose(mqdes);
/* Get the inode from the message queue structure */
inode = msgq->inode;
DEBUGASSERT(inode->u.i_mqueue == msgq);
/* Decrement the reference count on the inode, possibly freeing it */
mq_inode_release(inode);
sched_unlock();
}
return OK;
ret = mq_close_group(mqdes, rtcb->group);
sched_unlock();
return ret;
}
/****************************************************************************
* Name: mq_inode_release
*

View File

@ -52,26 +52,6 @@
#include "inode/inode.h"
#include "mqueue/mqueue.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -49,26 +49,6 @@
#include "inode/inode.h"
#include "mqueue/mqueue.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/

View File

@ -1,7 +1,7 @@
/****************************************************************************
* include/nuttx/mqueue.h
*
* Copyright (C) 2007, 2009, 2011, 2014-2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2011, 2014-2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -111,6 +111,10 @@ extern "C"
#define EXTERN extern
#endif
struct mq_attr; /* Forward reference */
struct tcb_s; /* Forward reference */
struct task_group_s; /* Forward reference */
/****************************************************************************
* Name: mq_msgqfree
*
@ -150,7 +154,6 @@ void mq_msgqfree(FAR struct mqueue_inode_s *msgq);
*
****************************************************************************/
struct mq_attr;
FAR struct mqueue_inode_s *mq_msgqalloc(mode_t mode,
FAR struct mq_attr *attr);
@ -171,12 +174,32 @@ FAR struct mqueue_inode_s *mq_msgqalloc(mode_t mode,
*
****************************************************************************/
struct tcb_s;
mqd_t mq_descreate(FAR struct tcb_s* mtcb, FAR struct mqueue_inode_s* msgq,
mqd_t mq_descreate(FAR struct tcb_s *mtcb, FAR struct mqueue_inode_s *msgq,
int oflags);
/****************************************************************************
* Name: mq_desclose
* Name: mq_close_group
*
* Description:
* This function is used to indicate that all threads in the group are
* finished with the specified message queue mqdes. The mq_close_group()
* deallocates any system resources allocated by the system for use by
* this task for its message queue.
*
* Parameters:
* mqdes - Message queue descriptor.
* group - Group that has the open descriptor.
*
* Return Value:
* 0 (OK) if the message queue is closed successfully,
* otherwise, -1 (ERROR).
*
****************************************************************************/
int mq_close_group(mqd_t mqdes, FAR struct task_group_s *group);
/****************************************************************************
* Name: mq_desclose_group
*
* Description:
* This function performs the portion of the mq_close operation related
@ -184,16 +207,17 @@ mqd_t mq_descreate(FAR struct tcb_s* mtcb, FAR struct mqueue_inode_s* msgq,
*
* Parameters:
* mqdes - Message queue descriptor.
* group - Group that has the open descriptor.
*
* Return Value:
* None
* None.
*
* Assumptions:
* - Called only from mq_close() with the scheduler locked.
*
****************************************************************************/
void mq_desclose(mqd_t mqdes);
void mq_desclose_group(mqd_t mqdes, FAR struct task_group_s *group);
#undef EXTERN
#ifdef __cplusplus

View File

@ -1,7 +1,7 @@
/****************************************************************************
* sched/mqueue/mq_desclose.c
*
* Copyright (C) 2007, 2009, 2013-2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2013-2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -45,6 +45,7 @@
#include <assert.h>
#include <queue.h>
#include <nuttx/sched.h>
#include <nuttx/mqueue.h>
#include "mqueue/mqueue.h"
@ -71,7 +72,7 @@
****************************************************************************/
/****************************************************************************
* Name: mq_close
* Name: mq_desclose_group
*
* Description:
* This function performs the portion of the mq_close operation related
@ -79,6 +80,7 @@
*
* Parameters:
* mqdes - Message queue descriptor.
* group - Group that has the open descriptor.
*
* Return Value:
* None.
@ -88,13 +90,11 @@
*
****************************************************************************/
void mq_desclose(mqd_t mqdes)
void mq_desclose_group(mqd_t mqdes, FAR struct task_group_s *group)
{
FAR struct tcb_s *rtcb = (FAR struct tcb_s *)sched_self();
FAR struct task_group_s *group = rtcb->group;
FAR struct mqueue_inode_s *msgq;
DEBUGASSERT(mqdes && group);
DEBUGASSERT(mqdes != NULL && group != NULL);
/* Remove the message descriptor from the current task's list of message
* descriptors.
@ -123,4 +123,3 @@ void mq_desclose(mqd_t mqdes)
mq_desfree(mqdes);
}

View File

@ -1,7 +1,7 @@
/****************************************************************************
* sched/mqueue/mq_descreate.c
*
* Copyright (C) 2007-2009, 2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2007-2009, 2013, 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -50,6 +50,7 @@
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include <nuttx/sched.h>
#include <nuttx/mqueue.h>
#include "signal/signal.h"

View File

@ -1,7 +1,7 @@
/****************************************************************************
* sched/mqueue/mq_release.c
*
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2013, 2016 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@ -67,6 +67,6 @@ void mq_release(FAR struct task_group_s *group)
{
while (group->tg_msgdesq.head)
{
mq_close((mqd_t)group->tg_msgdesq.head);
mq_close_group((mqd_t)group->tg_msgdesq.head, group);
}
}

View File

@ -136,6 +136,9 @@ EXTERN sq_queue_t g_desfree;
* Public Function Prototypes
****************************************************************************/
struct tcb_s; /* Forward reference */
struct task_group_s; /* Forward reference */
/* Functions defined in mq_initialize.c ************************************/
void weak_function mq_initialize(void);
@ -165,7 +168,6 @@ int mq_dosend(mqd_t mqdes, FAR struct mqueue_msg_s *mqmsg,
/* mq_release.c ************************************************************/
struct task_group_s; /* Forward reference */
void mq_release(FAR struct task_group_s *group);
/* mq_recover.c ************************************************************/

View File

@ -53,26 +53,6 @@
#include "signal/signal.h"
#include "task/task.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/