[netcore] Implement Thread.CurrentOSThreadId (mono/mono#15748)
authorRyan Lucia <rylucia@microsoft.com>
Fri, 19 Jul 2019 17:22:50 +0000 (13:22 -0400)
committerGitHub <noreply@github.com>
Fri, 19 Jul 2019 17:22:50 +0000 (13:22 -0400)
* [netcore] Implement Thread.CurrentOSThreadId

* Add comment explaining differences with mono_native_thread_id_get

* Add haiku implementation

* Attempt to fix linux builds

* Add MonoError parameter to icall

Commit migrated from https://github.com/mono/mono/commit/d685b72cb2c37ab73a8470b074a7557e22ce5b00

14 files changed:
src/mono/mono/metadata/icall-def-netcore.h
src/mono/mono/metadata/threads.c
src/mono/mono/utils/mono-threads-aix.c
src/mono/mono/utils/mono-threads-android.c
src/mono/mono/utils/mono-threads-freebsd.c
src/mono/mono/utils/mono-threads-haiku.c
src/mono/mono/utils/mono-threads-linux.c
src/mono/mono/utils/mono-threads-mach.c
src/mono/mono/utils/mono-threads-netbsd.c
src/mono/mono/utils/mono-threads-openbsd.c
src/mono/mono/utils/mono-threads-wasm.c
src/mono/mono/utils/mono-threads-windows.c
src/mono/mono/utils/mono-threads.h
src/mono/netcore/System.Private.CoreLib/src/System.Threading/Thread.cs

index f76eb81..c52222f 100644 (file)
@@ -501,6 +501,7 @@ HANDLES(SEMA_3, "ReleaseSemaphore_internal", ves_icall_System_Threading_Semaphor
 ICALL_TYPE(THREAD, "System.Threading.Thread", THREAD_1)
 HANDLES(THREAD_1, "ClrState", ves_icall_System_Threading_Thread_ClrState, void, 2, (MonoInternalThread, guint32))
 HANDLES(ITHREAD_2, "FreeInternal", ves_icall_System_Threading_InternalThread_Thread_free_internal, void, 1, (MonoInternalThread))
+HANDLES(THREAD_15, "GetCurrentOSThreadId", ves_icall_System_Threading_Thread_GetCurrentOSThreadId, guint64, 0, ())
 HANDLES(THREAD_3, "GetState", ves_icall_System_Threading_Thread_GetState, guint32, 1, (MonoInternalThread))
 HANDLES(THREAD_4, "InitInternal", ves_icall_System_Threading_Thread_InitInternal, void, 1, (MonoThreadObject))
 HANDLES(THREAD_5, "InitializeCurrentThread", ves_icall_System_Threading_Thread_GetCurrentThread, MonoThreadObject, 0, ())
index 490a342..a7da09e 100644 (file)
@@ -6738,4 +6738,10 @@ ves_icall_System_Threading_Thread_InitInternal (MonoThreadObjectHandle thread_ha
        MONO_OBJECT_SETREF_INTERNAL (internal, internal_thread, internal);
 }
 
+guint64
+ves_icall_System_Threading_Thread_GetCurrentOSThreadId (MonoError *error)
+{
+       return mono_native_thread_os_id_get ();
+}
+
 #endif
index 17a4878..ac2227e 100644 (file)
@@ -45,4 +45,14 @@ mono_threads_platform_is_main_thread (void)
        return pthread_self () == 1;
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       pthread_t t = pthread_self ();
+       struct __pthrdsinfo ti;
+       int err, size = 0;
+       err = pthread_getthrds_np (&t, PTHRDSINFO_QUERY_TID, &ti, sizeof (struct __pthrdsinfo), NULL, &size);
+       return (guint64)ti.__pi_tid;
+}
+
 #endif
index 6c60d82..f01e670 100644 (file)
@@ -11,6 +11,7 @@
 #include <inttypes.h>
 #include "glib.h"
 #include <mono/utils/mono-threads.h>
+#include <sys/syscall.h>
 
 static void
 slow_get_thread_bounds (guint8 *current, guint8 **staddr, size_t *stsize)
@@ -58,4 +59,10 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
                slow_get_thread_bounds (current, staddr, stsize);
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       return (guint64)syscall (SYS_gettid);
+}
+
 #endif
