Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
| 2 | // CHECK-NOT: ThreadSanitizer: data race |
| 3 | // CHECK: DONE |
| 4 | |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <pthread.h> |
| 8 | #include <unistd.h> |
| 9 | |
| 10 | const int kSize = 4; |
| 11 | volatile int kIter = 10; // prevent unwinding |
| 12 | int data[2][kSize]; |
| 13 | pthread_barrier_t barrier; |
| 14 | |
| 15 | void *thr(void *p) { |
| 16 | int idx = (int)(long)p; |
| 17 | for (int i = 0; i < kIter; i++) { |
| 18 | int *prev = data[i % 2]; |
| 19 | int *curr = data[(i + 1) % 2]; |
| 20 | int left = idx - 1 >= 0 ? prev[idx - 1] : 0; |
| 21 | int right = idx + 1 < kSize ? prev[idx + 1] : 0; |
| 22 | curr[idx] = (left + right) / 2; |
| 23 | pthread_barrier_wait(&barrier); |
| 24 | } |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | int main() { |
| 29 | pthread_barrier_init(&barrier, 0, kSize); |
| 30 | pthread_t th[kSize]; |
| 31 | for (int i = 0; i < kSize; i++) |
| 32 | pthread_create(&th[i], 0, thr, (void*)(long)i); |
| 33 | for (int i = 0; i < kSize; i++) |
| 34 | pthread_join(th[i], 0); |
| 35 | pthread_barrier_destroy(&barrier); |
| 36 | fprintf(stderr, "DONE\n"); |
| 37 | } |