--- innwebd/thread_pool.cpp 2004/07/03 09:37:58 1.2 +++ innwebd/thread_pool.cpp 2004/07/03 13:55:42 1.3 @@ -15,27 +15,21 @@ #include "StdAfx.h" #include ".\thread_pool.h" -thread_pool::thread_pool(UINT uThreadMax, clock_t thread_timeout) +thread_pool::thread_pool(UINT uThreadMax) { UINT i; this->uLastErrorCode = E_NOERROR; this->uThreadCount = 0; this->uThreadMax = uThreadMax; - this->thread_timeout = thread_timeout; + this->hThreadKiller = NULL; - if (this->uThreadCount > TS_MAX_THREAD) + if (this->uThreadMax > TS_MAX_THREAD) { - this->uThreadCount = 0; + this->uThreadMax = 0; this->uLastErrorCode = E_MAX_THREAD_EXCEEDED; } - if (this->thread_timeout < 0 || this->thread_timeout > TS_SETLOCK_TIMEOUT_MAX) - { - this->thread_timeout = TS_SETLOCK_TIMEOUT; - this->uLastErrorCode = E_INVALID_TIMEOUT; - } - for (i=0; i < this->uThreadMax; i++) { this->hThreadList[i] = NULL; @@ -44,17 +38,20 @@ thread_pool::thread_pool(UINT uThreadMax } this->SetLock(false); + + this->EnableKillDeadThread(); } thread_pool::~thread_pool(void) { + this->DisableKillDeadThread(); } -int thread_pool::AddThread(HANDLE hThread) +int thread_pool::AddThread(HANDLE hThread, clock_t thread_timeout) { UINT i; - if (this->SetLock(true) == 0) + if (this->SetLock(true) != 0) { return this->uLastErrorCode; } @@ -66,12 +63,19 @@ int thread_pool::AddThread(HANDLE hThrea return E_MAX_THREAD_EXCEEDED; } + if (thread_timeout < 0) + { + this->uLastErrorCode = E_INVALID_TIMEOUT; + return E_INVALID_TIMEOUT; + } + for (i=0; i < this->uThreadMax; i++) { if (this->uThreadStatusList[i] == S_UNUSED) { this->hThreadList[i] = hThread; this->uThreadStatusList[i] = S_WAITING; + this->thread_timeout[i] = thread_timeout; this->time_status_set[i] = clock(); this->uThreadCount++; this->SetLock(false); @@ -112,7 +116,7 @@ int thread_pool::SetLock(bool bLock, clo return E_NOERROR; } -int thread_pool::GetLastError(void) +int thread_pool::GetLastError(void) const { return this->uLastErrorCode; } @@ -121,7 +125,7 @@ int thread_pool::RemoveThread(HANDLE hTh { UINT i; - if (this->SetLock(true) == 0) + if (this->SetLock(true) != 0) { return this->uLastErrorCode; } @@ -155,7 +159,7 @@ int thread_pool::SetThreadStatus(HANDLE { UINTT i; - if (this->SetLock(true) == 0) + if (this->SetLock(true) != 0) { return this->uLastErrorCode; } @@ -207,29 +211,32 @@ int thread_pool::GetThreadStatus(HANDLE return S_UNKNOWN; } -int thread_pool::KillDeadThread(void) +DWORD thread_pool::KillDeadThread(LPVOID pParam) { - UINT i; - bool bKillStatus = true; + thread_pool *p; + UINT i; + bool bKillStatus = true; - if (this->SetLock(true) == 0) + p = (thread_pool*)pParam; + + if (p->SetLock(true) != 0) { - return this->uLastErrorCode; + return p->uLastErrorCode; } - if (this->uThreadCount <= 0) + if (p->uThreadCount <= 0) { - this->SetLock(false); - this->uLastErrorCode = E_THREAD_NOT_FOUND; + p->SetLock(false); + p->uLastErrorCode = E_THREAD_NOT_FOUND; return E_THREAD_NOT_FOUND; } - for (i=0; i < this->uThreadMax; i++) + for (i=0; i < p->uThreadMax; i++) { - if ((this->uThreadStatusList[i] == S_WORKING) && - (clock() - this->time_status_set[i] > this->thread_timeout)) + if ((p->uThreadStatusList[i] == S_WORKING) && + (clock() - p->time_status_set[i] > p->thread_timeout[i])) { - if (TerminateThread(this->hThreadList[i],-1)) + if (TerminateThread(p->hThreadList[i],-1)) { bKillStatus &= true; syslog << logfile::log_head << "Terminate dead thread ... OK" << endl; @@ -239,14 +246,55 @@ int thread_pool::KillDeadThread(void) bKillStatus &= false; syslog << logfile::log_head << "Terminate dead thread ... Failed" << endl; } - this->hThreadList[i] = NULL; - this->uThreadStatusList[i] = S_UNUSED; - this->time_status_set[i] = clock(); - this->uThreadCount--; + p->hThreadList[i] = NULL; + p->uThreadStatusList[i] = S_UNUSED; + p->time_status_set[i] = clock(); + p->uThreadCount--; } } - this->SetLock(false); - this->uLastErrorCode = (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); + p->SetLock(false); + p->uLastErrorCode = (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); return (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); } + +int thread_pool::GetThreadCount(void) const +{ + return this->uThreadCount; +} + +int thread_pool::EnableKillDeadThread(void) +{ + HANDLE hThreadCurrent; + ULONG ulThreadId; + if (hThreadCurrent = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)thread_pool::KillDeadThread,(LPVOID)this,0,&ulThreadId)) + { + this->hThreadKiller = hThreadCurrent; + syslog << logfile::log_head << "Create killer thread ... OK" << endl; + } + else + { + syslog << logfile::log_head << "Create killer thread ... Failed" << endl; + return -1; + } + + return 0; +} + +int thread_pool::DisableKillDeadThread(void) +{ + if (this->hThreadKiller) + { + if (TerminateThread(this->hThreadKiller,0)) + { + this->hThreadKiller = NULL; + syslog << logfile::log_head << "Terminate killer thread ... OK" << endl; + } + else + { + syslog << logfile::log_head << "Terminate killer thread ... Failed" << endl; + return -1; + } + } + return 0; +}