124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===-- PThreadMutex.cpp ----------------------------------------*- C++ -*-===// 224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// 324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// The LLVM Compiler Infrastructure 424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// 524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// This file is distributed under the University of Illinois Open Source 624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// License. See LICENSE.TXT for details. 724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// 824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===----------------------------------------------------------------------===// 924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// 1024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// Created by Greg Clayton on 12/9/08. 1124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// 1224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner//===----------------------------------------------------------------------===// 1324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 1424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "PThreadMutex.h" 1524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 1624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C Includes 1724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// C++ Includes 1824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// Other libraries and framework includes 1924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner// Project includes 2024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#include "DNBTimer.h" 2124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 2224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#if defined (DEBUG_PTHREAD_MUTEX_DEADLOCKS) 2324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 2424943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerPThreadMutex::Locker::Locker(PThreadMutex& m, const char *function, const char *file, const int line) : 2524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_pMutex(m.Mutex()), 2624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_function(function), 2724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_file(file), 2824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_line(line), 2924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_lock_time(0) 3024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{ 3124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner Lock(); 3224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner} 3324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 3424943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerPThreadMutex::Locker::Locker(PThreadMutex* m, const char *function, const char *file, const int line) : 3524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_pMutex(m ? m->Mutex() : NULL), 3624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_function(function), 3724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_file(file), 3824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_line(line), 3924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_lock_time(0) 4024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{ 4124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner Lock(); 4224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner} 4324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 4424943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerPThreadMutex::Locker::Locker(pthread_mutex_t *mutex, const char *function, const char *file, const int line) : 4524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_pMutex(mutex), 4624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_function(function), 4724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_file(file), 4824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_line(line), 4924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_lock_time(0) 5024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{ 5124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner Lock(); 5224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner} 5324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 5424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 5524943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerPThreadMutex::Locker::~Locker() 5624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{ 5724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner Unlock(); 5824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner} 5924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 6024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 6124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnervoid 6224943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerPThreadMutex::Locker::Lock() 6324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{ 6424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner if (m_pMutex) 6524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner { 6624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner m_lock_time = DNBTimer::GetTimeOfDay(); 6724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner if (::pthread_mutex_trylock (m_pMutex) != 0) 6824943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner { 6924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner fprintf(stdout, "::pthread_mutex_trylock (%8.8p) mutex is locked (function %s in %s:%i), waiting...\n", m_pMutex, m_function, m_file, m_line); 7024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner ::pthread_mutex_lock (m_pMutex); 7124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner fprintf(stdout, "::pthread_mutex_lock (%8.8p) succeeded after %6llu usecs (function %s in %s:%i)\n", m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, m_file, m_line); 7224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner } 7324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner } 7424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner} 7524943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 7624943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 7724943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattnervoid 7824943d2ee8bfaa7cf5893e4709143924157a5c1eChris LattnerPThreadMutex::Locker::Unlock() 7924943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner{ 8024943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner fprintf(stdout, "::pthread_mutex_unlock (%8.8p) had lock for %6llu usecs in %s in %s:%i\n", m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, m_file, m_line); 8124943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner ::pthread_mutex_unlock (m_pMutex); 8224943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner} 8324943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner 8424943d2ee8bfaa7cf5893e4709143924157a5c1eChris Lattner#endif 85