Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,6 @@ public ThreadPriority Priority
}
}

[ErrorHandler(typeof(QCallExceptionStatusMarshaller), ErrorLocation.HiddenLastParameter)]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ThreadNative_GetCurrentOSThreadId")]
private static partial ulong GetCurrentOSThreadId();

/// <summary>
/// Return the thread state as a consistent set of bits. This is more
/// general then IsAlive or IsBackground.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,6 @@ internal static extern unsafe IntPtr RhpCallPropagateExceptionCallback(
internal static extern unsafe void RhpCopyContextFromExInfo(void* pOSContext, int cbOSContext, EH.PAL_LIMITED_CONTEXT* pPalContext);
#endif

[RuntimeImport(RuntimeLibrary, "RhCurrentNativeThreadId")]
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern unsafe IntPtr RhCurrentNativeThreadId();

//------------------------------------------------------------------------------------------------------------
// PInvoke-based internal calls
//
Expand Down
18 changes: 0 additions & 18 deletions src/coreclr/nativeaot/Runtime/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1382,24 +1382,6 @@ FCIMPL2(void, RhRegisterInlinedThreadStaticRoot, Object** root, TypeManager* typ
}
FCIMPLEND

// This is function is used to quickly query a value that can uniquely identify a thread
FCIMPL0(uint8_t*, RhCurrentNativeThreadId)
{
#ifndef TARGET_UNIX
return PalNtCurrentTeb();
#else
return (uint8_t*)ThreadStore::RawGetCurrentThread();
#endif // TARGET_UNIX
}
FCIMPLEND

// This function is used to get the OS thread identifier for the current thread.
FCIMPL0(uint64_t, RhCurrentOSThreadId)
{
return PalGetCurrentOSThreadId();
}
FCIMPLEND

