Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / tsan / thread_name2.cpp
blob9ebac29ddb5ede9057b0141fb461d39f41154efc
1 // RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2 #include "test.h"
4 // OS X doesn't have pthread_setname_np(tid, name).
5 // UNSUPPORTED: darwin
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
16 long long Global;
18 void *Thread1(void *x) {
19 barrier_wait(&barrier);
20 Global++;
21 return 0;
24 void *Thread2(void *x) {
25 tsan_pthread_setname_np(pthread_self(), "foobar2");
26 Global--;
27 barrier_wait(&barrier);
28 return 0;
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);
42 // CHECK: WARNING: ThreadSanitizer: data race
43 // CHECK: Thread T1 'foobar1'
44 // CHECK: Thread T2 'foobar2'