| 15 |
#include "StdAfx.h" |
#include "StdAfx.h" |
| 16 |
#include ".\thread_pool.h" |
#include ".\thread_pool.h" |
| 17 |
|
|
| 18 |
thread_pool::thread_pool(UINT uThreadMax, double thread_timeout) |
thread_pool::thread_pool(UINT uThreadMax, clock_t thread_timeout) |
| 19 |
{ |
{ |
| 20 |
UINT i; |
UINT i; |
| 21 |
|
|
| 24 |
this->uThreadMax = uThreadMax; |
this->uThreadMax = uThreadMax; |
| 25 |
this->thread_timeout = thread_timeout; |
this->thread_timeout = thread_timeout; |
| 26 |
|
|
| 27 |
|
if (this->uThreadCount > TS_MAX_THREAD) |
| 28 |
|
{ |
| 29 |
|
this->uThreadCount = 0; |
| 30 |
|
this->uLastErrorCode = E_MAX_THREAD_EXCEEDED; |
| 31 |
|
} |
| 32 |
|
|
| 33 |
|
if (this->thread_timeout < 0 || this->thread_timeout > TS_SETLOCK_TIMEOUT_MAX) |
| 34 |
|
{ |
| 35 |
|
this->thread_timeout = TS_SETLOCK_TIMEOUT; |
| 36 |
|
this->uLastErrorCode = E_INVALID_TIMEOUT; |
| 37 |
|
} |
| 38 |
|
|
| 39 |
for (i=0; i < this->uThreadMax; i++) |
for (i=0; i < this->uThreadMax; i++) |
| 40 |
{ |
{ |
| 41 |
this->hThreadList[i] = NULL; |
this->hThreadList[i] = NULL; |
| 42 |
this->uThreadStatusList[i] = S_UNUSED; |
this->uThreadStatusList[i] = S_UNUSED; |
| 43 |
this->time_status_keep[i] = 0; |
this->time_status_set[i] = clock(); |
| 44 |
} |
} |
| 45 |
|
|
| 46 |
this->SetLock(false); |
this->SetLock(false); |
| 48 |
|
|
| 49 |
thread_pool::~thread_pool(void) |
thread_pool::~thread_pool(void) |
| 50 |
{ |
{ |
|
|
|
| 51 |
} |
} |
| 52 |
|
|
| 53 |
int thread_pool::AddThread(HANDLE hThread) |
int thread_pool::AddThread(HANDLE hThread) |
| 71 |
if (this->uThreadStatusList[i] == S_UNUSED) |
if (this->uThreadStatusList[i] == S_UNUSED) |
| 72 |
{ |
{ |
| 73 |
this->hThreadList[i] = hThread; |
this->hThreadList[i] = hThread; |
| 74 |
|
this->uThreadStatusList[i] = S_WAITING; |
| 75 |
|
this->time_status_set[i] = clock(); |
| 76 |
this->uThreadCount++; |
this->uThreadCount++; |
| 77 |
this->SetLock(false); |
this->SetLock(false); |
| 78 |
this->uLastErrorCode = E_NOERROR; |
this->uLastErrorCode = E_NOERROR; |
| 85 |
return E_MAX_THREAD_EXCEEDED; |
return E_MAX_THREAD_EXCEEDED; |
| 86 |
} |
} |
| 87 |
|
|
| 88 |
int thread_pool::SetLock(bool bLock, time_t tTimeout) |
int thread_pool::SetLock(bool bLock, clock_t tTimeout) |
| 89 |
{ |
{ |
| 90 |
time_t wait = 0; |
clock_t wait = 0; |
| 91 |
|
|
| 92 |
if (!bLock) // Free lock |
if (!bLock) // Free lock |
| 93 |
{ |
{ |
| 112 |
return E_NOERROR; |
return E_NOERROR; |
| 113 |
} |
} |
| 114 |
|
|
| 115 |
UINT thread_pool::GetLastError(void) |
int thread_pool::GetLastError(void) |
| 116 |
{ |
{ |
| 117 |
return this->uLastErrorCode; |
return this->uLastErrorCode; |
| 118 |
} |
} |
| 151 |
} |
} |
| 152 |
|
|
| 153 |
|
|
| 154 |
int thread_pool::SetThreadStatus(HANDLE hThread, UINT uStatus) |
int thread_pool::SetThreadStatus(HANDLE hThread, int uStatus) |
| 155 |
{ |
{ |
| 156 |
UINTT i; |
UINTT i; |
| 157 |
|
|
| 172 |
if (this->hThreadList[i] == hThread) |
if (this->hThreadList[i] == hThread) |
| 173 |
{ |
{ |
| 174 |
this->uThreadStatusList[i] = (thread_status)uStatus; |
this->uThreadStatusList[i] = (thread_status)uStatus; |
| 175 |
|
this->time_status_set[i] = clock(); |
| 176 |
this->SetLock(false); |
this->SetLock(false); |
| 177 |
this->uLastErrorCode = E_NOERROR; |
this->uLastErrorCode = E_NOERROR; |
| 178 |
return this->uLastErrorCode; |
return this->uLastErrorCode; |
| 184 |
return E_THREAD_NOT_FOUND; |
return E_THREAD_NOT_FOUND; |
| 185 |
} |
} |
| 186 |
|
|
| 187 |
UINT thread_pool::GetThreadStatus(HANDLE hThread) |
int thread_pool::GetThreadStatus(HANDLE hThread) |
| 188 |
{ |
{ |
| 189 |
UINT i; |
UINT i; |
| 190 |
|
|
| 206 |
this->uLastErrorCode = E_THREAD_NOT_FOUND; |
this->uLastErrorCode = E_THREAD_NOT_FOUND; |
| 207 |
return S_UNKNOWN; |
return S_UNKNOWN; |
| 208 |
} |
} |
| 209 |
|
|
| 210 |
|
int thread_pool::KillDeadThread(void) |
| 211 |
|
{ |
| 212 |
|
UINT i; |
| 213 |
|
bool bKillStatus = true; |
| 214 |
|
|
| 215 |
|
if (this->SetLock(true) == 0) |
| 216 |
|
{ |
| 217 |
|
return this->uLastErrorCode; |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
if (this->uThreadCount <= 0) |
| 221 |
|
{ |
| 222 |
|
this->SetLock(false); |
| 223 |
|
this->uLastErrorCode = E_THREAD_NOT_FOUND; |
| 224 |
|
return E_THREAD_NOT_FOUND; |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
for (i=0; i < this->uThreadMax; i++) |
| 228 |
|
{ |
| 229 |
|
if ((this->uThreadStatusList[i] == S_WORKING) && |
| 230 |
|
(clock() - this->time_status_set[i] > this->thread_timeout)) |
| 231 |
|
{ |
| 232 |
|
if (TerminateThread(this->hThreadList[i],-1)) |
| 233 |
|
{ |
| 234 |
|
bKillStatus &= true; |
| 235 |
|
syslog << logfile::log_head << "Terminate dead thread ... OK" << endl; |
| 236 |
|
} |
| 237 |
|
else |
| 238 |
|
{ |
| 239 |
|
bKillStatus &= false; |
| 240 |
|
syslog << logfile::log_head << "Terminate dead thread ... Failed" << endl; |
| 241 |
|
} |
| 242 |
|
this->hThreadList[i] = NULL; |
| 243 |
|
this->uThreadStatusList[i] = S_UNUSED; |
| 244 |
|
this->time_status_set[i] = clock(); |
| 245 |
|
this->uThreadCount--; |
| 246 |
|
} |
| 247 |
|
} |
| 248 |
|
|
| 249 |
|
this->SetLock(false); |
| 250 |
|
this->uLastErrorCode = (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); |
| 251 |
|
return (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); |
| 252 |
|
} |