[Mono] Don't set thread name of main thread on Linux (#36116)
authorAleksey Kliger (λgeek) <alklig@microsoft.com>
Thu, 14 May 2020 19:57:32 +0000 (15:57 -0400)
committerGitHub <noreply@github.com>
Thu, 14 May 2020 19:57:32 +0000 (15:57 -0400)
commit04708d5b5d25795fac25663886e3b5fa68f3fa3c
treed60c55521efb2236be6d5d45e679f777460790eb
parent459102612815c06dece8542b0a8b6c5e3eb42537
[Mono] Don't set thread name of main thread on Linux (#36116)

* [mono] Record MonoNativeThreadId of the main thread

We would like to know the MonoNativeThreadId (pthread_t on Linux) of the main
thread of the application.  We can identify the main thread (on linux) because
it is the one for which `gettid () == getpid ()`.  (`gettid()` returns a
`pid_t` which is not the same thing as a `pthread_t`, hence this roundabout way
of detecting it.)

A complication arises in embedding scenarios: the main thread is not
necessarily the one that calls `mono_jit_init` or otherwise interacts with the
runtime.  Therefore we do the `gettid() == getpid ()` test at `MonoThreadInfo`
creation time when we call `register_thread`.

If the main thread never interacts with Mono, the main thread is not known to
us.

* [mono] Don't set name of main thread on Linux

Setting the name of the main thread also changes the name of the process.

Fixes https://github.com/dotnet/runtime/issues/35908

The corresponding fix for CoreCLR is https://github.com/dotnet/runtime/pull/34064

* Re-enable test from https://github.com/dotnet/runtime/pull/34064
src/libraries/System.Threading.Thread/tests/ThreadTests.cs
src/mono/mono/utils/mono-threads-posix.c
src/mono/mono/utils/mono-threads.c
src/mono/mono/utils/mono-threads.h