• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
6dispatch_queue_t queue;
7dispatch_data_t data;
8dispatch_semaphore_t sem;
9const char *path;
10
11long my_global = 0;
12
13int main(int argc, const char *argv[]) {
14  fprintf(stderr, "Hello world.\n");
15
16  queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
17  sem = dispatch_semaphore_create(0);
18  NSString *ns_path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp-gcd-io.%d", getpid()]];
19  path = ns_path.fileSystemRepresentation;
20  NSData *ns_data = [NSMutableData dataWithLength:1000];
21  data = dispatch_data_create(ns_data.bytes, ns_data.length, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
22
23  dispatch_io_t channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) { });
24  if (! channel) abort();
25  dispatch_io_set_high_water(channel, 1);
26
27  for (int i = 0; i < 1000; i++) {
28    dispatch_io_barrier(channel, ^{
29      my_global = 42;
30    });
31  }
32
33  dispatch_io_barrier(channel, ^{
34    my_global = 43;
35
36    dispatch_semaphore_signal(sem);
37  });
38
39  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
40  dispatch_io_close(channel, 0);
41
42  fprintf(stderr, "Done.\n");
43  return 0;
44}
45
46// CHECK: Hello world.
47// CHECK-NOT: WARNING: ThreadSanitizer
48// CHECK: Done.
49