blob: b7f3266dc09bcf88175d92fdab68acc06d65f2da [file] [log] [blame]
Kuba Brecka440d0862015-11-24 13:36:06 +00001// RUN: %clang_tsan %s -o %t -framework Foundation
Kuba Mraceke4c1dd22017-01-24 21:37:50 +00002// RUN: %deflake %run %t 2>&1 | FileCheck %s
Kuba Brecka440d0862015-11-24 13:36:06 +00003
4#import <Foundation/Foundation.h>
5
6#import "../test.h"
7
8long global;
9
10int main() {
11 NSLog(@"Hello world.");
Kuba Breckaaafb41a2016-04-07 11:31:02 +000012 print_address("addr=", 1, &global);
Kuba Brecka440d0862015-11-24 13:36:06 +000013 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
Kuba Breckaaafb41a2016-04-07 11:31:02 +000043// CHECK: Location is global 'global' {{(of size 8 )?}}at [[ADDR]] (gcd-sync-race.mm.tmp+0x{{[0-9,a-f]+}})
Kuba Brecka440d0862015-11-24 13:36:06 +000044// CHECK: Done.