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 e4b7e713403dbbf2e3c722ead004afbaf9b30fa1
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Oct 29 12:27:23 2026 +0100

    libs/libc/pthread: Call the fork handlers in the order of POSIX.
    
    pthread_atfork() added each entry at the head of the list, thus the
    list was in the reverse order of registration.  fork() walks the list
    in reverse for the prepare handlers and forward for the parent and
    child handlers.  Thus the prepare handlers ran in the order of
    registration, and the parent and child handlers in the reverse order.
    POSIX requires the opposite in each of the three cases.
    
    pthread_atfork() now adds each entry at the tail.  The comments of
    atfork_child() and atfork_parent() named the wrong handlers.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 libs/libc/pthread/pthread_atfork.c | 2 +-
 libs/libc/unistd/lib_fork.c        | 9 ++++-----
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/libs/libc/pthread/pthread_atfork.c 
b/libs/libc/pthread/pthread_atfork.c
index f2f835235c1..fa336f14fe5 100644
--- a/libs/libc/pthread/pthread_atfork.c
+++ b/libs/libc/pthread/pthread_atfork.c
@@ -80,7 +80,7 @@ int pthread_atfork(CODE void (*prepare)(void),
   entry->child = child;
 
   nxrmutex_lock(&info->ta_lock);
-  list_add_head(list, &entry->node);
+  list_add_tail(list, &entry->node);
   nxrmutex_unlock(&info->ta_lock);
 #endif
 
diff --git a/libs/libc/unistd/lib_fork.c b/libs/libc/unistd/lib_fork.c
index ac72025ea7f..8fa5625e6eb 100644
--- a/libs/libc/unistd/lib_fork.c
+++ b/libs/libc/unistd/lib_fork.c
@@ -53,9 +53,8 @@ static void atfork_prepare(void)
   FAR struct list_node *list = &info->ta_atfork;
   FAR struct pthread_atfork_s *entry;
 
-  /* According to posix standard, the prepare handlers are called in reverse
-   * order of registration
-   * so we iterate over the func list in reverse order
+  /* POSIX: the prepare handlers are called in the reverse order of
+   * registration.  pthread_atfork() adds each entry at the tail.
    */
 
   nxrmutex_lock(&info->ta_lock);
@@ -85,7 +84,7 @@ static void atfork_child(void)
   FAR struct list_node *list = &info->ta_atfork;
   FAR struct pthread_atfork_s *entry;
 
-  /* The parent handlers are called in the order of registration */
+  /* The child handlers are called in the order of registration */
 
   nxrmutex_lock(&info->ta_lock);
   list_for_every_entry(list, entry,
@@ -114,7 +113,7 @@ static void atfork_parent(void)
   FAR struct list_node *list = &info->ta_atfork;
   FAR struct pthread_atfork_s *entry;
 
-  /* The child handlers are called in the order of registration */
+  /* The parent handlers are called in the order of registration */
 
   nxrmutex_lock(&info->ta_lock);
   list_for_every_entry(list, entry,

Reply via email to