mirror of
https://github.com/apache/nuttx.git
synced 2026-06-05 15:58:59 +08:00
Move mq_open.c, mq_close.c, and mq_unlink.c from sched/mqueue to fs/mqueue
This commit is contained in:
@@ -4,4 +4,11 @@
|
||||
#
|
||||
|
||||
if !DISABLE_MQUEUE
|
||||
|
||||
config FS_MQUEUE_MPATH
|
||||
string "Path to message queuee"
|
||||
default "/var/mqueue"
|
||||
---help---
|
||||
The path to where POSIX message queues will exist in the VFS namespace.
|
||||
|
||||
endif # !DISABLE_MQUEUE
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@
|
||||
|
||||
ifneq ($(CONFIG_DISABLE_MQUEUE),y)
|
||||
|
||||
#CSRCS +=
|
||||
CSRCS += mq_open.c mq_close.c mq_unlink.c
|
||||
|
||||
# Include POSIX message queue build support
|
||||
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
/****************************************************************************
|
||||
* sched/mqueue/mq_close.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2013 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 <mqueue.h>
|
||||
#include <sched.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "sched/sched.h"
|
||||
#include "mqueue/mqueue.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mq_desfree
|
||||
*
|
||||
* Description:
|
||||
* Deallocate a message queue descriptor but returning it to the free list
|
||||
*
|
||||
* Inputs:
|
||||
* mqdes - message queue descriptor to free
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#define mq_desfree(mqdes) sq_addlast((FAR sq_entry_t*)mqdes, &g_desfree)
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mq_close
|
||||
*
|
||||
* Description:
|
||||
* This function is used to indicate that the calling task is finished
|
||||
* with the specified message queued mqdes. The mq_close() deallocates
|
||||
* any system resources allocated by the system for use by this task for
|
||||
* its message queue.
|
||||
*
|
||||
* If the calling task has attached a notification to the message queue
|
||||
* via this mqdes, this attachment will be removed and the message queue
|
||||
* is available for another process to attach a notification.
|
||||
*
|
||||
* Parameters:
|
||||
* mqdes - Message queue descriptor.
|
||||
*
|
||||
* Return Value:
|
||||
* 0 (OK) if the message queue is closed successfully,
|
||||
* otherwise, -1 (ERROR).
|
||||
*
|
||||
* Assumptions:
|
||||
* - The behavior of a task that is blocked on either a mq_send() or
|
||||
* mq_receive() is undefined when mq_close() is called.
|
||||
* - The results of using this message queue descriptor after a successful
|
||||
* return from mq_close() is undefined.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int mq_close(mqd_t mqdes)
|
||||
{
|
||||
FAR struct tcb_s *rtcb = (FAR struct tcb_s*)g_readytorun.head;
|
||||
FAR struct task_group_s *group = rtcb->group;
|
||||
FAR struct mqueue_inode_s *msgq;
|
||||
irqstate_t saved_state;
|
||||
int ret = ERROR;
|
||||
|
||||
DEBUGASSERT(group);
|
||||
|
||||
/* Verify the inputs */
|
||||
|
||||
if (mqdes)
|
||||
{
|
||||
sched_lock();
|
||||
|
||||
/* Remove the message descriptor from the current task's
|
||||
* list of message descriptors.
|
||||
*/
|
||||
|
||||
sq_rem((FAR sq_entry_t*)mqdes, &group->tg_msgdesq);
|
||||
|
||||
/* Find the message queue associated with the message descriptor */
|
||||
|
||||
msgq = mqdes->msgq;
|
||||
|
||||
/* Check if the calling task has a notification attached to
|
||||
* the message queue via this mqdes.
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_DISABLE_SIGNALS
|
||||
if (msgq->ntmqdes == mqdes)
|
||||
{
|
||||
msgq->ntpid = INVALID_PROCESS_ID;
|
||||
msgq->ntsigno = 0;
|
||||
msgq->ntvalue.sival_int = 0;
|
||||
msgq->ntmqdes = NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Decrement the connection count on the message queue. */
|
||||
|
||||
if (msgq->nconnect)
|
||||
{
|
||||
msgq->nconnect--;
|
||||
}
|
||||
|
||||
/* If it is no longer connected to any message descriptor and if the
|
||||
* message queue has already been unlinked, then we can discard the
|
||||
* message queue.
|
||||
*/
|
||||
|
||||
if (!msgq->nconnect && msgq->unlinked)
|
||||
{
|
||||
/* Remove the message queue from the list of all
|
||||
* message queues
|
||||
*/
|
||||
|
||||
saved_state = irqsave();
|
||||
(void)sq_rem((FAR sq_entry_t*)msgq, &g_msgqueues);
|
||||
irqrestore(saved_state);
|
||||
|
||||
/* Then deallocate it (and any messages left in it) */
|
||||
|
||||
mq_msgqfree(msgq);
|
||||
}
|
||||
|
||||
/* Deallocate the message descriptor */
|
||||
|
||||
mq_desfree(mqdes);
|
||||
|
||||
sched_unlock();
|
||||
ret = OK;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
/****************************************************************************
|
||||
* sched/mqueue/mq_open.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 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 <sys/types.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <mqueue.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <sched.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
|
||||
#include "sched/sched.h"
|
||||
#include "mqueue/mqueue.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Global Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: mq_open
|
||||
*
|
||||
* Description:
|
||||
* This function establish a connection between a named message queue and
|
||||
* the calling task. After a successful call of mq_open(), the task can
|
||||
* reference the message queue using the address returned by the call. The
|
||||
* message queue remains usable until it is closed by a successful call to
|
||||
* mq_close().
|
||||
*
|
||||
* Parameters:
|
||||
* mq_name - Name of the queue to open
|
||||
* oflags - open flags
|
||||
* Optional parameters. When the O_CREAT flag is specified, two optional
|
||||
* parameters are expected:
|
||||
*
|
||||
* 1. mode_t mode (ignored), and
|
||||
* 2. struct mq_attr *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:
|
||||
* A message queue descriptor or -1 (ERROR)
|
||||
*
|
||||
* Assumptions:
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
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 */
|
||||
|
||||
/* Make sure that a non-NULL name is supplied */
|
||||
|
||||
if (mq_name)
|
||||
{
|
||||
sched_lock();
|
||||
namelen = strlen(mq_name);
|
||||
if (namelen > 0)
|
||||
{
|
||||
/* See if the message queue already exists */
|
||||
|
||||
msgq = mq_findnamed(mq_name);
|
||||
if (msgq)
|
||||
{
|
||||
/* It does. Check if the caller wanted to create a new
|
||||
* message queue with this name (i.e., O_CREAT|O_EXCL)
|
||||
*/
|
||||
|
||||
if ((oflags & O_CREAT) == 0 || (oflags & O_EXCL) == 0)
|
||||
{
|
||||
/* Create a message queue descriptor for the TCB */
|
||||
|
||||
mqdes = mq_descreate(rtcb, msgq, oflags);
|
||||
if (mqdes)
|
||||
{
|
||||
/* Allow a new connection to the message queue */
|
||||
|
||||
msgq->nconnect++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* It doesn't exist. Should we create one? */
|
||||
|
||||
else if ((oflags & O_CREAT) != 0)
|
||||
{
|
||||
/* 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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sched_unlock();
|
||||
}
|
||||
|
||||
if (mqdes == NULL)
|
||||
{
|
||||
return (mqd_t)ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
return mqdes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
/************************************************************************
|
||||
* sched/mqueue/mq_unlink.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009 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 <stdbool.h>
|
||||
#include <mqueue.h>
|
||||
#include <sched.h>
|
||||
|
||||
#include "mqueue/mqueue.h"
|
||||
|
||||
/************************************************************************
|
||||
* Pre-processor Definitions
|
||||
************************************************************************/
|
||||
|
||||
/************************************************************************
|
||||
* Private Type Declarations
|
||||
************************************************************************/
|
||||
|
||||
/************************************************************************
|
||||
* Global Variables
|
||||
************************************************************************/
|
||||
|
||||
/************************************************************************
|
||||
* Private Variables
|
||||
************************************************************************/
|
||||
|
||||
/************************************************************************
|
||||
* Private Functions
|
||||
************************************************************************/
|
||||
|
||||
/************************************************************************
|
||||
* Public Functions
|
||||
************************************************************************/
|
||||
|
||||
/************************************************************************
|
||||
* Name: mq_unlink
|
||||
*
|
||||
* Description:
|
||||
* This function removes the message queue named by "mq_name." If one
|
||||
* or more tasks have the message queue open when mq_unlink() is called,
|
||||
* removal of the message queue is postponed until all references to the
|
||||
* message queue have been closed.
|
||||
*
|
||||
* Parameters:
|
||||
* mq_name - Name of the message queue
|
||||
*
|
||||
* Return Value:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
*
|
||||
************************************************************************/
|
||||
|
||||
int mq_unlink(const char *mq_name)
|
||||
{
|
||||
FAR struct mqueue_inode_s *msgq;
|
||||
irqstate_t saved_state;
|
||||
int ret = ERROR;
|
||||
|
||||
/* Verify the input values */
|
||||
|
||||
if (mq_name)
|
||||
{
|
||||
sched_lock();
|
||||
|
||||
/* Find the named message queue */
|
||||
|
||||
msgq = mq_findnamed(mq_name);
|
||||
if (msgq)
|
||||
{
|
||||
/* If it is no longer connected, then we can just
|
||||
* discard the message queue now.
|
||||
*/
|
||||
|
||||
if (!msgq->nconnect)
|
||||
{
|
||||
/* Remove the message queue from the list of all
|
||||
* message queues
|
||||
*/
|
||||
|
||||
saved_state = irqsave();
|
||||
(void)sq_rem((FAR sq_entry_t*)msgq, &g_msgqueues);
|
||||
irqrestore(saved_state);
|
||||
|
||||
/* Then deallocate it (and any messages left in it) */
|
||||
|
||||
mq_msgqfree(msgq);
|
||||
}
|
||||
|
||||
/* If the message queue is still connected to a message descriptor,
|
||||
* then mark it for deletion when the last message descriptor is
|
||||
* closed
|
||||
*/
|
||||
|
||||
else
|
||||
{
|
||||
msgq->unlinked = true;
|
||||
}
|
||||
|
||||
ret = OK;
|
||||
}
|
||||
|
||||
sched_unlock();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user