1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
//===-- PThreadMutex.h ------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Created by Greg Clayton on 6/16/07.
//
//===----------------------------------------------------------------------===//
#ifndef __PThreadMutex_h__
#define __PThreadMutex_h__
#include <pthread.h>
#include <assert.h>
#include <stdint.h>
//#define DEBUG_PTHREAD_MUTEX_DEADLOCKS 1
#if defined (DEBUG_PTHREAD_MUTEX_DEADLOCKS)
#define PTHREAD_MUTEX_LOCKER(var, mutex) PThreadMutex::Locker var(mutex, __FUNCTION__, __FILE__, __LINE__)
#else
#define PTHREAD_MUTEX_LOCKER(var, mutex) PThreadMutex::Locker var(mutex)
#endif
class PThreadMutex
{
public:
class Locker
{
public:
#if defined (DEBUG_PTHREAD_MUTEX_DEADLOCKS)
Locker(PThreadMutex& m, const char *function, const char *file, int line);
Locker(PThreadMutex* m, const char *function, const char *file, int line);
Locker(pthread_mutex_t *mutex, const char *function, const char *file, int line);
~Locker();
void Lock();
void Unlock();
#else
Locker(PThreadMutex& m) :
m_pMutex(m.Mutex())
{
Lock();
}
Locker(PThreadMutex* m) :
m_pMutex(m ? m->Mutex() : NULL)
{
Lock();
}
Locker(pthread_mutex_t *mutex) :
m_pMutex(mutex)
{
Lock();
}
void Lock()
{
if (m_pMutex)
::pthread_mutex_lock (m_pMutex);
}
void Unlock()
{
if (m_pMutex)
::pthread_mutex_unlock (m_pMutex);
}
~Locker()
{
Unlock();
}
#endif
// unlock any the current mutex and lock the new one if it is valid
void Reset(pthread_mutex_t *pMutex = NULL)
{
Unlock();
m_pMutex = pMutex;
Lock();
}
pthread_mutex_t *m_pMutex;
#if defined (DEBUG_PTHREAD_MUTEX_DEADLOCKS)
const char *m_function;
const char *m_file;
int m_line;
uint64_t m_lock_time;
#endif
};
PThreadMutex()
{
int err;
err = ::pthread_mutex_init (&m_mutex, NULL); assert(err == 0);
}
PThreadMutex(int type)
{
int err;
::pthread_mutexattr_t attr;
err = ::pthread_mutexattr_init (&attr); assert(err == 0);
err = ::pthread_mutexattr_settype (&attr, type); assert(err == 0);
err = ::pthread_mutex_init (&m_mutex, &attr); assert(err == 0);
err = ::pthread_mutexattr_destroy (&attr); assert(err == 0);
}
~PThreadMutex()
{
int err;
err = ::pthread_mutex_destroy (&m_mutex);
if (err != 0)
{
err = Unlock();
if (err == 0)
::pthread_mutex_destroy (&m_mutex);
}
}
pthread_mutex_t *Mutex()
{
return &m_mutex;
}
int Lock()
{
return ::pthread_mutex_lock (&m_mutex);
}
int Unlock()
{
return ::pthread_mutex_unlock (&m_mutex);
}
protected:
pthread_mutex_t m_mutex;
};
#endif
|