2007-03-29 15:25:18 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/mqueue/mq_send.c
|
2007-02-18 00:21:28 +01: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-02-18 00:21:28 +01:00
|
|
|
*
|
2007-03-29 15:25:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2007-03-29 15:25:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2023-03-10 01:20:48 +01:00
|
|
|
#include <nuttx/config.h>
|
2012-05-17 18:55:13 +02:00
|
|
|
|
2023-03-10 01:20:48 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <mqueue.h>
|
|
|
|
#include <sys/types.h>
|
2012-05-17 18:55:13 +02:00
|
|
|
|
2023-03-10 01:20:48 +01:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/cancelpt.h>
|
|
|
|
#include <nuttx/irq.h>
|
2012-05-17 18:55:13 +02:00
|
|
|
|
2023-03-10 01:20:48 +01:00
|
|
|
#include "mqueue/mqueue.h"
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Public Functions
|
2007-03-29 15:25:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/****************************************************************************
|
2020-12-24 13:53:17 +01:00
|
|
|
* Name: file_mq_send
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2014-12-06 02:16:14 +01:00
|
|
|
* This function adds the specified message (msg) to the message queue
|
2020-12-24 13:53:17 +01:00
|
|
|
* (mq). This is an internal OS interface. It is functionally
|
2017-10-10 16:43:10 +02:00
|
|
|
* equivalent to mq_send() except that:
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2019-08-23 19:57:35 +02:00
|
|
|
* - It is not a cancellation point, and
|
2017-10-10 16:43:10 +02:00
|
|
|
* - It does not modify the errno value.
|
2007-03-29 15:25:18 +02:00
|
|
|
*
|
2017-10-10 16:43:10 +02:00
|
|
|
* See comments with mq_send() for a more complete description of the
|
|
|
|
* behavior of this function
|
2014-04-13 22:32:20 +02:00
|
|
|
*
|
2017-10-10 16:43:10 +02:00
|
|
|
* Input Parameters:
|
2020-12-24 13:53:17 +01:00
|
|
|
* mq - Message queue descriptor
|
2017-10-10 16:43:10 +02:00
|
|
|
* msg - Message to send
|
2007-02-18 00:21:28 +01:00
|
|
|
* msglen - The length of the message in bytes
|
2017-10-10 16:43:10 +02:00
|
|
|
* prio - The priority of the message
|
2007-03-28 16:48:42 +02:00
|
|
|
*
|
2017-10-10 16:43:10 +02:00
|
|
|
* Returned Value:
|
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
* (see mq_send() for the list list valid return values).
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2007-03-29 15:25:18 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
int file_mq_send(FAR struct file *mq, FAR const char *msg, size_t msglen,
|
|
|
|
unsigned int prio)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2020-12-22 15:02:48 +01:00
|
|
|
FAR struct mqueue_inode_s *msgq;
|
2022-06-08 14:39:03 +02:00
|
|
|
FAR struct mqueue_msg_s *mqmsg;
|
2016-02-14 15:17:46 +01:00
|
|
|
irqstate_t flags;
|
2017-10-10 16:43:10 +02:00
|
|
|
int ret;
|
2016-12-09 23:50:34 +01:00
|
|
|
|
2007-03-29 15:25:18 +02:00
|
|
|
/* Verify the input parameters -- setting errno appropriately
|
|
|
|
* on any failures to verify.
|
|
|
|
*/
|
2007-03-28 16:48:42 +02:00
|
|
|
|
2022-06-09 07:37:11 +02:00
|
|
|
ret = nxmq_verify_send(mq, msg, msglen, prio);
|
2017-10-10 22:24:13 +02:00
|
|
|
if (ret < 0)
|
2007-03-28 16:48:42 +02:00
|
|
|
{
|
2017-10-10 16:43:10 +02:00
|
|
|
return ret;
|
2007-03-28 16:48:42 +02:00
|
|
|
}
|
|
|
|
|
2022-06-09 07:37:11 +02:00
|
|
|
msgq = mq->f_inode->i_private;
|
|
|
|
|
2007-03-29 15:25:18 +02: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
|
2022-06-27 02:04:32 +02:00
|
|
|
* non-FULL. This would fail with EAGAIN, EINTR, or ETIMEDOUT.
|
2007-03-28 16:48:42 +02:00
|
|
|
*/
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2016-09-15 20:42:24 +02:00
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
if (!up_interrupt_context()) /* In an interrupt handler? */
|
|
|
|
{
|
|
|
|
/* No.. Not in an interrupt handler. Is the message queue FULL? */
|
2016-09-15 20:42:24 +02:00
|
|
|
|
2021-01-28 00:52:22 +01:00
|
|
|
if (msgq->nmsgs >= msgq->maxmsgs)
|
2016-09-15 20:42:24 +02:00
|
|
|
{
|
2019-03-01 17:50:02 +01:00
|
|
|
/* Yes.. the message queue is full. Wait for space to become
|
|
|
|
* available in the message queue.
|
2017-10-10 16:43:10 +02:00
|
|
|
*/
|
2016-09-15 20:42:24 +02:00
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
ret = nxmq_wait_send(msgq, mq->f_oflags);
|
2016-09-15 20:42:24 +02:00
|
|
|
}
|
2007-03-28 16:48:42 +02:00
|
|
|
}
|
2017-10-10 16:43:10 +02:00
|
|
|
|
|
|
|
/* ret can only be negative if nxmq_wait_send failed */
|
|
|
|
|
2022-06-08 14:39:03 +02:00
|
|
|
if (ret == OK)
|
2007-03-28 16:48:42 +02:00
|
|
|
{
|
2017-10-10 16:43:10 +02:00
|
|
|
/* Now allocate the message. */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
mqmsg = nxmq_alloc_msg();
|
2022-09-09 12:00:06 +02:00
|
|
|
DEBUGASSERT(mqmsg != NULL);
|
2017-10-10 16:43:10 +02:00
|
|
|
|
2018-09-14 14:55:45 +02:00
|
|
|
/* Check if the message was successfully allocated */
|
2017-10-10 16:43:10 +02:00
|
|
|
|
2016-09-15 20:42:24 +02:00
|
|
|
/* The allocation was successful (implying that we can also send the
|
|
|
|
* message). Perform the message send.
|
2015-03-10 17:41:21 +01:00
|
|
|
*
|
|
|
|
* NOTE: There is a race condition here: What if a message is added by
|
|
|
|
* interrupt related logic so that queue again becomes non-empty.
|
2017-10-09 17:06:46 +02:00
|
|
|
* That is handled because nxmq_do_send() will permit the maxmsgs limit
|
2015-03-10 17:41:21 +01:00
|
|
|
* to be exceeded in that case.
|
|
|
|
*/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-09-09 12:00:06 +02:00
|
|
|
ret = nxmq_do_send(msgq, mqmsg, msg, msglen, prio);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
|
|
|
|
2022-06-08 14:39:03 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-12-24 13:53:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxmq_send
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function adds the specified message (msg) to the message queue
|
|
|
|
* (mqdes). This is an internal OS interface. It is functionally
|
|
|
|
* equivalent to mq_send() except that:
|
|
|
|
*
|
|
|
|
* - It is not a cancellation point, and
|
|
|
|
* - It does not modify the errno value.
|
|
|
|
*
|
|
|
|
* See comments with mq_send() for a more complete description of the
|
|
|
|
* behavior of this function
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mqdes - Message queue descriptor
|
|
|
|
* msg - Message to send
|
|
|
|
* msglen - The length of the message in bytes
|
|
|
|
* prio - The priority of the message
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* This is an internal OS interface and should not be used by applications.
|
|
|
|
* It follows the NuttX internal error return policy: Zero (OK) is
|
|
|
|
* returned on success. A negated errno value is returned on failure.
|
|
|
|
* (see mq_send() for the list list valid return values).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nxmq_send(mqd_t mqdes, FAR const char *msg, size_t msglen,
|
|
|
|
unsigned int prio)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = fs_getfilep(mqdes, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_mq_send(filep, msg, msglen, prio);
|
|
|
|
}
|
|
|
|
|
2017-10-10 16:43:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: mq_send
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function adds the specified 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" argument.
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* mqdes - Message queue descriptor
|
|
|
|
* msg - Message to send
|
|
|
|
* msglen - The length of the message in bytes
|
|
|
|
* prio - The priority of the message
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* On success, mq_send() returns 0 (OK); on error, -1 (ERROR)
|
|
|
|
* is returned, with errno set to indicate the error:
|
|
|
|
*
|
|
|
|
* EAGAIN The queue was full 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.
|
2023-06-09 11:45:22 +02:00
|
|
|
* EBADF Message queue opened not opened for writing.
|
2017-10-10 16:43:10 +02:00
|
|
|
* EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the
|
|
|
|
* message queue.
|
|
|
|
* EINTR The call was interrupted by a signal handler.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-03-10 16:26:30 +01:00
|
|
|
int mq_send(mqd_t mqdes, FAR const char *msg, size_t msglen,
|
|
|
|
unsigned int prio)
|
2017-10-10 16:43:10 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* mq_send() is a cancellation point */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
enter_cancellation_point();
|
2017-10-10 16:43:10 +02:00
|
|
|
|
|
|
|
/* Let nxmq_send() do all of the work */
|
|
|
|
|
|
|
|
ret = nxmq_send(mqdes, msg, msglen, prio);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
set_errno(-ret);
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
|
|
|
|
2016-12-09 23:50:34 +01:00
|
|
|
leave_cancellation_point();
|
2007-03-29 15:25:18 +02:00
|
|
|
return ret;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|