1*7c3d14c8STreehugger Robot // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2*7c3d14c8STreehugger Robot // CHECK-NOT: WARNING
3*7c3d14c8STreehugger Robot // CHECK: OK
4*7c3d14c8STreehugger Robot // This test is failing on powerpc64 (VMA=44). After calling pthread_cancel,
5*7c3d14c8STreehugger Robot // the Thread-specific data destructors are not called, so the destructor
6*7c3d14c8STreehugger Robot // "thread_finalize" (defined in tsan_interceptors.cc) can not set the status
7*7c3d14c8STreehugger Robot // of the thread to "ThreadStatusFinished" failing a check in "SetJoined"
8*7c3d14c8STreehugger Robot // (defined in sanitizer_thread_registry.cc). It might seem a bug on glibc,
9*7c3d14c8STreehugger Robot // however the same version GLIBC-2.17 will not make fail the test on
10*7c3d14c8STreehugger Robot // powerpc64 BE (VMA=46)
11*7c3d14c8STreehugger Robot // XFAIL: powerpc64-unknown-linux-gnu
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robot #include "test.h"
14*7c3d14c8STreehugger Robot
15*7c3d14c8STreehugger Robot pthread_mutex_t m;
16*7c3d14c8STreehugger Robot pthread_cond_t c;
17*7c3d14c8STreehugger Robot int x;
18*7c3d14c8STreehugger Robot
my_cleanup(void * arg)19*7c3d14c8STreehugger Robot static void my_cleanup(void *arg) {
20*7c3d14c8STreehugger Robot printf("my_cleanup\n");
21*7c3d14c8STreehugger Robot pthread_mutex_unlock((pthread_mutex_t*)arg);
22*7c3d14c8STreehugger Robot }
23*7c3d14c8STreehugger Robot
thr1(void * p)24*7c3d14c8STreehugger Robot void *thr1(void *p) {
25*7c3d14c8STreehugger Robot pthread_mutex_lock(&m);
26*7c3d14c8STreehugger Robot pthread_cleanup_push(my_cleanup, &m);
27*7c3d14c8STreehugger Robot barrier_wait(&barrier);
28*7c3d14c8STreehugger Robot while (x == 0)
29*7c3d14c8STreehugger Robot pthread_cond_wait(&c, &m);
30*7c3d14c8STreehugger Robot pthread_cleanup_pop(1);
31*7c3d14c8STreehugger Robot return 0;
32*7c3d14c8STreehugger Robot }
33*7c3d14c8STreehugger Robot
main()34*7c3d14c8STreehugger Robot int main() {
35*7c3d14c8STreehugger Robot barrier_init(&barrier, 2);
36*7c3d14c8STreehugger Robot
37*7c3d14c8STreehugger Robot pthread_t th;
38*7c3d14c8STreehugger Robot
39*7c3d14c8STreehugger Robot pthread_mutex_init(&m, 0);
40*7c3d14c8STreehugger Robot pthread_cond_init(&c, 0);
41*7c3d14c8STreehugger Robot
42*7c3d14c8STreehugger Robot pthread_create(&th, 0, thr1, 0);
43*7c3d14c8STreehugger Robot barrier_wait(&barrier);
44*7c3d14c8STreehugger Robot sleep(1); // let it block on cond var
45*7c3d14c8STreehugger Robot pthread_cancel(th);
46*7c3d14c8STreehugger Robot
47*7c3d14c8STreehugger Robot pthread_join(th, 0);
48*7c3d14c8STreehugger Robot pthread_mutex_lock(&m);
49*7c3d14c8STreehugger Robot pthread_mutex_unlock(&m);
50*7c3d14c8STreehugger Robot fprintf(stderr, "OK\n");
51*7c3d14c8STreehugger Robot }
52