Separate mqueue allocation logic from mq_open() and put it in sched/mqueue/mq_msgqalloc.c

This commit is contained in:
Gregory Nutt 2014-09-29 14:09:31 -06:00
parent b0f80cc8db
commit 9e975a217d
9 changed files with 102 additions and 96 deletions

View File

@ -47,7 +47,7 @@
#include "mqueue/mqueue.h"
/****************************************************************************
* Definitions
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
@ -55,7 +55,7 @@
****************************************************************************/
/****************************************************************************
* Global Variables
* Public Variables
****************************************************************************/
/****************************************************************************

View File

@ -55,7 +55,7 @@
#include "mqueue/mqueue.h"
/****************************************************************************
* Definitions
* Pre-procesor Definitions
****************************************************************************/
/****************************************************************************
@ -63,7 +63,7 @@
****************************************************************************/
/****************************************************************************
* Global Variables
* Public Variables
****************************************************************************/
/****************************************************************************
@ -112,9 +112,10 @@ mqd_t mq_open(const char *mq_name, int oflags, ...)
FAR struct tcb_s *rtcb = (FAR struct tcb_s*)g_readytorun.head;
FAR struct mqueue_inode_s *msgq;
mqd_t mqdes = NULL;
va_list arg; /* Points to each un-named argument */
struct mq_attr *attr; /* MQ creation attributes */
int namelen; /* Length of MQ name */
va_list arg;
struct mq_attr *attr;
mode_t mode;
int namelen;
/* Make sure that a non-NULL name is supplied */
@ -151,73 +152,28 @@ mqd_t mq_open(const char *mq_name, int oflags, ...)
else if ((oflags & O_CREAT) != 0)
{
/* Yes.. Get the optional arguments needed to create a message
* queue.
*/
va_start(arg, oflags);
mode = va_arg(arg, mode_t);
attr = va_arg(arg, struct mq_attr*);
/* Allocate memory for the new message queue. The size to
* allocate is the size of the struct mqueue_inode_s header
* plus the size of the message queue name+1.
*/
msgq = (FAR struct mqueue_inode_s*)kmm_zalloc(SIZEOF_MQ_HEADER + namelen + 1);
msgq = (FAR struct mqueue_inode_s*)mq_msgqalloc(oflags, mode, attr);
if (msgq)
{
/* Create a message queue descriptor for the TCB */
mqdes = mq_descreate(rtcb, msgq, oflags);
if (mqdes)
{
/* Set up to get the optional arguments needed to create
* a message queue.
*/
va_start(arg, oflags);
(void)va_arg(arg, mode_t); /* MQ creation mode parameter (ignored) */
attr = va_arg(arg, struct mq_attr*);
/* Initialize the new named message queue */
sq_init(&msgq->msglist);
if (attr)
{
msgq->maxmsgs = (int16_t)attr->mq_maxmsg;
if (attr->mq_msgsize <= MQ_MAX_BYTES)
{
msgq->maxmsgsize = (int16_t)attr->mq_msgsize;
}
else
{
msgq->maxmsgsize = MQ_MAX_BYTES;
}
}
else
{
msgq->maxmsgs = MQ_MAX_MSGS;
msgq->maxmsgsize = MQ_MAX_BYTES;
}
msgq->nconnect = 1;
#ifndef CONFIG_DISABLE_SIGNALS
msgq->ntpid = INVALID_PROCESS_ID;
#endif
strcpy(msgq->name, mq_name);
/* Add the new message queue to the list of
* message queues
*/
sq_addlast((FAR sq_entry_t*)msgq, &g_msgqueues);
/* Clean-up variable argument stuff */
va_end(arg);
}
else
{
/* Deallocate the msgq structure. Since it is
* uninitialized, mq_deallocate() is not used.
*/
sched_kfree(msgq);
}
#warning Missing logic
}
/* Clean-up variable argument stuff */
va_end(arg);
}
}

View File

@ -54,7 +54,7 @@
************************************************************************/
/************************************************************************
* Global Variables
* Public Variables
************************************************************************/
/************************************************************************

View File

@ -52,6 +52,10 @@
# include <nuttx/semaphore.h>
#endif
#ifndef CONFIG_DISABLE_MQUEUE
# include <nuttx/mqueue.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
@ -218,14 +222,18 @@ struct mountpt_operations
union inode_ops_u
{
FAR const struct file_operations *i_ops; /* Driver operations for inode */
FAR const struct file_operations *i_ops; /* Driver operations for inode */
#ifndef CONFIG_DISABLE_MOUNTPOUNT
FAR const struct block_operations *i_bops; /* Block driver operations */
FAR const struct mountpt_operations *i_mops; /* Operations on a mountpoint */
FAR const struct block_operations *i_bops; /* Block driver operations */
FAR const struct mountpt_operations *i_mops; /* Operations on a mountpoint */
#endif
#ifdef CONFIG_FS_NAMED_SEMAPHORES
FAR struct nsem_inode_s *i_nsem; /* Named semaphore */
FAR struct nsem_inode_s *i_nsem; /* Named semaphore */
#endif
#ifndef CONFIG_DISABLE_MQUEUE
FAR struct mqueue_inode_s *i_mqueue; /* POSIX message queue */
#endif
};
/* This structure represents one inode in the Nuttx pseudo-file system */

View File

