Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
| 2 | // CHECK-NOT: WARNING |
| 3 | // CHECK: OK |
| 4 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 5 | #include "test.h" |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 6 | |
| 7 | pthread_mutex_t m; |
| 8 | pthread_cond_t c; |
| 9 | int x; |
| 10 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 11 | static void my_cleanup(void *arg) { |
| 12 | printf("my_cleanup\n"); |
| 13 | pthread_mutex_unlock((pthread_mutex_t*)arg); |
| 14 | } |
| 15 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 16 | void *thr1(void *p) { |
| 17 | pthread_mutex_lock(&m); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 18 | pthread_cleanup_push(my_cleanup, &m); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 19 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 20 | while (x == 0) |
| 21 | pthread_cond_wait(&c, &m); |
| 22 | pthread_cleanup_pop(1); |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | int main() { |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 27 | barrier_init(&barrier, 2); |
| 28 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 35 | barrier_wait(&barrier); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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 | } |