You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/09/14 15:23:32 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #7088: sched/msgq: add support of System V message queue

xiaoxiang781216 commented on code in PR #7088:
URL: https://github.com/apache/incubator-nuttx/pull/7088#discussion_r970932694


##########
include/nuttx/mqueue.h:
##########
@@ -83,16 +83,23 @@
  * Public Type Declarations
  ****************************************************************************/
 
+/* Common prologue of all message queue structures. */
+
+struct mqueue_comm_s
+{
+  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_comm_s comm;  /* Common prologue */

Review Comment:
   ditto



##########
fs/procfs/fs_procfsproc.c:
##########
@@ -411,7 +411,7 @@ static FAR const char * const g_statenames[] =
   "Inactive",
   "Waiting,Semaphore",
   "Waiting,Signal"
-#ifndef CONFIG_DISABLE_MQUEUE
+#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_MQUEUE_SYSV)

Review Comment:
   could we use a common symbol?



##########
sched/mqueue/msginternal.c:
##########
@@ -0,0 +1,137 @@
+/****************************************************************************
+ * 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"
+
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+struct list_node g_msgfreelist = LIST_INITIAL_VALUE(g_msgfreelist);
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static struct list_node g_msgqactivelist =
+                        LIST_INITIAL_VALUE(g_msgqactivelist);
+static struct list_node g_msgqfreelist   =
+                        LIST_INITIAL_VALUE(g_msgqfreelist);
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: nxmsg_initialize
+ ****************************************************************************/
+
+void nxmsg_initialize(void)
+{
+  FAR struct msgbuf_s *msg;
+
+  msg = (FAR struct msgbuf_s *)kmm_malloc(sizeof(*msg) *
+                                          CONFIG_PREALLOC_MQ_MSGS);
+  if (msg)
+    {
+      int i;
+      for (i = 0; i < CONFIG_PREALLOC_MQ_MSGS; i++)
+        {
+          list_add_tail(&g_msgfreelist, &msg->node);
+          msg++;
+        }
+    }
+}
+
+/****************************************************************************
+ * Name: nxmsg_alloc
+ ****************************************************************************/
+
+int nxmsg_alloc(FAR struct msgq_s **pmsgq)
+{
+  FAR struct msgq_s *msgq;
+
+  /* Allocate memory for the new message queue. */
+
+  msgq = list_remove_head_type(&g_msgqfreelist, struct msgq_s, node);
+  if (msgq == NULL)
+    {
+      msgq = (FAR struct msgq_s *)kmm_zalloc(sizeof(struct msgq_s));
+    }
+
+  if (msgq == NULL)
+    {
+      return -ENOSPC;
+    }
+
+  /* Initialize the new named message queue */
+
+  list_add_tail(&g_msgqactivelist, &msgq->node);
+  list_initialize(&msgq->msglist);
+
+  msgq->maxmsgs    = MSG_MAX_MSGS;
+  msgq->maxmsgsize = MSG_MAX_BYTES;
+
+  *pmsgq = msgq;
+  return OK;
+}
+
+/****************************************************************************
+ * Name: nxmsg_free
+ ****************************************************************************/
+
+void nxmsg_free(FAR struct msgq_s *msgq)
+{
+  FAR struct msgq_s *entry;
+  FAR struct msgq_s *tmp;
+
+  list_for_every_entry_safe(&msgq->msglist, entry, tmp, struct msgq_s, node)
+    {
+      list_delete(&entry->node);
+      list_add_tail(&g_msgfreelist, &entry->node);
+    }
+
+  list_delete(&msgq->node);
+  list_add_tail(&g_msgqfreelist, &msgq->node);
+}
+
+/****************************************************************************
+ * Name: nxmsg_lookup
+ ****************************************************************************/
+
+FAR struct msgq_s *nxmsg_lookup(key_t key)
+{
+  FAR struct msgq_s *msgq;
+
+  list_for_every_entry(&g_msgqactivelist, msgq, struct msgq_s, node)

Review Comment:
   can we change g_msgqactivelist to an array and identify the msq by index to improve the speed?



##########
include/nuttx/mqueue.h:
##########
@@ -83,16 +83,23 @@
  * Public Type Declarations
  ****************************************************************************/
 
+/* Common prologue of all message queue structures. */
+
+struct mqueue_comm_s

Review Comment:
   comm->cmn like:
   ```
   struct task_tcb_s
   {
     /* Common TCB fields ******************************************************/
   
     struct tcb_s cmn;                      /* Common TCB fields                   */
   
     /* Task Management Fields *************************************************/
   
   #ifdef CONFIG_SCHED_STARTHOOK
     starthook_t starthook;                 /* Task startup function               */
     FAR void *starthookarg;                /* The argument passed to the function */
   #endif
   };
   ```



##########
sched/mqueue/msgget.c:
##########
@@ -0,0 +1,95 @@
+/****************************************************************************
+ * sched/mqueue/msgget.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: 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)
+{
+  FAR struct msgq_s *msgq;
+  irqstate_t flags;
+  int ret = OK;
+
+  flags = enter_critical_section();
+
+  msgq = nxmsg_lookup(key);
+
+  if (msgq)
+    {
+      if ((msgflg & IPC_CREAT) && (msgflg & IPC_EXCL))
+        {
+          ret = -EEXIST;
+        }
+    }
+  else
+    {
+      if (key != IPC_PRIVATE && !(msgflg & IPC_CREAT))

Review Comment:
   how to identify the different private queue



-- 
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-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org