xref: /aosp_15_r20/external/compiler-rt/test/tsan/Darwin/gcd-io-cleanup.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_CONCURRENT);
12*7c3d14c8STreehugger Robot  dispatch_semaphore_t sem = dispatch_semaphore_create(0);
13*7c3d14c8STreehugger Robot  NSString *ns_path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp-gcd-io.%d", getpid()]];
14*7c3d14c8STreehugger Robot  const char *path = ns_path.fileSystemRepresentation;
15*7c3d14c8STreehugger Robot  dispatch_io_t channel;
16*7c3d14c8STreehugger Robot
17*7c3d14c8STreehugger Robot  dispatch_fd_t fd = open(path, O_CREAT | O_WRONLY, 0666);
18*7c3d14c8STreehugger Robot  my_global++;
19*7c3d14c8STreehugger Robot  channel = dispatch_io_create(DISPATCH_IO_STREAM, fd, queue, ^(int error) {
20*7c3d14c8STreehugger Robot    my_global++;
21*7c3d14c8STreehugger Robot    dispatch_semaphore_signal(sem);
22*7c3d14c8STreehugger Robot  });
23*7c3d14c8STreehugger Robot  if (! channel) abort();
24*7c3d14c8STreehugger Robot  my_global++;
25*7c3d14c8STreehugger Robot  dispatch_io_close(channel, 0);
26*7c3d14c8STreehugger Robot  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
27*7c3d14c8STreehugger Robot
28*7c3d14c8STreehugger Robot  my_global++;
29*7c3d14c8STreehugger Robot  channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) {
30*7c3d14c8STreehugger Robot    my_global++;
31*7c3d14c8STreehugger Robot    dispatch_semaphore_signal(sem);
32*7c3d14c8STreehugger Robot  });
33*7c3d14c8STreehugger Robot  if (! channel) abort();
34*7c3d14c8STreehugger Robot  my_global++;
35*7c3d14c8STreehugger Robot  dispatch_io_close(channel, 0);
36*7c3d14c8STreehugger Robot  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
37*7c3d14c8STreehugger Robot
38*7c3d14c8STreehugger Robot  my_global++;
39*7c3d14c8STreehugger Robot  dispatch_io_t other_channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) { });
40*7c3d14c8STreehugger Robot  channel = dispatch_io_create_with_io(DISPATCH_IO_STREAM, other_channel, queue, ^(int error) {
41*7c3d14c8STreehugger Robot    my_global++;
42*7c3d14c8STreehugger Robot    dispatch_semaphore_signal(sem);
43*7c3d14c8STreehugger Robot  });
44*7c3d14c8STreehugger Robot  if (! channel) abort();
45*7c3d14c8STreehugger Robot  my_global++;
46*7c3d14c8STreehugger Robot  dispatch_io_close(channel, 0);
47*7c3d14c8STreehugger Robot  dispatch_io_close(other_channel, 0);
48*7c3d14c8STreehugger Robot  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
49*7c3d14c8STreehugger Robot
50*7c3d14c8STreehugger Robot  fprintf(stderr, "Done.\n");
51*7c3d14c8STreehugger Robot  return 0;
52*7c3d14c8STreehugger Robot}
53*7c3d14c8STreehugger Robot
54*7c3d14c8STreehugger Robot// CHECK: Hello world.
55*7c3d14c8STreehugger Robot// CHECK-NOT: WARNING: ThreadSanitizer
56*7c3d14c8STreehugger Robot// CHECK: Done.
57