2014-05-30 22:08:51 +08:00
|
|
|
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
|
2015-01-21 21:50:02 +08:00
|
|
|
#include "test.h"
|
2012-12-04 23:46:05 +08:00
|
|
|
|
2014-10-24 17:27:34 +08:00
|
|
|
#if defined(__linux__)
|
|
|
|
#define USE_PTHREAD_SETNAME_NP __GLIBC_PREREQ(2, 12)
|
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
#include <pthread_np.h>
|
|
|
|
#define USE_PTHREAD_SETNAME_NP 1
|
|
|
|
#define pthread_setname_np pthread_set_name_np
|
|
|
|
#else
|
|
|
|
#define USE_PTHREAD_SETNAME_NP 0
|
|
|
|
#endif
|
|
|
|
|
2012-12-04 23:46:05 +08:00
|
|
|
extern "C" void AnnotateThreadName(const char *f, int l, const char *name);
|
|
|
|
|
|
|
|
int Global;
|
|
|
|
|
|
|
|
void *Thread1(void *x) {
|
2015-01-21 21:50:02 +08:00
|
|
|
barrier_wait(&barrier);
|
2012-12-04 23:46:05 +08:00
|
|
|
AnnotateThreadName(__FILE__, __LINE__, "Thread1");
|
|
|
|
Global++;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Thread2(void *x) {
|
2014-10-24 17:27:34 +08:00
|
|
|
#if USE_PTHREAD_SETNAME_NP
|
2013-01-18 14:43:13 +08:00
|
|
|
pthread_setname_np(pthread_self(), "Thread2");
|
2013-01-21 19:20:49 +08:00
|
|
|
#else
|
|
|
|
AnnotateThreadName(__FILE__, __LINE__, "Thread2");
|
|
|
|
#endif
|
2012-12-04 23:46:05 +08:00
|
|
|
Global--;
|
2015-01-21 21:50:02 +08:00
|
|
|
barrier_wait(&barrier);
|
2012-12-04 23:46:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2015-01-21 21:50:02 +08:00
|
|
|
barrier_init(&barrier, 2);
|
2012-12-04 23:46:05 +08:00
|
|
|
pthread_t t[2];
|
|
|
|
pthread_create(&t[0], NULL, Thread1, NULL);
|
|
|
|
pthread_create(&t[1], NULL, Thread2, NULL);
|
|
|
|
pthread_join(t[0], NULL);
|
|
|
|
pthread_join(t[1], NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CHECK: WARNING: ThreadSanitizer: data race
|
2012-12-06 20:16:15 +08:00
|
|
|
// CHECK: Thread T1 'Thread1'
|
|
|
|
// CHECK: Thread T2 'Thread2'
|