]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/debugserver/source/PThreadMutex.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / tools / debugserver / source / PThreadMutex.cpp
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,
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();
29 }
30
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();
36 }
37
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();
43 }
44
45 PThreadMutex::Locker::~Locker() { Unlock(); }
46
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);
59     }
60   }
61 }
62
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);
69 }
70
71 #endif