FCIMPL0(size_t, RhGetDefaultStackSize)
{
return GetDefaultStackSizeSetting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,6 @@ internal static IntPtr RhGetModuleSection(TypeManagerHandle module, ReadyToRunSe
[RuntimeImport(RuntimeLibrary, "RhRegisterInlinedThreadStaticRoot")]
internal static extern void RhRegisterInlinedThreadStaticRoot(ref object? root, TypeManagerHandle module);

[MethodImplAttribute(MethodImplOptions.InternalCall)]
[RuntimeImport(RuntimeLibrary, "RhCurrentNativeThreadId")]
internal static extern unsafe IntPtr RhCurrentNativeThreadId();

[MethodImplAttribute(MethodImplOptions.InternalCall)]
[RuntimeImport(RuntimeLibrary, "RhCurrentOSThreadId")]
internal static extern unsafe ulong RhCurrentOSThreadId();

[MethodImplAttribute(MethodImplOptions.InternalCall)]
[RuntimeImport(RuntimeLibrary, "RhGetDefaultStackSize")]
internal static extern unsafe IntPtr RhGetDefaultStackSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,6 @@ private static unsafe void RegisterThreadExitCallback()
RuntimeImports.RhSetThreadExitCallback(&OnThreadExit);
}

internal static ulong CurrentOSThreadId
{
get
{
return RuntimeImports.RhCurrentOSThreadId();
}
}

// Slow path executed once per thread
[MethodImpl(MethodImplOptions.NoInlining)]
private static Thread InitializeCurrentThread()
Expand Down
23 changes: 0 additions & 23 deletions src/coreclr/vm/comsynchronizable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,29 +340,6 @@ extern "C" void QCALLTYPE ThreadNative_GetCurrentThread(QCall::ObjectHandleOnSta
END_QCALL;
}

extern "C" UINT64 QCALLTYPE ThreadNative_GetCurrentOSThreadId(QCallExceptionStatus* qcallError)
{
QCALL_CONTRACT;

UINT64 threadId = 0;

BEGIN_QCALL;

// The Windows API GetCurrentThreadId returns a 32-bit integer thread ID.
// On some non-Windows platforms (e.g. OSX), the thread ID is a 64-bit value.
// We special case the API for non-Windows to get the 64-bit value and zero-extend
// the Windows value to return a single data type on all platforms.

#ifndef TARGET_UNIX
threadId = (UINT64) GetCurrentThreadId();
#else
threadId = (UINT64) PAL_GetCurrentOSThreadId();
#endif
END_QCALL;

return threadId;
}

extern "C" void QCALLTYPE ThreadNative_Initialize(QCall::ObjectHandleOnStack t, QCallExceptionStatus* qcallError)
{
QCALL_CONTRACT;
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/comsynchronizable.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ extern "C" BOOL QCALLTYPE ThreadNative_GetIsBackground(QCall::ThreadHandle threa
extern "C" void QCALLTYPE ThreadNative_SetIsBackground(QCall::ThreadHandle thread, BOOL value, QCallExceptionStatus* qcallError);
extern "C" void QCALLTYPE ThreadNative_InformThreadNameChange(QCall::ThreadHandle thread, LPCWSTR name, INT32 len, QCallExceptionStatus* qcallError);
extern "C" void QCALLTYPE ThreadNative_PollGC();
extern "C" UINT64 QCALLTYPE ThreadNative_GetCurrentOSThreadId(QCallExceptionStatus* qcallError);
extern "C" void QCALLTYPE ThreadNative_Initialize(QCall::ObjectHandleOnStack t, QCallExceptionStatus* qcallError);
extern "C" INT32 QCALLTYPE ThreadNative_GetThreadState(QCall::ThreadHandle thread);
extern "C" INT32 QCALLTYPE ThreadNative_ReentrantWaitAny(BOOL alertable, INT32 timeout, INT32 count, HANDLE *handles, QCallExceptionStatus* qcallError);
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ static const Entry s_QCall[] =
DllImportEntry(ThreadNative_GetIsBackground)
DllImportEntry(ThreadNative_SetIsBackground)
DllImportEntry(ThreadNative_InformThreadNameChange)
DllImportEntry(ThreadNative_GetCurrentOSThreadId)
DllImportEntry(ThreadNative_Initialize)
DllImportEntry(ThreadNative_GetThreadState)
DllImportEntry(ThreadNative_ReentrantWaitAny)
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/vm/wasm/browser/callhelpers-pinvoke.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/coreclr/vm/wasm/wasi/callhelpers-pinvoke.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ internal unsafe partial class Sys
[return: MarshalAs(UnmanagedType.Bool)]
internal static unsafe partial bool CreateThread(IntPtr stackSize, delegate* unmanaged<IntPtr, IntPtr> startAddress, IntPtr parameter);

#if TARGET_OSX
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetUInt64OSThreadId")]
internal static unsafe partial ulong GetUInt64OSThreadId();
#else
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_TryGetUInt32OSThreadId")]
internal static unsafe partial uint TryGetUInt32OSThreadId();
#endif
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetCurrentThreadId")]
[SuppressGCTransition]
internal static unsafe partial ulong GetCurrentThreadId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ internal static partial class Kernel32
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[LibraryImport(Libraries.Kernel32)]
[SuppressGCTransition]
public static safe partial int GetCurrentThreadId();
public static safe partial uint GetCurrentThreadId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ internal static partial class Kernel32

[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[LibraryImport(Libraries.Kernel32, SetLastError = true)]
internal static partial SafeThreadHandle OpenThread(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, int dwThreadId);
internal static partial SafeThreadHandle OpenThread(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwThreadId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ internal static partial class Kernel32

[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[LibraryImport(Libraries.Kernel32, SetLastError = true)]
internal static partial IntPtr OpenThread(uint desiredAccess, [MarshalAs(UnmanagedType.Bool)] bool inherited, int threadID);
internal static partial IntPtr OpenThread(uint desiredAccess, [MarshalAs(UnmanagedType.Bool)] bool inherited, uint threadID);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ internal static partial class User32
{
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
[LibraryImport(Libraries.User32)]
public static unsafe partial int GetWindowThreadProcessId(IntPtr handle, int* processId);
public static unsafe partial uint GetWindowThreadProcessId(IntPtr handle, int* processId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ public static void InvokeOnEventsThread(Delegate method)
unsafe
{
int pid;
int thread = Interop.User32.GetWindowThreadProcessId(s_systemEvents!._windowHandle, &pid);
uint thread = Interop.User32.GetWindowThreadProcessId(s_systemEvents!._windowHandle, &pid);
Debug.Assert(s_windowThread == null || thread != Interop.Kernel32.GetCurrentThreadId(), "Don't call MarshaledInvoke on the system events thread");
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ public static SafeProcessHandle OpenProcess(int processId, int access, bool thro
throw new Win32Exception(result);
}

public static SafeThreadHandle OpenThread(int threadId, int access)
public static SafeThreadHandle OpenThread(uint threadId, int access)
{
SafeThreadHandle threadHandle = Interop.Kernel32.OpenThread(access, false, threadId);
int result = Marshal.GetLastWin32Error();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private ProcessThreadTimes GetThreadTimes()
private SafeThreadHandle OpenThreadHandle(int access)
{
EnsureState(State.IsLocal);
return ProcessManager.OpenThread((int)_threadInfo._threadId, access);
return ProcessManager.OpenThread((uint)_threadInfo._threadId, access);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ internal static void PollWasiEventLoopUntilResolvedVoid(Task mainTask)
}
#endif // TARGET_WASI

/// <summary>Returns the operating system identifier for the current thread.</summary>
internal static ulong CurrentOSThreadId => Interop.Sys.GetCurrentThreadId();

// the closest analog to Sleep(0) on Unix is sched_yield
internal static void UninterruptibleSleep0() => Thread.Yield();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace System.Threading
{
public sealed partial class Thread
{
/// <summary>Returns the operating system identifier for the current thread.</summary>
internal static ulong CurrentOSThreadId => Interop.Kernel32.GetCurrentThreadId();

internal static void UninterruptibleSleep0() => Interop.Kernel32.Sleep(0);

internal static void SleepInternal(int millisecondsTimeout)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,6 @@ public static void Sleep(int millisecondsTimeout)
SleepInternal(millisecondsTimeout);
}

#if !NATIVEAOT
/// <summary>Returns the operating system identifier for the current thread.</summary>
internal static ulong CurrentOSThreadId => GetCurrentOSThreadId();
#endif

#if !MONO
[Intrinsic]
internal static void FastPollGC() => FastPollGC();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,6 @@ private static void OnThreadExiting(Thread thread)
#endif
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern ulong GetCurrentOSThreadId();

[MemberNotNull(nameof(self))]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern void InitInternal(Thread thread);
Expand Down
1 change: 0 additions & 1 deletion src/mono/mono/metadata/icall-def.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,6 @@ ICALL_TYPE(THREAD, "System.Threading.Thread", THREAD_1)
HANDLES(THREAD_1, "ClrState", ves_icall_System_Threading_Thread_ClrState, void, 2, (MonoInternalThread, guint32))
NOHANDLES(ICALL(THREAD_16, "CurrentThreadIsFinalizerThread", ves_icall_System_Threading_Thread_CurrentThreadIsFinalizerThread))
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, ())
NOHANDLES(ICALL(THREAD_5, "GetCurrentThread", ves_icall_System_Threading_Thread_GetCurrentThread))
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))
Expand Down
6 changes: 0 additions & 6 deletions src/mono/mono/metadata/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -4891,9 +4891,3 @@ ves_icall_System_Threading_Thread_InitInternal (MonoThreadObjectHandle thread_ha
internal->state = ThreadState_Unstarted;
MONO_OBJECT_SETREF_INTERNAL (internal, internal_thread, internal);
}

guint64
ves_icall_System_Threading_Thread_GetCurrentOSThreadId (MonoError *error)
{
return mono_native_thread_os_id_get ();
}
3 changes: 1 addition & 2 deletions src/native/libs/System.Native/entrypoints.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ static const Entry s_sysNative[] =
DllImportEntry(SystemNative_GetEnviron)
DllImportEntry(SystemNative_FreeEnviron)
DllImportEntry(SystemNative_GetGroupName)
DllImportEntry(SystemNative_GetUInt64OSThreadId)
DllImportEntry(SystemNative_TryGetUInt32OSThreadId)
DllImportEntry(SystemNative_GetCurrentThreadId)
DllImportEntry(SystemNative_LowLevelCrossProcessMutex_Size)
DllImportEntry(SystemNative_LowLevelCrossProcessMutex_Init)
DllImportEntry(SystemNative_LowLevelCrossProcessMutex_Acquire)
Expand Down
13 changes: 2 additions & 11 deletions src/native/libs/System.Native/pal_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,16 +300,7 @@ void SystemNative_Abort(void)
abort();
}

// Gets a non-truncated OS thread ID that is also suitable for diagnostics, for platforms that offer a 64-bit ID
uint64_t SystemNative_GetUInt64OSThreadId(void)
uint64_t SystemNative_GetCurrentThreadId(void)
{
return (uint64_t)minipal_get_current_thread_id();
}

// Tries to get a non-truncated OS thread ID that is also suitable for diagnostics, for platforms that offer a 32-bit ID.
// Returns (uint32_t)-1 when the implementation does not know how to get the OS thread ID.
uint32_t SystemNative_TryGetUInt32OSThreadId(void)
{
uint32_t result = (uint32_t)minipal_get_current_thread_id();
return result == 0 ? (uint32_t)-1 : result;
return minipal_get_current_thread_id();
}
3 changes: 1 addition & 2 deletions src/native/libs/System.Native/pal_threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ PALEXPORT __attribute__((noreturn)) void SystemNative_Exit(int32_t exitCode);

PALEXPORT __attribute__((noreturn)) void SystemNative_Abort(void);

PALEXPORT uint64_t SystemNative_GetUInt64OSThreadId(void);
PALEXPORT uint32_t SystemNative_TryGetUInt32OSThreadId(void);
PALEXPORT uint64_t SystemNative_GetCurrentThreadId(void);
14 changes: 3 additions & 11 deletions src/native/libs/System.Native/pal_threading_wasi.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <minipal/thread.h>

#ifdef DEBUG
#define DEBUGNOTRETURN __attribute__((noreturn))
Expand Down Expand Up @@ -82,16 +83,7 @@ void SystemNative_Abort(void)
abort();
}

// Gets a non-truncated OS thread ID that is also suitable for diagnostics, for platforms that offer a 64-bit ID
uint64_t SystemNative_GetUInt64OSThreadId(void)
uint64_t SystemNative_GetCurrentThreadId(void)
{
assert(false);
return 0;
}

// Tries to get a non-truncated OS thread ID that is also suitable for diagnostics, for platforms that offer a 32-bit ID.
// Returns (uint32_t)-1 when the implementation does not know how to get the OS thread ID.
uint32_t SystemNative_TryGetUInt32OSThreadId(void)
{
return (uint32_t)-1;
return minipal_get_current_thread_id();
}
Loading