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


The following commit(s) were added to refs/heads/master by this push:
     new 542abdd449e libs/libc/pthread: Make thrd_exit() a real function.
542abdd449e is described below

commit 542abdd449e30e09380560762fdc64f3a77c8efd
Author: Marco Casaroli <[email protected]>
AuthorDate: Sat Oct 17 19:55:06 2026 +0200

    libs/libc/pthread: Make thrd_exit() a real function.
    
    C11 7.26.5.5 defines thrd_exit() as a library function. In threads.h
    it was a static inline function. So a program could not take its
    address, and "#pragma weak thrd_exit" failed with the error "weak
    declaration of 'thrd_exit' must be public".
    
    Add thrd_exit() to libc next to the pthread functions. It calls
    pthread_exit() with the result, as the inline version did. The cast
    now goes through intptr_t, so the conversion from int to a pointer
    is correct on 64-bit targets. threads.h now has only the prototype,
    inside an extern "C" block. Add the function to libc.csv.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/threads.h                | 14 +++++++---
 libs/libc/libc.csv               |  1 +
 libs/libc/pthread/CMakeLists.txt |  3 ++-
 libs/libc/pthread/Make.defs      |  1 +
 libs/libc/pthread/thrd_exit.c    | 55 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/include/threads.h b/include/threads.h
index e97c1a77b7c..e00184bc709 100644
--- a/include/threads.h
+++ b/include/threads.h
@@ -101,6 +101,11 @@ typedef CODE void (*tss_dtor_t)(FAR void *);
  * Public Function Prototypes
  ****************************************************************************/
 
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
 /* Threads ******************************************************************/
 
 /* thrd_create: creates a thread
@@ -166,10 +171,7 @@ static inline void thrd_yield(void)
  * _Noreturn void thrd_exit(int res);
  */
 
-static inline noreturn void thrd_exit(int res)
-{
-  pthread_exit((pthread_addr_t)res);
-}
+void thrd_exit(int res) noreturn_function;
 
 /* thrd_detach: detaches a thread
  *
@@ -339,4 +341,8 @@ static inline int mtx_init(FAR mtx_t *mutex, int type)
 
 #define tss_delete(tss_id) pthread_key_delete(tss_id)
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* __INCLUDE_THREADS_H */
diff --git a/libs/libc/libc.csv b/libs/libc/libc.csv
index 1cceb425198..4b80e00415e 100644
--- a/libs/libc/libc.csv
+++ b/libs/libc/libc.csv
@@ -337,6 +337,7 @@
 "tcsetattr","termios.h","","int","int","int","FAR const struct termios *"
 "telldir","dirent.h","","off_t","FAR DIR *"
 "textdomain","libintl.h","defined(CONFIG_LIBC_LOCALE_GETTEXT)","FAR char 
*","FAR const char *"
+"thrd_exit","threads.h","!defined(CONFIG_DISABLE_PTHREAD)","noreturn","int"
 "time","time.h","","time_t","FAR time_t *"
 "tolower","ctype.h","","int","int"
 "toupper","ctype.h","","int","int"
diff --git a/libs/libc/pthread/CMakeLists.txt b/libs/libc/pthread/CMakeLists.txt
index 523f2818f7e..752b35a69e0 100644
--- a/libs/libc/pthread/CMakeLists.txt
+++ b/libs/libc/pthread/CMakeLists.txt
@@ -117,7 +117,8 @@ if(NOT CONFIG_DISABLE_PTHREAD)
     pthread_getcpuclockid.c
     pthread_self.c
     pthread_gettid_np.c
-    pthread_concurrency.c)
+    pthread_concurrency.c
+    thrd_exit.c)
 
   if(NOT CONFIG_PTHREAD_MUTEX_UNSAFE)
     list(APPEND SRCS pthread_mutex_consistent.c pthread_mutex_inconsistent.c)
diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs
index 25869be1a04..876eb5ffd1f 100644
--- a/libs/libc/pthread/Make.defs
+++ b/libs/libc/pthread/Make.defs
@@ -69,6 +69,7 @@ CSRCS += pthread_testcancel.c pthread_getcpuclockid.c
 CSRCS += pthread_self.c pthread_gettid_np.c
 CSRCS += pthread_concurrency.c
 CSRCS += pthread_kill.c
+CSRCS += thrd_exit.c
 
 ifeq ($(CONFIG_SMP),y)
 CSRCS += pthread_attr_getaffinity.c pthread_attr_setaffinity.c
diff --git a/libs/libc/pthread/thrd_exit.c b/libs/libc/pthread/thrd_exit.c
new file mode 100644
index 00000000000..08313eb9c21
--- /dev/null
+++ b/libs/libc/pthread/thrd_exit.c
@@ -0,0 +1,55 @@
+/****************************************************************************
+ * libs/libc/pthread/thrd_exit.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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/config.h>
+
+#include <stdint.h>
+#include <pthread.h>
+#include <threads.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: thrd_exit
+ *
+ * Description:
+ *   Terminate the calling thread.  thrd_join() of this thread gets res as
+ *   the result of the thread.
+ *
+ * Input Parameters:
+ *   res - The result of the thread
+ *
+ * Returned Value:
+ *   This function does not return.
+ *
+ ****************************************************************************/
+
+void thrd_exit(int res)
+{
+  pthread_exit((pthread_addr_t)(intptr_t)res);
+}

Reply via email to