index f854fba..9bbd488 100644 (file)
@@ -9,6 +9,7 @@
 #include <mono/utils/mono-threads.h>
 #include <pthread.h>
 #include <pthread_np.h>
+#include <sys/thr.h>
 
 void
 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
@@ -26,4 +27,12 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
        pthread_attr_destroy (&attr);
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       long tid;
+       thr_self (&tid);
+       return (guint64)tid;
+}
+
 #endif
index e5c3563..7ec6f76 100644 (file)
@@ -16,4 +16,10 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
        *stsize = ti.stack_end - ti.stack_base;
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       return (guint64)get_pthread_thread_id (pthread_self ());
+}
+
 #endif
index e73b860..2fee712 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <mono/utils/mono-threads.h>
 #include <pthread.h>
+#include <sys/syscall.h>
 
 void
 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
@@ -36,4 +37,10 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
 
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       return (guint64)syscall (SYS_gettid);
+}
+
 #endif
index 31a6d2c..8506282 100644 (file)
@@ -278,4 +278,12 @@ mono_threads_platform_is_main_thread (void)
 {
        return pthread_main_np () == 1;
 }
+
+guint64
+mono_native_thread_os_id_get (void)
+{
+       uint64_t tid;
+       pthread_threadid_np (pthread_self (), &tid);
+       return tid;
+}
 #endif
index fd8fb8f..b4704c6 100644 (file)
@@ -8,6 +8,7 @@
 
 #include <mono/utils/mono-threads.h>
 #include <pthread.h>
+#include <lwp.h>
 
 void
 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
@@ -24,4 +25,10 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
        pthread_attr_destroy (&attr);
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       return (guint64)_lwp_self ();
+}
+
 #endif
index b679059..fb2e3eb 100644 (file)
@@ -23,4 +23,10 @@ mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
        *stsize = ss.ss_size;
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       return (guint64)getthrid ();
+}
+
 #endif
index f712711..212d741 100644 (file)
@@ -100,6 +100,16 @@ mono_native_thread_id_get (void)
 #endif
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+#ifdef __EMSCRIPTEN_PTHREADS__
+       return (guint64)pthread_self ();
+#else
+       return 1;
+#endif
+}
+
 MONO_API gboolean
 mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
 {
index 2a716c8..4c0f162 100644 (file)
@@ -406,6 +406,12 @@ mono_native_thread_id_get (void)
        return GetCurrentThreadId ();
 }
 
+guint64
+mono_native_thread_os_id_get (void)
+{
+       return (guint64)GetCurrentThreadId ();
+}
+
 gboolean
 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
 {
index f6c6e49..198f5aa 100644 (file)
@@ -622,6 +622,16 @@ void mono_threads_coop_end_global_suspend (void);
 MONO_API MonoNativeThreadId
 mono_native_thread_id_get (void);
 
+/*
+ * This does _not_ return the same value as mono_native_thread_id_get, except on Windows.
+ * On POSIX, mono_native_thread_id_get returns the value from pthread_self, which is then
+ * passed around as an identifier to other pthread functions. However this function, where 
+ * possible, returns the OS-unique thread id value, fetched in a platform-specific manner. 
+ * It will not work with the various pthread functions, should never be used as a
+ * MonoNativeThreadId, and is intended solely to match the output of various diagonistic tools.
+ */
+guint64 mono_native_thread_os_id_get (void);
+
 MONO_API gboolean
 mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2);
 
index 9a161e8..cfd9827 100644 (file)
@@ -90,7 +90,7 @@ namespace System.Threading
 
                internal static ulong CurrentOSThreadId {
                        get {
-                               throw new NotImplementedException ();
+                               return GetCurrentOSThreadId ();
                        }
                }
 
@@ -270,6 +270,9 @@ namespace System.Threading
                }
 
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               private extern static ulong GetCurrentOSThreadId ();
+
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
                extern static void InitInternal (Thread thread);
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]