2014-09-30 04:59:31 +08:00
|
|
|
/****************************************************************************
|
2021-03-09 05:39:04 +08:00
|
|
|
* sched/mqueue/mq_msgqalloc.c
|
2014-09-30 04:59:31 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-03-10 23:26:30 +08: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-30 04:59:31 +08:00
|
|
|
*
|
2020-03-10 23:26:30 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-09-30 04:59:31 +08:00
|
|
|
*
|
2020-03-10 23:26:30 +08: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-30 04:59:31 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <mqueue.h>
|
2014-12-06 21:18:48 +08:00
|
|
|
#include <assert.h>
|
2014-09-30 04:59:31 +08: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 22:43:10 +08:00
|
|
|
* Name: nxmq_alloc_msgq
|
2014-09-30 04:59:31 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function implements a part of the POSIX message queue open logic.
|
2014-12-06 21:18:48 +08:00
|
|
|
* It allocates and initializes a struct mqueue_inode_s structure.
|
2014-09-30 04:59:31 +08:00
|
|
|
*
|
2018-03-13 23:52:27 +08:00
|
|
|
* Input Parameters:
|
2014-09-30 04:59:31 +08: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 21:18:48 +08:00
|
|
|
* messages that may be placed in the message queue.
|
2021-07-12 22:15:25 +08:00
|
|
|
* pmsgq - This parameter is a address of a pointer
|
2014-09-30 04:59:31 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Returned Value:
|
2021-07-12 22:15:25 +08: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-30 04:59:31 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-07-12 22:15:25 +08:00
|
|
|
int nxmq_alloc_msgq(FAR struct mq_attr *attr,
|
|
|
|
FAR struct mqueue_inode_s **pmsgq)
|
2014-09-30 04:59:31 +08:00
|
|
|
{
|
|
|
|
FAR struct mqueue_inode_s *msgq;
|
|
|
|
|
2014-12-06 21:18:48 +08:00
|
|
|
/* Check if the caller is attempting to allocate a message for messages
|
|
|
|
* larger than the configured maximum message size.
|
|
|
|
*/
|
|
|
|
|
2021-07-12 22:15:25 +08:00
|
|
|
DEBUGASSERT((!attr || attr->mq_msgsize <= MQ_MAX_BYTES) && pmsgq);
|
|
|
|
if ((attr && attr->mq_msgsize > MQ_MAX_BYTES) || !pmsgq)
|
2014-12-06 21:18:48 +08:00
|
|
|
{
|
2021-07-12 22:15:25 +08:00
|
|
|
return -EINVAL;
|
2014-12-06 21:18:48 +08:00
|
|
|
}
|
|
|
|
|
2014-09-30 04:59:31 +08:00
|
|
|
/* Allocate memory for the new message queue. */
|
|
|
|
|
2015-10-08 09:59:14 +08:00
|
|
|
msgq = (FAR struct mqueue_inode_s *)
|
2014-12-06 21:18:48 +08:00
|
|
|
kmm_zalloc(sizeof(struct mqueue_inode_s));
|
|
|
|
|
2014-09-30 04:59:31 +08:00
|
|
|
if (msgq)
|
|
|
|
{
|
|
|
|
/* Initialize the new named message queue */
|
|
|
|
|
2022-06-08 22:42:01 +08:00
|
|
|
list_initialize(&msgq->msglist);
|
2014-09-30 04:59:31 +08:00
|
|
|
if (attr)
|
|
|
|
{
|
2014-12-06 21:18:48 +08:00
|
|
|
msgq->maxmsgs = (int16_t)attr->mq_maxmsg;
|
|
|
|
msgq->maxmsgsize = (int16_t)attr->mq_msgsize;
|
2014-09-30 04:59:31 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msgq->maxmsgs = MQ_MAX_MSGS;
|
|
|
|
msgq->maxmsgsize = MQ_MAX_BYTES;
|
|
|
|
}
|
|
|
|
|
2022-06-09 15:19:01 +08:00
|
|
|
#ifndef CONFIG_DISABLE_MQUEUE_NOTIFICATION
|
2014-09-30 04:59:31 +08:00
|
|
|
msgq->ntpid = INVALID_PROCESS_ID;
|
2022-06-09 15:19:01 +08:00
|
|
|
#endif
|
2022-09-09 14:26:43 +08:00
|
|
|
|
2022-10-17 15:49:16 +08:00
|
|
|
dq_init(&msgq->cmn.waitfornotempty);
|
|
|
|
dq_init(&msgq->cmn.waitfornotfull);
|
2014-09-30 04:59:31 +08:00
|
|
|
}
|
2021-07-12 22:15:25 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return -ENOSPC;
|
|
|
|
}
|
2014-09-30 04:59:31 +08:00
|
|
|
|
2021-07-12 22:15:25 +08:00
|
|
|
*pmsgq = msgq;
|
|
|
|
return OK;
|
2014-09-30 04:59:31 +08:00
|
|
|
}
|