-
Notifications
You must be signed in to change notification settings - Fork 37
Refactor ThreadName() to improve its portability #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| // Distributed under the MIT software license, see the accompanying | ||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||
|
|
||
| #include <mp/config.h> | ||
| #include <mp/util.h> | ||
|
|
||
| #include <kj/array.h> | ||
|
|
@@ -13,12 +14,17 @@ | |
| #include <sys/types.h> | ||
| #include <sys/un.h> | ||
| #include <sys/wait.h> | ||
| #include <thread> | ||
| #include <unistd.h> | ||
|
|
||
| #if __linux__ | ||
| #include <syscall.h> | ||
| #endif | ||
|
|
||
| #ifdef HAVE_PTHREAD_GETTHREADID_NP | ||
| #include <pthread_np.h> | ||
| #endif // HAVE_PTHREAD_GETTHREADID_NP | ||
|
|
||
| namespace mp { | ||
| namespace { | ||
|
|
||
|
|
@@ -37,16 +43,28 @@ size_t MaxFd() | |
|
|
||
| std::string ThreadName(const char* exe_name) | ||
| { | ||
| char thread_name[17] = {0}; | ||
| char thread_name[16] = {0}; | ||
|
Comment on lines
-40
to
+46
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: This change looks right. Previous code was trying to make room for an extra null byte, but pthread_getname_np documentation does say 16 byte max includes the null byte like PR description says
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, the doc also says that the output name will be null terminated so it may seem ok to remove the initialization |
||
| #ifdef HAVE_PTHREAD_GETNAME_NP | ||
| pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name)); | ||
| uint64_t tid = 0; | ||
| #endif // HAVE_PTHREAD_GETNAME_NP | ||
|
|
||
| std::ostringstream buffer; | ||
| buffer << (exe_name ? exe_name : "") << "-" << getpid() << "/" << thread_name << "-"; | ||
|
|
||
| // Prefer platform specific thread ids over the standard C++11 ones because | ||
| // the former are shorter and are the same as what gdb prints "LWP ...". | ||
| #if __linux__ | ||
| tid = syscall(SYS_gettid); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like keep using
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Done. And also resurrected the other call to I checked that on FreeBSD |
||
| #else | ||
| buffer << syscall(SYS_gettid); | ||
| #elif defined(HAVE_PTHREAD_THREADID_NP) | ||
| uint64_t tid = 0; | ||
| pthread_threadid_np(NULL, &tid); | ||
| buffer << tid; | ||
| #elif defined(HAVE_PTHREAD_GETTHREADID_NP) | ||
| buffer << pthread_getthreadid_np(); | ||
| #else | ||
| buffer << std::this_thread::get_id(); | ||
| #endif | ||
| std::ostringstream buffer; | ||
| buffer << (exe_name ? exe_name : "") << "-" << getpid() << "/" << thread_name << "-" << tid; | ||
|
|
||
| return std::move(buffer.str()); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.