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