1// RUN: %clang_tsan %s -o %t
2// RUN: %deflake %run %t 2>&1 | FileCheck %s
3
4#include <dispatch/dispatch.h>
5
6#include "../test.h"
7
8long global;
9
10int main(int argc, const char *argv[]) {
11 barrier_init(barrier: &barrier, count: 2);
12 fprintf(stderr, format: "start\n");
13
14 // Warm up GCD (workaround for macOS Sierra where dispatch_apply might run single-threaded).
15 dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ });
16
17 dispatch_queue_t q = dispatch_queue_create("my.queue", DISPATCH_QUEUE_CONCURRENT);
18 dispatch_apply(2, q, ^(size_t i) {
19 global = i;
20 barrier_wait(barrier: &barrier);
21 });
22
23 fprintf(stderr, format: "done\n");
24 return 0;
25}
26
27// CHECK: start
28// CHECK: WARNING: ThreadSanitizer: data race
29// CHECK: Location is global 'global'
30// CHECK: done
31

source code of compiler-rt/test/tsan/libdispatch/apply-race.c