2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2009-12-15 03:30:18 +08:00
|
|
|
* sched/mq_send.c
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
2009-12-15 03:30:18 +08:00
|
|
|
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
|
2007-02-18 07:21:28 +08:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* 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.
|
2009-12-15 03:30:18 +08:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 07:21:28 +08:00
|
|
|
* 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.
|
|
|
|
*
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Included Files
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <sys/types.h>
|
2007-03-01 07:45:56 +08:00
|
|
|
#include <mqueue.h>
|
2007-03-28 22:48:42 +08:00
|
|
|
#include <errno.h>
|
2007-03-01 07:45:56 +08:00
|
|
|
#include <debug.h>
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include "os_internal.h"
|
|
|
|
#include "mq_internal.h"
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Definitions
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Private Type Declarations
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Global Variables
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Private Variables
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Private Functions
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Public Functions
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/****************************************************************************
|
2007-02-18 07:21:28 +08:00
|
|
|
* Function: mq_send
|
|
|
|
*
|
|
|
|
* Description:
|
2007-03-29 21:25:18 +08:00
|
|
|
* This function adds the specificied message (msg) to the message queue
|
|
|
|
* (mqdes). The "msglen" parameter specifies the length of the message
|
|
|
|
* in bytes pointed to by "msg." This length must not exceed the maximum
|
|
|
|
* message length from the mq_getattr().
|
|
|
|
*
|
|
|
|
* If the message queue is not full, mq_send() place the message in the
|
|
|
|
* message queue at the position indicated by the "prio" argrument.
|
|
|
|
* Messages with higher priority will be inserted before lower priority
|
|
|
|
* messages. The value of "prio" must not exceed MQ_PRIO_MAX.
|
|
|
|
*
|
|
|
|
* If the specified message queue is full and O_NONBLOCK is not set in the
|
|
|
|
* message queue, then mq_send() will block until space becomes available
|
|
|
|
* to the queue the message.
|
|
|
|
*
|
|
|
|
* If the message queue is full and O_NONBLOCK is set, the message is not
|
|
|
|
* queued and ERROR is returned.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* mqdes - Message queue descriptor
|
|
|
|
* msg - Message to send
|
|
|
|
* msglen - The length of the message in bytes
|
|
|
|
* prio - The priority of the message
|
|
|
|
*
|
|
|
|
* Return Value:
|
2007-03-29 00:00:57 +08:00
|
|
|
* On success, mq_send() returns 0 (OK); on error, -1 (ERROR)
|
|
|
|
* is returned, with errno set to indicate the error:
|
2007-03-28 22:48:42 +08:00
|
|
|
*
|
2007-03-29 21:25:18 +08:00
|
|
|
* EAGAIN The queue was empty, and the O_NONBLOCK flag was set for the
|
|
|
|
* message queue description referred to by mqdes.
|
|
|
|
* EINVAL Either msg or mqdes is NULL or the value of prio is invalid.
|
2007-03-28 22:48:42 +08:00
|
|
|
* EPERM Message queue opened not opened for writing.
|
2007-03-29 21:25:18 +08:00
|
|
|
* EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the
|
|
|
|
* message queue.
|
2007-03-28 22:48:42 +08:00
|
|
|
* EINTR The call was interrupted by a signal handler.
|
2007-02-18 07:21:28 +08:00
|
|
|
*
|
|
|
|
* Assumptions/restrictions:
|
|
|
|
*
|
2007-03-29 21:25:18 +08:00
|
|
|
****************************************************************************/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
|
|
|
int mq_send(mqd_t mqdes, const void *msg, size_t msglen, int prio)
|
|
|
|
{
|
2007-02-28 05:17:21 +08:00
|
|
|
FAR msgq_t *msgq;
|
2007-03-29 21:25:18 +08:00
|
|
|
FAR mqmsg_t *mqmsg = NULL;
|
2007-02-28 05:17:21 +08:00
|
|
|
irqstate_t saved_state;
|
|
|
|
int ret = ERROR;
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/* Verify the input parameters -- setting errno appropriately
|
|
|
|
* on any failures to verify.
|
|
|
|
*/
|
2007-03-28 22:48:42 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
if (mq_verifysend(mqdes, msg, msglen, prio) != OK)
|
2007-03-28 22:48:42 +08:00
|
|
|
{
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the message queue */
|
|
|
|
|
2007-02-18 07:21:28 +08:00
|
|
|
sched_lock();
|
2007-03-28 22:48:42 +08:00
|
|
|
msgq = mqdes->msgq;
|
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/* Allocate a message structure:
|
|
|
|
* - Immediately if we are called from an interrupt handler.
|
|
|
|
* - Immediately if the message queue is not full, or
|
|
|
|
* - After successfully waiting for the message queue to become
|
|
|
|
* non-FULL. This would fail with EAGAIN, EINTR, or ETIMEOUT.
|
2007-03-28 22:48:42 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
saved_state = irqsave();
|
2007-03-29 21:25:18 +08:00
|
|
|
if (up_interrupt_context() || /* In an interrupt handler */
|
|
|
|
msgq->nmsgs < msgq->maxmsgs || /* OR Message queue not full */
|
|
|
|
mq_waitsend(mqdes) == OK) /* OR Successfully waited for mq not full */
|
2007-02-18 07:21:28 +08:00
|
|
|
{
|
2007-03-29 21:25:18 +08:00
|
|
|
/* Allocate the message */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
irqrestore(saved_state);
|
|
|
|
mqmsg = mq_msgalloc();
|
2007-03-28 22:48:42 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-03-29 21:25:18 +08:00
|
|
|
/* We cannot send the message (and didn't even try to allocate it)
|
|
|
|
* because:
|
|
|
|
* - We are not in an interrupt handler AND
|
|
|
|
* - The message queue is full AND
|
|
|
|
* - When we tried waiting, the wait was unsuccessful.
|
2007-03-28 22:48:42 +08:00
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-28 22:48:42 +08:00
|
|
|
irqrestore(saved_state);
|
2007-03-29 21:25:18 +08:00
|
|
|
}
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
/* Check if we were able to get a message structure -- this can fail
|
|
|
|
* either because we cannot send the message (and didn't bother trying
|
|
|
|
* to allocate it) or because the allocation failed.
|
|
|
|
*/
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
if (mqmsg)
|
|
|
|
{
|
|
|
|
/* Yes, peforrm the message send. */
|
2007-02-18 07:21:28 +08:00
|
|
|
|
2007-03-29 21:25:18 +08:00
|
|
|
ret = mq_dosend(mqdes, mqmsg, msg, msglen, prio);
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
2007-03-29 21:25:18 +08:00
|
|
|
return ret;
|
2007-02-18 07:21:28 +08:00
|
|
|
}
|
|
|
|
|