1 // Check that calling dispatch_once from a report callback works.
2 
3 // RUN: %clang_tsan %s -o %t
4 // RUN: not %env_tsan_opts=ignore_noninstrumented_modules=0 %run %t 2>&1 | FileCheck %s
5 
6 #include <dispatch/dispatch.h>
7 
8 #include <pthread.h>
9 #include <stdio.h>
10 
11 long g = 0;
12 long h = 0;
13 
14 __attribute__((disable_sanitizer_instrumentation))
15 void f() {
16   static dispatch_once_t onceToken;
17   dispatch_once(&onceToken, ^{
18     g++;
19   });
20   h++;
21 }
22 
23 __attribute__((disable_sanitizer_instrumentation))
24 void __tsan_on_report() {
25   fprintf(stderr, "Report.\n");
26   f();
27 }
28 
29 int main() {
30   fprintf(stderr, "Hello world.\n");
31 
32   f();
33 
34   pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
35   pthread_mutex_unlock(&mutex); // Unlock of an unlocked mutex
36 
37   fprintf(stderr, "g = %ld.\n", g);
38   fprintf(stderr, "h = %ld.\n", h);
39   fprintf(stderr, "Done.\n");
40 }
41 
42 // CHECK: Hello world.
43 // CHECK: Report.
44 // CHECK: g = 1
45 // CHECK: h = 2
46 // CHECK: Done.
47