--- innwebd/thread_pool.cpp 2004/07/03 09:37:58 1.2 +++ innwebd/thread_pool.cpp 2004/07/03 15:10:23 1.4 @@ -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,46 +211,102 @@ 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; + int t_count = 0; - if (this->SetLock(true) == 0) + p = (thread_pool*)pParam; + + while(!p->bTerminateThreadKiller) { - return this->uLastErrorCode; + if (t_count>=100) + { + t_count=0; + + if (p->SetLock(true) == 0) + { + for (i=0; i < p->uThreadMax; i++) + { + if ((p->hThreadList[i] != NULL) && + (p->uThreadStatusList[i] == S_WORKING) && + (clock() - p->time_status_set[i] > p->thread_timeout[i])) + { + if (TerminateThread(p->hThreadList[i],-1)) + { + syslog << logfile::log_head << "Terminate dead thread ... OK" << endl; + } + else + { + syslog << logfile::log_head << "Terminate dead thread ... Failed" << endl; + } + p->hThreadList[i] = NULL; + p->uThreadStatusList[i] = S_UNUSED; + p->time_status_set[i] = clock(); + p->uThreadCount--; + } + } + } + + p->SetLock(false); + } + + Sleep(100); + t_count++; } - if (this->uThreadCount <= 0) + return (E_NOERROR); +} + +int thread_pool::GetThreadCount(void) const +{ + return this->uThreadCount; +} + +int thread_pool::EnableKillDeadThread(void) +{ + HANDLE hThreadCurrent; + ULONG ulThreadId; + + this->bTerminateThreadKiller = false; + if (hThreadCurrent = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)thread_pool::KillDeadThread,(LPVOID)this,0,&ulThreadId)) { - this->SetLock(false); - this->uLastErrorCode = E_THREAD_NOT_FOUND; - return E_THREAD_NOT_FOUND; + this->hThreadKiller = hThreadCurrent; + syslog << logfile::log_head << "Create killer thread ... OK" << endl; + } + else + { + syslog << logfile::log_head << "Create killer thread ... Failed" << endl; + return -1; } - for (i=0; i < this->uThreadMax; i++) + return 0; +} + +int thread_pool::DisableKillDeadThread(void) +{ + bTerminateThreadKiller = true; + + Sleep(1000); + + if (!bTerminateThreadKiller) { - if ((this->uThreadStatusList[i] == S_WORKING) && - (clock() - this->time_status_set[i] > this->thread_timeout)) + syslog << logfile::log_head << "Terminate killer thread ... OK" << endl; + } + + if (bTerminateThreadKiller && this->hThreadKiller) + { + if (TerminateThread(this->hThreadKiller,0)) { - if (TerminateThread(this->hThreadList[i],-1)) - { - bKillStatus &= true; - syslog << logfile::log_head << "Terminate dead thread ... OK" << endl; - } - else - { - 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--; + this->hThreadKiller = NULL; + syslog << logfile::log_head << "Terminate killer thread ... OK" << endl; + } + else + { + syslog << logfile::log_head << "Terminate killer thread ... Failed" << endl; + return -1; } } - - this->SetLock(false); - this->uLastErrorCode = (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); - return (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); + return 0; }