1// RUN: %clangxx_tsan %s -o %t -framework Foundation
2// RUN: %run %t 2>&1 | FileCheck %s
3
4#import <Foundation/Foundation.h>
5
6#import <memory>
7
8#import "../test.h"
9
10long my_global;
11
12struct MyStruct {
13 void setGlobal() {
14 my_global = 42;
15 }
16 ~MyStruct() {
17 my_global = 43;
18 }
19};
20
21int main(int argc, const char *argv[]) {
22 fprintf(stderr, format: "Hello world.\n");
23 print_address("addr=", 1, &my_global);
24 barrier_init(barrier: &barrier, count: 2);
25
26 std::shared_ptr<MyStruct> shared(new MyStruct());
27
28 dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
29
30 std::weak_ptr<MyStruct> weak(shared);
31
32 dispatch_async(q, ^{
33 {
34 std::shared_ptr<MyStruct> strong = weak.lock();
35 if (!strong) exit(status: 1);
36
37 strong->setGlobal();
38 }
39 barrier_wait(barrier: &barrier);
40 });
41
42 barrier_wait(barrier: &barrier);
43 shared.reset();
44
45 fprintf(stderr, format: "Done.\n");
46}
47
48// CHECK: Hello world.
49// CHECK: Done.
50// CHECK-NOT: WARNING: ThreadSanitizer
51

source code of compiler-rt/test/tsan/Darwin/libcxx-shared-ptr.mm