• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
f()13 void f() {
14   static dispatch_once_t onceToken;
15   dispatch_once(&onceToken, ^{
16     g++;
17   });
18   h++;
19 }
20 
__tsan_on_report()21 void __tsan_on_report() {
22   fprintf(stderr, "Report.\n");
23   f();
24 }
25 
main()26 int main() {
27   fprintf(stderr, "Hello world.\n");
28 
29   f();
30 
31   pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
32   pthread_mutex_unlock(&mutex); // Unlock of an unlocked mutex
33 
34   fprintf(stderr, "g = %ld.\n", g);
35   fprintf(stderr, "h = %ld.\n", h);
36   fprintf(stderr, "Done.\n");
37 }
38 
39 // CHECK: Hello world.
40 // CHECK: Report.
41 // CHECK: g = 1
42 // CHECK: h = 2
43 // CHECK: Done.
44