This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 745a6e53a430bd52cda10877ffe3ac386c9b69b3
Author: anjiahao <[email protected]>
AuthorDate: Wed Jul 15 17:19:12 2026 +0800

    spawn: allow zero stacksize/priority to use binary loader defaults
    
    posix_spawnattr_init() no longer pre-fills attr->stacksize and
    attr->priority (it leaves them zero, as memset already does).  When a
    caller does not set them, the binary loader supplies them from the
    loaded ELF (binp->stacksize / binp->priority, parsed from the nx_*
    symbols), and nxtask_spawn_exec() falls back to the parent priority and
    CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE for the posix_spawn() function-task
    path.
    
    This lets an application's stack size and priority embedded as ELF
    symbols actually drive the spawned task, instead of being overridden by
    the spawnattr defaults.
    
    Signed-off-by: anjiahao <[email protected]>
---
 libs/libc/spawn/lib_psa_init.c | 17 +++++++++--------
 sched/task/task_spawn.c        | 20 ++++++++++++++++++++
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/libs/libc/spawn/lib_psa_init.c b/libs/libc/spawn/lib_psa_init.c
index 20313348710..804d1561d81 100644
--- a/libs/libc/spawn/lib_psa_init.c
+++ b/libs/libc/spawn/lib_psa_init.c
@@ -75,7 +75,10 @@ int posix_spawnattr_init(posix_spawnattr_t *attr)
       return _SCHED_ERRNO(ret);
     }
 
-  attr->priority            = param.sched_priority;
+  /* attr->priority left at 0: callers that do not set a priority will have
+   * it filled in from the loaded binary (binp->priority) by the binary
+   * loader, or from the parent task by task_spawn().
+   */
 
   /* Set the default scheduler policy to the policy of this task */
 
@@ -104,13 +107,11 @@ int posix_spawnattr_init(posix_spawnattr_t *attr)
   attr->budget.tv_nsec      = param.sched_ss_init_budget.tv_nsec;
 #endif
 
-  /* Default stack size */
-
-  attr->stacksize           = CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE;
-
-#ifndef CONFIG_BUILD_KERNEL
-  attr->stackaddr           = NULL;
-#endif
+  /* attr->stacksize and attr->stackaddr left at 0 (memset above): callers
+   * that do not set a stack size will have it filled in from the loaded
+   * binary (binp->stacksize) by the binary loader, or from
+   * CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE by task_spawn().
+   */
 
   return OK;
 }
diff --git a/sched/task/task_spawn.c b/sched/task/task_spawn.c
index 43f3c9a708d..a29db7f893c 100644
--- a/sched/task/task_spawn.c
+++ b/sched/task/task_spawn.c
@@ -219,6 +219,26 @@ static int nxtask_spawn_exec(FAR pid_t *pidp, FAR const 
char *name,
       stacksize = CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE;
     }
 
+  /* A zero priority/stacksize means "use the default": inherit the parent
+   * task's priority and fall back to CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE.
+   * This lets posix_spawnattr_init() leave these fields zero so that the
+   * binary loader can supply them from the loaded ELF (binp->priority /
+   * binp->stacksize) instead.
+   */
+
+  if (priority == 0)
+    {
+      struct sched_param param;
+
+      nxsched_get_param(0, &param);
+      priority = param.sched_priority;
+    }
+
+  if (stacksize == 0)
+    {
+      stacksize = CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE;
+    }
+
   /* Start the task */
 
   pid = nxtask_spawn_create(name, priority, stackaddr,

Reply via email to