xref: /llvm-project/compiler-rt/test/tsan/libdispatch/apply-race.c (revision 4d4f64cdddba31fa507b2dc055af94ede76d9335)
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 
8 long global;
9 
main(int argc,const char * argv[])10 int main(int argc, const char *argv[]) {
11   barrier_init(&barrier, 2);
12   fprintf(stderr, "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);
21   });
22 
23   fprintf(stderr, "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