Skip to content

Commit 76837f9

Browse files
committed
Fix support for setting thread names on linux and macos
Signed-off-by: Rye <[email protected]>
1 parent d2e8003 commit 76837f9

File tree

7 files changed

+40
-33
lines changed

7 files changed

+40
-33
lines changed

indra/integration_tests/llimage_libtest/llimage_libtest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ class LogThread : public LLThread
345345

346346
int main(int argc, char** argv)
347347
{
348+
// Call Tracy first thing to have it allocate memory
349+
// https://github.com/wolfpld/tracy/issues/196
350+
LL_PROFILER_FRAME_END;
351+
LL_PROFILER_SET_THREAD_NAME("App");
352+
348353
// List of input and output files
349354
std::list<std::string> input_filenames;
350355
std::list<std::string> output_filenames;

indra/llappearanceutility/appearance_utility.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434

3535
int main(int argc, char** argv)
3636
{
37+
// Call Tracy first thing to have it allocate memory
38+
// https://github.com/wolfpld/tracy/issues/196
39+
LL_PROFILER_FRAME_END;
40+
LL_PROFILER_SET_THREAD_NAME("App");
41+
3742
// Create an application instance.
3843
ll_init_apr();
3944
LLAppAppearanceUtility* app = new LLAppAppearanceUtility(argc, argv);

indra/llcommon/llthread.cpp

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
#include <sched.h>
4343
#endif
4444

45+
#if LL_DARWIN || LL_LINUX
46+
#include <pthread.h>
47+
#endif
48+
4549

4650
#ifdef LL_WINDOWS
4751

@@ -56,25 +60,32 @@ typedef struct tagTHREADNAME_INFO
5660
DWORD dwFlags; // Reserved for future use, must be zero.
5761
} THREADNAME_INFO;
5862
#pragma pack(pop)
63+
#endif
5964

60-
void set_thread_name( DWORD dwThreadID, const char* threadName)
65+
void set_thread_name(const char* threadName)
6166
{
67+
#if LL_WINDOWS
6268
THREADNAME_INFO info;
63-
info.dwType = 0x1000;
64-
info.szName = threadName;
65-
info.dwThreadID = dwThreadID;
66-
info.dwFlags = 0;
69+
info.dwType = 0x1000;
70+
info.szName = threadName;
71+
info.dwThreadID = GetCurrentThreadId();
72+
info.dwFlags = 0;
6773

6874
__try
6975
{
70-
::RaiseException( MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (ULONG_PTR*)&info );
76+
::RaiseException(MS_VC_EXCEPTION, 0, sizeof(info) / sizeof(DWORD), (ULONG_PTR*)&info);
7177
}
72-
__except(EXCEPTION_CONTINUE_EXECUTION)
78+
__except (EXCEPTION_CONTINUE_EXECUTION)
7379
{
7480
}
75-
}
81+
#elif LL_DARWIN
82+
std::string truncated_name(std::string_view(threadName).substr(0, 15));
83+
pthread_setname_np(truncated_name.c_str());
84+
#elif LL_LINUX
85+
std::string truncated_name(std::string_view(threadName).substr(0, 15));
86+
pthread_setname_np(pthread_self(), truncated_name.c_str());
7687
#endif
77-
88+
}
7889

7990
//----------------------------------------------------------------------------
8091
// Usage:
@@ -148,27 +159,12 @@ LL_COMMON_API bool assert_main_thread()
148159
return false;
149160
}
150161

151-
// this function has become moot
152-
void LLThread::registerThreadID() {}
153-
154162
//
155163
// Handed to the APR thread creation function
156164
//
157165
void LLThread::threadRun()
158166
{
159-
#ifdef LL_WINDOWS
160-
set_thread_name(-1, mName.c_str());
161-
162-
#if 0 // probably a bad idea, see usage of SetThreadIdealProcessor in LLWindowWin32)
163-
HANDLE hThread = GetCurrentThread();
164-
if (hThread)
165-
{
166-
SetThreadAffinityMask(hThread, (DWORD_PTR) 0xFFFFFFFFFFFFFFFE);
167-
}
168-
#endif
169-
170-
#endif
171-
167+
set_thread_name(mName.c_str());
172168
LL_PROFILER_SET_THREAD_NAME( mName.c_str() );
173169

174170
// this is the first point at which we're actually running in the new thread

indra/llcommon/llthread.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
#define LL_LLTHREAD_H
2929

3030
#include "llapr.h"
31-
#include "boost/intrusive_ptr.hpp"
3231
#include "llrefcount.h"
3332
#include <thread>
3433

34+
extern void set_thread_name(const char* threadName);
35+
3536
namespace LLTrace
3637
{
3738
class ThreadRecorder;
@@ -86,11 +87,6 @@ class LL_COMMON_API LLThread
8687

8788
id_t getID() const { return mID; }
8889

89-
// Called by threads *not* created via LLThread to register some
90-
// internal state used by LLMutex. You must call this once early
91-
// in the running thread to prevent collisions with the main thread.
92-
static void registerThreadID();
93-
9490
private:
9591
bool mPaused;
9692
std::thread::native_handle_type mNativeHandle; // for termination in case of issues

indra/llcommon/threadpool.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ void LL::ThreadPoolBase::start()
7878
std::string tname{ stringize(mName, ':', (i+1), '/', mThreadCount) };
7979
mThreads.emplace_back(tname, [this, tname]()
8080
{
81+
set_thread_name(tname.c_str());
8182
LL_PROFILER_SET_THREAD_NAME(tname.c_str());
8283
run(tname);
8384
});

indra/llcorehttp/_httpservice.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,12 +283,11 @@ void HttpService::shutdown()
283283
// requested to stop.
284284
void HttpService::threadRun(LLCoreInt::HttpThread * thread)
285285
{
286+
set_thread_name("HttpService");
286287
LL_PROFILER_SET_THREAD_NAME("HttpService");
287288

288289
boost::this_thread::disable_interruption di;
289290

290-
LLThread::registerThreadID();
291-
292291
ELoopSpeed loop(REQUEST_SLEEP);
293292
while (! mExitRequested)
294293
{

indra/test/test.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,11 @@ static LLTrace::ThreadRecorder* sMasterThreadRecorder = NULL;
505505

506506
int main(int argc, char **argv)
507507
{
508+
// Call Tracy first thing to have it allocate memory
509+
// https://github.com/wolfpld/tracy/issues/196
510+
LL_PROFILER_FRAME_END;
511+
LL_PROFILER_SET_THREAD_NAME("App");
512+
508513
ll_init_apr();
509514
apr_getopt_t* os = NULL;
510515
if(APR_SUCCESS != apr_getopt_init(&os, gAPRPoolp, argc, argv))

0 commit comments

Comments
 (0)