summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorJake Tesler <jake.tesler@gmail.com>2019-05-22 15:43:17 (GMT)
committerVictor Stinner <vstinner@redhat.com>2019-05-22 15:43:16 (GMT)
commitb121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5 (patch)
treef8e48c2d296e82b1378bb2bf25a34e1ca8321c85 /Python
parentb3be4072888a4ce054993c2801802721466ea02d (diff)
downloadcpython-b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5.zip
cpython-b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5.tar.gz
cpython-b121f63155d8e3c7c42ab6122e36eaf7f5e9f7f5.tar.bz2
bpo-36084: Add native thread ID (TID) to threading.Thread (GH-13463)
Add native thread ID (TID) to threading.Thread objects (supported platforms: Windows, FreeBSD, Linux, macOS).
Diffstat (limited to 'Python')
-rw-r--r--Python/thread_nt.h23
-rw-r--r--Python/thread_pthread.h26
2 files changed, 49 insertions, 0 deletions
diff --git a/Python/thread_nt.h b/Python/thread_nt.h
index 5e00c35..a5246dd 100644
--- a/Python/thread_nt.h
+++ b/Python/thread_nt.h
@@ -143,6 +143,10 @@ LeaveNonRecursiveMutex(PNRMUTEX mutex)
unsigned long PyThread_get_thread_ident(void);
+#ifdef PY_HAVE_THREAD_NATIVE_ID
+unsigned long PyThread_get_thread_native_id(void);
+#endif
+
/*
* Initialization of the C package, should not be needed.
*/
@@ -227,6 +231,25 @@ PyThread_get_thread_ident(void)
return GetCurrentThreadId();
}
+#ifdef PY_HAVE_THREAD_NATIVE_ID
+/*
+ * Return the native Thread ID (TID) of the calling thread.
+ * The native ID of a thread is valid and guaranteed to be unique system-wide
+ * from the time the thread is created until the thread has been terminated.
+ */
+unsigned long
+PyThread_get_thread_native_id(void)
+{
+ if (!initialized) {
+ PyThread_init_thread();
+ }
+
+ DWORD native_id;
+ native_id = GetCurrentThreadId();
+ return (unsigned long) native_id;
+}
+#endif
+
void _Py_NO_RETURN
PyThread_exit_thread(void)
{
diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h
index 4c106d9..f57a1e7 100644
--- a/Python/thread_pthread.h
+++ b/Python/thread_pthread.h
@@ -12,6 +12,12 @@
#endif
#include <signal.h>
+#if defined(__linux__)
+# include <sys/syscall.h> /* syscall(SYS_gettid) */
+#elif defined(__FreeBSD__)
+# include <pthread_np.h> /* pthread_getthreadid_np() */
+#endif
+
/* The POSIX spec requires that use of pthread_attr_setstacksize
be conditional on _POSIX_THREAD_ATTR_STACKSIZE being defined. */
#ifdef _POSIX_THREAD_ATTR_STACKSIZE
@@ -302,6 +308,26 @@ PyThread_get_thread_ident(void)
return (unsigned long) threadid;
}
+#ifdef PY_HAVE_THREAD_NATIVE_ID
+unsigned long
+PyThread_get_thread_native_id(void)
+{
+ if (!initialized)
+ PyThread_init_thread();
+#ifdef __APPLE__
+ uint64_t native_id;
+ (void) pthread_threadid_np(NULL, &native_id);
+#elif defined(__linux__)
+ pid_t native_id;
+ native_id = syscall(SYS_gettid);
+#elif defined(__FreeBSD__)
+ int native_id;
+ native_id = pthread_getthreadid_np();
+#endif
+ return (unsigned long) native_id;
+}
+#endif
+
void _Py_NO_RETURN
PyThread_exit_thread(void)
{