blob: ddfb745174f63e1fdd8371d364e02c887140bec9 [file] [log] [blame]
Stephen Hines2d1fdb22014-05-28 23:58:16 -07001// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2// CHECK-NOT: WARNING
3// CHECK: OK
4
Stephen Hines86277eb2015-03-23 12:06:32 -07005#include "test.h"
Stephen Hines2d1fdb22014-05-28 23:58:16 -07006
7pthread_mutex_t m;
8pthread_cond_t c;
9int x;
10
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070011static void my_cleanup(void *arg) {
12 printf("my_cleanup\n");
13 pthread_mutex_unlock((pthread_mutex_t*)arg);
14}
15
Stephen Hines2d1fdb22014-05-28 23:58:16 -070016void *thr1(void *p) {
17 pthread_mutex_lock(&m);
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -070018 pthread_cleanup_push(my_cleanup, &m);
Stephen Hines86277eb2015-03-23 12:06:32 -070019 barrier_wait(&barrier);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070020 while (x == 0)
21 pthread_cond_wait(&c, &m);
22 pthread_cleanup_pop(1);
23 return 0;
24}
25
26int main() {
Stephen Hines86277eb2015-03-23 12:06:32 -070027 barrier_init(&barrier, 2);
28
Stephen Hines2d1fdb22014-05-28 23:58:16 -070029 pthread_t th;
30
31 pthread_mutex_init(&m, 0);
32 pthread_cond_init(&c, 0);
33
34 pthread_create(&th, 0, thr1, 0);
Stephen Hines86277eb2015-03-23 12:06:32 -070035 barrier_wait(&barrier);
Stephen Hines2d1fdb22014-05-28 23:58:16 -070036 sleep(1); // let it block on cond var
37 pthread_cancel(th);
38
39 pthread_join(th, 0);
40 pthread_mutex_lock(&m);
41 pthread_mutex_unlock(&m);
42 fprintf(stderr, "OK\n");
43}