]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/tsan/static_init6.cc
Vendor import of compiler-rt trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / tsan / static_init6.cc
1 // RUN: %clangxx_tsan %linux_static_libstdcplusplus -O1 %s -o %t && %run %t 2>&1 \
2 // RUN: | FileCheck %s
3 #include <pthread.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <sched.h>
7
8 struct Cache {
9   int x;
10   explicit Cache(int x)
11     : x(x) {
12   }
13 };
14
15 void *AsyncInit(void *p) {
16   return new Cache((int)(long)p);
17 }
18
19 Cache *CreateCache() {
20   pthread_t t;
21   pthread_create(&t, 0, AsyncInit, (void*)(long)rand());
22   void *res;
23   pthread_join(t, &res);
24   return (Cache*)res;
25 }
26
27 void *Thread1(void *x) {
28   static Cache *c = CreateCache();
29   if (c->x >= RAND_MAX)
30     exit(1);
31   return 0;
32 }
33
34 int main() {
35   pthread_t t[2];
36   pthread_create(&t[0], 0, Thread1, 0);
37   pthread_create(&t[1], 0, Thread1, 0);
38   pthread_join(t[0], 0);
39   pthread_join(t[1], 0);
40   fprintf(stderr, "PASS\n");
41 }
42
43 // CHECK-NOT: WARNING: ThreadSanitizer: data race