You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "xiaoxiang781216 (via GitHub)" <gi...@apache.org> on 2023/04/26 17:15:21 UTC

[GitHub] [nuttx] xiaoxiang781216 commented on a diff in pull request #9100: sched/spawn: remove spawn proxy thread to simplify task/posix_spawn()

xiaoxiang781216 commented on code in PR #9100:
URL: https://github.com/apache/nuttx/pull/9100#discussion_r1178168151


##########
include/nuttx/spawn.h:
##########
@@ -100,6 +100,9 @@ extern "C"
 void add_file_action(FAR posix_spawn_file_actions_t *file_action,
                      FAR struct spawn_general_file_action_s *entry);
 
+int spawn_fileactions(FAR struct tcb_s *tcb,

Review Comment:
   spawn_file_actions



##########
sched/task/task_spawn.c:
##########
@@ -321,35 +324,13 @@ int task_spawn(FAR const char *name, main_t entry,
                FAR const posix_spawnattr_t *attr,
                FAR char * const argv[], FAR char * const envp[])
 {
-  struct sched_param param;
-  pid_t proxy;
   pid_t pid = INVALID_PROCESS_ID;
-#ifdef CONFIG_SCHED_WAITPID
-  int status;
-#endif
   int ret;
 
   sinfo("name=%s entry=%p file_actions=%p attr=%p argv=%p\n",
         name, entry, file_actions, attr, argv);
 
-  /* If there are no file actions to be performed and there is no change to
-   * the signal mask, then start the new child task directly from the parent
-   * task.
-   */
-
-  if ((file_actions == NULL || *file_actions == NULL) &&
-      (attr == NULL || (attr->flags & POSIX_SPAWN_SETSIGMASK) == 0))
-    {
-      ret = nxtask_spawn_exec(&pid, name, entry, attr, argv, envp);
-      if (ret < 0)
-        {
-          return ret;
-        }
-
-      return pid;
-    }
-
-  /* Otherwise, we will have to go through an intermediary/proxy task in
+  /* We will have to go through an intermediary/proxy task in

Review Comment:
   ditto



##########
sched/task/task_spawn.c:
##########
@@ -360,103 +341,16 @@ int task_spawn(FAR const char *name, main_t entry,
    * structure.
    */
 
-  /* Get exclusive access to the global parameter structure */
-
-  ret = nxmutex_lock(&g_spawn_parmlock);
-  if (ret < 0)
-    {
-      serr("ERROR: nxmutex_lock failed: %d\n", ret);
-      return ret;
-    }
-
-  /* Populate the parameter structure */
-
-  g_spawn_parms.result       = ENOSYS;
-  g_spawn_parms.pid          = &pid;
-  g_spawn_parms.file_actions = file_actions ? *file_actions : NULL;
-  g_spawn_parms.attr         = attr;
-  g_spawn_parms.argv         = argv;
-  g_spawn_parms.envp         = envp;
-  g_spawn_parms.u.task.name  = name;
-  g_spawn_parms.u.task.entry = entry;
-
-  /* Get the priority of this (parent) task */
-
-  ret = nxsched_get_param(0, &param);
-  if (ret < 0)
-    {
-      serr("ERROR: nxsched_get_param failed: %d\n", ret);
-      g_spawn_parms.pid = NULL;
-      nxmutex_unlock(&g_spawn_parmlock);
-      return ret;
-    }
-
-#ifdef CONFIG_SCHED_WAITPID
-  /* Disable pre-emption so that the proxy does not run until waitpid
-   * is called.  This is probably unnecessary since the nxtask_spawn_proxy
-   * has the same priority as this thread; it should be schedule behind
-   * this task in the ready-to-run list.
-   *
-   * REVISIT:  This will may not have the desired effect in SMP mode.
-   */
-
-  sched_lock();
-#endif
-
-  /* Start the intermediary/proxy task at the same priority as the parent
-   * task.
-   */
-
-  proxy = nxtask_create("nxtask_spawn_proxy", param.sched_priority,
-                        NULL, CONFIG_POSIX_SPAWN_PROXY_STACKSIZE,
-                        nxtask_spawn_proxy, NULL, NULL);
-  if (proxy < 0)
+  if (attr != NULL)
     {
-      ret = proxy;
-      serr("ERROR: Failed to start nxtask_spawn_proxy: %d\n", ret);
-      goto errout_with_lock;
+      spawn_proxyattrs(attr);
     }
 
-  /* Wait for the proxy to complete its job */
+  ret = nxtask_spawn_exec(&pid, name, entry,
+                          file_actions != NULL ? *file_actions : NULL,

Review Comment:
   ditto



##########
sched/task/task_spawnparms.c:
##########
@@ -68,76 +58,47 @@ struct spawn_parms_s g_spawn_parms;
  *
  ****************************************************************************/
 
-static inline int nxspawn_close(FAR struct spawn_close_file_action_s *action)
+static inline int nxspawn_close(FAR struct tcb_s *tcb,
+                                FAR struct spawn_close_file_action_s *action)
 {
   /* The return value from nx_close() is ignored */
 
   sinfo("Closing fd=%d\n", action->fd);
 
-  nx_close(action->fd);
-  return OK;
+  return nx_close_from_list(&tcb->group->tg_filelist, action->fd);
 }
 
-static inline int nxspawn_dup2(FAR struct spawn_dup2_file_action_s *action)
+static inline int nxspawn_dup2(FAR struct tcb_s *tcb,
+                               FAR struct spawn_dup2_file_action_s *action)
 {
-  int ret;
-
   /* Perform the dup */
 
   sinfo("Dup'ing %d->%d\n", action->fd1, action->fd2);
 
-  ret = nx_dup2(action->fd1, action->fd2);
-  if (ret < 0)
-    {
-      serr("ERROR: dup2 failed: %d\n", ret);
-      return ret;
-    }
-
-  return OK;
+  return nx_dup2_from_list(&tcb->group->tg_filelist,
+                           action->fd1, action->fd2);
 }
 
-static inline int nxspawn_open(FAR struct spawn_open_file_action_s *action)
+static inline int nxspawn_open(FAR struct tcb_s *tcb,
+                               FAR struct spawn_open_file_action_s *action)
 {
-  int fd;
-  int ret = OK;
+  FAR struct filelist *list = &tcb->group->tg_filelist;
+  FAR struct file *filep;
 
   /* Open the file */
 
   sinfo("Open'ing path=%s oflags=%04x mode=%04x\n",
         action->path, action->oflags, action->mode);
 
-  fd = nx_open(action->path, action->oflags, action->mode);
-  if (fd < 0)
-    {
-      ret = fd;
-      serr("ERROR: open failed: %d\n", ret);
-    }
-
-  /* Does the return file descriptor happen to match the required file
-   * descriptor number?
-   */
+  filep = &list->fl_files[action->fd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK]
+                         [action->fd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK];
 
-  else if (fd != action->fd)
+  if (filep->f_inode != NULL)
     {
-      /* No.. dup2 to get the correct file number */
-
-      sinfo("Dup'ing %d->%d\n", fd, action->fd);
-
-      ret = nx_dup2(fd, action->fd);
-      if (ret < 0)
-        {
-          serr("ERROR: dup2 failed: %d\n", ret);
-        }
-      else
-        {
-          ret = OK;
-        }
-
-      sinfo("Closing fd=%d\n", fd);
-      nx_close(fd);
+      file_close(filep);
     }
 
-  return ret;
+  return file_open(filep, action->path, action->oflags, action->mode);

Review Comment:
   let's add nx_open_from_list too



##########
sched/task/task_posixspawn.c:
##########
@@ -345,91 +253,12 @@ int posix_spawn(FAR pid_t *pid, FAR const char *path,
    * structure.
    */
 
-  /* Get exclusive access to the global parameter structure */
-
-  ret = nxmutex_lock(&g_spawn_parmlock);
-  if (ret < 0)
-    {
-      serr("ERROR: nxmutex_lock failed: %d\n", ret);
-      return -ret;
-    }
-
-  /* Populate the parameter structure */
-
-  g_spawn_parms.result       = ENOSYS;
-  g_spawn_parms.pid          = pid;
-  g_spawn_parms.file_actions = file_actions ? *file_actions : NULL;
-  g_spawn_parms.attr         = attr;
-  g_spawn_parms.argv         = argv;
-  g_spawn_parms.envp         = envp;
-  g_spawn_parms.u.posix.path = path;
-
-  /* Get the priority of this (parent) task */
-
-  ret = nxsched_get_param(0, &param);
-  if (ret < 0)
-    {
-      serr("ERROR: nxsched_get_param failed: %d\n", ret);
-      nxmutex_unlock(&g_spawn_parmlock);
-      return -ret;
-    }
-
-  /* Disable pre-emption so that the proxy does not run until waitpid
-   * is called.  This is probably unnecessary since the nxposix_spawn_proxy
-   * has the same priority as this thread; it should be schedule behind
-   * this task in the ready-to-run list.
-   */
-
-#ifdef CONFIG_SCHED_WAITPID
-  sched_lock();
-#endif
-
-  /* Start the intermediary/proxy task at the same priority as the parent
-   * task.
-   */
-
-  proxy = nxthread_create("nxposix_spawn_proxy", TCB_FLAG_TTYPE_KERNEL,
-                          param.sched_priority, NULL,
-                          CONFIG_POSIX_SPAWN_PROXY_STACKSIZE,
-                          nxposix_spawn_proxy, NULL, environ);
-  if (proxy < 0)
-    {
-      ret = -proxy;
-      serr("ERROR: Failed to start nxposix_spawn_proxy: %d\n", ret);
-      goto errout_with_lock;
-    }
-
-  /* Wait for the proxy to complete its job */
-
-#ifdef CONFIG_SCHED_WAITPID
-  /* REVISIT: This should not call waitpid() directly.  waitpid is a
-   * cancellation point and modifies the errno value.  It is inappropriate
-   * for use within the OS.
-   */
-
-  ret = nxsched_waitpid((pid_t)proxy, &status, 0);
-  if (ret < 0)
-    {
-      serr("ERROR: waitpid() failed: %d\n", ret);
-      goto errout_with_lock;
-    }
-#else
-  ret = nxsem_wait_uninterruptible(&g_spawn_execsem);
-  if (ret < 0)
+  if (attr != NULL)
     {
-      serr("ERROR: nxsem_wait_uninterruptible() failed: %d\n", ret);
-      goto errout_with_lock;
+      spawn_proxyattrs(attr);
     }
-#endif
 
-  /* Get the result and relinquish our access to the parameter structure */
-
-  ret = g_spawn_parms.result;
-
-errout_with_lock:
-#ifdef CONFIG_SCHED_WAITPID
-  sched_unlock();
-#endif
-  nxmutex_unlock(&g_spawn_parmlock);
-  return ret;
+  return nxposix_spawn_exec(pid, path,
+                            file_actions != NULL ?
+                            *file_actions : NULL, attr, argv, envp);

Review Comment:
   why not pass file_actions directly



##########
sched/task/task_posixspawn.c:
##########
@@ -311,30 +239,10 @@ int posix_spawn(FAR pid_t *pid, FAR const char *path,
                 FAR const posix_spawnattr_t *attr,
                 FAR char * const argv[], FAR char * const envp[])
 {
-  struct sched_param param;
-  int proxy;
-#ifdef CONFIG_SCHED_WAITPID
-  int status;
-#endif
-  int ret;
-
-  DEBUGASSERT(path);
-
   sinfo("pid=%p path=%s file_actions=%p attr=%p argv=%p\n",
         pid, path, file_actions, attr, argv);
 
-  /* If there are no file actions to be performed and there is no change to
-   * the signal mask, then start the new child task directly from the parent
-   * task.
-   */
-
-  if ((file_actions == NULL || *file_actions == NULL) &&
-      (attr == NULL || (attr->flags & POSIX_SPAWN_SETSIGMASK) == 0))
-    {
-      return nxposix_spawn_exec(pid, path, attr, argv, envp);
-    }
-
-  /* Otherwise, we will have to go through an intermediary/proxy task in
+  /* We will have to go through an intermediary/proxy task in

Review Comment:
   no intermediary/proxy task anymore?



-- 
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