Greg Fitzgerald | b8aae54 | 2014-04-30 21:34:17 +0000 | [diff] [blame] | 1 | // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
Kostya Serebryany | ff15ef0 | 2012-05-10 14:18:22 +0000 | [diff] [blame] | 2 | #include <pthread.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdio.h> |
| 5 | #include <sched.h> |
| 6 | |
| 7 | struct Cache { |
| 8 | int x; |
Alexey Samsonov | c3a8119 | 2012-08-30 14:22:21 +0000 | [diff] [blame] | 9 | explicit Cache(int x) |
Kostya Serebryany | ff15ef0 | 2012-05-10 14:18:22 +0000 | [diff] [blame] | 10 | : x(x) { |
| 11 | } |
| 12 | }; |
| 13 | |
| 14 | void *AsyncInit(void *p) { |
| 15 | return new Cache((int)(long)p); |
| 16 | } |
| 17 | |
| 18 | Cache *CreateCache() { |
| 19 | pthread_t t; |
Dmitry Vyukov | 95b9a36 | 2012-11-06 14:05:20 +0000 | [diff] [blame] | 20 | pthread_create(&t, 0, AsyncInit, (void*)(long)rand()); |
Kostya Serebryany | ff15ef0 | 2012-05-10 14:18:22 +0000 | [diff] [blame] | 21 | void *res; |
| 22 | pthread_join(t, &res); |
| 23 | return (Cache*)res; |
| 24 | } |
| 25 | |
| 26 | void *Thread1(void *x) { |
| 27 | static Cache *c = CreateCache(); |
| 28 | if (c->x >= RAND_MAX) |
| 29 | exit(1); |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | int main() { |
| 34 | pthread_t t[2]; |
| 35 | pthread_create(&t[0], 0, Thread1, 0); |
| 36 | pthread_create(&t[1], 0, Thread1, 0); |
| 37 | pthread_join(t[0], 0); |
| 38 | pthread_join(t[1], 0); |
Renato Golin | 1f42286 | 2016-04-15 12:34:00 +0000 | [diff] [blame] | 39 | fprintf(stderr, "PASS\n"); |
Kostya Serebryany | ff15ef0 | 2012-05-10 14:18:22 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // CHECK-NOT: WARNING: ThreadSanitizer: data race |