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
+68 -46
View File
@@ -44,35 +44,71 @@
#include <assert.h> #include <assert.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/sched.h>
#include <nuttx/mqueue.h> #include <nuttx/mqueue.h>
#include "inode/inode.h" #include "inode/inode.h"
#include "mqueue/mqueue.h" #include "mqueue/mqueue.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Public 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 * Name: mq_close
* *
@@ -103,38 +139,24 @@
int mq_close(mqd_t mqdes) int mq_close(mqd_t mqdes)
{ {
FAR struct mqueue_inode_s *msgq; FAR struct tcb_s *rtcb = (FAR struct tcb_s *)sched_self();
FAR struct inode *inode; 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; ret = mq_close_group(mqdes, rtcb->group);
DEBUGASSERT(msgq && msgq->inode); sched_unlock();
return ret;
/* 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;
} }
/**************************************************************************** /****************************************************************************
* Name: mq_inode_release * Name: mq_inode_release
* *
-20
View File
@@ -52,26 +52,6 @@
#include "inode/inode.h" #include "inode/inode.h"
#include "mqueue/mqueue.h" #include "mqueue/mqueue.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
-20
View File
@@ -49,26 +49,6 @@
#include "inode/inode.h" #include "inode/inode.h"
#include "mqueue/mqueue.h" #include "mqueue/mqueue.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
+31 -7
View File
@@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* include/nuttx/mqueue.h * 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> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -111,6 +111,10 @@ extern "C"
#define EXTERN extern #define EXTERN extern
#endif #endif
struct mq_attr; /* Forward reference */
struct tcb_s; /* Forward reference */
struct task_group_s; /* Forward reference */
/**************************************************************************** /****************************************************************************
* Name: mq_msgqfree * 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 mqueue_inode_s *mq_msgqalloc(mode_t mode,
FAR struct mq_attr *attr); 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); 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: * Description:
* This function performs the portion of the mq_close operation related * 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: * Parameters:
* mqdes - Message queue descriptor. * mqdes - Message queue descriptor.
* group - Group that has the open descriptor.
* *
* Return Value: * Return Value:
* None * None.
* *
* Assumptions: * Assumptions:
* - Called only from mq_close() with the scheduler locked. * - 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 #undef EXTERN
#ifdef __cplusplus #ifdef __cplusplus
+6 -7
View File
@@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* sched/mqueue/mq_desclose.c * 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> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -45,6 +45,7 @@
#include <assert.h> #include <assert.h>
#include <queue.h> #include <queue.h>
#include <nuttx/sched.h>
#include <nuttx/mqueue.h> #include <nuttx/mqueue.h>
#include "mqueue/mqueue.h" #include "mqueue/mqueue.h"
@@ -71,7 +72,7 @@
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: mq_close * Name: mq_desclose_group
* *
* Description: * Description:
* This function performs the portion of the mq_close operation related * This function performs the portion of the mq_close operation related
@@ -79,6 +80,7 @@
* *
* Parameters: * Parameters:
* mqdes - Message queue descriptor. * mqdes - Message queue descriptor.
* group - Group that has the open descriptor.
* *
* Return Value: * Return Value:
* None. * 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; 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 /* Remove the message descriptor from the current task's list of message
* descriptors. * descriptors.
@@ -123,4 +123,3 @@ void mq_desclose(mqd_t mqdes)
mq_desfree(mqdes); mq_desfree(mqdes);
} }
+2 -1
View File
@@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* sched/mqueue/mq_descreate.c * 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> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -50,6 +50,7 @@
#include <nuttx/arch.h> #include <nuttx/arch.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/sched.h>
#include <nuttx/mqueue.h> #include <nuttx/mqueue.h>
#include "signal/signal.h" #include "signal/signal.h"
+2 -2
View File
@@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* sched/mqueue/mq_release.c * 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> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * 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) while (group->tg_msgdesq.head)
{ {
mq_close((mqd_t)group->tg_msgdesq.head); mq_close_group((mqd_t)group->tg_msgdesq.head, group);
} }
} }
+3 -1
View File
@@ -136,6 +136,9 @@ EXTERN sq_queue_t g_desfree;
* Public Function Prototypes * Public Function Prototypes
****************************************************************************/ ****************************************************************************/
struct tcb_s; /* Forward reference */
struct task_group_s; /* Forward reference */
/* Functions defined in mq_initialize.c ************************************/ /* Functions defined in mq_initialize.c ************************************/
void weak_function mq_initialize(void); 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 ************************************************************/ /* mq_release.c ************************************************************/
struct task_group_s; /* Forward reference */
void mq_release(FAR struct task_group_s *group); void mq_release(FAR struct task_group_s *group);
/* mq_recover.c ************************************************************/ /* mq_recover.c ************************************************************/
-20
View File
@@ -53,26 +53,6 @@
#include "signal/signal.h" #include "signal/signal.h"
#include "task/task.h" #include "task/task.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Type Declarations
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/