Skip to content

Commit

Permalink
[netcore] Implement Thread.GetCurrentProcessorId (mono/mono#18450)
Browse files Browse the repository at this point in the history
CoreCLR has a managed cache surrounding this, but for the moment the naive implementation will suffice. We can later move their managed cache to shared on top of this PR with minimal trouble.

We can't guarantee the id will be [0..CpuCount) for a variety of reasons, and a followup PR to the dotnet docs will clarify this. I've chosen not to intentionally add an offset, but no one should be relying on that behavior, and if someone thinks adding an offset would drive the point home the perf cost is irrelevant.

Commit migrated from mono/mono@3b19228
  • Loading branch information
CoffeeFlux authored and akoeplinger committed Jan 14, 2020
1 parent d6222a3 commit 590f336
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/mono/mono/metadata/icall-def-netcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ 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_16, "GetCurrentProcessorNumber", ves_icall_System_Threading_Thread_GetCurrentProcessorNumber, gint32, 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, ())
Expand Down
6 changes: 6 additions & 0 deletions src/mono/mono/metadata/threads.c
Original file line number Diff line number Diff line change
Expand Up @@ -6768,4 +6768,10 @@ ves_icall_System_Threading_Thread_GetCurrentOSThreadId (MonoError *error)
return mono_native_thread_os_id_get ();
}

gint32
ves_icall_System_Threading_Thread_GetCurrentProcessorNumber (MonoError *error)
{
return mono_native_thread_processor_id_get ();
}

#endif
10 changes: 10 additions & 0 deletions src/mono/mono/utils/mono-threads-posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,16 @@ mono_memory_barrier_process_wide (void)
g_assert (status == 0);
}

gint32
mono_native_thread_processor_id_get (void)
{
#ifdef HAVE_SCHED_GETCPU
return sched_getcpu ();
#else
return -1;
#endif
}

#endif /* defined(_POSIX_VERSION) */

#if defined(USE_POSIX_BACKEND)
Expand Down
6 changes: 6 additions & 0 deletions src/mono/mono/utils/mono-threads-wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ mono_native_thread_os_id_get (void)
#endif
}

gint32
mono_native_thread_processor_id_get (void)
{
return -1;
}

MONO_API gboolean
mono_native_thread_create (MonoNativeThreadId *tid, gpointer func, gpointer arg)
{
Expand Down
8 changes: 8 additions & 0 deletions src/mono/mono/utils/mono-threads-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ mono_native_thread_os_id_get (void)
return (guint64)GetCurrentThreadId ();
}

gint32
mono_native_thread_processor_id_get (void)
{
PROCESSOR_NUMBER proc_num;
GetCurrentProcessorNumberEx (&proc_num);
return ((proc_num.Group << 6) | proc_num.Number);
}

gboolean
mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2)
{
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/utils/mono-threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ mono_native_thread_id_get (void);
*/
guint64 mono_native_thread_os_id_get (void);

gint32 mono_native_thread_processor_id_get (void);

MONO_API gboolean
mono_native_thread_id_equals (MonoNativeThreadId id1, MonoNativeThreadId id2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,17 @@ public void DisableComObjectEagerCleanup ()
// no-op
}

[MethodImplAttribute(MethodImplOptions.InternalCall)]
extern static int GetCurrentProcessorNumber ();

public static int GetCurrentProcessorId ()
{
// TODO: Implement correctly
return Environment.CurrentManagedThreadId;
int id = GetCurrentProcessorNumber ();

if (id < 0)
id = Environment.CurrentManagedThreadId;

return id;
}

public void Interrupt ()
Expand Down

0 comments on commit 590f336

Please sign in to comment.