2008-01-30 15:01:22 +01:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/mqueue/mq_rcvinternal.c
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-03-10 16:26:30 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
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>
|
|
|
|
|
2022-11-13 13:39:14 +01:00
|
|
|
#include <stdbool.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:
|
2020-12-22 15:02:48 +01:00
|
|
|
* msgq - Message queue descriptor
|
|
|
|
* msg - Buffer to receive the message
|
2007-03-29 15:25:18 +02:00
|
|
|
* msglen - Size of the buffer in bytes
|
|
|
|
*
|
2017-10-10 17:57:40 +02:00
|
|
|
* Returned Value:
|
2021-05-06 04:18:49 +02:00
|
|
|
* On success, zero (OK) is returned. A negated errno value is returned
|
2017-10-10 17:57:40 +02:00
|
|
|
* on any failure:
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2023-06-09 04:13:19 +02:00
|
|
|
* EBADF 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.
|
2020-12-22 15:02:48 +01:00
|
|
|
* EINVAL Invalid 'msg' or 'msgq'
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2008-01-30 15:01:22 +01:00
|
|
|
****************************************************************************/
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2022-06-09 07:37:11 +02:00
|
|
|
#ifdef CONFIG_DEBUG_FEATURES
|
|
|
|
int nxmq_verify_receive(FAR struct file *mq, FAR char *msg, size_t msglen)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2022-06-09 07:37:11 +02:00
|
|
|
FAR struct inode *inode = mq->f_inode;
|
|
|
|
FAR struct mqueue_inode_s *msgq;
|
|
|
|
|
|
|
|
if (inode == NULL)
|
|
|
|
{
|
|
|
|
return -EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
msgq = inode->i_private;
|
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/* Verify the input parameters */
|
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
if (!msg || !msgq)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
return -EINVAL;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 07:37:11 +02:00
|
|
|
if ((mq->f_oflags & O_RDOK) == 0)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2023-06-09 04:13:19 +02:00
|
|
|
return -EBADF;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
if (msglen < (size_t)msgq->maxmsgsize)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
return -EMSGSIZE;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2022-06-09 07:37:11 +02:00
|
|
|
#endif
|
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_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:
|
2020-12-22 15:02:48 +01:00
|
|
|
* msgq - Message queue descriptor
|
|
|
|
* oflags - flags from user set
|
2017-10-10 17:57:40 +02:00
|
|
|
* 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:
|
2021-05-06 04:18:49 +02:00
|
|
|
* On success, zero (OK) is returned. A negated errno value is returned
|
2017-10-10 17:57:40 +02:00
|
|
|
* 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
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
int nxmq_wait_receive(FAR struct mqueue_inode_s *msgq,
|
|
|
|
int oflags, FAR struct mqueue_msg_s **rcvmsg)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2017-10-10 17:57:40 +02:00
|
|
|
FAR struct mqueue_msg_s *newmsg;
|
2022-06-08 14:39:03 +02:00
|
|
|
FAR struct tcb_s *rtcb;
|
2017-10-10 17:57:40 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(rcvmsg != NULL);
|
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 the message from the head of the queue */
|
|
|
|
|
2022-06-08 16:42:01 +02:00
|
|
|
while ((newmsg = (FAR struct mqueue_msg_s *)
|
|
|
|
list_remove_head(&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
|
|
|
*/
|
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
if ((oflags & O_NONBLOCK) == 0)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2008-01-30 15:01:22 +01:00
|
|
|
/* Yes.. Block and try again */
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2022-09-22 04:31:40 +02:00
|
|
|
rtcb = this_task();
|
|
|
|
rtcb->waitobj = msgq;
|
2022-10-17 09:49:16 +02:00
|
|
|
msgq->cmn.nwaitnotempty++;
|
2007-03-29 15:25:18 +02:00
|
|
|
|
2020-05-07 22:15:09 +02:00
|
|
|
/* Initialize the 'errcode" used to communication wake-up error
|
2017-10-10 17:57:40 +02:00
|
|
|
* conditions.
|
|
|
|
*/
|
|
|
|
|
2020-05-07 22:15:09 +02:00
|
|
|
rtcb->errcode = OK;
|
2017-10-10 17:57:40 +02:00
|
|
|
|
2018-11-30 13:54:15 +01:00
|
|
|
/* Make sure this is not the idle task, descheduling that
|
|
|
|
* isn't going to end well.
|
|
|
|
*/
|
|
|
|
|
2022-10-28 08:22:17 +02:00
|
|
|
DEBUGASSERT(!is_idle_task(rtcb));
|
2022-11-13 13:39:14 +01:00
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
/* Remove the tcb task from the running list. */
|
2022-11-13 13:39:14 +01:00
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
nxsched_remove_self(rtcb);
|
2022-11-13 13:39:14 +01:00
|
|
|
|
|
|
|
/* Add the task to the specified blocked task list */
|
|
|
|
|
|
|
|
rtcb->task_state = TSTATE_WAIT_MQNOTEMPTY;
|
|
|
|
nxsched_add_prioritized(rtcb, MQ_WNELIST(msgq->cmn));
|
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
/* Now, perform the context switch */
|
2022-11-13 13:39:14 +01:00
|
|
|
|
2024-01-19 13:57:04 +01:00
|
|
|
up_switch_context(this_task(), rtcb);
|
2007-03-29 15:25:18 +02:00
|
|
|
|
|
|
|
/* 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).
|
|
|
|
*/
|
|
|
|
|
2022-06-08 14:39:03 +02:00
|
|
|
if (rtcb->errcode != OK)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2022-06-08 14:39:03 +02:00
|
|
|
return -rtcb->errcode;
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The queue was empty, and the O_NONBLOCK flag was set for the
|
2020-12-22 15:02:48 +01:00
|
|
|
* message queue description.
|
2007-03-29 15:25:18 +02:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
{
|
2021-01-08 06:43:33 +01:00
|
|
|
if (msgq->nmsgs-- == msgq->maxmsgs)
|
|
|
|
{
|
|
|
|
nxmq_pollnotify(msgq, POLLOUT);
|
|
|
|
}
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
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:
|
2020-12-22 15:02:48 +01:00
|
|
|
* msgq - Message queue descriptor
|
2007-03-29 15:25:18 +02:00
|
|
|
* 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:
|
2020-03-10 16:26:30 +01: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
|
|
|
|
2020-12-22 15:02:48 +01:00
|
|
|
ssize_t nxmq_do_receive(FAR struct mqueue_inode_s *msgq,
|
|
|
|
FAR struct mqueue_msg_s *mqmsg,
|
2022-08-16 06:20:39 +02:00
|
|
|
FAR char *ubuffer, FAR unsigned int *prio)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2013-02-04 19:46:28 +01:00
|
|
|
FAR struct tcb_s *btcb;
|
|
|
|
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. */
|
|
|
|
|
2022-10-17 09:49:16 +02:00
|
|
|
if (msgq->cmn.nwaitnotfull > 0)
|
2007-03-29 15:25:18 +02:00
|
|
|
{
|
2022-11-01 02:52:11 +01:00
|
|
|
FAR struct tcb_s *rtcb = this_task();
|
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/* Find the highest priority task that is waiting for
|
2022-09-09 08:26:43 +02:00
|
|
|
* this queue to be not-full in waitfornotfull list.
|
2007-03-29 15:25:18 +02:00
|
|
|
* This must be performed in a critical section because
|
|
|
|
* messages can be sent from interrupt handlers.
|
|
|
|
*/
|
|
|
|
|
2022-11-01 02:52:11 +01:00
|
|
|
btcb = (FAR struct tcb_s *)dq_remfirst(MQ_WNFLIST(msgq->cmn));
|
2007-03-29 15:25:18 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
2022-09-06 10:33:58 +02:00
|
|
|
if (WDOG_ISACTIVE(&btcb->waitdog))
|
|
|
|
{
|
|
|
|
wd_cancel(&btcb->waitdog);
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:49:16 +02:00
|
|
|
msgq->cmn.nwaitnotfull--;
|
2022-11-01 02:52:11 +01:00
|
|
|
|
|
|
|
/* Indicate that the wait is over. */
|
|
|
|
|
|
|
|
btcb->waitobj = NULL;
|
|
|
|
|
|
|
|
/* Add the task to ready-to-run task list and
|
|
|
|
* perform the context switch if one is needed
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nxsched_add_readytorun(btcb))
|
|
|
|
{
|
2022-11-15 09:52:40 +01:00
|
|
|
up_switch_context(btcb, rtcb);
|
2022-11-01 02:52:11 +01:00
|
|
|
}
|
2007-03-29 15:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the length of the message transferred to the user buffer */
|
|
|
|
|
|
|
|
return rcvmsglen;
|
|
|
|
}
|