1// RUN: %clangxx_tsan %s -o %t
2// RUN: %deflake %run %t 2>&1 | FileCheck %s
3
4#include <thread>
5
6#import "../test.h"
7
8void *tag;
9
10__attribute__((no_sanitize("thread")))
11void ExternalWrite(void *addr) {
12 __tsan_external_write(addr, caller_pc: __builtin_return_address(0), tag);
13}
14
15int main(int argc, char *argv[]) {
16 barrier_init(barrier: &barrier, count: 2);
17 tag = __tsan_external_register_tag(object_type: "HelloWorld");
18 fprintf(stderr, format: "Start.\n");
19 // CHECK: Start.
20
21 for (int i = 0; i < 4; i++) {
22 void *opaque_object = malloc(size: 16);
23 std::thread t1([opaque_object] {
24 ExternalWrite(opaque_object);
25 barrier_wait(&barrier);
26 });
27 std::thread t2([opaque_object] {
28 barrier_wait(&barrier);
29 ExternalWrite(opaque_object);
30 });
31 // CHECK: WARNING: ThreadSanitizer: race on HelloWorld
32 t1.join();
33 t2.join();
34 }
35
36 fprintf(stderr, format: "First phase done.\n");
37 // CHECK: First phase done.
38
39 for (int i = 0; i < 4; i++) {
40 void *opaque_object = malloc(size: 16);
41 std::thread t1([opaque_object] {
42 ExternalWrite(opaque_object);
43 barrier_wait(&barrier);
44 });
45 std::thread t2([opaque_object] {
46 barrier_wait(&barrier);
47 ExternalWrite(opaque_object);
48 });
49 // CHECK: WARNING: ThreadSanitizer: race on HelloWorld
50 t1.join();
51 t2.join();
52 }
53
54 fprintf(stderr, format: "Second phase done.\n");
55 // CHECK: Second phase done.
56}
57
58// CHECK: ThreadSanitizer: reported 2 warnings
59

source code of compiler-rt/test/tsan/Darwin/external-dups.cpp