pkarashchenko commented on code in PR #7088: URL: https://github.com/apache/incubator-nuttx/pull/7088#discussion_r999598724
########## include/sys/msg.h: ########## @@ -0,0 +1,212 @@ +/**************************************************************************** + * include/sys/msg.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_SYS_MSG_H +#define __INCLUDE_SYS_MSG_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <sys/ipc.h> +#include <time.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The MSG_NOERROR identifier value, the msqid_ds struct and the msg struct + * are as defined by the SV API Intel 386 Processor Supplement. + */ + +#define MSG_NOERROR 010000 /* No error if message is too big */ +#define MSG_EXCEPT 020000 /* Recv any msg except of specified type.*/ +#define MSG_COPY 040000 /* Copy (not remove) all queue messages */ + +/**************************************************************************** + * Public Type Declarations + ****************************************************************************/ + +typedef unsigned long msgqnum_t; +typedef unsigned long msglen_t; + +struct msqid_ds +{ + struct ipc_perm msg_perm; /* Ownership and permissions */ + time_t msg_stime; /* Time of last msgsnd(2) */ + time_t msg_rtime; /* Time of last msgrcv(2) */ + time_t msg_ctime; /* Time of last change */ + unsigned long msg_cbytes; /* Current number of bytes in + * queue (nonstandard) */ + msgqnum_t msg_qnum; /* Current number of messages + * in queue */ + msglen_t msg_qbytes; /* Maximum number of bytes + * allowed in queue */ + pid_t msg_lspid; /* PID of last msgsnd(2) */ + pid_t msg_lrpid; /* PID of last msgrcv(2) */ +}; + +/* Structure describing a message. The SVID doesn't suggest any + * particular name for this structure. There is a reference in the + * msgop man page that reads "The structure mymsg is an example of what + * this user defined buffer might look like, and includes the following + * members:". This sentence is followed by two lines equivalent + * to the mtype and mtext field declarations below. It isn't clear + * if "mymsg" refers to the name of the structure type or the name of an + * instance of the structure... + */ + +struct mymsg +{ + long mtype; /* message type (+ve integer) */ + char mtext[1]; /* message body */ +}; + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: msgctl + * + * Description: + * System V message control operations. + * msgctl() performs the control operation specified by cmd on the + * System V message queue with identifier msqid. + * + * Input Parameters: + * msqid - System V message queue identifier + * cmd - Command operations + * msqid_ds - Defines a message queue + * + * Returned Value: + * On success, IPC_STAT, IPC_SET, and IPC_RMID return 0. A + * successful IPC_INFO or MSG_INFO operation returns the index of + * the highest used entry in the kernel's internal array recording + * information about all message queues. (This information can be + * used with repeated MSG_STAT or MSG_STAT_ANY operations to obtain + * information about all queues on the system.) A successful + * MSG_STAT or MSG_STAT_ANY operation returns the identifier of the + * queue whose index was given in msqid. + * + * On failure, -1 is returned and errno is set to indicate the error. + * + ****************************************************************************/ + +int msgctl(int msqid, int cmd, FAR struct msqid_ds *buf); + +/**************************************************************************** + * Name: msgget + * + * Description: + * Get a System V message queue identifier + * The msgget() system call returns the System V message queue + * identifier associated with the value of the key argument. It may + * be used either to obtain the identifier of a previously created + * message queue (when msgflg is zero and key does not have the + * value IPC_PRIVATE), or to create a new set. + * + * Input Parameters: + * key - Key associated with the message queue + * msgflg - Operations and permissions flag + * + * Returned Value: + * On success, msgget() returns the message queue identifier (a + * nonnegative integer). On failure, -1 is returned, and errno is + * set to indicate the error. + * + ****************************************************************************/ + +int msgget(key_t key, int msgflg); + +/**************************************************************************** + * Name: msgsnd + * + * Description: + * The msgsnd() function is used to send a message to the queue + * associated with the message queue identifier specified by msqid. + * The msgp argument points to a user-defined buffer that must contain + * first a field of type long int that will specify the type of the + * message, and then a data portion that will hold the data bytes of + * the message. + * + * Input Parameters: + * msqid - Message queue identifier + * msgp - Pointer to a buffer with the message to be sent + * msgsz - Length of the data part of the message to be sent + * msgflg - Operations flags + * + * 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. + * EPERM Message queue opened not opened for writing. + * EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the + * message queue. + * EINTR The call was interrupted by a signal handler. + * + ****************************************************************************/ + +int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); + +/**************************************************************************** + * Name: msgrcv + * + * Description: + * The msgrcv() function reads a message from the message queue specified + * by the msqid parameter and places it in the user-defined buffer + * pointed to by the *msgp parameter. + * + * Input Parameters: + * msqid - Message queue identifier + * msgp - Pointer to a buffer in which the received message will be + * stored + * msgsz - Length of the data part of the buffer + * msgtyp - Type of message to be received. + * msgflg - Operations flags. + * + * Returned Value: + * On success, msgrcv() returns the number of bytes actually copied + * into the mtext array. + * On failure, both functions return -1, and set errno to indicate + * the error. + * + ****************************************************************************/ + +ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg); Review Comment: ```suggestion ssize_t msgrcv(int msqid, FAR void *msgp, size_t msgsz, long msgtyp, int msgflg); ``` ########## sched/mqueue/msg.h: ########## @@ -0,0 +1,98 @@ +/******************************************************************************** + * sched/mqueue/msg.h + * + * 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. + * + ********************************************************************************/ + +#ifndef __SCHED_MQUEUE_MSG_H +#define __SCHED_MQUEUE_MSG_H + +/******************************************************************************** + * Included Files + ********************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <nuttx/irq.h> +#include <nuttx/list.h> +#include <nuttx/mqueue.h> +#include <sys/msg.h> + +#include <errno.h> + +#if defined(CONFIG_MQ_MAXMSGSIZE) && CONFIG_MQ_MAXMSGSIZE > 0 + +/******************************************************************************** + * Pre-processor Definitions + ********************************************************************************/ + +#define MSG_MAX_BYTES CONFIG_MQ_MAXMSGSIZE +#define MSG_MAX_MSGS 16 + +/******************************************************************************** + * Public Type Definitions + ********************************************************************************/ + +struct msgq_s +{ + struct mqueue_cmn_s cmn; + struct list_node msglist; /* Prioritized message list */ + key_t key; + int16_t maxmsgs; /* Maximum number of messages in the queue */ + int16_t nmsgs; /* Number of message in the queue */ + uint16_t maxmsgsize; /* Max size of message in message queue */ +}; + +struct msgbuf_s +{ + struct list_node node; + uint16_t msize; /* Message data length */ + long mtype; /* Message type, must be > 0 */ + char mtext[MSG_MAX_BYTES]; /* Message data */ +}; + +/******************************************************************************** + * Public Data + ********************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +EXTERN struct list_node g_msgfreelist; + +/******************************************************************************** + * Public Function Prototypes + ********************************************************************************/ + +void nxmsg_initialize(void); +int nxmsg_alloc(FAR struct msgq_s **pmsgq); +void nxmsg_free(FAR struct msgq_s *msgq); +FAR struct msgq_s *nxmsg_lookup(key_t key); Review Comment: description is missing ########## sched/mqueue/msginternal.c: ########## @@ -0,0 +1,198 @@ +/**************************************************************************** + * sched/mqueue/msginternal.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/kmalloc.h> +#include "mqueue/msg.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define MQ_PERBLOCK 10 + +/**************************************************************************** + * Private Type Definitions + ****************************************************************************/ + +struct nxmsg_manager_s +{ + uint8_t nmsgq; /* The number of groups of msgs array */ + FAR struct msgq_s **msgqs; /* The pointer of two layer file descriptors array */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static struct nxmsg_manager_s g_msgmgrs; + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +struct list_node g_msgfreelist = LIST_INITIAL_VALUE(g_msgfreelist); + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxmsg_alloc_internal + ****************************************************************************/ + +static FAR struct msgq_s *nxmsg_alloc_internal(void) +{ + FAR struct msgq_s *msgq; + FAR struct msgq_s **tmp; + int i; + + msgq = kmm_zalloc(sizeof(struct msgq_s)); + if (msgq == NULL) + { + return NULL; + } + + for (i = 0; i < g_msgmgrs.nmsgq; i++) + { + if (g_msgmgrs.msgqs[i] == NULL) + { + g_msgmgrs.msgqs[i] = msgq; + msgq->key = i + 1; + return msgq; + } + } + + tmp = kmm_realloc(g_msgmgrs.msgqs, sizeof(FAR void *) * + (g_msgmgrs.nmsgq + MQ_PERBLOCK)); Review Comment: ```suggestion tmp = kmm_realloc(g_msgmgrs.msgqs, sizeof(FAR void *) * (g_msgmgrs.nmsgq + MQ_PERBLOCK)); ``` ########## sched/mqueue/msg.h: ########## @@ -0,0 +1,98 @@ +/******************************************************************************** + * sched/mqueue/msg.h + * + * 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. + * + ********************************************************************************/ + +#ifndef __SCHED_MQUEUE_MSG_H +#define __SCHED_MQUEUE_MSG_H + +/******************************************************************************** + * Included Files + ********************************************************************************/ + +#include <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <nuttx/irq.h> +#include <nuttx/list.h> +#include <nuttx/mqueue.h> +#include <sys/msg.h> + +#include <errno.h> + +#if defined(CONFIG_MQ_MAXMSGSIZE) && CONFIG_MQ_MAXMSGSIZE > 0 + +/******************************************************************************** + * Pre-processor Definitions + ********************************************************************************/ + +#define MSG_MAX_BYTES CONFIG_MQ_MAXMSGSIZE +#define MSG_MAX_MSGS 16 + +/******************************************************************************** + * Public Type Definitions + ********************************************************************************/ + +struct msgq_s +{ + struct mqueue_cmn_s cmn; + struct list_node msglist; /* Prioritized message list */ + key_t key; + int16_t maxmsgs; /* Maximum number of messages in the queue */ + int16_t nmsgs; /* Number of message in the queue */ + uint16_t maxmsgsize; /* Max size of message in message queue */ +}; + +struct msgbuf_s +{ + struct list_node node; + uint16_t msize; /* Message data length */ + long mtype; /* Message type, must be > 0 */ + char mtext[MSG_MAX_BYTES]; /* Message data */ +}; + +/******************************************************************************** + * Public Data + ********************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +EXTERN struct list_node g_msgfreelist; + +/******************************************************************************** Review Comment: comments in this file seems to be too long ########## include/nuttx/mqueue.h: ########## @@ -85,25 +85,32 @@ # define nxmq_pollnotify(msgq, eventset) #endif -# define MQ_WNELIST(mq) (&((mq)->waitfornotempty)) -# define MQ_WNFLIST(mq) (&((mq)->waitfornotfull)) +# define MQ_WNELIST(cmn) (&((cmn).waitfornotempty)) +# define MQ_WNFLIST(cmn) (&((cmn).waitfornotfull)) /**************************************************************************** * Public Type Declarations ****************************************************************************/ +/* Common prologue of all message queue structures. */ + +struct mqueue_cmn_s +{ + dq_queue_t waitfornotempty; /* Task list waiting for not empty */ + dq_queue_t waitfornotfull; /* Task list waiting for not full */ + int16_t nwaitnotfull; /* Number tasks waiting for not full */ + int16_t nwaitnotempty; /* Number tasks waiting for not empty */ +}; + /* This structure defines a message queue */ struct mqueue_inode_s { + struct mqueue_cmn_s cmn; /* Common prologue */ Review Comment: Can we name it something like `struct mqueue_wait_node_s wnode;`? This is just a suggestion. ########## sched/mqueue/msgctl.c: ########## @@ -0,0 +1,124 @@ +/**************************************************************************** + * sched/mqueue/msgctl.c + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include "mqueue/msg.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: msgctl + * + * Description: + * System V message control operations. + * msgctl() performs the control operation specified by cmd on the + * System V message queue with identifier msqid. + * + * Input Parameters: + * msqid - System V message queue identifier + * cmd - Command operations + * msqid_ds - Defines a message queue + * + * Returned Value: + * On success, IPC_STAT, IPC_SET, and IPC_RMID return 0. A + * successful IPC_INFO or MSG_INFO operation returns the index of + * the highest used entry in the kernel's internal array recording + * information about all message queues. (This information can be + * used with repeated MSG_STAT or MSG_STAT_ANY operations to obtain + * information about all queues on the system.) A successful + * MSG_STAT or MSG_STAT_ANY operation returns the identifier of the + * queue whose index was given in msqid. + * + * On failure, -1 is returned and errno is set to indicate the error. + * + ****************************************************************************/ + +int msgctl(int msqid, int cmd, struct msqid_ds *buf) Review Comment: ```suggestion int msgctl(int msqid, int cmd, FAR struct msqid_ds *buf) ``` ########## include/sys/msg.h: ########## @@ -0,0 +1,212 @@ +/**************************************************************************** + * include/sys/msg.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_SYS_MSG_H +#define __INCLUDE_SYS_MSG_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <sys/ipc.h> +#include <time.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The MSG_NOERROR identifier value, the msqid_ds struct and the msg struct + * are as defined by the SV API Intel 386 Processor Supplement. + */ + +#define MSG_NOERROR 010000 /* No error if message is too big */ +#define MSG_EXCEPT 020000 /* Recv any msg except of specified type.*/ +#define MSG_COPY 040000 /* Copy (not remove) all queue messages */ + +/**************************************************************************** + * Public Type Declarations + ****************************************************************************/ + +typedef unsigned long msgqnum_t; +typedef unsigned long msglen_t; + +struct msqid_ds +{ + struct ipc_perm msg_perm; /* Ownership and permissions */ + time_t msg_stime; /* Time of last msgsnd(2) */ + time_t msg_rtime; /* Time of last msgrcv(2) */ + time_t msg_ctime; /* Time of last change */ + unsigned long msg_cbytes; /* Current number of bytes in + * queue (nonstandard) */ + msgqnum_t msg_qnum; /* Current number of messages + * in queue */ + msglen_t msg_qbytes; /* Maximum number of bytes + * allowed in queue */ + pid_t msg_lspid; /* PID of last msgsnd(2) */ + pid_t msg_lrpid; /* PID of last msgrcv(2) */ +}; + +/* Structure describing a message. The SVID doesn't suggest any + * particular name for this structure. There is a reference in the + * msgop man page that reads "The structure mymsg is an example of what + * this user defined buffer might look like, and includes the following + * members:". This sentence is followed by two lines equivalent + * to the mtype and mtext field declarations below. It isn't clear + * if "mymsg" refers to the name of the structure type or the name of an + * instance of the structure... + */ + +struct mymsg +{ + long mtype; /* message type (+ve integer) */ + char mtext[1]; /* message body */ +}; + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: msgctl + * + * Description: + * System V message control operations. + * msgctl() performs the control operation specified by cmd on the + * System V message queue with identifier msqid. + * + * Input Parameters: + * msqid - System V message queue identifier + * cmd - Command operations + * msqid_ds - Defines a message queue + * + * Returned Value: + * On success, IPC_STAT, IPC_SET, and IPC_RMID return 0. A + * successful IPC_INFO or MSG_INFO operation returns the index of + * the highest used entry in the kernel's internal array recording + * information about all message queues. (This information can be + * used with repeated MSG_STAT or MSG_STAT_ANY operations to obtain + * information about all queues on the system.) A successful + * MSG_STAT or MSG_STAT_ANY operation returns the identifier of the + * queue whose index was given in msqid. + * + * On failure, -1 is returned and errno is set to indicate the error. + * + ****************************************************************************/ + +int msgctl(int msqid, int cmd, FAR struct msqid_ds *buf); + +/**************************************************************************** + * Name: msgget + * + * Description: + * Get a System V message queue identifier + * The msgget() system call returns the System V message queue + * identifier associated with the value of the key argument. It may + * be used either to obtain the identifier of a previously created + * message queue (when msgflg is zero and key does not have the + * value IPC_PRIVATE), or to create a new set. + * + * Input Parameters: + * key - Key associated with the message queue + * msgflg - Operations and permissions flag + * + * Returned Value: + * On success, msgget() returns the message queue identifier (a + * nonnegative integer). On failure, -1 is returned, and errno is + * set to indicate the error. + * + ****************************************************************************/ + +int msgget(key_t key, int msgflg); + +/**************************************************************************** + * Name: msgsnd + * + * Description: + * The msgsnd() function is used to send a message to the queue + * associated with the message queue identifier specified by msqid. + * The msgp argument points to a user-defined buffer that must contain + * first a field of type long int that will specify the type of the + * message, and then a data portion that will hold the data bytes of + * the message. + * + * Input Parameters: + * msqid - Message queue identifier + * msgp - Pointer to a buffer with the message to be sent + * msgsz - Length of the data part of the message to be sent + * msgflg - Operations flags + * + * 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. + * EPERM Message queue opened not opened for writing. + * EMSGSIZE 'msglen' was greater than the maxmsgsize attribute of the + * message queue. + * EINTR The call was interrupted by a signal handler. + * + ****************************************************************************/ + +int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); Review Comment: ```suggestion int msgsnd(int msqid, FAR const void *msgp, size_t msgsz, int msgflg); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org