Even sleep(1) lead to episodical flakes on some machines.
Use an invisible by tsan barrier to enforce required execution order instead.
This makes the tests deterministic and faster.
llvm-svn: 226659
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *a) {
__atomic_fetch_add((int*)a, 1, __ATOMIC_SEQ_CST);
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
int *a = new int(0);
pthread_t t;
pthread_create(&t, 0, Thread, a);
- sleep(1);
+ barrier_wait(&barrier);
delete a;
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
__atomic_fetch_add((int*)a, 1, __ATOMIC_SEQ_CST);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
int *a = new int(0);
pthread_t t;
pthread_create(&t, 0, Thread, a);
delete a;
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
const int kTestCount = 4;
typedef long long T;
for (int i = 0; i < kTestCount; i++) {
Test(i, &atomics[i], false);
}
- sleep(2);
+ barrier_wait(&barrier);
+ barrier_wait(&barrier);
for (int i = 0; i < kTestCount; i++) {
fprintf(stderr, "Test %d reverse\n", i);
Test(i, &atomics[kTestCount + i], false);
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
for (int i = 0; i < kTestCount; i++) {
fprintf(stderr, "Test %d\n", i);
Test(i, &atomics[i], true);
for (int i = 0; i < kTestCount; i++) {
Test(i, &atomics[kTestCount + i], true);
}
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
-#include <stdio.h>
+#include "test.h"
const int kTestCount = 4;
typedef long long T;
for (int i = 0; i < kTestCount; i++) {
Test(i, &atomics[i], false);
}
- sleep(4);
+ barrier_wait(&barrier);
+ barrier_wait(&barrier);
for (int i = 0; i < kTestCount; i++) {
fprintf(stderr, "Test %d reverse\n", i);
Test(i, &atomics[kTestCount + i], false);
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
for (int i = 0; i < kTestCount; i++) {
fprintf(stderr, "Test %d\n", i);
Test(i, &atomics[i], true);
for (int i = 0; i < kTestCount; i++) {
Test(i, &atomics[kTestCount + i], true);
}
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
__atomic_fetch_add(&Global, 1, __ATOMIC_RELAXED);
return NULL;
}
void *Thread2(void *x) {
Global++;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
int WTFGlobal;
void *Thread(void *x) {
Global = 42;
WTFGlobal = 142;
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
AnnotateBenignRaceSized(__FILE__, __LINE__,
&Global, sizeof(Global), "Race on Global");
WTFAnnotateBenignRaceSized(__FILE__, __LINE__,
"Race on WTFGlobal");
pthread_t t;
pthread_create(&t, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
Global = 43;
WTFGlobal = 143;
pthread_join(t, 0);
// RUN: %clangxx_tsan -O1 %s -fsanitize-blacklist=%t.blacklist -o %t
// RUN: %deflake %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
// CHECK: ThreadSanitizer: data race
// CHECK: Write of size 4
// CHECK: #0 Thread1{{.*}}blacklist2.cc:[[@LINE+1]]
Global--;
// CHECK: #2 Blacklisted_Thread2{{.*}}blacklist2.cc:[[@LINE+1]]
CallTouchGlobal();
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Blacklisted_Thread2, NULL);
// CHECK-NOT: WARNING
// CHECK: OK
-#include <stdio.h>
-#include <stdlib.h>
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
pthread_mutex_t m;
pthread_cond_t c;
void *thr1(void *p) {
pthread_mutex_lock(&m);
pthread_cleanup_push((void(*)(void *arg))pthread_mutex_unlock, &m);
+ barrier_wait(&barrier);
while (x == 0)
pthread_cond_wait(&c, &m);
pthread_cleanup_pop(1);
}
int main() {
+ barrier_init(&barrier, 2);
+
pthread_t th;
pthread_mutex_init(&m, 0);
pthread_cond_init(&c, 0);
pthread_create(&th, 0, thr1, 0);
+ barrier_wait(&barrier);
sleep(1); // let it block on cond var
pthread_cancel(th);
// CHECK: ThreadSanitizer: data race
// CHECK: pthread_cond_signal
-#include <stdio.h>
-#include <stdlib.h>
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
struct Ctx {
pthread_mutex_t m;
c->done = true;
pthread_mutex_unlock(&c->m);
pthread_cond_signal(&c->c);
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
Ctx *c = new Ctx();
pthread_mutex_init(&c->m, 0);
pthread_cond_init(&c->c, 0);
while (!c->done)
pthread_cond_wait(&c->c, &c->m);
pthread_mutex_unlock(&c->m);
- // w/o this sleep, it can be reported as use-after-free
- sleep(1);
+ // otherwise it can be reported as use-after-free
+ barrier_wait(&barrier);
delete c;
pthread_join(th, 0);
}
// RUN: TSAN_OPTIONS=detect_deadlocks=1 %deflake %run %t | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-RD
// RUN: %clangxx_tsan %s -o %t -DLockType=PthreadRecursiveMutex
// RUN: TSAN_OPTIONS=detect_deadlocks=1 %deflake %run %t | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-REC
-#include <pthread.h>
+#include "test.h"
#undef NDEBUG
#include <assert.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
#include <new>
#ifndef LockType
fprintf(stderr, "Starting Test5\n");
// CHECK: Starting Test5
Init(5);
- RunThreads(&LockTest::Lock_0_1, &LockTest::Lock_1_0);
+ RunThreads(&LockTest::Lock_0_1<true>, &LockTest::Lock_1_0<true>);
// CHECK: WARNING: ThreadSanitizer: lock-order-inversion
// CHECK: Cycle in lock order graph: [[M1:M[0-9]+]] ({{.*}}) => [[M2:M[0-9]+]] ({{.*}}) => [[M1]]
// CHECK: Mutex [[M2]] acquired here while holding mutex [[M1]] in thread [[T1:T[0-9]+]]
private:
void Lock2(size_t l1, size_t l2) { L(l1); L(l2); U(l2); U(l1); }
- void Lock_0_1() { Lock2(0, 1); }
- void Lock_1_0() { sleep(1); Lock2(1, 0); }
+
+ template<bool wait = false>
+ void Lock_0_1() {
+ Lock2(0, 1);
+ if (wait)
+ barrier_wait(&barrier);
+ }
+
+ template<bool wait = false>
+ void Lock_1_0() {
+ if (wait)
+ barrier_wait(&barrier);
+ Lock2(1, 0);
+ }
+
void Lock1_Loop(size_t i, size_t n_iter) {
for (size_t it = 0; it < n_iter; it++) {
// if ((it & (it - 1)) == 0) fprintf(stderr, "%zd", i);
};
int main(int argc, char **argv) {
+ barrier_init(&barrier, 2);
if (argc > 1)
test_number = atoi(argv[1]);
if (argc > 2)
// RUN: %clangxx_tsan -O1 %s -o %t -DORDER1 && %deflake %run %t | FileCheck %s
// RUN: %clangxx_tsan -O1 %s -o %t -DORDER2 && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
volatile int X;
volatile int N;
void *Thread(void *p) {
#ifdef ORDER1
- sleep(1);
+ barrier_wait(&barrier);
#endif
F();
+#ifdef ORDER2
+ barrier_wait(&barrier);
+#endif
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
N = 50000;
F = foo;
pthread_t t;
pthread_attr_setstacksize(&a, N * 256 + (1 << 20));
pthread_create(&t, &a, Thread, 0);
#ifdef ORDER2
- sleep(1);
+ barrier_wait(&barrier);
#endif
X = 43;
+#ifdef ORDER1
+ barrier_wait(&barrier);
+#endif
+
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void *Thread1(void *x) {
int f = open("/dev/random", O_RDONLY);
close(f);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
int f = open("/dev/random", O_RDONLY);
close(f);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int fds[2];
void *Thread1(void *x) {
write(fds[1], "a", 1);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
close(fds[0]);
close(fds[1]);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pipe(fds);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int fds[2];
void *Thread1(void *x) {
write(fds[1], "a", 1);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
close(fds[0]);
close(fds[1]);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pipe(fds);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int X;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
int f = open("/dev/random", O_RDONLY);
char buf;
read(f, &buf, 1);
void *Thread2(void *x) {
X = 43;
write(STDOUT_FILENO, "a", 1);
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && TSAN_OPTIONS="atexit_sleep_ms=50" %run %t 2>&1 | FileCheck %s
-#include <stdlib.h>
-#include <stdio.h>
+#include "test.h"
#include <errno.h>
-#include <pthread.h>
-#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
}
static void *watchdog(void *p) {
- sleep(100);
+ sleep(100); // is not intended to exit
fprintf(stderr, "timed out after 100 seconds\n");
exit(1);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t th1, th2;
pthread_create(&th1, 0, incrementer, 0);
pthread_create(&th2, 0, watchdog, 0);
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-DIE
// RUN: %clangxx_tsan -O1 %s -o %t && TSAN_OPTIONS="die_after_fork=0" %run %t 2>&1 | FileCheck %s -check-prefix=CHECK-NODIE
-#include <stdlib.h>
-#include <stdio.h>
+#include "test.h"
#include <errno.h>
-#include <pthread.h>
-#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
static void *sleeper(void *p) {
- sleep(10);
+ sleep(10); // not intended to exit during test
+ return 0;
+}
+
+static void *nop(void *p) {
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t th;
pthread_create(&th, 0, sleeper, 0);
switch (fork()) {
case 0: // child
{
pthread_t th2;
- pthread_create(&th2, 0, sleeper, 0);
+ pthread_create(&th2, 0, nop, 0);
exit(0);
break;
}
// RUN: %deflake %run %t | FileCheck %s --check-prefix=CHECK-NOZUPP
// RUN: TSAN_OPTIONS="suppressions='%s.supp' print_suppressions=1" %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SUPP
-#include <pthread.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
int *mem;
pthread_mutex_t mtx;
pthread_mutex_lock(&mtx);
free(mem);
pthread_mutex_unlock(&mtx);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_lock(&mtx);
mem[0] = 42;
pthread_mutex_unlock(&mtx);
}
int main() {
+ barrier_init(&barrier, 2);
mem = (int*)malloc(100);
pthread_mutex_init(&mtx, 0);
pthread_t t;
// RUN: %clangxx_tsan -O1 %s -o %T/global_race.cc.exe && %deflake %run %T/global_race.cc.exe | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
int GlobalData[10];
void *Thread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
GlobalData[2] = 42;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
// On FreeBSD, the %p conversion specifier works as 0x%x and thus does not
// match to the format used in the diagnotic message.
fprintf(stderr, "addr=0x%012lx\n", (unsigned long) GlobalData);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
GlobalData[2] = 43;
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
int x;
void *Thread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
x = 1;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
// On FreeBSD, the %p conversion specifier works as 0x%x and thus does not
// match to the format used in the diagnotic message.
fprintf(stderr, "addr2=0x%012lx\n", (unsigned long) &x);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
x = 0;
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
namespace XXX {
struct YYY {
}
void *Thread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
XXX::YYY::ZZZ[0] = 1;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
// On FreeBSD, the %p conversion specifier works as 0x%x and thus does not
// match to the format used in the diagnotic message.
fprintf(stderr, "addr3=0x%012lx\n", (unsigned long) XXX::YYY::ZZZ);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
XXX::YYY::ZZZ[0] = 0;
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clang_tsan -O1 %s -o %t && TSAN_OPTIONS="$TSAN_OPTIONS halt_on_error=1" %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int X;
void *Thread(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
X = 42;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
fprintf(stderr, "BEFORE\n");
pthread_t t;
pthread_create(&t, 0, Thread, 0);
X = 43;
+ barrier_wait(&barrier);
pthread_join(t, 0);
fprintf(stderr, "AFTER\n");
return 0;
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
extern "C" {
void AnnotateIgnoreReadsBegin(const char *f, int l);
void *Thread(void *p) {
*(int*)p = 42;
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
int *p = new int(0);
pthread_t t;
pthread_create(&t, 0, Thread, p);
- sleep(1);
+ barrier_wait(&barrier);
AnnotateIgnoreReadsBegin(__FILE__, __LINE__);
AnnotateIgnoreWritesBegin(__FILE__, __LINE__);
free(p);
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
extern "C" {
void AnnotateIgnoreReadsBegin(const char *f, int l);
void *Thread(void *a) {
int *p = 0;
while ((p = __atomic_load_n(&g, __ATOMIC_RELAXED)) == 0)
- usleep(100);
+ usleep(100); // spin-wait
*p = 42;
return 0;
}
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
Global = 42;
AnnotateIgnoreReadsEnd(__FILE__, __LINE__);
AnnotateIgnoreWritesEnd(__FILE__, __LINE__);
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
Global = 43;
pthread_join(t, 0);
printf("OK\n");
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
+#include "test.h"
#include <string.h>
-#include <unistd.h>
int x[4], z[4];
void *MemCpyThread(void *a) {
memcpy((int*)a, z, 16);
+ barrier_wait(&barrier);
return NULL;
}
void *MemSetThread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
memset((int*)a, 0, 16);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
// Race on x between memcpy and memset
pthread_create(&t[0], NULL, MemCpyThread, x);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
+#include "test.h"
#include <string.h>
-#include <unistd.h>
int y[4], z[4];
void *MemMoveThread(void *a) {
memmove((int*)a, z, 16);
+ barrier_wait(&barrier);
return NULL;
}
void *MemSetThread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
memset((int*)a, 0, 16);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
// Race on y between memmove and memset
pthread_create(&t[0], NULL, MemMoveThread, y);
-#include <pthread.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
extern "C" {
typedef unsigned long jptr; // NOLINT
#include "java.h"
void *Thread(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
__tsan_java_finalize();
*(int*)p = 42;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
pthread_t th;
pthread_create(&th, 0, Thread, (void*)jheap);
*(int*)jheap = 43;
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(jheap, kBlockSize);
fprintf(stderr, "DONE\n");
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
#include "java.h"
-#include <unistd.h>
jptr varaddr;
jptr lockaddr;
void *Thread(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
__tsan_java_mutex_lock(lockaddr);
*(int*)varaddr = 42;
__tsan_java_mutex_unlock(lockaddr);
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
__tsan_java_mutex_lock(lockaddr);
*(int*)varaddr = 43;
__tsan_java_mutex_unlock(lockaddr);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(jheap, kBlockSize);
fprintf(stderr, "DONE\n");
jptr lockaddr2;
void *Thread(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
__tsan_java_mutex_lock(lockaddr2);
*(int*)varaddr2 = 42;
__tsan_java_mutex_unlock(lockaddr2);
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
*(int*)varaddr = 43;
__tsan_java_mutex_unlock(lockaddr);
__tsan_java_move(varaddr, varaddr2, kBlockSize);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(varaddr2, kBlockSize);
printf("DONE\n");
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
#include "java.h"
-#include <unistd.h>
jptr varaddr;
jptr lockaddr;
printf("FAILED 0 rec=%d\n", rec);
exit(1);
}
- sleep(2);
+ barrier_wait(&barrier);
+ barrier_wait(&barrier);
__tsan_java_mutex_lock_rec(lockaddr, rec);
if (*(int*)varaddr != 43) {
printf("FAILED 3 var=%d\n", *(int*)varaddr);
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
lockaddr = jheap + 8;
pthread_t th;
pthread_create(&th, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
__tsan_java_mutex_lock(lockaddr);
if (*(int*)varaddr != 42) {
printf("FAILED 1 var=%d\n", *(int*)varaddr);
}
*(int*)varaddr = 43;
__tsan_java_mutex_unlock(lockaddr);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(jheap, kBlockSize);
printf("DONE\n");
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
#include "java.h"
-#include <unistd.h>
jptr varaddr;
jptr lockaddr;
exit(1);
}
*(int*)varaddr = 42;
- sleep(2);
+ barrier_wait(&barrier);
+ barrier_wait(&barrier);
__tsan_java_mutex_lock_rec(lockaddr, rec);
__tsan_java_mutex_unlock(lockaddr);
__tsan_java_mutex_unlock(lockaddr);
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
lockaddr = jheap + 8;
pthread_t th;
pthread_create(&th, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
__tsan_java_mutex_lock(lockaddr);
*(int*)varaddr = 43;
__tsan_java_mutex_unlock(lockaddr);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(jheap, kBlockSize);
printf("DONE\n");
jptr lockaddr2_new;
void *Thread(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
__tsan_java_mutex_lock(lockaddr1_new);
*(char*)varaddr1_new = 43;
__tsan_java_mutex_unlock(lockaddr1_new);
}
int main(int argc, char **argv) {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
void *jheap = malloc(kHeapSize);
jheap = (char*)jheap + 8;
__tsan_java_mutex_unlock(lockaddr2_old);
__tsan_java_move(varaddr1_old, varaddr1_new, kBlockSize);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(varaddr1_new, kBlockSize);
printf("DONE\n");
jptr varaddr2_new;
void *Thread(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
*(int*)varaddr1_new = 43;
*(int*)varaddr2_new = 43;
return 0;
}
int main(int argc, char **argv) {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
void *jheap = malloc(kHeapSize);
jheap = (char*)jheap + 8;
*(int*)varaddr2_old = 43;
__tsan_java_move(varaddr1_old, varaddr1_new, kBlockSize);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(varaddr1_new, kBlockSize);
printf("DONE\n");
jptr varaddr2;
void *Thread(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
*(int*)varaddr2 = 42;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
pthread_create(&th, 0, Thread, 0);
*(int*)varaddr = 43;
__tsan_java_move(varaddr, varaddr2, kBlockSize);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(varaddr2, kBlockSize);
fprintf(stderr, "DONE\n");
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
#include "java.h"
-#include <unistd.h>
jptr varaddr;
jptr lockaddr;
void *Thread(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
__tsan_java_mutex_read_lock(lockaddr);
*(int*)varaddr = 42;
__tsan_java_mutex_read_unlock(lockaddr);
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
__tsan_java_mutex_lock(lockaddr);
*(int*)varaddr = 43;
__tsan_java_mutex_unlock(lockaddr);
+ barrier_wait(&barrier);
pthread_join(th, 0);
__tsan_java_free(jheap, kBlockSize);
printf("DONE\n");
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
#include "java.h"
-#include <unistd.h>
jptr varaddr;
jptr lockaddr;
void *Thread(void *p) {
while (__atomic_load_n((int*)lockaddr, __ATOMIC_RELAXED) == 0)
- usleep(1000);
+ usleep(1000); // spin-wait
__tsan_java_acquire(lockaddr);
*(int*)varaddr = 42;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
int const kHeapSize = 1024 * 1024;
jptr jheap = (jptr)malloc(kHeapSize + 8) + 8;
__tsan_java_init(jheap, kHeapSize);
#ifdef BUILD_SO
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
int GLOB_SHARED = 0;
extern "C"
+void init_so() {
+ barrier_init(&barrier, 2);
+}
+
+extern "C"
void *write_from_so(void *unused) {
- if (unused)
- sleep(1);
+ if (unused == 0)
+ barrier_wait(&barrier);
GLOB_SHARED++;
+ if (unused != 0)
+ barrier_wait(&barrier);
return NULL;
}
#else // BUILD_SO
+#include "test.h"
#include <dlfcn.h>
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
-
#include <string>
int GLOB = 0;
void *write_glob(void *unused) {
- if (unused)
- sleep(1);
+ if (unused == 0)
+ barrier_wait(&barrier);
GLOB++;
+ if (unused != 0)
+ barrier_wait(&barrier);
return NULL;
}
}
int main(int argc, char *argv[]) {
+ barrier_init(&barrier, 2);
std::string path = std::string(argv[0]) + std::string("-so.so");
race_two_threads(write_glob);
// CHECK: write_glob
printf("error in dlopen(): %s\n", dlerror());
return 1;
}
+ void (*init_so)();
+ *(void **)&init_so = dlsym(lib, "init_so");
+ init_so();
void *(*write_from_so)(void *unused);
*(void **)&write_from_so = dlsym(lib, "write_from_so");
race_two_threads(write_from_so);
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
_Atomic(int*) p;
void *thr(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
int *pp = __c11_atomic_load(&p, __ATOMIC_RELAXED);
*pp = 42;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t th;
pthread_create(&th, 0, thr, p);
__c11_atomic_store(&p, new int, __ATOMIC_RELAXED);
+ barrier_wait(&barrier);
pthread_join(th, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
+#include "test.h"
#include <stdint.h>
-#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
void *Thread(void *ptr) {
*(int*)ptr = 42;
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
void *ptr = mmap(0, 128 << 10, PROT_READ|PROT_WRITE,
MAP_32BIT|MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
fprintf(stderr, "ptr=%p\n", ptr);
}
pthread_t t;
pthread_create(&t, 0, Thread, ptr);
- sleep(1);
+ barrier_wait(&barrier);
*(int*)ptr = 42;
pthread_join(t, 0);
munmap(ptr, 128 << 10);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
+#include "test.h"
#include <string.h>
-#include <unistd.h>
char *data = new char[10];
char *data1 = new char[10];
void *Thread1(void *x) {
static volatile int size = 1;
memcpy(data+5, data1, size);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
static volatile int size = 4;
- sleep(1);
+ barrier_wait(&barrier);
memcpy(data+3, data2, size);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
fprintf(stderr, "addr=%p\n", &data[5]);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
void *Thread1(void *x) {
int *p = (int*)x;
p[0] = 1;
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
char *p = (char*)x;
p[2] = 1;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
int *data = new int(42);
fprintf(stderr, "ptr1=%p\n", data);
fprintf(stderr, "ptr2=%p\n", (char*)data + 2);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
int *p = (int*)x;
p[0] = 1;
return NULL;
void *Thread2(void *x) {
char *p = (char*)x;
p[2] = 1;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
int *data = new int(42);
fprintf(stderr, "ptr1=%p\n", data);
fprintf(stderr, "ptr2=%p\n", (char*)data + 2);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
pthread_mutex_t mtx;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_lock(&mtx);
Global++;
pthread_mutex_unlock(&mtx);
void *Thread2(void *x) {
Global--;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T1
// CHECK: (mutexes: write [[M1:M[0-9]+]]):
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
pthread_mutex_t mtx;
pthread_mutex_lock(&mtx);
Global++;
pthread_mutex_unlock(&mtx);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global--;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T2:
// CHECK: Previous write of size 4 at {{.*}} by thread T1
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
pthread_mutex_t mtx1;
pthread_mutex_t mtx2;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_lock(&mtx1);
pthread_mutex_lock(&mtx2);
Global++;
void *Thread2(void *x) {
Global--;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T1
// CHECK: (mutexes: write [[M1:M[0-9]+]], write [[M2:M[0-9]+]]):
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
pthread_mutex_t mtx1;
Global++;
pthread_mutex_unlock(&mtx2);
pthread_mutex_unlock(&mtx1);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global--;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T2:
// CHECK: Previous write of size 4 at {{.*}} by thread T1
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
pthread_mutex_t mtx1;
pthread_mutex_t mtx2;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_lock(&mtx1);
Global++;
pthread_mutex_unlock(&mtx1);
pthread_mutex_lock(&mtx2);
Global--;
pthread_mutex_unlock(&mtx2);
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T1
// CHECK: (mutexes: write [[M1:M[0-9]+]]):
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
pthread_mutex_t mtx1;
pthread_rwlock_t mtx3;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_lock(&mtx1);
Global++;
pthread_mutex_unlock(&mtx1);
Global--;
pthread_spin_unlock(&mtx2);
pthread_rwlock_unlock(&mtx3);
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T1
// CHECK: (mutexes: write [[M1:M[0-9]+]]):
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
__thread int huge[1024*1024];
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global++;
return NULL;
}
pthread_mutex_unlock(mtx);
pthread_mutex_destroy(mtx);
delete mtx;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
pthread_mutex_t *mtx;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_lock(mtx);
Global++;
pthread_mutex_unlock(mtx);
void *Thread2(void *x) {
Global--;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T1
// CHECK: (mutexes: write [[M1:M[0-9]+]]):
+++ /dev/null
-#include <time.h>
-
-static void process_sleep(int sec) {
- clock_t beg = clock();
- while((clock() - beg) / CLOCKS_PER_SEC < sec)
- usleep(100);
-}
// When the data race was reported, pthread_atfork() handler used to be
// executed which caused another race report in the same thread, which resulted
// in a deadlock.
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int glob = 0;
void *worker(void *unused) {
- sleep(1);
+ barrier_wait(&barrier);
glob++;
return NULL;
}
}
int main() {
+ barrier_init(&barrier, 2);
pthread_atfork(atfork, NULL, NULL);
pthread_t t;
pthread_create(&t, NULL, worker, NULL);
glob++;
+ barrier_wait(&barrier);
pthread_join(t, NULL);
// CHECK: ThreadSanitizer: data race
// CHECK-NOT: ATFORK
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
pthread_barrier_t B;
int Global;
void *Thread1(void *x) {
pthread_barrier_init(&B, 0, 2);
+ barrier_wait(&barrier);
pthread_barrier_wait(&B);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_barrier_wait(&B);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, NULL, Thread1, NULL);
Thread2(0);
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
pthread_mutex_t Mtx;
int Global;
pthread_mutex_lock(&Mtx);
Global = 42;
pthread_mutex_unlock(&Mtx);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_lock(&Mtx);
Global = 43;
pthread_mutex_unlock(&Mtx);
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK-NEXT: Atomic read of size 1 at {{.*}} by thread T2:
// CHECK-NEXT: #0 pthread_mutex_lock
-// CHECK-NEXT: #1 Thread2{{.*}} {{.*}}race_on_mutex.c:20{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #1 Thread2{{.*}} {{.*}}race_on_mutex.c:18{{(:3)?}} ({{.*}})
// CHECK: Previous write of size 1 at {{.*}} by thread T1:
// CHECK-NEXT: #0 pthread_mutex_init {{.*}} ({{.*}})
-// CHECK-NEXT: #1 Thread1{{.*}} {{.*}}race_on_mutex.c:11{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #1 Thread1{{.*}} {{.*}}race_on_mutex.c:8{{(:3)?}} ({{.*}})
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *x) {
pthread_mutex_lock((pthread_mutex_t*)x);
pthread_mutex_unlock((pthread_mutex_t*)x);
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_mutex_t Mtx;
pthread_mutex_init(&Mtx, 0);
pthread_t t;
pthread_create(&t, 0, Thread, &Mtx);
- sleep(1);
+ barrier_wait(&barrier);
pthread_mutex_destroy(&Mtx);
pthread_join(t, 0);
return 0;
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
char s[] = "abracadabra";
void *Thread0(void *p) {
puts(s);
+ barrier_wait(&barrier);
return 0;
}
void *Thread1(void *p) {
+ barrier_wait(&barrier);
s[3] = 'z';
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t th[2];
pthread_create(&th[0], 0, Thread0, 0);
pthread_create(&th[1], 0, Thread1, 0);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
-#include <unistd.h>
#include <errno.h>
int fd;
char buf;
-void *Thread(void *x) {
- sleep(1);
+void *Thread1(void *x) {
+ barrier_wait(&barrier);
read(fd, &buf, 1);
return NULL;
}
+void *Thread2(void *x) {
+ read(fd, &buf, 1);
+ barrier_wait(&barrier);
+ return NULL;
+}
+
int main() {
+ barrier_init(&barrier, 2);
fd = open("/dev/random", O_RDONLY);
if (fd < 0) {
fprintf(stderr, "failed to open /dev/random (%d)\n", errno);
return 1;
}
pthread_t t[2];
- pthread_create(&t[0], NULL, Thread, NULL);
- pthread_create(&t[1], NULL, Thread, NULL);
+ pthread_create(&t[0], NULL, Thread1, NULL);
+ pthread_create(&t[1], NULL, Thread2, NULL);
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
close(fd);
// RUN: %clangxx_tsan -O1 %s -o %t && %run %t | FileCheck %s
// Regtest for https://code.google.com/p/thread-sanitizer/issues/detail?id=40
// This is a correct program and tsan should not report a race.
-#include <pthread.h>
-#include <unistd.h>
-#include <stdio.h>
+#include "test.h"
+
int g;
__attribute__((noinline))
int foo(int cond) {
return g;
return 0;
}
+
void *Thread1(void *p) {
+ barrier_wait(&barrier);
long res = foo((long)p);
- sleep(1);
return (void*) res;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread1, 0);
g = 1;
+ barrier_wait(&barrier);
pthread_join(t, 0);
printf("PASS\n");
+ // CHECK-NOT: ThreadSanitizer: data race
// CHECK: PASS
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
void *Thread1(void *x) {
buf = 1;
- sleep(1);
+ barrier_wait(&barrier);
return NULL;
}
}
int main() {
+ barrier_init(&barrier, 2);
fd = open("/dev/null", O_WRONLY);
if (fd < 0) return 1;
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
- sleep(1);
+ barrier_wait(&barrier);
pthread_create(&t[1], NULL, Thread2, NULL);
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
+#include "test.h"
// Ensure that we can restore a stack of a finished thread.
void *Thread1(void *x) {
foobar(&g_data);
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
+ sleep(1); // let the thread finish and exit
g_data = 43;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
volatile int x;
__atomic_store_n(&x, 1, __ATOMIC_RELEASE);
foo();
data[0]++;
+ if (a != 0)
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
for (int i = 0; i < 50; i++) {
pthread_t t;
pthread_create(&t, 0, Thread, 0);
pthread_join(t, 0);
}
pthread_t t;
- pthread_create(&t, 0, Thread, 0);
- sleep(5);
+ pthread_create(&t, 0, Thread, (void*)1);
+ barrier_wait(&barrier);
for (int i = 0; i < kSize; i++)
data[i]++;
pthread_join(t, 0);
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "test.h"
#include <signal.h>
#include <sys/types.h>
-#include <unistd.h>
#include <errno.h>
pthread_t mainth;
}
static void* sendsignal(void *p) {
- sleep(1);
+ barrier_wait(&barrier);
pthread_kill(mainth, SIGPROF);
return 0;
}
static __attribute__((noinline)) void loop() {
+ barrier_wait(&barrier);
while (done == 0) {
volatile char *p = (char*)malloc(1);
p[0] = 0;
}
int main() {
+ barrier_init(&barrier, 2);
mainth = pthread_self();
struct sigaction act = {};
act.sa_sigaction = &MyHandler;
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <stdio.h>
-#include <stdlib.h>
+#include "test.h"
#include <signal.h>
#include <sys/types.h>
-#include <unistd.h>
static void handler(int, siginfo_t*, void*) {
// CHECK: WARNING: ThreadSanitizer: signal-unsafe call inside of a signal
act.sa_sigaction = &handler;
sigaction(SIGPROF, &act, 0);
kill(getpid(), SIGPROF);
- sleep(1);
+ sleep(1); // let the signal handler run
return 0;
}
// Test case for recursive signal handlers, adopted from:
// https://code.google.com/p/thread-sanitizer/issues/detail?id=71
-#include <pthread.h>
+#include "test.h"
#include <semaphore.h>
#include <signal.h>
-#include <unistd.h>
#include <errno.h>
-#include <stdlib.h>
-#include <stdio.h>
-
-#include "process_sleep.h"
static const int kSigSuspend = SIGUSR1;
static const int kSigRestart = SIGUSR2;
-static sigset_t g_suspend_handler_mask;
static sem_t g_thread_suspend_ack_sem;
// Mono walks thread stacks to detect unreferenced objects.
// If last object reference is kept in register the object will be collected
// This is why threads can't be suspended with something like pthread_suspend
-};
+}
static void fail(const char *what) {
fprintf(stderr, "FAILED: %s (errno=%d)\n", what, errno);
static void SuspendHandler(int sig) {
int old_errno = errno;
SaveRegistersInStack();
+
+ // Enable kSigRestart handling, tsan disables signals around signal handlers.
+ sigset_t sigset;
+ sigemptyset(&sigset);
+ pthread_sigmask(SIG_SETMASK, &sigset, 0);
+
// Acknowledge that thread is saved and suspended
if (sem_post(&g_thread_suspend_ack_sem) != 0)
fail("sem_post failed");
- do {
- g_busy_thread_received_restart = false;
- if (sigsuspend(&g_suspend_handler_mask) != -1 || errno != EINTR)
- fail("sigsuspend failed");
- } while (!g_busy_thread_received_restart);
+ // Wait for wakeup signal.
+ while (!g_busy_thread_received_restart)
+ usleep(100); // wait for kSigRestart signal
// Acknowledge that thread restarted
if (sem_post(&g_thread_suspend_ack_sem) != 0)
static void CollectGarbage(pthread_t thread) {
StopWorld(thread);
// Walk stacks
- process_sleep(1);
StartWorld(thread);
}
static void Init() {
- if (sigfillset(&g_suspend_handler_mask) != 0)
- fail("sigfillset failed");
- if (sigdelset(&g_suspend_handler_mask, kSigRestart) != 0)
- fail("sigdelset failed");
if (sem_init(&g_thread_suspend_ack_sem, 0, 0) != 0)
fail("sem_init failed");
struct sigaction act = {};
act.sa_flags = SA_RESTART;
- sigfillset(&act.sa_mask);
act.sa_handler = &SuspendHandler;
if (sigaction(kSigSuspend, &act, NULL) != 0)
fail("sigaction failed");
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "test.h"
#include <signal.h>
#include <sys/types.h>
#include <sys/time.h>
-#include <unistd.h>
#include <errno.h>
volatile int X;
static void* thr(void *p) {
for (int i = 0; i != 1000; i++)
- usleep(1000);
+ usleep(1000); // process signals
return 0;
}
act.sa_sigaction = &handler;
sigaction(SIGPROF, &act, 0);
kill(getpid(), SIGPROF);
- sleep(1);
+ sleep(1); // let the signal handler run, can't use barrier in sig handler
fprintf(stderr, "DONE\n");
return 0;
}
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global = 42;
return NULL;
}
void *Thread2(void *x) {
Global = 43;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global++;
return NULL;
}
void *Thread2(void *x) {
Global--;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
}
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
bar1();
return NULL;
}
void *Thread2(void *x) {
bar2();
+ barrier_wait(&barrier);
return NULL;
}
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
StartThread(&t[0], Thread1);
StartThread(&t[1], Thread2);
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK-NEXT: Write of size 4 at {{.*}} by thread T1:
-// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack.c:9{{(:3)?}} ({{.*}})
-// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack.c:14{{(:3)?}} ({{.*}})
-// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack.c:28{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack.c:7{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack.c:12{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack.c:26{{(:3)?}} ({{.*}})
// CHECK: Previous read of size 4 at {{.*}} by thread T2:
-// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack.c:18{{(:20)?}} ({{.*}})
-// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack.c:23{{(:3)?}} ({{.*}})
-// CHECK-NEXT: #2 Thread2{{.*}} {{.*}}simple_stack.c:33{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack.c:16{{(:20)?}} ({{.*}})
+// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack.c:21{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #2 Thread2{{.*}} {{.*}}simple_stack.c:31{{(:3)?}} ({{.*}})
// CHECK: Thread T1 (tid={{.*}}, running) created by main thread at:
// CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}})
-// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})
// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:43{{(:3)?}} ({{.*}})
// CHECK: Thread T2 ({{.*}}) created by main thread at:
// CHECK-NEXT: #0 pthread_create {{.*}} ({{.*}})
-// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:38{{(:3)?}} ({{.*}})
+// CHECK-NEXT: #1 StartThread{{.*}} {{.*}}simple_stack.c:37{{(:3)?}} ({{.*}})
// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack.c:44{{(:3)?}} ({{.*}})
// RUN: %clangxx_tsan -O1 %s -o %T/simple_stack2.cc.exe && %deflake %run %T/simple_stack2.cc.exe | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
}
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
bar1();
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, NULL, Thread1, NULL);
bar2();
+ barrier_wait(&barrier);
pthread_join(t, NULL);
}
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK-NEXT: Write of size 4 at {{.*}} by thread T1:
-// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack2.cc:9{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
-// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack2.cc:16{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
-// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack2.cc:34{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
+// CHECK-NEXT: #0 foo1{{.*}} {{.*}}simple_stack2.cc:7{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
+// CHECK-NEXT: #1 bar1{{.*}} {{.*}}simple_stack2.cc:14{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
+// CHECK-NEXT: #2 Thread1{{.*}} {{.*}}simple_stack2.cc:32{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
// CHECK: Previous read of size 4 at {{.*}} by main thread:
-// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack2.cc:20{{(:22)?}} (simple_stack2.cc.exe+{{.*}})
-// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack2.cc:29{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
-// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack2.cc:41{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
+// CHECK-NEXT: #0 foo2{{.*}} {{.*}}simple_stack2.cc:18{{(:22)?}} (simple_stack2.cc.exe+{{.*}})
+// CHECK-NEXT: #1 bar2{{.*}} {{.*}}simple_stack2.cc:27{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
+// CHECK-NEXT: #2 main{{.*}} {{.*}}simple_stack2.cc:40{{(:3)?}} (simple_stack2.cc.exe+{{.*}})
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
int X = 0;
void MySleep() {
- sleep(1);
+ sleep(1); // the sleep that must appear in the report
}
void *Thread(void *p) {
+ barrier_wait(&barrier);
MySleep(); // Assume the main thread has done the write.
X = 42;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
X = 43;
+ barrier_wait(&barrier);
pthread_join(t, 0);
return 0;
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
int X = 0;
void *Thread(void *p) {
X = 42;
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
- sleep(1);
+ sleep(1); // must not appear in the report
pthread_create(&t, 0, Thread, 0);
+ barrier_wait(&barrier);
X = 43;
pthread_join(t, 0);
return 0;
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
*(int*)a = 43;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
int Var = 42;
pthread_t t;
pthread_create(&t, 0, Thread, &Var);
Var = 43;
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
void *Thread2(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
*(int*)a = 43;
return 0;
}
pthread_t t;
pthread_create(&t, 0, Thread2, &Var);
Var = 42;
+ barrier_wait(&barrier);
pthread_join(t, 0);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
pthread_join(t, 0);
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
+#include "test.h"
// Test case https://code.google.com/p/thread-sanitizer/issues/detail?id=87
// Tsan sees false HB edge on address pointed to by syncp variable.
void *Thread(void *x) {
while (__atomic_load_n(&syncp, __ATOMIC_ACQUIRE) == 0)
- usleep(1000);
+ usleep(1000); // spin wait
global = 42;
__atomic_store_n(syncp, 1, __ATOMIC_RELEASE);
__atomic_store_n(&syncp, 0, __ATOMIC_RELAXED);
__atomic_store_n(&s, 0, __ATOMIC_RELAXED);
__atomic_store_n(&syncp, &s, __ATOMIC_RELEASE);
while (__atomic_load_n(&syncp, __ATOMIC_RELAXED) != 0)
- usleep(1000);
+ usleep(1000); // spin wait
}
void __attribute__((noinline)) barfoo() {
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
volatile int X;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
X = 42;
X = 66;
X = 78;
X = 11;
X = 99;
X = 73;
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread1, 0);
Thread2(0);
// RUN: %clang_tsan -O1 %s -o %t && TSAN_OPTIONS="$TSAN_OPTIONS suppressions='%s.supp'" %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global = 42;
return NULL;
}
void *Thread2(void *x) {
Global = 43;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clang_tsan -O1 %s -o %t && TSAN_OPTIONS="$TSAN_OPTIONS suppressions='%s.supp'" %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
int Global;
void *Thread1(void *x) {
Global = 42;
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global = 43;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
--- /dev/null
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <dlfcn.h>
+#include <stddef.h>
+
+// TSan-invisible barrier.
+// Tests use it to establish necessary execution order in a way that does not
+// interfere with tsan (does not establish synchronization between threads).
+__typeof(pthread_barrier_wait) *barrier_wait;
+
+void barrier_init(pthread_barrier_t *barrier, unsigned count) {
+ if (barrier_wait == 0) {
+ void *h = dlopen("libpthread.so.0", RTLD_LAZY);
+ if (h == 0) {
+ fprintf(stderr, "failed to dlopen libpthread.so.0, exiting\n");
+ exit(1);
+ }
+ barrier_wait = (__typeof(barrier_wait))dlsym(h, "pthread_barrier_wait");
+ if (barrier_wait == 0) {
+ fprintf(stderr, "failed to resolve pthread_barrier_wait, exiting\n");
+ exit(1);
+ }
+ }
+ pthread_barrier_init(barrier, 0, count);
+}
+
+// Default instance of the barrier, but a test can declare more manually.
+pthread_barrier_t barrier;
+
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *x) {
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
pthread_detach(t);
printf("PASS\n");
return 0;
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *x) {
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
- sleep(1);
+ barrier_wait(&barrier);
+ sleep(1); // wait for the thread to finish and exit
return 0;
}
// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
-#include <stdio.h>
+#include "test.h"
void *Thread(void *x) {
- sleep(10);
+ sleep(100); // leave the thread "running"
return 0;
}
int main() {
pthread_t t;
pthread_create(&t, 0, Thread, 0);
- printf("OK\n");
+ printf("DONE\n");
return 0;
}
+// CHECK: DONE
// CHECK-NOT: WARNING: ThreadSanitizer: thread leak
+
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *x) {
+ barrier_wait(&barrier);
return 0;
}
int main() {
volatile int N = 5; // prevent loop unrolling
+ barrier_init(&barrier, N + 1);
for (int i = 0; i < N; i++) {
pthread_t t;
pthread_create(&t, 0, Thread, 0);
}
- sleep(1);
+ barrier_wait(&barrier);
+ sleep(1); // wait for the threads to finish and exit
return 0;
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
#if defined(__linux__)
#define USE_PTHREAD_SETNAME_NP __GLIBC_PREREQ(2, 12)
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
AnnotateThreadName(__FILE__, __LINE__, "Thread1");
Global++;
return NULL;
AnnotateThreadName(__FILE__, __LINE__, "Thread2");
#endif
Global--;
+ barrier_wait(&barrier);
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
#if defined(__FreeBSD__)
#include <pthread_np.h>
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global++;
return 0;
}
void *Thread2(void *x) {
pthread_setname_np(pthread_self(), "foobar2");
Global--;
+ barrier_wait(&barrier);
return 0;
}
int main() {
+ barrier_init(&barrier, 3);
pthread_t t[2];
pthread_create(&t[0], 0, Thread1, 0);
pthread_create(&t[1], 0, Thread2, 0);
pthread_setname_np(t[0], "foobar1");
+ barrier_wait(&barrier);
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
}
// RUN: %clang_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
int Global;
void *Thread1(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
Global = 42;
return x;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread1, 0);
Global = 43;
+ barrier_wait(&barrier);
pthread_join(t, 0);
return Global;
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
void *Thread(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
*(int*)a = 43;
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
static __thread int Var = 42;
pthread_t t;
pthread_create(&t, 0, Thread, &Var);
Var = 43;
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stddef.h>
-#include <unistd.h>
+#include "test.h"
void *Thread2(void *a) {
- sleep(1);
+ barrier_wait(&barrier);
*(int*)a = 43;
return 0;
}
pthread_t t;
pthread_create(&t, 0, Thread2, &Var);
Var = 42;
+ barrier_wait(&barrier);
pthread_join(t, 0);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t;
pthread_create(&t, 0, Thread, 0);
pthread_join(t, 0);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "test.h"
#include <stdint.h>
-#include <unistd.h>
#define NOINLINE __attribute__((noinline))
void *Thread(void *p) {
(void)p;
- sleep(1);
+ barrier_wait(&barrier);
Test(false);
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t th;
pthread_create(&th, 0, Thread, 0);
Test(true);
+ barrier_wait(&barrier);
pthread_join(th, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
+#include "test.h"
#include <semaphore.h>
-#include <stdio.h>
-#include <unistd.h>
struct A {
A() {
void *Thread1(void *x) {
obj->F();
obj->Done();
+ barrier_wait(&barrier);
return NULL;
}
void *Thread2(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
delete obj;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
+#include "test.h"
#include <semaphore.h>
-#include <stdio.h>
-#include <unistd.h>
struct A {
A() {
static A *obj = new B;
void *Thread1(void *x) {
- sleep(1);
obj->F();
+ barrier_wait(&barrier);
obj->Done();
return NULL;
}
void *Thread2(void *x) {
+ barrier_wait(&barrier);
delete obj;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
+#include "test.h"
#include <semaphore.h>
-#include <stdio.h>
-#include <unistd.h>
struct A {
A() {
static void (A::*fn)() = &A::F;
void *Thread1(void *x) {
- sleep(1);
(obj->*fn)();
+ barrier_wait(&barrier);
obj->Done();
return NULL;
}
void *Thread2(void *x) {
+ barrier_wait(&barrier);
delete obj;
return NULL;
}
int main() {
+ barrier_init(&barrier, 2);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <stdio.h>
-#include <unistd.h>
+#include "test.h"
struct A {
virtual void F() {
};
void *Thread(void *x) {
- sleep(1);
+ barrier_wait(&barrier);
((A*)x)->F();
return 0;
}
int main() {
+ barrier_init(&barrier, 2);
A *obj = new B;
pthread_t t;
pthread_create(&t, 0, Thread, obj);
delete obj;
+ barrier_wait(&barrier);
pthread_join(t, 0);
}
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
-#include <pthread.h>
-#include <unistd.h>
+#include "test.h"
pthread_rwlock_t rwlock;
int GLOB;
void *Thread1(void *p) {
(void)p;
pthread_rwlock_rdlock(&rwlock);
+ barrier_wait(&barrier);
// Write under reader lock.
- sleep(1);
GLOB++;
pthread_rwlock_unlock(&rwlock);
return 0;
}
int main(int argc, char *argv[]) {
+ barrier_init(&barrier, 2);
pthread_rwlock_init(&rwlock, NULL);
pthread_rwlock_rdlock(&rwlock);
pthread_t t;
volatile int x = GLOB;
(void)x;
pthread_rwlock_unlock(&rwlock);
+ barrier_wait(&barrier);
pthread_join(t, 0);
pthread_rwlock_destroy(&rwlock);
return 0;
// CHECK: WARNING: ThreadSanitizer: data race
// CHECK: Write of size 4 at {{.*}} by thread T1{{.*}}:
-// CHECK: #0 Thread1(void*) {{.*}}write_in_reader_lock.cc:13
+// CHECK: #0 Thread1(void*) {{.*}}write_in_reader_lock.cc:12
// CHECK: Previous read of size 4 at {{.*}} by main thread{{.*}}:
// CHECK: #0 main {{.*}}write_in_reader_lock.cc:23