cond_cancel.c revision 909fff81b83df049ecc6e02407394640435d7bef
1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2// CHECK-NOT: WARNING
3// CHECK: OK
4
5#include "test.h"
6
7pthread_mutex_t m;
8pthread_cond_t c;
9int x;
10
11static void my_cleanup(void *arg) {
12  printf("my_cleanup\n");
13  pthread_mutex_unlock((pthread_mutex_t*)arg);
14}
15
16void *thr1(void *p) {
17  pthread_mutex_lock(&m);
18  pthread_cleanup_push(my_cleanup, &m);
19  barrier_wait(&barrier);
20  while (x == 0)
21    pthread_cond_wait(&c, &m);
22  pthread_cleanup_pop(1);
23  return 0;
24}
25
26int main() {
27  barrier_init(&barrier, 2);
28
29  pthread_t th;
30
31  pthread_mutex_init(&m, 0);
32  pthread_cond_init(&c, 0);
33
34  pthread_create(&th, 0, thr1, 0);
35  barrier_wait(&barrier);
36  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}
44