summaryrefslogtreecommitdiff
path: root/libartbase/base/utils.cc
diff options
context:
space:
mode:
author Hans Boehm <hboehm@google.com> 2024-06-05 16:06:33 -0700
committer Hans Boehm <hboehm@google.com> 2024-06-26 19:40:34 +0000
commit330fb44dd9845a229ac1024fab0eed366ac2ab69 (patch)
tree7e1ed7777d362e4234561f0fa73a22570f832a5e /libartbase/base/utils.cc
parent83cc7f2316fe6f3584483543b54033bd7885b0db (diff)
Add 2275-pthread-name test for Thread.setName
This checks that we set the pthread name correctly in response to Thread.setName() calls. We did not. In fact, we incorrectly set the pthread name for the calling thread, rather than the `this` thread. Fix that, by never erroneously setting the calling threads name, and by making a good effort to set the correct one. This difers from the documented RI behavior of setting the pthread name only if this == self. That decision is not a slam dunk either way, and opinions are welcome. This approach almost certainly matches naive expectations more of the time, but it currently has the disadvantage that it can fail under much less common, but also much less predictable, circumstances. It could possibly be made fully predictable if we managed to clean up some other issues, which I highlighted in the code. Test: Host run tests, Treehugger Change-Id: I295e0af0157a530919df4120c5dbf347dd9416a2
Diffstat (limited to 'libartbase/base/utils.cc')
-rw-r--r--libartbase/base/utils.cc12
1 files changed, 9 insertions, 3 deletions
diff --git a/libartbase/base/utils.cc b/libartbase/base/utils.cc
index 0ec262e696..3cc5924593 100644
--- a/libartbase/base/utils.cc
+++ b/libartbase/base/utils.cc
@@ -283,7 +283,7 @@ template void Split(const std::string_view& s,
size_t len,
std::string_view* out_result);
-void SetThreadName(const char* thread_name) {
+void SetThreadName(pthread_t thr, const char* thread_name) {
bool hasAt = false;
bool hasDot = false;
const char* s = thread_name;
@@ -306,15 +306,21 @@ void SetThreadName(const char* thread_name) {
char buf[16]; // MAX_TASK_COMM_LEN=16 is hard-coded in the kernel.
strncpy(buf, s, sizeof(buf)-1);
buf[sizeof(buf)-1] = '\0';
- errno = pthread_setname_np(pthread_self(), buf);
+ errno = pthread_setname_np(thr, buf);
if (errno != 0) {
PLOG(WARNING) << "Unable to set the name of current thread to '" << buf << "'";
}
#else // __APPLE__
- pthread_setname_np(thread_name);
+ if (pthread_equal(thr, pthread_self())) {
+ pthread_setname_np(thread_name);
+ } else {
+ PLOG(WARNING) << "Unable to set the name of another thread to '" << thread_name << "'";
+ }
#endif
}
+void SetThreadName(const char* thread_name) { SetThreadName(pthread_self(), thread_name); }
+
void GetTaskStats(pid_t tid, char* state, int* utime, int* stime, int* task_cpu) {
*utime = *stime = *task_cpu = 0;
#ifdef _WIN32