1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include "test.h"
3 
4 #if defined(__linux__)
5 #define USE_PTHREAD_SETNAME_NP __GLIBC_PREREQ(2, 12)
6 #elif defined(__FreeBSD__)
7 #include <pthread_np.h>
8 #define USE_PTHREAD_SETNAME_NP 1
9 #define pthread_setname_np pthread_set_name_np
10 #else
11 #define USE_PTHREAD_SETNAME_NP 0
12 #endif
13 
14 extern "C" void AnnotateThreadName(const char *f, int l, const char *name);
15 
16 int Global;
17 
Thread1(void * x)18 void *Thread1(void *x) {
19   barrier_wait(&barrier);
20   AnnotateThreadName(__FILE__, __LINE__, "Thread1");
21   Global++;
22   return NULL;
23 }
24 
Thread2(void * x)25 void *Thread2(void *x) {
26 #if USE_PTHREAD_SETNAME_NP
27   pthread_setname_np(pthread_self(), "Thread2");
28 #else
29   AnnotateThreadName(__FILE__, __LINE__, "Thread2");
30 #endif
31   Global--;
32   barrier_wait(&barrier);
33   return NULL;
34 }
35 
main()36 int main() {
37   barrier_init(&barrier, 2);
38   pthread_t t[2];
39   pthread_create(&t[0], NULL, Thread1, NULL);
40   pthread_create(&t[1], NULL, Thread2, NULL);
41   pthread_join(t[0], NULL);
42   pthread_join(t[1], NULL);
43 }
44 
45 // CHECK: WARNING: ThreadSanitizer: data race
46 // CHECK:   Thread T1 'Thread1'
47 // CHECK:   Thread T2 'Thread2'
48