180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <pthread.h>
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include <unistd.h>
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruint Global;
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid *Thread1(void *x) {
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  sleep(1);
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  __atomic_fetch_add(&Global, 1, __ATOMIC_RELAXED);
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return NULL;
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid *Thread2(void *x) {
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  Global++;
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  return NULL;
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruint main() {
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  pthread_t t[2];
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  pthread_create(&t[0], NULL, Thread1, NULL);
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  pthread_create(&t[1], NULL, Thread2, NULL);
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  pthread_join(t[0], NULL);
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru  pthread_join(t[1], NULL);
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// CHECK: WARNING: ThreadSanitizer: data race
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// CHECK:   Atomic write of size 4
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// CHECK:     #0 __tsan_atomic32_fetch_add
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru// CHECK:     #1 Thread1
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru