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