| 1 | // RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s |
|---|---|
| 2 | // REQUIRES: glibc-2.30 |
| 3 | #include <pthread.h> |
| 4 | #include <stdio.h> |
| 5 | |
| 6 | pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER; |
| 7 | struct timespec ts = {.tv_sec: 0}; |
| 8 | |
| 9 | void *tfunc(void *p) { |
| 10 | if (!pthread_mutex_trylock(mutex: &m)) { |
| 11 | puts(s: "Second thread could not lock mutex"); |
| 12 | pthread_mutex_unlock(mutex: &m); |
| 13 | } |
| 14 | return p; |
| 15 | } |
| 16 | |
| 17 | int main() { |
| 18 | if (!pthread_mutex_clocklock(mutex: &m, CLOCK_REALTIME, abstime: &ts)) { |
| 19 | pthread_t thr; |
| 20 | pthread_create(newthread: &thr, attr: 0, start_routine: tfunc, arg: 0); |
| 21 | pthread_join(th: thr, thread_return: 0); |
| 22 | pthread_mutex_unlock(mutex: &m); |
| 23 | } else |
| 24 | puts(s: "Failed to lock mutex"); |
| 25 | fprintf(stderr, format: "PASS\n"); |
| 26 | } |
| 27 | |
| 28 | // CHECK-NOT: WARNING: ThreadSanitizer: unlock of an unlocked mutex |
| 29 | // CHECK: PASS |
| 30 |
