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 %run %t 2>&1 | FileCheck %s
      3 
      4 #import <Foundation/Foundation.h>
      5 
      6 long my_global = 0;
      7 
      8 int main(int argc, const char *argv[]) {
      9   fprintf(stderr, "Hello world.\n");
     10 
     11   dispatch_queue_t queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL);
     12   dispatch_semaphore_t sem = dispatch_semaphore_create(0);
     13 
     14   NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp-gcd-io.%d", getpid()]];
     15 
     16   dispatch_io_t channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path.fileSystemRepresentation, O_CREAT | O_WRONLY,
     17       0666, queue, ^(int error) { });
     18   dispatch_io_set_high_water(channel, 1);
     19 
     20   NSData *ns_data = [NSMutableData dataWithLength:1000];
     21   dispatch_data_t data = dispatch_data_create(ns_data.bytes, ns_data.length, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
     22 
     23   my_global++;
     24   dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t remainingData, int error) {
     25     my_global++;
     26     dispatch_async(queue, ^{
     27       my_global++;
     28       if (done) {
     29         dispatch_semaphore_signal(sem);
     30       }
     31     });
     32   });
     33 
     34   dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
     35   my_global++;
     36   dispatch_io_close(channel, 0);
     37   channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path.fileSystemRepresentation, O_RDONLY,
     38       0, queue, ^(int error) { });
     39   dispatch_io_set_high_water(channel, 1);
     40 
     41   my_global++;
     42   dispatch_io_read(channel, 0, SIZE_MAX, queue, ^(bool done, dispatch_data_t remainingData, int error) {
     43     my_global++;
     44     dispatch_async(queue, ^{
     45       my_global++;
     46       if (done) {
     47         dispatch_semaphore_signal(sem);
     48       }
     49     });
     50   });
     51 
     52   dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
     53   my_global++;
     54   fprintf(stderr, "Done.\n");
     55   return 0;
     56 }
     57 
     58 // CHECK: Hello world.
     59 // CHECK-NOT: WARNING: ThreadSanitizer
     60 // CHECK: Done.
     61