1 //===-- PThreadMutex.cpp ----------------------------------------*- C++ -*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // Created by Greg Clayton on 12/9/08.
11 //===----------------------------------------------------------------------===//
13 #include "PThreadMutex.h"
17 #if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
19 PThreadMutex::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
),
26 PThreadMutex::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) {
33 PThreadMutex::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
),
40 PThreadMutex::Locker::~Locker() { Unlock(); }
42 void PThreadMutex::Locker::Lock() {
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
,
58 void PThreadMutex::Locker::Unlock() {
59 fprintf(stdout
, "::pthread_mutex_unlock (%8.8p) had lock for %6llu usecs in "
61 m_pMutex
, DNBTimer::GetTimeOfDay() - m_lock_time
, m_function
, m_file
,
63 ::pthread_mutex_unlock(m_pMutex
);