From 3823c58def7367c6bc4910111ae4b55169f08b9a Mon Sep 17 00:00:00 2001 From: Ryan Lucia Date: Fri, 19 Jul 2019 13:22:50 -0400 Subject: [PATCH] [netcore] Implement Thread.CurrentOSThreadId (mono/mono#15748) * [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 --- src/mono/mono/metadata/icall-def-netcore.h | 1 + src/mono/mono/metadata/threads.c | 6 ++++++ src/mono/mono/utils/mono-threads-aix.c | 10 ++++++++++ src/mono/mono/utils/mono-threads-android.c | 7 +++++++ src/mono/mono/utils/mono-threads-freebsd.c | 9 +++++++++ src/mono/mono/utils/mono-threads-haiku.c | 6 ++++++ src/mono/mono/utils/mono-threads-linux.c | 7 +++++++ src/mono/mono/utils/mono-threads-mach.c | 8 ++++++++ src/mono/mono/utils/mono-threads-netbsd.c | 7 +++++++ src/mono/mono/utils/mono-threads-openbsd.c | 6 ++++++ src/mono/mono/utils/mono-threads-wasm.c | 10 ++++++++++ src/mono/mono/utils/mono-threads-windows.c | 6 ++++++ src/mono/mono/utils/mono-threads.h | 10 ++++++++++ .../System.Private.CoreLib/src/System.Threading/Thread.cs | 5 ++++- 14 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/icall-def-netcore.h b/src/mono/mono/metadata/icall-def-netcore.h index f76eb81..c52222f 100644 --- a/src/mono/mono/metadata/icall-def-netcore.h +++ b/src/mono/mono/metadata/icall-def-netcore.h @@ -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, ()) diff --git a/src/mono/mono/metadata/threads.c b/src/mono/mono/metadata/threads.c index 490a342..a7da09e 100644 --- a/src/mono/mono/metadata/threads.c +++ b/src/mono/mono/metadata/threads.c @@ -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 diff --git a/src/mono/mono/utils/mono-threads-aix.c b/src/mono/mono/utils/mono-threads-aix.c index 17a4878..ac2227e 100644 --- a/src/mono/mono/utils/mono-threads-aix.c +++ b/src/mono/mono/utils/mono-threads-aix.c @@ -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 diff --git a/src/mono/mono/utils/mono-threads-android.c b/src/mono/mono/utils/mono-threads-android.c index 6c60d82..f01e670 100644 --- a/src/mono/mono/utils/mono-threads-android.c +++ b/src/mono/mono/utils/mono-threads-android.c @@ -11,6 +11,7 @@ #include #include "glib.h" #include +#include 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 diff --git a/src/mono/mono/utils/mono-threads-freebsd.c b/src/mono/mono/utils/mono-threads-freebsd.c index f854fba..9bbd488 100644 --- a/src/mono/mono/utils/mono-threads-freebsd.c +++ b/src/mono/mono/utils/mono-threads-freebsd.c @@ -9,6 +9,7 @@ #include #include #include +#include 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 diff --git a/src/mono/mono/utils/mono-threads-haiku.c b/src/mono/mono/utils/mono-threads-haiku.c index e5c3563..7ec6f76 100644 --- a/src/mono/mono/utils/mono-threads-haiku.c +++ b/src/mono/mono/utils/mono-threads-haiku.c @@ -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 diff --git a/src/mono/mono/utils/mono-threads-linux.c b/src/mono/mono/utils/mono-threads-linux.c index e73b860..2fee712 100644 --- a/src/mono/mono/utils/mono-threads-linux.c +++ b/src/mono/mono/utils/mono-threads-linux.c @@ -8,6 +8,7 @@ #include #include +#include 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 diff --git a/src/mono/mono/utils/mono-threads-mach.c b/src/mono/mono/utils/mono-threads-mach.c index 31a6d2c..8506282 100644 --- a/src/mono/mono/utils/mono-threads-mach.c +++ b/src/mono/mono/utils/mono-threads-mach.c @@ -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 diff --git a/src/mono/mono/utils/mono-threads-netbsd.c b/src/mono/mono/utils/mono-threads-netbsd.c index fd8fb8f..b4704c6 100644 --- a/src/mono/mono/utils/mono-threads-netbsd.c +++ b/src/mono/mono/utils/mono-threads-netbsd.c @@ -8,6 +8,7 @@ #include #include +#include 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 diff --git a/src/mono/mono/utils/mono-threads-openbsd.c b/src/mono/mono/utils/mono-threads-openbsd.c index b679059..fb2e3eb 100644 --- a/src/mono/mono/utils/mono-threads-openbsd.c +++ b/src/mono/mono/utils/mono-threads-openbsd.c @@ -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 diff --git a/src/mono/mono/utils/mono-threads-wasm.c b/src/mono/mono/utils/mono-threads-wasm.c index f712711..212d741 100644 --- a/src/mono/mono/utils/mono-threads-wasm.c +++ b/src/mono/mono/utils/mono-threads-wasm.c @@ -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) { diff --git a/src/mono/mono/utils/mono-threads-windows.c b/src/mono/mono/utils/mono-threads-windows.c index 2a716c8..4c0f162 100644 --- a/src/mono/mono/utils/mono-threads-windows.c +++ b/src/mono/mono/utils/mono-threads-windows.c @@ -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) { diff --git a/src/mono/mono/utils/mono-threads.h b/src/mono/mono/utils/mono-threads.h index f6c6e49..198f5aa 100644 --- a/src/mono/mono/utils/mono-threads.h +++ b/src/mono/mono/utils/mono-threads.h @@ -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); diff --git a/src/mono/netcore/System.Private.CoreLib/src/System.Threading/Thread.cs b/src/mono/netcore/System.Private.CoreLib/src/System.Threading/Thread.cs index 9a161e8..cfd9827 100644 --- a/src/mono/netcore/System.Private.CoreLib/src/System.Threading/Thread.cs +++ b/src/mono/netcore/System.Private.CoreLib/src/System.Threading/Thread.cs @@ -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)] -- 2.7.4