ThreadPool.cpp
7.02 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
#include <windows.h>
#include "ThreadPool.h"
#include <process.h>
#include <assert.h>
#include "ResgenLogCAPI.h"
#define THREADP_TAG "THREADPOLL"
#define THREADP_LOG_D(format, args...) RES_LOG_D((THREADP_TAG), (format) , ##args)
#define THREADP_LOG_V(format, args...) RES_LOG_V((THREADP_TAG), (format) , ##args)
#define THREADP_LOG_W(format, args...) RES_LOG_W((THREADP_TAG), (format) , ##args)
#define THREADP_LOG_E(format, args...) RES_LOG_E((THREADP_TAG), (format) , ##args)
ThreadPool::ThreadPool(int poolSize, int waitTimeForThreadsToCompleteMS)
{
m_nWaitForThreadsToDieMS = waitTimeForThreadsToCompleteMS;
m_hNotifyShutdown = NULL;
InitializeCriticalSection(&m_cs);
if (poolSize > 0 && !Create(poolSize))
throw "Unable to create thread pool";
}
ThreadPool::~ThreadPool()
{
Destroy();
DeleteCriticalSection(&m_cs);
}
bool ThreadPool::Create(int poolSize)
{
Destroy();
if (poolSize == 0)
return true;
// create the event which will signal the threads to stop
m_hNotifyShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
assert(m_hNotifyShutdown != NULL);
if (m_hNotifyShutdown == NULL)
return false;
// create the threads
for (int nIndex = 0; nIndex < poolSize; ++nIndex)
{
UINT uThreadId;
HANDLE hThread = (HANDLE)_beginthreadex(
NULL, 0, ThreadPool::ThreadProc, this,
CREATE_SUSPENDED, (UINT*)&uThreadId);
DWORD dwThreadId = uThreadId;
assert(INVALID_HANDLE_VALUE != hThread);
if (hThread == INVALID_HANDLE_VALUE){
THREADP_LOG_E("Thread %d creation failed in ThreadPool::Create()", nIndex);
return false;
}
// add the entry to the thread list
ThreadData td;
td.hWait = CreateEvent(NULL, TRUE, FALSE, NULL);
td.hThread = hThread;
td.dwThreadId = dwThreadId;
THREADP_LOG_D("Create Thread, ID = %d", td.dwThreadId);
m_threads.push_back(td);
ResumeThread(hThread);
}
return true;
}
void ThreadPool::Destroy()
{
// Never created?
if (!m_hNotifyShutdown)
return;
// tell all threads to shutdown.
SetEvent(m_hNotifyShutdown);
int timElapsed = 0;
while (GetWorkingThreadCount() > 0 && timElapsed < m_nWaitForThreadsToDieMS)
{
Sleep(100); // give the threads a chance to complete
timElapsed += 100;
}
// walk through the events and threads and close them all
for (ThreadList::iterator iter = m_threads.begin(); iter != m_threads.end(); ++iter)
{
THREADP_LOG_D("Close hWait handle: 0x%x, thread ID = %d", iter->hWait, iter->dwThreadId);
if(CloseHandle(iter->hWait)== 0){
THREADP_LOG_E("Cannnot close hWait handle, error = 0x%x", GetLastError());
}
THREADP_LOG_D("Close hThread handle: 0x%x, thread ID = %d", iter->hThread, iter->dwThreadId);
if(CloseHandle(iter->hThread)==0){
THREADP_LOG_E("Cannnot close hThread handle, error = 0x%x", GetLastError());
}
}
// close the shutdown event
CloseHandle(m_hNotifyShutdown);
m_hNotifyShutdown = NULL;
// free any jobs not released
for (JobList::iterator funcIter = m_jobList.begin();
funcIter != m_jobList.end(); funcIter++)
if (funcIter->runObject && funcIter->runObject->AutoDelete())
delete funcIter->runObject;
// empty all collections
m_jobList.clear();
m_threads.clear();
}
int ThreadPool::GetWorkingThreadCount()
{
int nCount = 0;
EnterCriticalSection(&m_cs);
for (ThreadList::iterator iter = m_threads.begin(); iter != m_threads.end(); ++iter)
if (WaitForSingleObject(iter->hWait, 0) == WAIT_OBJECT_0)
++nCount;
LeaveCriticalSection(&m_cs);
return nCount;
}
void ThreadPool::AddJobToQueue(
LPTHREAD_START_ROUTINE pFunc, LPVOID pData,
IRunObject* runObject, ThreadPriority priority)
{
assert(pFunc != NULL || runObject != NULL);
JobData jobData;
jobData.lpStartAddress = pFunc;
jobData.pData = pData;
jobData.runObject = runObject;
// add it to the list
EnterCriticalSection(&m_cs);
{
if (priority == Low)
m_jobList.push_back(jobData);
else
m_jobList.push_front(jobData);
// See if any threads are free
for (ThreadList::iterator iter = m_threads.begin(); iter != m_threads.end(); ++iter)
{
if (WaitForSingleObject(iter->hWait, 0) == WAIT_TIMEOUT)
{
// here is a free thread, wake it up
SetEvent(iter->hWait);
break;
}
}
}
LeaveCriticalSection(&m_cs);
}
// Called by the worker thread to get its wake-up event handle
HANDLE ThreadPool::GetWaitHandle(DWORD dwThreadId)
{
HANDLE hWait = NULL;
EnterCriticalSection(&m_cs);
for (ThreadList::iterator iter = m_threads.begin(); !hWait && iter != m_threads.end(); ++iter)
if (iter->dwThreadId == dwThreadId)
hWait = iter->hWait;
LeaveCriticalSection(&m_cs);
return hWait;
}
// static method:
// This is the worker thread function which will run
// continuously till the Thread Pool is deleted.
UINT __stdcall ThreadPool::ThreadProc(LPVOID pParam)
{
ThreadPool* pool = static_cast<ThreadPool*>(pParam);
assert(NULL != pool);
if (pool == NULL)
return 1;
DWORD dwThreadId = GetCurrentThreadId();
HANDLE hWaits[2] = { pool->GetWaitHandle(dwThreadId), pool->m_hNotifyShutdown };
bool bContinue = true;
while (bContinue)
{
DWORD waitResult = WaitForMultipleObjects(2, hWaits, FALSE, INFINITE);
if (waitResult == WAIT_FAILED)
throw "Wait failed";
if (waitResult == WAIT_OBJECT_0 || waitResult == WAIT_OBJECT_0 + 1)
{
// Shutting down?
if (pool->CheckThreadStop())
break;
// We have been woken up. Get the next available job
EnterCriticalSection(&pool->m_cs);
JobList::iterator iter = pool->m_jobList.begin();
if (iter == pool->m_jobList.end())
{
// No job available, go back to sleep
ResetEvent(hWaits[0]);
LeaveCriticalSection(&pool->m_cs);
continue;
}
else
{
// Get the job and remove it from the queue
LPTHREAD_START_ROUTINE proc = iter->lpStartAddress;
LPVOID data = iter->pData;
IRunObject* runObject = iter->runObject;
pool->m_jobList.pop_front();
LeaveCriticalSection(&pool->m_cs);
// Run the job
if (proc)
proc(data);
else if (runObject)
{
bool autoDelete = runObject->AutoDelete();
runObject->Run(); // may execute "delete this"
if (autoDelete)
delete runObject;
}
}
}
}
// Thread is shutting down
ResetEvent(hWaits[0]);
return 0;
}