xref: /aosp_15_r20/external/compiler-rt/test/tsan/Darwin/gcd-groups-destructor.mm (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot// RUN: %clangxx_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 Robot#import <memory>
7*7c3d14c8STreehugger Robot#import <stdatomic.h>
8*7c3d14c8STreehugger Robot
9*7c3d14c8STreehugger Robot_Atomic(long) destructor_counter = 0;
10*7c3d14c8STreehugger Robot
11*7c3d14c8STreehugger Robotstruct MyStruct {
12*7c3d14c8STreehugger Robot  virtual ~MyStruct() {
13*7c3d14c8STreehugger Robot    usleep(10000);
14*7c3d14c8STreehugger Robot    atomic_fetch_add_explicit(&destructor_counter, 1, memory_order_relaxed);
15*7c3d14c8STreehugger Robot  }
16*7c3d14c8STreehugger Robot};
17*7c3d14c8STreehugger Robot
18*7c3d14c8STreehugger Robotint main(int argc, const char *argv[]) {
19*7c3d14c8STreehugger Robot  fprintf(stderr, "Hello world.\n");
20*7c3d14c8STreehugger Robot
21*7c3d14c8STreehugger Robot  dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
22*7c3d14c8STreehugger Robot  dispatch_group_t g = dispatch_group_create();
23*7c3d14c8STreehugger Robot
24*7c3d14c8STreehugger Robot  for (int i = 0; i < 100; i++) {
25*7c3d14c8STreehugger Robot    std::shared_ptr<MyStruct> shared(new MyStruct());
26*7c3d14c8STreehugger Robot
27*7c3d14c8STreehugger Robot    dispatch_group_async(g, q, ^{
28*7c3d14c8STreehugger Robot      shared.get(); // just to make sure the object is captured by the block
29*7c3d14c8STreehugger Robot    });
30*7c3d14c8STreehugger Robot  }
31*7c3d14c8STreehugger Robot
32*7c3d14c8STreehugger Robot  dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
33*7c3d14c8STreehugger Robot
34*7c3d14c8STreehugger Robot  if (destructor_counter != 100) {
35*7c3d14c8STreehugger Robot    abort();
36*7c3d14c8STreehugger Robot  }
37*7c3d14c8STreehugger Robot
38*7c3d14c8STreehugger Robot  fprintf(stderr, "Done.\n");
39*7c3d14c8STreehugger Robot}
40*7c3d14c8STreehugger Robot
41*7c3d14c8STreehugger Robot// CHECK: Hello world.
42*7c3d14c8STreehugger Robot// CHECK-NOT: WARNING: ThreadSanitizer
43*7c3d14c8STreehugger Robot// CHECK: Done.
44