2014-09-29 22:59:31 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* sched/mqueue/mq_msgqalloc.c
|
2014-09-29 22:59:31 +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
|
2014-09-29 22:59:31 +02:00
|
|
|
*
|
2020-03-10 16:26:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-09-29 22:59:31 +02:00
|
|
|
*
|
2020-03-10 16:26:30 +01:00
|
|
|
* 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.
|
2014-09-29 22:59:31 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <mqueue.h>
|
2014-12-06 14:18:48 +01:00
|
|
|
#include <assert.h>
|
2014-09-29 22:59:31 +02:00
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/sched.h>
|
|
|
|
#include <nuttx/mqueue.h>
|
|
|
|
|
|
|
|
#include "sched/sched.h"
|
|
|
|
#include "mqueue/mqueue.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-10-10 16:43:10 +02:00
|
|
|
* Name: nxmq_alloc_msgq
|
2014-09-29 22:59:31 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements a part of the POSIX message queue open logic.
|
2014-12-06 14:18:48 +01:00
|
|
|
* It allocates and initializes a struct mqueue_inode_s structure.
|
2014-09-29 22:59:31 +02:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2014-09-29 22:59:31 +02:00
|
|
|
* attr - The mq_maxmsg attribute is used at the time that the message
|
|
|
|
* queue is created to determine the maximum number of
|
2014-12-06 14:18:48 +01:00
|
|
|
* messages that may be placed in the message queue.
|
2021-07-12 16:15:25 +02:00
|
|
|
* pmsgq - This parameter is a address of a pointer
|
2014-09-29 22:59:31 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2021-07-12 16:15:25 +02:00
|
|
|
* Zero (OK) is returned on success. Otherwise, a negated errno value is
|
|
|
|
* returned to indicate the nature of the failure.
|
|
|
|
*
|
|
|
|
* EINVAL attr is NULL or either attr->mq_mqssize or attr->mq_maxmsg
|
|
|
|
* have an invalid value
|
|
|
|
* ENOSPC There is insufficient space for the creation of the new
|
|
|
|
* message queue
|
2014-09-29 22:59:31 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-07-12 16:15:25 +02:00
|
|
|
int nxmq_alloc_msgq(FAR struct mq_attr *attr,
|
|
|
|
FAR struct mqueue_inode_s **pmsgq)
|
2014-09-29 22:59:31 +02:00
|
|
|
{
|
|
|
|
FAR struct mqueue_inode_s *msgq;
|
|
|
|
|
2014-12-06 14:18:48 +01:00
|
|
|
/* Check if the caller is attempting to allocate a message for messages
|
|
|
|
* larger than the configured maximum message size.
|
|
|
|
*/
|
|
|
|
|
2021-07-12 16:15:25 +02:00
|
|
|
DEBUGASSERT((!attr || attr->mq_msgsize <= MQ_MAX_BYTES) && pmsgq);
|
|
|
|
if ((attr && attr->mq_msgsize > MQ_MAX_BYTES) || !pmsgq)
|
2014-12-06 14:18:48 +01:00
|
|
|
{
|
2021-07-12 16:15:25 +02:00
|
|
|
return -EINVAL;
|
2014-12-06 14:18:48 +01:00
|
|
|
}
|
|
|
|
|
2014-09-29 22:59:31 +02:00
|
|
|
/* Allocate memory for the new message queue. */
|
|
|
|
|
2015-10-08 03:59:14 +02:00
|
|
|
msgq = (FAR struct mqueue_inode_s *)
|
2014-12-06 14:18:48 +01:00
|
|
|
kmm_zalloc(sizeof(struct mqueue_inode_s));
|
|
|
|
|
2014-09-29 22:59:31 +02:00
|
|
|
if (msgq)
|
|
|
|
{
|
|
|
|
/* Initialize the new named message queue */
|
|
|
|
|
2022-06-08 16:42:01 +02:00
|
|
|
list_initialize(&msgq->msglist);
|
2014-09-29 22:59:31 +02:00
|
|
|
if (attr)
|
|
|
|
{
|
2014-12-06 14:18:48 +01:00
|
|
|
msgq->maxmsgs = (int16_t)attr->mq_maxmsg;
|
|
|
|
msgq->maxmsgsize = (int16_t)attr->mq_msgsize;
|
2014-09-29 22:59:31 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msgq->maxmsgs = MQ_MAX_MSGS;
|
|
|
|
msgq->maxmsgsize = MQ_MAX_BYTES;
|
|
|
|
}
|
|
|
|
|
2022-06-09 09:19:01 +02:00
|
|
|
#ifndef CONFIG_DISABLE_MQUEUE_NOTIFICATION
|
2014-09-29 22:59:31 +02:00
|
|
|
msgq->ntpid = INVALID_PROCESS_ID;
|
2022-06-09 09:19:01 +02:00
|
|
|
#endif
|
2022-09-09 08:26:43 +02:00
|
|
|
|
2022-10-17 09:49:16 +02:00
|
|
|
dq_init(&msgq->cmn.waitfornotempty);
|
|
|
|
dq_init(&msgq->cmn.waitfornotfull);
|
2014-09-29 22:59:31 +02:00
|
|
|
}
|
2021-07-12 16:15:25 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
2014-09-29 22:59:31 +02:00
|
|
|
|
2021-07-12 16:15:25 +02:00
|
|
|
*pmsgq = msgq;
|
|
|
|
return OK;
|
2014-09-29 22:59:31 +02:00
|
|
|
}
|