@ -84,8 +84,6 @@ struct mqueue_inode_s
#endif
};
#define SIZEOF_MQ_HEADER ((int)(((struct mqueue_inode_s*)NULL)->name))
/* This describes the message queue descriptor that is held in the
* task's TCB
*/
@ -107,11 +105,55 @@ struct mq_des
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C" {
extern "C"
{
#else
#define EXTERN extern
#endif
/************************************************************************
* Name: mq_msgqfree
*
* Description:
* This function deallocates an initialized message queue structure.
* First, it deallocates all of the queued messages in the message
* queue. It is assumed that this message is fully unlinked and
* closed so that no thread will attempt access it while it is being
* deleted.
*
* Inputs:
* msgq - Named essage queue to be freed
*
* Return Value:
* None
*
************************************************************************/
void mq_msgqfree(FAR struct mqueue_inode_s *msgq);
/****************************************************************************
* Name: mq_msgqalloc
*
* Description:
* This function implements a part of the POSIX message queue open logic.
* It allocates and initializes a structu mqueue_inode_s structure.
*
* Parameters:
* oflags - open flags
* mode - mode_t value is ignored
* attr - The mq_maxmsg attribute is used at the time that the message
* queue is created to determine the maximum number of
* messages that may be placed in the message queue.
*
* Return Value:
* The allocated and initalized message queue structure or NULL in the
* event of a failure.
*
****************************************************************************/
FAR struct mqueue_inode_s *mq_msgqalloc(int oflags, mode_t mode,
FAR struct mq_attr *attr);
#undef EXTERN
#ifdef __cplusplus
}

View File

@ -37,8 +37,8 @@ ifneq ($(CONFIG_DISABLE_MQUEUE),y)
MQUEUE_SRCS = mq_send.c mq_timedsend.c mq_sndinternal.c mq_receive.c
MQUEUE_SRCS += mq_timedreceive.c mq_rcvinternal.c mq_initialize.c
MQUEUE_SRCS += mq_descreate.c mq_msgfree.c mq_msgqfree.c mq_release.c
MQUEUE_SRCS += mq_recover.c
MQUEUE_SRCS += mq_descreate.c mq_msgfree.c mq_msgqalloc.c mq_msgqfree.c
MQUEUE_SRCS += mq_release.c mq_recover.c
ifneq ($(CONFIG_DISABLE_SIGNALS),y)
MQUEUE_SRCS += mq_waitirq.c mq_notify.c

View File

@ -71,11 +71,11 @@
* Name: mq_msgqfree
*
* Description:
* This function deallocates an initialized message queue
* structure. First, it deallocates all of the queued
* messages in the message Q. It is assumed that this
* message is fully unlinked and closed so that not thread
* will attempt access it while it is being deleted.
* This function deallocates an initialized message queue structure.
* First, it deallocates all of the queued messages in the message
* queue. It is assumed that this message is fully unlinked and
* closed so that no thread will attempt access it while it is being
* deleted.
*
* Inputs:
* msgq - Named essage queue to be freed

View File

@ -1,7 +1,7 @@
/****************************************************************************
* sched/mqueue/mq_send.c
*
* Copyright (C) 2007, 2009, 2013 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2013-2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without

View File

@ -90,15 +90,15 @@ enum mqalloc_e
struct mqueue_msg_s
{
FAR struct mqmsg *next; /* Forward link to next message */
uint8_t type; /* (Used to manage allocations) */
uint8_t priority; /* priority of message */
FAR struct mqueue_msg_s *next; /* Forward link to next message */
uint8_t type; /* (Used to manage allocations) */
uint8_t priority; /* priority of message */
#if MQ_MAX_BYTES < 256
uint8_t msglen; /* Message data length */
uint8_t msglen; /* Message data length */
#else
uint16_t msglen; /* Message data length */
uint16_t msglen; /* Message data length */
#endif
uint8_t mail[MQ_MAX_BYTES]; /* Message data */
uint8_t mail[MQ_MAX_BYTES]; /* Message data */
};
/****************************************************************************
@ -141,10 +141,10 @@ EXTERN sq_queue_t g_desfree;
void weak_function mq_initialize(void);
void mq_desblockalloc(void);
mqd_t mq_descreate(FAR struct tcb_s* mtcb, FAR msgq_t* msgq, int oflags);
FAR msgq_t *mq_findnamed(const char *mq_name);
mqd_t mq_descreate(FAR struct tcb_s* mtcb, FAR struct mqueue_inode_s* msgq,
int oflags);
FAR struct mqueue_inode_s *mq_findnamed(FAR const char *mq_name);
void mq_msgfree(FAR struct mqueue_msg_s *mqmsg);
void mq_msgqfree(FAR msgq_t *msgq);
/* mq_waitirq.c ************************************************************/
@ -154,16 +154,16 @@ void mq_waitirq(FAR struct tcb_s *wtcb, int errcode);
int mq_verifyreceive(mqd_t mqdes, void *msg, size_t msglen);
FAR struct mqueue_msg_s *mq_waitreceive(mqd_t mqdes);
ssize_t mq_doreceive(mqd_t mqdes, struct mqueue_msg_s *mqmsg, void *ubuffer,
int *prio);
ssize_t mq_doreceive(mqd_t mqdes, FAR struct mqueue_msg_s *mqmsg,
FAR void *ubuffer, FAR int *prio);
/* mq_sndinternal.c ********************************************************/
int mq_verifysend(mqd_t mqdes, const void *msg, size_t msglen, int prio);
int mq_verifysend(mqd_t mqdes, FAR const void *msg, size_t msglen, int prio);
FAR struct mqueue_msg_s *mq_msgalloc(void);
int mq_waitsend(mqd_t mqdes);
int mq_dosend(mqd_t mqdes, FAR struct mqueue_msg_s *mqmsg, const void *msg,
size_t msglen, int prio);
int mq_dosend(mqd_t mqdes, FAR struct mqueue_msg_s *mqmsg,
FAR const void *msg, size_t msglen, int prio);
/* mq_release.c ************************************************************/