Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- PThreadMutex.cpp ----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Created by Greg Clayton on 12/9/08. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PThreadMutex.h" |
| 15 | |
| 16 | // C Includes |
| 17 | // C++ Includes |
| 18 | // Other libraries and framework includes |
| 19 | // Project includes |
| 20 | #include "DNBTimer.h" |
| 21 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 22 | #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | PThreadMutex::Locker::Locker(PThreadMutex &m, const char *function, |
| 25 | const char *file, const int line) |
| 26 | : m_pMutex(m.Mutex()), m_function(function), m_file(file), m_line(line), |
| 27 | m_lock_time(0) { |
| 28 | Lock(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | PThreadMutex::Locker::Locker(PThreadMutex *m, const char *function, |
| 32 | const char *file, const int line) |
| 33 | : m_pMutex(m ? m->Mutex() : NULL), m_function(function), m_file(file), |
| 34 | m_line(line), m_lock_time(0) { |
| 35 | Lock(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | PThreadMutex::Locker::Locker(pthread_mutex_t *mutex, const char *function, |
| 39 | const char *file, const int line) |
| 40 | : m_pMutex(mutex), m_function(function), m_file(file), m_line(line), |
| 41 | m_lock_time(0) { |
| 42 | Lock(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | PThreadMutex::Locker::~Locker() { Unlock(); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | void PThreadMutex::Locker::Lock() { |
| 48 | if (m_pMutex) { |
| 49 | m_lock_time = DNBTimer::GetTimeOfDay(); |
| 50 | if (::pthread_mutex_trylock(m_pMutex) != 0) { |
| 51 | fprintf(stdout, "::pthread_mutex_trylock (%8.8p) mutex is locked " |
| 52 | "(function %s in %s:%i), waiting...\n", |
| 53 | m_pMutex, m_function, m_file, m_line); |
| 54 | ::pthread_mutex_lock(m_pMutex); |
| 55 | fprintf(stdout, "::pthread_mutex_lock (%8.8p) succeeded after %6llu " |
| 56 | "usecs (function %s in %s:%i)\n", |
| 57 | m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, |
| 58 | m_file, m_line); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 59 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | void PThreadMutex::Locker::Unlock() { |
| 64 | fprintf(stdout, "::pthread_mutex_unlock (%8.8p) had lock for %6llu usecs in " |
| 65 | "%s in %s:%i\n", |
| 66 | m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, m_file, |
| 67 | m_line); |
| 68 | ::pthread_mutex_unlock(m_pMutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | #endif |