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