-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathDeadLockDetectMutex.cpp
More file actions
176 lines (151 loc) · 4.61 KB
/
DeadLockDetectMutex.cpp
File metadata and controls
176 lines (151 loc) · 4.61 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SipPlatformDefine.h"
#include "DeadLockDetectMutex.h"
#include "Log.h"
#ifdef WIN32
#define THREAD_ID_FORMAT "%u"
#else
#define THREAD_ID_FORMAT "%lu"
#endif
typedef std::map< CDeadLockDetectMutex *, int > MUTEX_POINTER_MAP;
/**
* @ingroup SipPlatform
* @brief 뮤텍스의 포인터를 저장하는 클래스
*/
class CMutexPointerMap
{
public:
MUTEX_POINTER_MAP m_clsMap;
};
static CSipMutex * gpclsMapMutex = NULL;
static CMutexPointerMap * gpclsMutexPointMap = NULL;
CDeadLockDetectMutex::CDeadLockDetectMutex()
{
if( gpclsMapMutex == NULL )
{
gpclsMapMutex = new CSipMutex();
}
if( gpclsMapMutex )
{
gpclsMapMutex->acquire();
if( gpclsMutexPointMap == NULL )
{
gpclsMutexPointMap = new CMutexPointerMap();
}
if( gpclsMutexPointMap )
{
gpclsMutexPointMap->m_clsMap.insert( MUTEX_POINTER_MAP::value_type( this, 1 ) );
}
gpclsMapMutex->release();
}
}
CDeadLockDetectMutex::~CDeadLockDetectMutex()
{
if( gpclsMapMutex )
{
MUTEX_POINTER_MAP::iterator itMap;
gpclsMapMutex->acquire();
itMap = gpclsMutexPointMap->m_clsMap.find( this );
if( itMap != gpclsMutexPointMap->m_clsMap.end() )
{
gpclsMutexPointMap->m_clsMap.erase( itMap );
}
gpclsMapMutex->release();
}
}
/**
* @ingroup SipPlatform
* @brief mutex lock 한다.
* @returns mutex lock 에 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CDeadLockDetectMutex::acquire()
{
THREAD_ID_LIST::iterator itTL;
THREAD_ID_TYPE iThreadId = GetCurrentThreadId();
m_clsListMutex.acquire();
// Window CRITICAL_SECTION 은 하나의 쓰레드에서 2번 이상 lock 을 허용하므로 현재 쓰레드에 대한 dead lock 검사를 수행하지 않는다.
#ifndef WIN32
for( itTL = m_clsThreadIdList.begin(); itTL != m_clsThreadIdList.end(); ++itTL )
{
if( *itTL == iThreadId )
{
CLog::Print( LOG_ERROR, "dead lock in single thread" );
CLog::PrintCallStack( LOG_ERROR );
break;
}
}
#endif
m_clsThreadIdList.push_back( iThreadId );
if( m_clsThreadIdList.size() >= 2 && gpclsMapMutex && gpclsMutexPointMap )
{
// 2개의 쓰레드에서 dead lock 되었는지 검사한다.
MUTEX_POINTER_MAP::iterator itMap;
THREAD_ID_LIST::iterator itCTL;
gpclsMapMutex->acquire();
for( itMap = gpclsMutexPointMap->m_clsMap.begin(); itMap != gpclsMutexPointMap->m_clsMap.end(); ++itMap )
{
// 현재 mutex 에 대해서는 검사하지 않는다.
if( itMap->first == this ) continue;
// 다른 mutex 에서 현재 thread 의 lock 다음에 존재하는 thread 중에서 현재 mutex 에 존재하는 쓰레드가 존재하면 dead lock 이다.
bool bFoundMe = false;
for( itTL = itMap->first->m_clsThreadIdList.begin(); itTL != itMap->first->m_clsThreadIdList.end(); ++itTL )
{
if( *itTL == iThreadId )
{
bFoundMe = true;
}
else if( bFoundMe )
{
for( itCTL = m_clsThreadIdList.begin(); itCTL != m_clsThreadIdList.end(); ++itCTL )
{
if( *itCTL == *itTL )
{
CLog::Print( LOG_ERROR, "dead lock in multi thread - thread#1(" THREAD_ID_FORMAT ") thread#2(" THREAD_ID_FORMAT ")", iThreadId, *itTL );
CLog::PrintCallStack( LOG_ERROR );
}
}
}
}
}
gpclsMapMutex->release();
}
m_clsListMutex.release();
return m_clsMutex.acquire();
}
/**
* @ingroup SipPlatform
* @brief mutex unlock 한다.
* @returns mutex unlock 에 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CDeadLockDetectMutex::release()
{
THREAD_ID_LIST::reverse_iterator itTL;
THREAD_ID_TYPE iThreadId = GetCurrentThreadId();
m_clsListMutex.acquire();
for( itTL = m_clsThreadIdList.rbegin(); itTL != m_clsThreadIdList.rend(); ++itTL )
{
if( *itTL == iThreadId )
{
m_clsThreadIdList.erase( --itTL.base() );
break;
}
}
m_clsListMutex.release();
return m_clsMutex.release();
}