aboutsummaryrefslogtreecommitdiff
path: root/test/tsan/Darwin/libcxx-shared-ptr.mm
blob: 6187c438fec075ea64acf1751c90c4b32b636f54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// RUN: %clangxx_tsan %s -o %t -framework Foundation
// RUN: %env_tsan_opts=ignore_interceptors_accesses=1 %run %t 2>&1 | FileCheck %s

#import <Foundation/Foundation.h>

#import <memory>

#import "../test.h"

long my_global;

struct MyStruct {
  void setGlobal() {
    my_global = 42;
  }
  ~MyStruct() {
    my_global = 43;
  }
};

int main(int argc, const char *argv[]) {
  fprintf(stderr, "Hello world.\n");
  print_address("addr=", 1, &my_global);
  barrier_init(&barrier, 2);

  std::shared_ptr<MyStruct> shared(new MyStruct());

  dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

  std::weak_ptr<MyStruct> weak(shared);
  
  dispatch_async(q, ^{
    {
      std::shared_ptr<MyStruct> strong = weak.lock();
      if (!strong) exit(1);

      strong->setGlobal();
    }
    barrier_wait(&barrier);
  });

  barrier_wait(&barrier);
  shared.reset();

  fprintf(stderr, "Done.\n");
}

// CHECK: Hello world.
// CHECK: Done.
// CHECK-NOT: WARNING: ThreadSanitizer