1// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2#include "test.h"
3
4// OS X doesn't have pthread_setname_np(tid, name).
5// UNSUPPORTED: darwin
6
7#if defined(__FreeBSD__)
8#include <pthread_np.h>
9#define tsan_pthread_setname_np pthread_set_name_np
10#elif defined(__NetBSD__)
11#define tsan_pthread_setname_np(a, b) pthread_setname_np((a), "%s", (void *)(b))
12#else
13#define tsan_pthread_setname_np pthread_setname_np
14#endif
15
16long long Global;
17
18void *Thread1(void *x) {
19 barrier_wait(barrier: &barrier);
20 Global++;
21 return 0;
22}
23
24void *Thread2(void *x) {
25 tsan_pthread_setname_np(target_thread: pthread_self(), name: "foobar2");
26 Global--;
27 barrier_wait(barrier: &barrier);
28 return 0;
29}
30
31int main() {
32 barrier_init(barrier: &barrier, count: 3);
33 pthread_t t[2];
34 pthread_create(newthread: &t[0], attr: 0, start_routine: Thread1, arg: 0);
35 pthread_create(newthread: &t[1], attr: 0, start_routine: Thread2, arg: 0);
36 tsan_pthread_setname_np(target_thread: t[0], name: "foobar1");
37 barrier_wait(barrier: &barrier);
38 pthread_join(th: t[0], NULL);
39 pthread_join(th: t[1], NULL);
40}
41
42// CHECK: WARNING: ThreadSanitizer: data race
43// CHECK: Thread T1 'foobar1'
44// CHECK: Thread T2 'foobar2'
45

source code of compiler-rt/test/tsan/thread_name2.cpp