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 
16 long long Global;
17 
Thread1(void * x)18 void *Thread1(void *x) {
19   barrier_wait(&barrier);
20   Global++;
21   return 0;
22 }
23 
Thread2(void * x)24 void *Thread2(void *x) {
25   tsan_pthread_setname_np(pthread_self(), "foobar2");
26   Global--;
27   barrier_wait(&barrier);
28   return 0;
29 }
30 
main()31 int main() {
32   barrier_init(&barrier, 3);
33   pthread_t t[2];
34   pthread_create(&t[0], 0, Thread1, 0);
35   pthread_create(&t[1], 0, Thread2, 0);
36   tsan_pthread_setname_np(t[0], "foobar1");
37   barrier_wait(&barrier);
38   pthread_join(t[0], NULL);
39   pthread_join(t[1], NULL);
40 }
41 
42 // CHECK: WARNING: ThreadSanitizer: data race
43 // CHECK:   Thread T1 'foobar1'
44 // CHECK:   Thread T2 'foobar2'
45