1*7c3d14c8STreehugger Robot// RUN: %clang_tsan %s -o %t -framework Foundation 2*7c3d14c8STreehugger Robot// RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %deflake %run %t 2>&1 | FileCheck %s 3*7c3d14c8STreehugger Robot 4*7c3d14c8STreehugger Robot#import <Foundation/Foundation.h> 5*7c3d14c8STreehugger Robot 6*7c3d14c8STreehugger Robot#import "../test.h" 7*7c3d14c8STreehugger Robot 8*7c3d14c8STreehugger Robotdispatch_queue_t queue; 9*7c3d14c8STreehugger Robotdispatch_data_t data; 10*7c3d14c8STreehugger Robotdispatch_semaphore_t sem; 11*7c3d14c8STreehugger Robotconst char *path; 12*7c3d14c8STreehugger Robot 13*7c3d14c8STreehugger Robotlong my_global = 0; 14*7c3d14c8STreehugger Robot 15*7c3d14c8STreehugger Robotint main(int argc, const char *argv[]) { 16*7c3d14c8STreehugger Robot fprintf(stderr, "Hello world.\n"); 17*7c3d14c8STreehugger Robot print_address("addr=", 1, &my_global); 18*7c3d14c8STreehugger Robot barrier_init(&barrier, 2); 19*7c3d14c8STreehugger Robot 20*7c3d14c8STreehugger Robot queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT); 21*7c3d14c8STreehugger Robot sem = dispatch_semaphore_create(0); 22*7c3d14c8STreehugger Robot NSString *ns_path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp-gcd-io.%d", getpid()]]; 23*7c3d14c8STreehugger Robot path = ns_path.fileSystemRepresentation; 24*7c3d14c8STreehugger Robot NSData *ns_data = [NSMutableData dataWithLength:1000]; 25*7c3d14c8STreehugger Robot data = dispatch_data_create(ns_data.bytes, ns_data.length, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT); 26*7c3d14c8STreehugger Robot 27*7c3d14c8STreehugger Robot dispatch_io_t channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) { }); 28*7c3d14c8STreehugger Robot if (! channel) abort(); 29*7c3d14c8STreehugger Robot dispatch_io_set_high_water(channel, 1); 30*7c3d14c8STreehugger Robot 31*7c3d14c8STreehugger Robot dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t remainingData, int error) { 32*7c3d14c8STreehugger Robot if (error) abort(); 33*7c3d14c8STreehugger Robot my_global = 42; 34*7c3d14c8STreehugger Robot barrier_wait(&barrier); 35*7c3d14c8STreehugger Robot }); 36*7c3d14c8STreehugger Robot 37*7c3d14c8STreehugger Robot dispatch_io_barrier(channel, ^{ 38*7c3d14c8STreehugger Robot barrier_wait(&barrier); 39*7c3d14c8STreehugger Robot my_global = 43; 40*7c3d14c8STreehugger Robot 41*7c3d14c8STreehugger Robot dispatch_semaphore_signal(sem); 42*7c3d14c8STreehugger Robot }); 43*7c3d14c8STreehugger Robot 44*7c3d14c8STreehugger Robot dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER); 45*7c3d14c8STreehugger Robot dispatch_io_close(channel, 0); 46*7c3d14c8STreehugger Robot 47*7c3d14c8STreehugger Robot fprintf(stderr, "Done.\n"); 48*7c3d14c8STreehugger Robot return 0; 49*7c3d14c8STreehugger Robot} 50*7c3d14c8STreehugger Robot 51*7c3d14c8STreehugger Robot// CHECK: Hello world. 52*7c3d14c8STreehugger Robot// CHECK: addr=[[ADDR:0x[0-9,a-f]+]] 53*7c3d14c8STreehugger Robot// CHECK: WARNING: ThreadSanitizer: data race 54*7c3d14c8STreehugger Robot// CHECK: Location is global 'my_global' {{(of size 8 )?}}at [[ADDR]] (gcd-io-barrier-race.mm.tmp+0x{{[0-9,a-f]+}}) 55*7c3d14c8STreehugger Robot// CHECK: Done. 56