blob: 075fdc9114f334038f8cecc316c1c1554d43086f [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- PThreadMutex.h ------------------------------------------*- 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 6/16/07.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef __PThreadMutex_h__
14#define __PThreadMutex_h__
15
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include <assert.h>
Kate Stoneb9c1b512016-09-06 20:57:50 +000017#include <pthread.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include <stdint.h>
19
20//#define DEBUG_PTHREAD_MUTEX_DEADLOCKS 1
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022#if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
23#define PTHREAD_MUTEX_LOCKER(var, mutex) \
24 PThreadMutex::Locker var(mutex, __FUNCTION__, __FILE__, __LINE__)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
26#else
27#define PTHREAD_MUTEX_LOCKER(var, mutex) PThreadMutex::Locker var(mutex)
28#endif
29
Kate Stoneb9c1b512016-09-06 20:57:50 +000030class PThreadMutex {
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 class Locker {
33 public:
34#if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 Locker(PThreadMutex &m, const char *function, const char *file, int line);
37 Locker(PThreadMutex *m, const char *function, const char *file, int line);
38 Locker(pthread_mutex_t *mutex, const char *function, const char *file,
39 int line);
40 ~Locker();
41 void Lock();
42 void Unlock();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
44#else
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 Locker(PThreadMutex &m) : m_pMutex(m.Mutex()) { Lock(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 Locker(PThreadMutex *m) : m_pMutex(m ? m->Mutex() : NULL) { Lock(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 Locker(pthread_mutex_t *mutex) : m_pMutex(mutex) { Lock(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 void Lock() {
52 if (m_pMutex)
53 ::pthread_mutex_lock(m_pMutex);
54 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 void Unlock() {
57 if (m_pMutex)
58 ::pthread_mutex_unlock(m_pMutex);
59 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 ~Locker() { Unlock(); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
63#endif
64
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 // unlock any the current mutex and lock the new one if it is valid
66 void Reset(pthread_mutex_t *pMutex = NULL) {
67 Unlock();
68 m_pMutex = pMutex;
69 Lock();
70 }
71 pthread_mutex_t *m_pMutex;
72#if defined(DEBUG_PTHREAD_MUTEX_DEADLOCKS)
73 const char *m_function;
74 const char *m_file;
75 int m_line;
76 uint64_t m_lock_time;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 };
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 PThreadMutex() {
81 int err;
82 err = ::pthread_mutex_init(&m_mutex, NULL);
83 assert(err == 0);
84 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 PThreadMutex(int type) {
87 int err;
88 ::pthread_mutexattr_t attr;
89 err = ::pthread_mutexattr_init(&attr);
90 assert(err == 0);
91 err = ::pthread_mutexattr_settype(&attr, type);
92 assert(err == 0);
93 err = ::pthread_mutex_init(&m_mutex, &attr);
94 assert(err == 0);
95 err = ::pthread_mutexattr_destroy(&attr);
96 assert(err == 0);
97 }
98
99 ~PThreadMutex() {
100 int err;
101 err = ::pthread_mutex_destroy(&m_mutex);
102 if (err != 0) {
103 err = Unlock();
104 if (err == 0)
105 ::pthread_mutex_destroy(&m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 pthread_mutex_t *Mutex() { return &m_mutex; }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 int Lock() { return ::pthread_mutex_lock(&m_mutex); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112
Kate Stoneb9c1b512016-09-06 20:57:50 +0000113 int Unlock() { return ::pthread_mutex_unlock(&m_mutex); }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114
115protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000116 pthread_mutex_t m_mutex;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117};
118
119#endif