xref: /aosp_15_r20/external/compiler-rt/test/tsan/Darwin/gcd-io-barrier.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 Robotdispatch_queue_t queue;
7*7c3d14c8STreehugger Robotdispatch_data_t data;
8*7c3d14c8STreehugger Robotdispatch_semaphore_t sem;
9*7c3d14c8STreehugger Robotconst char *path;
10*7c3d14c8STreehugger Robot
11*7c3d14c8STreehugger Robotlong my_global = 0;
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robotint main(int argc, const char *argv[]) {
14*7c3d14c8STreehugger Robot  fprintf(stderr, "Hello world.\n");
15*7c3d14c8STreehugger Robot
16*7c3d14c8STreehugger Robot  queue = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
17*7c3d14c8STreehugger Robot  sem = dispatch_semaphore_create(0);
18*7c3d14c8STreehugger Robot  NSString *ns_path = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"temp-gcd-io.%d", getpid()]];
19*7c3d14c8STreehugger Robot  path = ns_path.fileSystemRepresentation;
20*7c3d14c8STreehugger Robot  NSData *ns_data = [NSMutableData dataWithLength:1000];
21*7c3d14c8STreehugger Robot  data = dispatch_data_create(ns_data.bytes, ns_data.length, NULL, DISPATCH_DATA_DESTRUCTOR_DEFAULT);
22*7c3d14c8STreehugger Robot
23*7c3d14c8STreehugger Robot  dispatch_io_t channel = dispatch_io_create_with_path(DISPATCH_IO_STREAM, path, O_CREAT | O_WRONLY, 0666, queue, ^(int error) { });
24*7c3d14c8STreehugger Robot  if (! channel) abort();
25*7c3d14c8STreehugger Robot  dispatch_io_set_high_water(channel, 1);
26*7c3d14c8STreehugger Robot
27*7c3d14c8STreehugger Robot  for (int i = 0; i < 1000; i++) {
28*7c3d14c8STreehugger Robot    dispatch_io_barrier(channel, ^{
29*7c3d14c8STreehugger Robot      my_global = 42;
30*7c3d14c8STreehugger Robot    });
31*7c3d14c8STreehugger Robot  }
32*7c3d14c8STreehugger Robot
33*7c3d14c8STreehugger Robot  dispatch_io_barrier(channel, ^{
34*7c3d14c8STreehugger Robot    my_global = 43;
35*7c3d14c8STreehugger Robot
36*7c3d14c8STreehugger Robot    dispatch_semaphore_signal(sem);
37*7c3d14c8STreehugger Robot  });
38*7c3d14c8STreehugger Robot
39*7c3d14c8STreehugger Robot  dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
40*7c3d14c8STreehugger Robot  dispatch_io_close(channel, 0);
41*7c3d14c8STreehugger Robot
42*7c3d14c8STreehugger Robot  fprintf(stderr, "Done.\n");
43*7c3d14c8STreehugger Robot  return 0;
44*7c3d14c8STreehugger Robot}
45*7c3d14c8STreehugger Robot
46*7c3d14c8STreehugger Robot// CHECK: Hello world.
47*7c3d14c8STreehugger Robot// CHECK-NOT: WARNING: ThreadSanitizer
48*7c3d14c8STreehugger Robot// CHECK: Done.
49