Runtime: Provide a method to retrieve the native handle for a Thread object

Created on 4 Jun 2020  路  13Comments  路  Source: dotnet/runtime

Background and Motivation

For native interop, it can be incredibly useful to have access to the underlying thread handle value. For example

  • Working with native fibres
  • Creating custom fibre libraries
  • Performing operations like setting thread affinity

Proposed API

public class Thread
{
+    public IntPtr GetNativeHandle();
+    public bool TryGetNativeHandle(out IntPtr handle);
}

GetNativeHandle would throw an exception (not sure which one) if the Thread object wasn't a true thread (currently afaik they all are, but in the future they could be fibers etc), whereas TryGetNativeHandle would return false

Usage Examples

var thread = ...;

// lock the thread to the first logical processor
Win32.SetThreadAffinityMask(thread.GetNativeHandle(), 1);

Alternative Designs

You could, in this case, just expose SetThreadAffinity - but it doesn't make sense if a thread isn't always a "real" thread

Risks

It isn't a breaking change. If misused, I am sure the handle could do some messy things, but that applies to a lot of the framework 馃槃

api-suggestion area-System.Threading

Most helpful comment

If you run managed code on fibers, you will get arbitrary crashes.

Ah, my code already does that so it's fine 馃槅

All 13 comments

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

cc @tannergooding

What is this API going to return on non-Windows?

I would assume the pthread_t* on linux systems, and on other systems the type the runtime uses to create them with the OS

@kouvel fyi..

The problem is that there are typically number of different kinds of thread handles. For example, OSX has pthread_t*, mach_port_t and TID. Even pthread_t* on Linux is not the ubiquitous thread ID. For example, the Linux OS syscalls take pid_t, not pthread_t*.

Working with native fibres
Creating custom fibre libraries

.NET does not work with Windows fibres for fundamendal reasons. This API won't make it possible.

Isn't pid_t the process ID and not the thread handle?

pid_t is used for both process id and thread id (gettid() returns it for current thread, for example)

Thanks! For reference: https://man7.org/linux/man-pages/man2/gettid.2.html

In a single-threaded process, the thread ID is equal to the process ID (PID, as returned by getpid(2)). In a multithreaded process, all threads have the same PID, but each one has a unique TID.
...
The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3)).

.NET does not work with Windows fibres for fundamendal reasons. This API won't make it possible.

is it specific to how the win32 fibre implementation works? - or is it fundamentally impossible to use fibres at all in .NET

or is it fundamentally impossible to use fibres at all in .NET

Correct. We do not support running managed code on fibers. If you run managed code on fibers, you will get arbitrary crashes.

If you run managed code on fibers, you will get arbitrary crashes.

Ah, my code already does that so it's fine 馃槅

Being able to do things like set affinity would still be useful, I think. Althought, there could be a managed API to do that specific thing

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chunseoklee picture chunseoklee  路  3Comments

v0l picture v0l  路  3Comments

matty-hall picture matty-hall  路  3Comments

bencz picture bencz  路  3Comments

yahorsi picture yahorsi  路  3Comments