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