blob: 118921aee5a817c20973a327b63f2fd2295723f4 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- PThreadMutex.cpp ----------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Created by Greg Clayton on 12/9/08.
10//
11//===----------------------------------------------------------------------===//
12
13#include "PThreadMutex.h"
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "DNBTimer.h"
16
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
Kate Stoneb9c1b512016-09-06 20:57:50 +000019PThreadMutex::Locker::Locker(PThreadMutex &m, const char *function,
20 const char *file, const int line)
21 : m_pMutex(m.Mutex()), m_function(function), m_file(file), m_line(line),
22 m_lock_time(0) {
23 Lock();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024}
25
Kate Stoneb9c1b512016-09-06 20:57:50 +000026PThreadMutex::Locker::Locker(PThreadMutex *m, const char *function,
27 const char *file, const int line)
28 : m_pMutex(m ? m->Mutex() : NULL), m_function(function), m_file(file),
29 m_line(line), m_lock_time(0) {
30 Lock();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031}
32
Kate Stoneb9c1b512016-09-06 20:57:50 +000033PThreadMutex::Locker::Locker(pthread_mutex_t *mutex, const char *function,
34 const char *file, const int line)
35 : m_pMutex(mutex), m_function(function), m_file(file), m_line(line),
36 m_lock_time(0) {
37 Lock();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038}
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040PThreadMutex::Locker::~Locker() { Unlock(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
Kate Stoneb9c1b512016-09-06 20:57:50 +000042void PThreadMutex::Locker::Lock() {
43 if (m_pMutex) {
44 m_lock_time = DNBTimer::GetTimeOfDay();
45 if (::pthread_mutex_trylock(m_pMutex) != 0) {
46 fprintf(stdout, "::pthread_mutex_trylock (%8.8p) mutex is locked "
47 "(function %s in %s:%i), waiting...\n",
48 m_pMutex, m_function, m_file, m_line);
49 ::pthread_mutex_lock(m_pMutex);
50 fprintf(stdout, "::pthread_mutex_lock (%8.8p) succeeded after %6llu "
51 "usecs (function %s in %s:%i)\n",
52 m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function,
53 m_file, m_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058void PThreadMutex::Locker::Unlock() {
59 fprintf(stdout, "::pthread_mutex_unlock (%8.8p) had lock for %6llu usecs in "
60 "%s in %s:%i\n",
61 m_pMutex, DNBTimer::GetTimeOfDay() - m_lock_time, m_function, m_file,
62 m_line);
63 ::pthread_mutex_unlock(m_pMutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
66#endif