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

source code of compiler-rt/test/tsan/cond_cancel.c