1// This test verifies that dispatch_sync() doesn't actually copy the block under TSan (without TSan, it doesn't).
2
3// RUN: %clangxx_tsan %s -o %t_no_tsan -fno-sanitize=thread
4// RUN: %clangxx_tsan %s -o %t_with_tsan
5
6// RUN: %run %t_no_tsan 2>&1 | FileCheck %s
7// RUN: %run %t_with_tsan 2>&1 | FileCheck %s --implicit-check-not='ThreadSanitizer'
8
9#include <dispatch/dispatch.h>
10
11#include <stdio.h>
12
13struct MyClass {
14 static int copyCount;
15 static void printCopyCount() {
16 fprintf(stderr, format: "copyCount = %d\n", copyCount);
17 }
18 MyClass(){};
19 MyClass(const MyClass &obj) { copyCount++; };
20 void foo() const {
21 fprintf(stderr, format: "MyClass::foo\n");
22 }
23};
24int MyClass::copyCount = 0;
25
26int main(int argc, const char* argv[]) {
27 dispatch_queue_t q = dispatch_queue_create("my.queue", NULL);
28 MyClass obj;
29 MyClass::printCopyCount();
30 void (^block)(void) = ^{
31 obj.foo();
32 };
33 MyClass::printCopyCount();
34 dispatch_sync(q, block);
35 MyClass::printCopyCount();
36
37 fprintf(stderr, format: "Done.\n");
38 return 0;
39}
40
41// CHECK: copyCount = 0
42// CHECK: copyCount = 1
43// CHECK: MyClass::foo
44// CHECK: copyCount = 1
45// CHECK: Done.
46

source code of compiler-rt/test/tsan/libdispatch/sync-block-copy.cpp