| 1 | // RUN: %clangxx_tsan -O1 %s -o %t |
| 2 | // RUN: %run %t 2>&1 | FileCheck %s |
| 3 | // RUN: %run %t arg 2>&1 | FileCheck %s |
| 4 | // RUN: %run %t arg arg 2>&1 | FileCheck %s |
| 5 | #include "test.h" |
| 6 | |
| 7 | // Test for destruction of pthread_cond_t. |
| 8 | // POSIX states that it is safe to destroy a condition variable upon which no |
| 9 | // threads are currently blocked. That is, it is not necessary to wait untill |
| 10 | // other threads return from pthread_cond_wait, they just need to be unblocked. |
| 11 | |
| 12 | pthread_mutex_t m; |
| 13 | pthread_cond_t c; |
| 14 | bool done1, done2; |
| 15 | |
| 16 | void *thr(void *p) { |
| 17 | pthread_mutex_lock(mutex: &m); |
| 18 | done1 = true; |
| 19 | pthread_cond_signal(cond: &c); |
| 20 | while (!done2) |
| 21 | pthread_cond_wait(cond: &c, mutex: &m); |
| 22 | pthread_mutex_unlock(mutex: &m); |
| 23 | return 0; |
| 24 | } |
| 25 | |
| 26 | int main(int argc, char **argv) { |
| 27 | pthread_t th; |
| 28 | pthread_mutex_init(mutex: &m, mutexattr: 0); |
| 29 | pthread_cond_init(cond: &c, cond_attr: 0); |
| 30 | pthread_create(newthread: &th, attr: 0, start_routine: thr, arg: 0); |
| 31 | pthread_mutex_lock(mutex: &m); |
| 32 | while (!done1) |
| 33 | pthread_cond_wait(cond: &c, mutex: &m); |
| 34 | done2 = true; |
| 35 | // Any of these sequences is legal. |
| 36 | if (argc == 1) { |
| 37 | pthread_cond_signal(cond: &c); |
| 38 | pthread_mutex_unlock(mutex: &m); |
| 39 | pthread_cond_destroy(cond: &c); |
| 40 | } else if (argc == 2) { |
| 41 | pthread_mutex_unlock(mutex: &m); |
| 42 | pthread_cond_signal(cond: &c); |
| 43 | pthread_cond_destroy(cond: &c); |
| 44 | } else { |
| 45 | pthread_cond_signal(cond: &c); |
| 46 | pthread_cond_destroy(cond: &c); |
| 47 | pthread_mutex_unlock(mutex: &m); |
| 48 | } |
| 49 | pthread_join(th: th, thread_return: 0); |
| 50 | fprintf(stderr, format: "DONE\n" ); |
| 51 | } |
| 52 | |
| 53 | // CHECK-NOT: ThreadSanitizer: data race |
| 54 | |