From 0b38a553b1eb497b08227b015a6e1190e4d94681 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sat, 7 Dec 2019 12:49:05 -0700 Subject: [PATCH] LibThread: Allow setting thread name in constructor Thread now has an optional thread_name parameter to the consturctor that will call pthread_setname_np if given. --- Libraries/LibThread/Thread.cpp | 7 ++++++- Libraries/LibThread/Thread.h | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Libraries/LibThread/Thread.cpp b/Libraries/LibThread/Thread.cpp index 7c2e515524..ac97ce8d1b 100644 --- a/Libraries/LibThread/Thread.cpp +++ b/Libraries/LibThread/Thread.cpp @@ -2,9 +2,10 @@ #include #include -LibThread::Thread::Thread(Function action) +LibThread::Thread::Thread(Function action, StringView thread_name) : CObject(nullptr) , m_action(move(action)) + , m_thread_name(thread_name.is_null() ? "" : thread_name) { } @@ -31,6 +32,10 @@ void LibThread::Thread::start() static_cast(this)); ASSERT(rc == 0); + if (!m_thread_name.is_empty()) { + rc = pthread_setname_np(m_tid, m_thread_name.characters(), m_thread_name.length()); + ASSERT(rc == 0); + } dbg() << "Started a thread, tid = " << m_tid; } diff --git a/Libraries/LibThread/Thread.h b/Libraries/LibThread/Thread.h index d9562da10f..b3b3c39375 100644 --- a/Libraries/LibThread/Thread.h +++ b/Libraries/LibThread/Thread.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace LibThread { @@ -9,7 +10,7 @@ class Thread final : public CObject { C_OBJECT(Thread); public: - explicit Thread(Function action); + explicit Thread(Function action, StringView thread_name = nullptr); virtual ~Thread(); void start(); @@ -18,6 +19,7 @@ public: private: Function m_action; int m_tid { -1 }; + String m_thread_name; }; }