1// RUN: %clang_tsan %s -o %t -framework Foundation
2// RUN: %deflake %run %t 2>&1
3
4#import <Foundation/Foundation.h>
5
6#import "../test.h"
7
8long global;
9
10int main() {
11  NSLog(@"Hello world.");
12  NSLog(@"addr=%p\n", &global);
13  barrier_init(&barrier, 2);
14
15  dispatch_queue_t q1 = dispatch_queue_create("my.queue1", DISPATCH_QUEUE_CONCURRENT);
16  dispatch_queue_t q2 = dispatch_queue_create("my.queue2", DISPATCH_QUEUE_CONCURRENT);
17
18  global = 42;
19  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
20    dispatch_sync(q1, ^{
21      global = 43;
22      barrier_wait(&barrier);
23    });
24  });
25  dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
26    dispatch_sync(q2, ^{
27      barrier_wait(&barrier);
28      global = 44;
29
30      dispatch_sync(dispatch_get_main_queue(), ^{
31        CFRunLoopStop(CFRunLoopGetCurrent());
32      });
33    });
34  });
35
36  CFRunLoopRun();
37  NSLog(@"Done.");
38}
39
40// CHECK: Hello world.
41// CHECK: addr=[[ADDR:0x[0-9,a-f]+]]
42// CHECK: WARNING: ThreadSanitizer: data race
43// CHECK: Location is global 'global' at [[ADDR]] (global_race.cc.exe+0x{{[0-9,a-f]+}})
44// CHECK: Done.
45