1 | //===-- PThreadMutex.cpp ----------------------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Created by Greg Clayton on 12/9/08. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "PThreadMutex.h" |
14 | |
15 | #include "DNBTimer.h" |
16 | |
17 | #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS) |
18 | |
19 | PThreadMutex::Locker::Locker(PThreadMutex &m, const char *function, |
20 | const char *file, const int line) |
21 | : m_pMutex(m.Mutex()), m_function(function), m_file(file), m_line(line), |
22 | m_lock_time(0) { |
23 | Lock(); |
24 | } |
25 | |
26 | PThreadMutex::Locker::Locker(PThreadMutex *m, const char *function, |
27 | const char *file, const int line) |
28 | : m_pMutex(m ? m->Mutex() : NULL), m_function(function), m_file(file), |
29 | m_line(line), m_lock_time(0) { |
30 | Lock(); |
31 | } |
32 | |
33 | PThreadMutex::Locker::Locker(pthread_mutex_t *mutex, const char *function, |
34 | const char *file, const int line) |
35 | : m_pMutex(mutex), m_function(function), m_file(file), m_line(line), |
36 | m_lock_time(0) { |
37 | Lock(); |
38 | } |
39 | |
40 | PThreadMutex::Locker::~Locker() { Unlock(); } |
41 | |
42 | void PThreadMutex::Locker::Lock() { |
43 | if (m_pMutex) { |
44 | m_lock_time = DNBTimer::GetTimeOfDay(); |
45 | if (::pthread_mutex_trylock(m_pMutex) != 0) { |
46 | fprintf(stdout, "::pthread_mutex_trylock (%8.8p) mutex is locked " |
47 | "(function %s in %s:%i), waiting...\n" , |
48 | m_pMutex, m_function, m_file, m_line); |
49 | ::pthread_mutex_lock(m_pMutex); |
50 | fprintf(stdout, "::pthread_mutex_lock (%8.8p) succeeded after %6llu " |
51 | "usecs (function %s in %s:%i)\n" , |
52 | m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, |
53 | m_file, m_line); |
54 | } |
55 | } |
56 | } |
57 | |
58 | void PThreadMutex::Locker::Unlock() { |
59 | fprintf(stdout, "::pthread_mutex_unlock (%8.8p) had lock for %6llu usecs in " |
60 | "%s in %s:%i\n" , |
61 | m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, m_file, |
62 | m_line); |
63 | ::pthread_mutex_unlock(m_pMutex); |
64 | } |
65 | |
66 | #endif |
67 | |