blob: 32db862f6b420847b6e836ab3b3da458dd9ffb0d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
Kate Stoneb9c1b512016-09-06 20:57:50 +000024PThreadMutex::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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029}
30
Kate Stoneb9c1b512016-09-06 20:57:50 +000031PThreadMutex::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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036}
37
Kate Stoneb9c1b512016-09-06 20:57:50 +000038PThreadMutex::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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045PThreadMutex::Locker::~Locker() { Unlock(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047void 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061}
62
Kate Stoneb9c1b512016-09-06 20:57:50 +000063void 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);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069}
70
71#endif