Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- PThreadMutex.cpp ----------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Created by Greg Clayton on 12/9/08. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "PThreadMutex.h" |
| 14 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "DNBTimer.h" |
| 16 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 17 | #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | PThreadMutex::Locker::~Locker() { Unlock(); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | #endif |