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