Index: lib/tsan/rtl/tsan_interface_ann.cc =================================================================== --- lib/tsan/rtl/tsan_interface_ann.cc +++ lib/tsan/rtl/tsan_interface_ann.cc @@ -420,6 +420,12 @@ ThreadSetName(thr, name); } +void INTERFACE_ATTRIBUTE AnnotateThreadNameByUserId( + uptr thread, char *f, int l, char *name) { + SCOPED_ANNOTATION(AnnotateThreadName); + ctx->thread_registry->SetThreadNameByUserId(thread, name); +} + // We deliberately omit the implementation of WTFAnnotateHappensBefore() and // WTFAnnotateHappensAfter(). Those are being used by Webkit to annotate // atomic operations, which should be handled by ThreadSanitizer correctly. Index: test/tsan/thread_name.cc =================================================================== --- test/tsan/thread_name.cc +++ test/tsan/thread_name.cc @@ -3,6 +3,12 @@ #include #include +#if defined(__linux__) +#define USE_PTHREAD_SETNAME_NP GLIBC_PREREQ(2, 12) +#else +#define USE_PTHREAD_SETNAME_NP 0 +#endif + extern "C" void AnnotateThreadName(const char *f, int l, const char *name); int Global; @@ -15,7 +21,7 @@ } void *Thread2(void *x) { -#if SANITIZER_LINUX && __GLIBC_PREREQ(2, 12) +#if USE_PTHREAD_SETNAME_NP pthread_setname_np(pthread_self(), "Thread2"); #else AnnotateThreadName(__FILE__, __LINE__, "Thread2"); @@ -35,4 +41,3 @@ // CHECK: WARNING: ThreadSanitizer: data race // CHECK: Thread T1 'Thread1' // CHECK: Thread T2 'Thread2' - Index: test/tsan/thread_name2.cc =================================================================== --- test/tsan/thread_name2.cc +++ test/tsan/thread_name2.cc @@ -3,6 +3,23 @@ #include #include +#if defined(__linux__) +#define USE_PTHREAD_SETNAME_NP GLIBC_PREREQ(2, 12) +#else +#define USE_PTHREAD_SETNAME_NP 0 +#endif + +#if USE_PTHREAD_SETNAME_NP +#define SET_THREAD_NAME(thread, name) \ + pthread_setname_np(thread, name) +#else +#define SET_THREAD_NAME(thread, name) \ + AnnotateThreadNameByUserId(thread, __FILE__, __LINE__, name) + +extern "C" void AnnotateThreadNameByUserId(void *t, const char *f, + int l, const char *name); +#endif + int Global; void *Thread1(void *x) { @@ -12,7 +29,7 @@ } void *Thread2(void *x) { - pthread_setname_np(pthread_self(), "foobar2"); + SET_THREAD_NAME(pthread_self(), "foobar2"); Global--; return 0; } @@ -21,7 +38,7 @@ pthread_t t[2]; pthread_create(&t[0], 0, Thread1, 0); pthread_create(&t[1], 0, Thread2, 0); - pthread_setname_np(t[0], "foobar1"); + SET_THREAD_NAME(t[0], "foobar1"); pthread_join(t[0], NULL); pthread_join(t[1], NULL); } @@ -29,4 +46,3 @@ // CHECK: WARNING: ThreadSanitizer: data race // CHECK: Thread T1 'foobar1' // CHECK: Thread T2 'foobar2' -