2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2014-08-08 20:31:23 +02:00
|
|
|
* sched/mqueue/mq_rcvinternal.c
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Copyright (C) 2007, 2008, 2012-2013, 2016-2017 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-05-17 18:55:13 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
|
|
|
* 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.
|
2008-01-30 15:01:22 +01:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-03-29 15:25:18 +02: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.
|
|
|
|
*
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2007-03-29 15:25:18 +02:00
|
|
|
* Included Files
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2012-05-17 18:55:13 +02:00
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
#include <sys/types.h>
|
2012-05-17 18:55:13 +02:00
|
|
|
#include <fcntl.h>
|
2007-03-29 15:25:18 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <mqueue.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <debug.h>
|
2012-05-17 18:55:13 +02:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2007-03-29 15:25:18 +02:00
|
|
|
#include <nuttx/arch.h>
|
2016-12-10 16:08:26 +01:00
|
|
|
#include <nuttx/cancelpt.h>
|
2012-05-17 18:55:13 +02:00
|
|
|
|
2014-08-09 01:53:55 +02:00
|
|
|
#include "sched/sched.h"
|
2014-08-08 20:31:23 +02:00
|
|
|
#include "mqueue/mqueue.h"
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2007-03-29 15:25:18 +02:00
|
|
|
* Public Functions
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2017-10-09 17:06:46 +02:00
|
|
|
* Name: nxmq_verify_receive
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-10-10 17:57:40 +02:00
|
|
|
* This is internal, common logic shared by both [nx]mq_receive and
|
|
|
|
* [nx]mq_timedreceive. This function verifies the input parameters that
|
|
|
|
* are common to both functions.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Input Parameters:
|
2007-03-29 15:25:18 +02:00
|
|
|
* mqdes - Message Queue Descriptor
|
|
|
|
* msg - Buffer to receive the message
|
|
|
|
* msglen - Size of the buffer in bytes
|
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Returned Value:
|
|
|
|
* One success, zero (OK) is returned. A negated errno value is returned
|
|
|
|
* on any failure:
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
|
|
|
* EPERM Message queue opened not opened for reading.
|
2012-07-14 21:30:31 +02:00
|
|
|
* EMSGSIZE 'msglen' was less than the maxmsgsize attribute of the message
|
|
|
|
* queue.
|
2007-03-29 15:25:18 +02:00
|
|
|
* EINVAL Invalid 'msg' or 'mqdes'
|
|
|
|
*
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2017-10-09 17:06:46 +02:00
|
|
|
int nxmq_verify_receive(mqd_t mqdes, FAR char *msg, size_t msglen)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
|
|
|
/* Verify the input parameters */
|
|
|
|
|
|
|
|
if (!msg || !mqdes)
|
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
return -EINVAL;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((mqdes->oflags & O_RDOK) == 0)
|
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
return -EPERM;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msglen < (size_t)mqdes->msgq->maxmsgsize)
|
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
return -EMSGSIZE;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2017-10-09 17:06:46 +02:00
|
|
|
* Name: nxmq_wait_receive
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-10-10 17:57:40 +02:00
|
|
|
* This is internal, common logic shared by both [nx]mq_receive and
|
|
|
|
* [nx]mq_timedreceive. This function waits for a message to be received
|
|
|
|
* on the specified message queue, removes the message from the queue, and
|
2012-07-14 21:30:31 +02:00
|
|
|
* returns it.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* mqdes - Message queue descriptor
|
|
|
|
* rcvmsg - The caller-provided location in which to return the newly
|
|
|
|
* received message.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Returned Value:
|
|
|
|
* One success, zero (OK) is returned. A negated errno value is returned
|
|
|
|
* on any failure.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2012-07-14 21:30:31 +02:00
|
|
|
* - The caller has provided all validity checking of the input parameters
|
2017-10-09 17:06:46 +02:00
|
|
|
* using nxmq_verify_receive.
|
2012-07-14 21:30:31 +02:00
|
|
|
* - Interrupts should be disabled throughout this call. This is necessary
|
|
|
|
* because messages can be sent from interrupt level processing.
|
|
|
|
* - For mq_timedreceive, setting of the timer and this wait must be atomic.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
int nxmq_wait_receive(mqd_t mqdes, FAR struct mqueue_msg_s **rcvmsg)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *rtcb;
|
2014-09-29 21:19:11 +02:00
|
|
|
FAR struct mqueue_inode_s *msgq;
|
2017-10-10 17:57:40 +02:00
|
|
|
FAR struct mqueue_msg_s *newmsg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(rcvmsg != NULL);
|
|
|
|
*rcvmsg = NULL; /* Assume failure */
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
#ifdef CONFIG_CANCELLATION_POINTS
|
|
|
|
/* nxmq_wait_receive() is not a cancellation point, but it may be called
|
|
|
|
* from mq_receive() or mq_timedreceive() which are cancellation point.
|
2016-12-10 15:36:58 +01:00
|
|
|
*/
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
if (check_cancellation_point())
|
2016-12-10 15:36:58 +01:00
|
|
|
{
|
|
|
|
/* If there is a pending cancellation, then do not perform
|
|
|
|
* the wait. Exit now with ECANCELED.
|
|
|
|
*/
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
return -ECANCELED;
|
2016-12-10 15:36:58 +01:00
|
|
|
}
|
2017-10-10 17:57:40 +02:00
|
|
|
#endif
|
2016-12-10 15:36:58 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/* Get a pointer to the message queue */
|
|
|
|
|
|
|
|
msgq = mqdes->msgq;
|
|
|
|
|
|
|
|
/* Get the message from the head of the queue */
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
while ((newmsg = (FAR struct mqueue_msg_s *)sq_remfirst(&msgq->msglist)) == NULL)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2008-01-30 15:01:22 +01:00
|
|
|
/* The queue is empty! Should we block until there the above condition
|
|
|
|
* has been satisfied?
|
2007-03-29 15:25:18 +02:00
|
|
|
*/
|
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
if ((mqdes->oflags & O_NONBLOCK) == 0)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
int saved_errno;
|
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/* Yes.. Block and try again */
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
rtcb = this_task();
|
2007-03-29 15:25:18 +02:00
|
|
|
rtcb->msgwaitq = msgq;
|
|
|
|
msgq->nwaitnotempty++;
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
/* "Borrow" the per-task errno to communication wake-up error
|
|
|
|
* conditions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
saved_errno = rtcb->pterrno;
|
|
|
|
rtcb->pterrno = OK;
|
|
|
|
|
2018-11-30 13:54:15 +01:00
|
|
|
/* Make sure this is not the idle task, descheduling that
|
|
|
|
* isn't going to end well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(NULL != rtcb->flink);
|
2007-03-29 15:25:18 +02:00
|
|
|
up_block_task(rtcb, TSTATE_WAIT_MQNOTEMPTY);
|
|
|
|
|
|
|
|
/* When we resume at this point, either (1) the message queue
|
|
|
|
* is no longer empty, or (2) the wait has been interrupted by
|
|
|
|
* a signal. We can detect the latter case be examining the
|
|
|
|
* errno value (should be either EINTR or ETIMEDOUT).
|
|
|
|
*/
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
ret = rtcb->pterrno;
|
|
|
|
rtcb->pterrno = saved_errno;
|
|
|
|
|
|
|
|
if (ret != OK)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
return -ret;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The queue was empty, and the O_NONBLOCK flag was set for the
|
|
|
|
* message queue description referred to by 'mqdes'.
|
|
|
|
*/
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
return -EAGAIN;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we got message, then decrement the number of messages in
|
|
|
|
* the queue while we are still in the critical section
|
|
|
|
*/
|
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
if (newmsg)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
|
|
|
msgq->nmsgs--;
|
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2017-10-10 17:57:40 +02:00
|
|
|
*rcvmsg = newmsg;
|
|
|
|
return OK;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2017-10-09 17:06:46 +02:00
|
|
|
* Name: nxmq_do_receive
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-10-10 17:57:40 +02:00
|
|
|
* This is internal, common logic shared by both [nx]mq_receive and
|
|
|
|
* [nx]mq_timedreceive. This function accepts the message obtained by
|
2012-07-14 21:30:31 +02:00
|
|
|
* mq_waitmsg, provides the message content to the user, notifies any
|
|
|
|
* threads that were waiting for the message queue to become non-full,
|
|
|
|
* and disposes of the message structure
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Input Parameters:
|
2007-03-29 15:25:18 +02:00
|
|
|
* mqdes - Message queue descriptor
|
|
|
|
* mqmsg - The message obtained by mq_waitmsg()
|
2012-07-14 21:30:31 +02:00
|
|
|
* ubuffer - The address of the user provided buffer to receive the message
|
|
|
|
* prio - The user-provided location to return the message priority.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Returned Value:
|
2012-07-14 21:30:31 +02:00
|
|
|
* Returns the length of the received message. This function does not fail.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2012-07-14 21:30:31 +02:00
|
|
|
* - The caller has provided all validity checking of the input parameters
|
2017-10-09 17:06:46 +02:00
|
|
|
* using nxmq_verify_receive.
|
2012-07-14 21:30:31 +02:00
|
|
|
* - The user buffer, ubuffer, is known to be large enough to accept the
|
|
|
|
* largest message that an be sent on this message queue
|
2007-03-29 15:25:18 +02:00
|
|
|
* - Pre-emption should be disabled throughout this call.
|
|
|
|
*
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2017-10-09 17:06:46 +02:00
|
|
|
ssize_t nxmq_do_receive(mqd_t mqdes, FAR struct mqueue_msg_s *mqmsg,
|
2019-02-16 02:18:55 +01:00
|
|
|
FAR char *ubuffer, unsigned int *prio)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *btcb;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2014-09-29 21:19:11 +02:00
|
|
|
FAR struct mqueue_inode_s *msgq;
|
2013-02-04 19:46:28 +01:00
|
|
|
ssize_t rcvmsglen;
|
2007-03-29 15:25:18 +02:00
|
|
|
|
|
|
|
/* Get the length of the message (also the return value) */
|
|
|
|
|
|
|
|
rcvmsglen = mqmsg->msglen;
|
|
|
|
|
|
|
|
/* Copy the message into the caller's buffer */
|
|
|
|
|
2015-10-08 03:59:14 +02:00
|
|
|
memcpy(ubuffer, (FAR const void *)mqmsg->mail, rcvmsglen);
|
2007-03-29 15:25:18 +02:00
|
|
|
|
|
|
|
/* Copy the message priority as well (if a buffer is provided) */
|
|
|
|
|
|
|
|
if (prio)
|
|
|
|
{
|
|
|
|
*prio = mqmsg->priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are done with the message. Deallocate it now. */
|
|
|
|
|
2017-10-09 17:06:46 +02:00
|
|
|
nxmq_free_msg(mqmsg);
|
2007-03-29 15:25:18 +02:00
|
|
|
|
|
|
|
/* Check if any tasks are waiting for the MQ not full event. */
|
|
|
|
|
|
|
|
msgq = mqdes->msgq;
|
|
|
|
if (msgq->nwaitnotfull > 0)
|
|
|
|
{
|
|
|
|
/* Find the highest priority task that is waiting for
|
|
|
|
* this queue to be not-full in g_waitingformqnotfull list.
|
|
|
|
* This must be performed in a critical section because
|
|
|
|
* messages can be sent from interrupt handlers.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2015-10-08 03:59:14 +02:00
|
|
|
for (btcb = (FAR struct tcb_s *)g_waitingformqnotfull.head;
|
2007-03-29 15:25:18 +02:00
|
|
|
btcb && btcb->msgwaitq != msgq;
|
|
|
|
btcb = btcb->flink);
|
|
|
|
|
|
|
|
/* If one was found, unblock it. NOTE: There is a race
|
|
|
|
* condition here: the queue might be full again by the
|
|
|
|
* time the task is unblocked
|
|
|
|
*/
|
|
|
|
|
2018-02-06 16:13:16 +01:00
|
|
|
DEBUGASSERT(btcb != NULL);
|
2013-04-25 23:19:59 +02:00
|
|
|
|
2017-08-15 01:19:27 +02:00
|
|
|
btcb->msgwaitq = NULL;
|
|
|
|
msgq->nwaitnotfull--;
|
|
|
|
up_unblock_task(btcb);
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the length of the message transferred to the user buffer */
|
|
|
|
|
|
|
|
return rcvmsglen;
|
|
|
|
}
|