-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThread.cpp
More file actions
262 lines (225 loc) · 7.17 KB
/
Thread.cpp
File metadata and controls
262 lines (225 loc) · 7.17 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "Base.h"
#include "Thread.h"
#include "ThreadUtils.h"
#include "ExceptionUtils.h"
#ifndef _WIN32
#include "TimeUtils.h"
#endif
NS_BEGIN
std::unordered_map<size_t, PCNSTR, CNumberHashTraits<size_t>> CThread::s_mapThreads;
std::mutex CThread::s_mutexThreads;
//#################################################################################################
CThread::CThread(const CStr &strThreadName)
: m_strThreadName(strThreadName),
m_pfnProc(nullptr),
m_pParam(nullptr),
m_bActive(false)
{
}
//#################################################################################################
CThread::~CThread(void)
{
Wait();
}
//#################################################################################################
bool CThread::Start(void)
{
bool bStarted = false;
std::lock_guard<std::mutex> lock(m_mutexThread);
if(!m_pThread)
{
m_pThread = std::make_unique<std::thread>(InternalThreadProc, this);
m_bActive = true;
m_event.Signal(); // Signal the thread that m_pThread has finished initialization
bStarted = true;
}
return bStarted;
}
//#################################################################################################
bool CThread::Start(PFNPROC pfnProc, PVOID pParam)
{
bool bStarted = false;
std::lock_guard<std::mutex> lock(m_mutexThread);
if(!m_pThread && pfnProc)
{
m_pfnProc = pfnProc;
m_pParam = pParam;
m_pThread = std::make_unique<std::thread>(InternalThreadProc, this);
m_bActive = true;
m_event.Signal(); // Signal the thread that m_pThread has finished initialization
bStarted = true;
}
return bStarted;
}
//#################################################################################################
void CThread::Wait(void)
{
std::lock_guard<std::mutex> lock(m_mutexThread);
if(m_pThread && m_pThread->joinable())
m_pThread->join();
}
//#################################################################################################
void CThread::Reset(void)
{
std::lock_guard<std::mutex> lock(m_mutexThread);
if(m_pThread)
{
if(m_pThread->joinable())
m_pThread->join();
m_pThread.reset();
m_event.Reset();
}
}
//#################################################################################################
const CStr &CThread::GetName(void) const noexcept
{
return m_strThreadName;
}
//#################################################################################################
std::thread::native_handle_type CThread::GetHandle(void) const
{
std::lock_guard<std::mutex> lock(m_mutexThread);
#ifndef __linux__
return (m_pThread) ? m_pThread->native_handle() : nullptr;
#else
return (m_pThread) ? m_pThread->native_handle() : 0;
#endif
}
//#################################################################################################
std::thread::id CThread::GetId(void) const
{
std::lock_guard<std::mutex> lock(m_mutexThread);
return (m_pThread) ? m_pThread->get_id() : std::thread::id();
}
//#################################################################################################
bool CThread::IsActive(void) const noexcept
{
return m_bActive;
}
//#################################################################################################
PCNSTR CThread::GetThreadName(const std::thread::id &id)
{
PCNSTR szThreadName = nullptr;
size_t nThreadId = GetThreadId(id);
std::unique_lock<std::mutex> lock(s_mutexThreads);
auto itr = s_mapThreads.find(nThreadId);
if(itr != s_mapThreads.end())
szThreadName = itr->second;
lock.unlock();
return szThreadName;
}
//#################################################################################################
bool CThread::MonitorThread(const CStr &strThreadName, PFNPROC pfnProc, PVOID pParam)
{
bool bMonitored = false;
if(pfnProc == nullptr)
return bMonitored;
size_t nThreadId = GetThreadId();
bMonitored = AddThread(nThreadId, strThreadName);
if(bMonitored)
{
#ifdef _WIN32
__try{
SetThreadExceptionHandlers();
#else
try{
#endif
pfnProc(pParam);
#ifdef _WIN32
}
__except(CreateMiniDump(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER)
{
throw std::exception(); // Throw an exception to ensure the program crashes
}
#else
}
catch(const std::exception &e)
{
CStr8 strDetails = "Unhandled standard exception! Time: " + GetFormattedTime() + ", Thread: " + strThreadName + ", Details: " + e.what() + g_szEol;
CreateMiniDump(strDetails);
throw; // Rethrow the exception to ensure the program crashes
}
catch(...)
{
CStr8 strDetails = "Unhandled exception! Time: " + GetFormattedTime() + ", Thread: " + strThreadName + g_szEol;
CreateMiniDump(strDetails);
throw; // Rethrow the exception to ensure the program crashes
}
#endif
RemoveThread(nThreadId);
}
return bMonitored;
}
//#################################################################################################
void CThread::ThreadProc(void)
{
}
//#################################################################################################
bool CThread::AddThread(const size_t nThreadId, PCNSTR szThreadName)
{
bool bAdded = false;
std::unique_lock<std::mutex> lock(s_mutexThreads);
auto itr = s_mapThreads.find(nThreadId);
if(itr == s_mapThreads.end())
{
s_mapThreads[nThreadId] = szThreadName;
bAdded = true;
}
lock.unlock();
return bAdded;
}
//#################################################################################################
void CThread::RemoveThread(const size_t nThreadId)
{
std::lock_guard<std::mutex> lock(s_mutexThreads);
auto itr = s_mapThreads.find(nThreadId);
if(itr != s_mapThreads.end())
s_mapThreads.erase(itr);
}
//#################################################################################################
void CThread::InternalThreadProc(CThread *pThis)
{
if(pThis)
{
pThis->m_event.Wait(); // Wait for the spawning thread to finish initialization of the m_pThread object
size_t nThreadId = GetThreadId();
PCNSTR szThreadName = pThis->GetName();
AddThread(nThreadId, szThreadName);
#ifdef _WIN32
__try{
SetThreadExceptionHandlers();
#else
try{
#endif
if(pThis->m_pfnProc)
pThis->m_pfnProc(pThis->m_pParam);
else
pThis->ThreadProc();
#ifdef _WIN32
}
__except(CreateMiniDump(GetExceptionInformation()), EXCEPTION_EXECUTE_HANDLER)
{
throw std::exception(); // Throw an exception to ensure the program crashes
}
#else
}
catch(const std::exception &e)
{
CStr8 strDetails = "Unhandled standard exception! Time: " + GetFormattedTime() + ", Thread: " + pThis->m_strThreadName + ", Details: " + e.what() + g_szEol;
CreateMiniDump(strDetails);
std::cerr << strDetails;
throw; // Rethrow the exception to ensure the program crashes
}
catch(...)
{
CStr8 strDetails = "Unhandled exception! Time: " + GetFormattedTime() + ", Thread: " + pThis->m_strThreadName + g_szEol;
CreateMiniDump(strDetails);
std::cerr << strDetails;
throw; // Rethrow the exception to ensure the program crashes
}
#endif
RemoveThread(nThreadId);
pThis->m_bActive = false;
}
}
NS_END