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 | |
| 22 | #if defined (DEBUG_PTHREAD_MUTEX_DEADLOCKS) |
| 23 | |
| 24 | PThreadMutex::Locker::Locker(PThreadMutex& m, const char *function, const char *file, const int line) : |
| 25 | m_pMutex(m.Mutex()), |
| 26 | m_function(function), |
| 27 | m_file(file), |
| 28 | m_line(line), |
| 29 | m_lock_time(0) |
| 30 | { |
| 31 | Lock(); |
| 32 | } |
| 33 | |
| 34 | PThreadMutex::Locker::Locker(PThreadMutex* m, const char *function, const char *file, const int line) : |
| 35 | m_pMutex(m ? m->Mutex() : NULL), |
| 36 | m_function(function), |
| 37 | m_file(file), |
| 38 | m_line(line), |
| 39 | m_lock_time(0) |
| 40 | { |
| 41 | Lock(); |
| 42 | } |
| 43 | |
| 44 | PThreadMutex::Locker::Locker(pthread_mutex_t *mutex, const char *function, const char *file, const int line) : |
| 45 | m_pMutex(mutex), |
| 46 | m_function(function), |
| 47 | m_file(file), |
| 48 | m_line(line), |
| 49 | m_lock_time(0) |
| 50 | { |
| 51 | Lock(); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | PThreadMutex::Locker::~Locker() |
| 56 | { |
| 57 | Unlock(); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | void |
| 62 | PThreadMutex::Locker::Lock() |
| 63 | { |
| 64 | if (m_pMutex) |
| 65 | { |
| 66 | m_lock_time = DNBTimer::GetTimeOfDay(); |
| 67 | if (::pthread_mutex_trylock (m_pMutex) != 0) |
| 68 | { |
| 69 | 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); |
| 70 | ::pthread_mutex_lock (m_pMutex); |
| 71 | 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); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void |
| 78 | PThreadMutex::Locker::Unlock() |
| 79 | { |
| 80 | 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); |
| 81 | ::pthread_mutex_unlock (m_pMutex); |
| 82 | } |
| 83 | |
| 84 | #endif |