1 // RUN: %clang_tsan %s -o %t -framework Foundation
2 // RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %run %t 2>&1 | FileCheck %s
4 #import <Foundation/Foundation.h>
8 int main(int argc, const char *argv[]) {
9 fprintf(stderr, "Hello world.\n");
11 dispatch_queue_t queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_SERIAL);
12 dispatch_semaphore_t sem = dispatch_semaphore_create(0);
14 NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp-gcd-io.%d", getpid()]];
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);
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);
24 dispatch_io_write(channel, 0, data, queue, ^(bool done, dispatch_data_t remainingData, int error) {
26 dispatch_async(queue, ^{
29 dispatch_semaphore_signal(sem);
34 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
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);
42 dispatch_io_read(channel, 0, SIZE_MAX, queue, ^(bool done, dispatch_data_t remainingData, int error) {
44 dispatch_async(queue, ^{
47 dispatch_semaphore_signal(sem);
52 dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
54 fprintf(stderr, "Done.\n");
58 // CHECK: Hello world.
59 // CHECK-NOT: WARNING: ThreadSanitizer