1// RUN: %clangxx_tsan %s -o %t -framework Foundation -std=c++11
2// RUN: %run %t 2>&1 | FileCheck %s
3
4#import <Foundation/Foundation.h>
5#import <libkern/OSAtomic.h>
6
7#include <thread>
8
9volatile int64_t retainCount = 0;
10
11long g = 0;
12
13void dealloc() {
14  g = 42;
15}
16
17void release() {
18  if (OSAtomicAdd64Barrier(-1, &retainCount) == 0) {
19    dealloc();
20  }
21}
22
23void retain() {
24  OSAtomicAdd64Barrier(1, &retainCount);
25}
26
27int main(int argc, const char * argv[]) {
28  fprintf(stderr, "start\n");
29  retain();
30  retain();
31
32  std::thread t([]{
33    release();
34  });
35
36  g = 47;
37
38  release();
39  t.join();
40
41  fprintf(stderr, "end, g = %ld\n", g);
42
43  return 0;
44}
45
46// CHECK: start
47// CHECK: end, g = 42
48// CHECK-NOT: WARNING: ThreadSanitizer
49