[PATCH] pthread: add PTHREAD_NULL

Alyssa Ross hi@alyssa.is
Sat Nov 1 19:32:14 GMT 2025


New in POSIX.1-2024.

In htl, pthread_t values are integers starting at 1, so 0 is a safe
null value.

In nptl, pthread_t values are non-null pointers, so 0 is a safe null
value.
---
I wasn't sure whether this should be behind a __USE macro, but there
isn't one for POSIX.1-2024 yet:
https://inbox.sourceware.org/libc-alpha/d34488044ace7c01311fb1c1948e65a050e93252.1747972803.git.collin.funk1@gmail.com/

 manual/threads.texi    | 5 +++++
 sysdeps/htl/pthread.h  | 2 ++
 sysdeps/nptl/pthread.h | 3 +++
 3 files changed, 10 insertions(+)

diff --git a/manual/threads.texi b/manual/threads.texi
index 1e9be32d66..d3692e8939 100644
--- a/manual/threads.texi
+++ b/manual/threads.texi
@@ -1099,6 +1099,11 @@ This function gets the kind of mutex @var{mutex} is.
 @node POSIX Threads Other APIs
 @subsection POSIX Threads Other APIs
 
+@deftypevr Macro pthread_t PTHREAD_NULL
+@standards{POSIX.1-2024, pthread.h}
+A value for pthread_t that is not considered equal to any thread ID.
+@end deftypevr
+
 @deftypefun int pthread_equal (pthread_t @var{thread1}, pthread_t @var{thread2})
 Compares two thread IDs.  If they are the same, returns nonzero, else returns zero.
 @manpagefunctionstub{pthread_equal, 3}
diff --git a/sysdeps/htl/pthread.h b/sysdeps/htl/pthread.h
index a299fec278..d490f3cdf8 100644
--- a/sysdeps/htl/pthread.h
+++ b/sysdeps/htl/pthread.h
@@ -55,6 +55,8 @@ __BEGIN_DECLS
 
 #include <bits/pthread.h>
 
+#define PTHREAD_NULL ((pthread_t)0)
+
 /* Possible values for the process shared attribute.  */
 #define PTHREAD_PROCESS_PRIVATE __PTHREAD_PROCESS_PRIVATE
 #define PTHREAD_PROCESS_SHARED __PTHREAD_PROCESS_SHARED
diff --git a/sysdeps/nptl/pthread.h b/sysdeps/nptl/pthread.h
index 92957a620d..b47b732a9f 100644
--- a/sysdeps/nptl/pthread.h
+++ b/sysdeps/nptl/pthread.h
@@ -33,6 +33,9 @@
 # include <bits/pthread_stack_min-dynamic.h>
 #endif
 
+#define PTHREAD_NULL ((pthread_t)0)
+
+
 /* Detach state.  */
 enum
 {

base-commit: 720e89163702ffa1e921d926b6c36b53c3ccbee4
-- 
2.51.0



More information about the Libc-alpha mailing list