1 //===-- PThreadMutex.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Created by Greg Clayton on 12/9/08.
12 //===----------------------------------------------------------------------===//
14 #include "PThreadMutex.h"
18 // Other libraries and framework includes
22 #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
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),
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) {
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),
45 PThreadMutex::Locker::~Locker() { Unlock(); }
47 void PThreadMutex::Locker::Lock() {
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,
63 void PThreadMutex::Locker::Unlock() {
64 fprintf(stdout, "::pthread_mutex_unlock (%8.8p) had lock for %6llu usecs in "
66 m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, m_file,
68 ::pthread_mutex_unlock(m_pMutex);