Skip to content

Commit 112ef3d

Browse files
authored
Share pthread_setname via minipal (#108370)
1 parent 48dbc4f commit 112ef3d

File tree

14 files changed

+201
-355
lines changed

14 files changed

+201
-355
lines changed

src/coreclr/gc/unix/gcenv.unix.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "gcenv.unix.inl"
2222
#include "volatile.h"
2323
#include "numasupport.h"
24+
#include <minipal/thread.h>
2425

2526
#if HAVE_SWAPCTL
2627
#include <sys/swap.h>
@@ -389,18 +390,7 @@ void GCToOSInterface::Shutdown()
389390
// Numeric id of the current thread, as best we can retrieve it.
390391
uint64_t GCToOSInterface::GetCurrentThreadIdForLogging()
391392
{
392-
#if defined(__linux__)
393-
return (uint64_t)syscall(SYS_gettid);
394-
#elif HAVE_PTHREAD_GETTHREADID_NP
395-
return (uint64_t)pthread_getthreadid_np();
396-
#elif HAVE_PTHREAD_THREADID_NP
397-
unsigned long long tid;
398-
pthread_threadid_np(pthread_self(), &tid);
399-
return (uint64_t)tid;
400-
#else
401-
// Fallback in case we don't know how to get integer thread id on the current platform
402-
return (uint64_t)pthread_self();
403-
#endif
393+
return (uint64_t)minipal_get_current_thread_id();
404394
}
405395

406396
// Get the process ID of the process.

src/coreclr/nativeaot/Runtime/eventpipe/ep-rt-aot.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,7 @@ ep_rt_aot_current_thread_get_id (void)
405405
{
406406
STATIC_CONTRACT_NOTHROW;
407407
#ifdef TARGET_UNIX
408-
static __thread uint64_t tid;
409-
if (!tid)
410-
tid = PalGetCurrentOSThreadId();
411-
return static_cast<ep_rt_thread_id_t>(tid);
408+
return static_cast<ep_rt_thread_id_t>(PalGetCurrentOSThreadId());
412409
#else
413410
return static_cast<ep_rt_thread_id_t>(::GetCurrentThreadId ());
414411
#endif

src/coreclr/nativeaot/Runtime/unix/PalCreateDump.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "RhConfig.h"
4949

5050
#include <minipal/utils.h>
51+
#include <minipal/thread.h>
5152
#include <generatedumpflags.h>
5253

5354
#if !defined(HOST_MACCATALYST) && !defined(HOST_IOS) && !defined(HOST_TVOS)
@@ -59,31 +60,6 @@ const char* g_argvCreateDump[MAX_ARGV_ENTRIES] = { nullptr };
5960
char* g_szCreateDumpPath = nullptr;
6061
char* g_ppidarg = nullptr;
6162

62-
/*++
63-
Function:
64-
PlatformGetCurrentThreadId
65-
66-
Returns the current thread id
67-
*/
68-
69-
#if defined(__linux__)
70-
#define PlatformGetCurrentThreadId() (uint32_t)syscall(SYS_gettid)
71-
#elif defined(__APPLE__)
72-
inline uint32_t PlatformGetCurrentThreadId() {
73-
uint64_t tid;
74-
pthread_threadid_np(pthread_self(), &tid);
75-
return (uint32_t)tid;
76-
}
77-
#elif defined(__FreeBSD__)
78-
#include <pthread_np.h>
79-
#define PlatformGetCurrentThreadId() (uint32_t)pthread_getthreadid_np()
80-
#elif defined(__NetBSD__)
81-
#include <lwp.h>
82-
#define PlatformGetCurrentThreadId() (uint32_t)_lwp_self()
83-
#else
84-
#define PlatformGetCurrentThreadId() (uint32_t)pthread_self()
85-
#endif
86-
8763
const size_t MaxUnsigned32BitDecString = STRING_LENGTH("4294967295");
8864
const size_t MaxUnsigned64BitDecString = STRING_LENGTH("18446744073709551615");
8965

@@ -386,7 +362,7 @@ PalCreateCrashDumpIfEnabled(int signal, siginfo_t* siginfo, void* exceptionRecor
386362
}
387363

388364
// Add the current thread id to the command line. This function is always called on the crashing thread.
389-
crashThreadArg = FormatInt(PlatformGetCurrentThreadId());
365+
crashThreadArg = FormatInt(minipal_get_current_thread_id());
390366
if (crashThreadArg != nullptr)
391367
{
392368
argv[argc++] = "--crashthread";

src/coreclr/nativeaot/Runtime/unix/PalRedhawkUnix.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <sys/time.h>
4343
#include <cstdarg>
4444
#include <signal.h>
45+
#include <minipal/thread.h>
4546

4647
#if HAVE_PTHREAD_GETTHREADID_NP
4748
#include <pthread_np.h>
@@ -718,15 +719,14 @@ REDHAWK_PALEXPORT bool REDHAWK_PALAPI PalStartBackgroundWork(_In_ BackgroundCall
718719

719720
REDHAWK_PALIMPORT bool REDHAWK_PALAPI PalSetCurrentThreadName(const char* name)
720721
{
721-
const int MAX_THREAD_NAME_SIZE = 15;
722-
char name_copy[MAX_THREAD_NAME_SIZE + 1];
723-
strncpy(name_copy, name, MAX_THREAD_NAME_SIZE);
724-
name_copy[MAX_THREAD_NAME_SIZE] = '\0';
725-
#ifdef __APPLE__
726-
pthread_setname_np(name_copy);
727-
#else
728-
pthread_setname_np(pthread_self(), name_copy);
729-
#endif //__APPLE__
722+
// Ignore requests to set the main thread name because
723+
// it causes the value returned by Process.ProcessName to change.
724+
if ((pid_t)PalGetCurrentOSThreadId() != getpid())
725+
{
726+
int setNameResult = minipal_set_thread_name(pthread_self(), name);
727+
(void)setNameResult; // used
728+
assert(setNameResult == 0);
729+
}
730730
return true;
731731
}
732732

@@ -1292,19 +1292,5 @@ extern "C" uint64_t PalQueryPerformanceFrequency()
12921292

12931293
extern "C" uint64_t PalGetCurrentOSThreadId()
12941294
{
1295-
#if defined(__linux__)
1296-
return (uint64_t)syscall(SYS_gettid);
1297-
#elif defined(__APPLE__)
1298-
uint64_t tid;
1299-
pthread_threadid_np(pthread_self(), &tid);
1300-
return (uint64_t)tid;
1301-
#elif HAVE_PTHREAD_GETTHREADID_NP
1302-
return (uint64_t)pthread_getthreadid_np();
1303-
#elif HAVE_LWP_SELF
1304-
return (uint64_t)_lwp_self();
1305-
#else
1306-
// Fallback in case we don't know how to get integer thread id on the current platform
1307-
return (uint64_t)pthread_self();
1308-
#endif
1295+
return (uint64_t)minipal_get_current_thread_id();
13091296
}
1310-

src/coreclr/pal/src/include/pal/thread.hpp

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Module Name:
3333
#include "threadinfo.hpp"
3434
#include "synchobjects.hpp"
3535
#include <errno.h>
36+
#include <minipal/thread.h>
3637

3738
namespace CorUnix
3839
{
@@ -91,13 +92,6 @@ namespace CorUnix
9192
HANDLE *phThread
9293
);
9394

94-
PAL_ERROR
95-
InternalSetThreadDescription(
96-
CPalThread *,
97-
HANDLE,
98-
PCWSTR
99-
);
100-
10195
PAL_ERROR
10296
CreateThreadData(
10397
CPalThread **ppThread
@@ -192,14 +186,6 @@ namespace CorUnix
192186
int
193187
);
194188

195-
friend
196-
PAL_ERROR
197-
InternalSetThreadDescription(
198-
CPalThread *,
199-
HANDLE,
200-
PCWSTR
201-
);
202-
203189
friend
204190
PAL_ERROR
205191
CreateThreadData(
@@ -723,50 +709,9 @@ TLSCleanup(
723709
extern PAL_ActivationFunction g_activationFunction;
724710
extern PAL_SafeActivationCheckFunction g_safeActivationCheckFunction;
725711

726-
/*++
727-
Macro:
728-
THREADSilentGetCurrentThreadId
729-
730-
Abstract:
731-
Same as GetCurrentThreadId, but it doesn't output any traces.
732-
It is useful for tracing functions to display the thread ID
733-
without generating any new traces.
734-
735-
TODO: how does the perf of pthread_self compare to
736-
InternalGetCurrentThread when we find the thread in the
737-
cache?
738-
739-
If the perf of pthread_self is comparable to that of the stack
740-
bounds based lookaside system, why aren't we using it in the
741-
cache?
742-
743-
In order to match the thread ids that debuggers use at least for
744-
linux we need to use gettid().
745-
746-
--*/
747-
#if defined(__linux__)
748-
#define PlatformGetCurrentThreadId() (SIZE_T)syscall(SYS_gettid)
749-
#elif defined(__APPLE__)
750-
inline SIZE_T PlatformGetCurrentThreadId() {
751-
uint64_t tid;
752-
pthread_threadid_np(pthread_self(), &tid);
753-
return (SIZE_T)tid;
754-
}
755-
#elif defined(__FreeBSD__)
756-
#include <pthread_np.h>
757-
#define PlatformGetCurrentThreadId() (SIZE_T)pthread_getthreadid_np()
758-
#elif defined(__NetBSD__)
759-
#include <lwp.h>
760-
#define PlatformGetCurrentThreadId() (SIZE_T)_lwp_self()
761-
#else
762-
#define PlatformGetCurrentThreadId() (SIZE_T)pthread_self()
763-
#endif
764-
765-
inline SIZE_T THREADSilentGetCurrentThreadId() {
766-
static __thread SIZE_T tid;
767-
if (!tid)
768-
tid = PlatformGetCurrentThreadId();
769-
return tid;
712+
inline SIZE_T THREADSilentGetCurrentThreadId()
713+
{
714+
return minipal_get_current_thread_id();
770715
}
771716

772717
#endif // _PAL_THREAD_HPP_

src/coreclr/pal/src/misc/perfjitdump.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace
102102
{
103103
JitCodeLoadRecord() :
104104
pid(getpid()),
105-
tid((uint32_t)PlatformGetCurrentThreadId())
105+
tid((uint32_t)THREADSilentGetCurrentThreadId())
106106
{
107107
header.id = JIT_CODE_LOAD;
108108
header.timestamp = GetTimeStampNS();

src/coreclr/pal/src/synchmgr/synchmanager.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,9 +1708,7 @@ namespace CorUnix
17081708
reinterpret_cast<CPalSynchronizationManager*>(pArg);
17091709
CPalThread * pthrWorker = InternalGetCurrentThread();
17101710

1711-
InternalSetThreadDescription(pthrWorker,
1712-
PAL_GetCurrentThread(),
1713-
W(".NET SynchManager"));
1711+
SetThreadDescription(PAL_GetCurrentThread(), W(".NET SynchManager"));
17141712

17151713
while (!fWorkerIsDone)
17161714
{

0 commit comments

Comments
 (0)