add gettid function
authorRich Felker <dalias@aerifal.cx>
Tue, 18 Aug 2020 00:12:53 +0000 (20:12 -0400)
committerRich Felker <dalias@aerifal.cx>
Tue, 18 Aug 2020 00:12:53 +0000 (20:12 -0400)
this is a prerequisite for addition of other interfaces that use
kernel tids, including futex and SIGEV_THREAD_ID.

there is some ambiguity as to whether the semantic return type should
be int or pid_t. either way, futex API imposes a contract that the
values fit in int (excluding some upper reserved bits). glibc used
pid_t, so in the interest of not having gratuitous mismatch (the
underlying types are the same anyway), pid_t is used here as well.

while conceptually this is a syscall, the copy stored in the thread
structure is always valid in all contexts where it's valid to call
libc functions, so it's used to avoid the syscall.

include/unistd.h
src/linux/gettid.c [new file with mode: 0644]

index 7bcbff9..07584a2 100644 (file)
@@ -190,6 +190,7 @@ int syncfs(int);
 int euidaccess(const char *, int);
 int eaccess(const char *, int);
 ssize_t copy_file_range(int, off_t *, int, off_t *, size_t, unsigned);
+pid_t gettid(void);
 #endif
 
 #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
diff --git a/src/linux/gettid.c b/src/linux/gettid.c
new file mode 100644 (file)
index 0000000..7076713
--- /dev/null
@@ -0,0 +1,8 @@
+#define _GNU_SOURCE
+#include <unistd.h>
+#include "pthread_impl.h"
+
+pid_t gettid(void)
+{
+       return __pthread_self()->tid;
+}