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 #include "DNBTimer.h" 17 18 #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS) 19 20 PThreadMutex::Locker::Locker(PThreadMutex &m, const char *function, 21 const char *file, const int line) 22 : m_pMutex(m.Mutex()), m_function(function), m_file(file), m_line(line), 23 m_lock_time(0) { 24 Lock(); 25 } 26 27 PThreadMutex::Locker::Locker(PThreadMutex *m, const char *function, 28 const char *file, const int line) 29 : m_pMutex(m ? m->Mutex() : NULL), m_function(function), m_file(file), 30 m_line(line), m_lock_time(0) { 31 Lock(); 32 } 33 34 PThreadMutex::Locker::Locker(pthread_mutex_t *mutex, const char *function, 35 const char *file, const int line) 36 : m_pMutex(mutex), m_function(function), m_file(file), m_line(line), 37 m_lock_time(0) { 38 Lock(); 39 } 40 41 PThreadMutex::Locker::~Locker() { Unlock(); } 42 43 void PThreadMutex::Locker::Lock() { 44 if (m_pMutex) { 45 m_lock_time = DNBTimer::GetTimeOfDay(); 46 if (::pthread_mutex_trylock(m_pMutex) != 0) { 47 fprintf(stdout, "::pthread_mutex_trylock (%8.8p) mutex is locked " 48 "(function %s in %s:%i), waiting...\n", 49 m_pMutex, m_function, m_file, m_line); 50 ::pthread_mutex_lock(m_pMutex); 51 fprintf(stdout, "::pthread_mutex_lock (%8.8p) succeeded after %6llu " 52 "usecs (function %s in %s:%i)\n", 53 m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, 54 m_file, m_line); 55 } 56 } 57 } 58 59 void PThreadMutex::Locker::Unlock() { 60 fprintf(stdout, "::pthread_mutex_unlock (%8.8p) had lock for %6llu usecs in " 61 "%s in %s:%i\n", 62 m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, m_file, 63 m_line); 64 ::pthread_mutex_unlock(m_pMutex); 65 } 66 67 #endif 68