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
6 #import <Foundation/Foundation.h>
10 dispatch_queue_t queue;
12 dispatch_semaphore_t sem;
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);
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);
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);
33 dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t remainingData, int error) {
35 barrier_wait(&barrier);
38 dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t remainingData, int error) {
39 barrier_wait(&barrier);
42 dispatch_semaphore_signal(sem);
45 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
46 dispatch_io_close(channel, 0);
48 fprintf(stderr, "Done.\n");
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]+}})