--- innwebd/thread_pool.cpp 2004/07/03 08:38:54 1.1 +++ innwebd/thread_pool.cpp 2004/07/03 09:37:58 1.2 @@ -15,7 +15,7 @@ #include "StdAfx.h" #include ".\thread_pool.h" -thread_pool::thread_pool(UINT uThreadMax, double thread_timeout) +thread_pool::thread_pool(UINT uThreadMax, clock_t thread_timeout) { UINT i; @@ -24,11 +24,23 @@ thread_pool::thread_pool(UINT uThreadMax this->uThreadMax = uThreadMax; this->thread_timeout = thread_timeout; + if (this->uThreadCount > TS_MAX_THREAD) + { + this->uThreadCount = 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; this->uThreadStatusList[i] = S_UNUSED; - this->time_status_keep[i] = 0; + this->time_status_set[i] = clock(); } this->SetLock(false); @@ -36,7 +48,6 @@ thread_pool::thread_pool(UINT uThreadMax thread_pool::~thread_pool(void) { - } int thread_pool::AddThread(HANDLE hThread) @@ -60,6 +71,8 @@ int thread_pool::AddThread(HANDLE hThrea if (this->uThreadStatusList[i] == S_UNUSED) { this->hThreadList[i] = hThread; + this->uThreadStatusList[i] = S_WAITING; + this->time_status_set[i] = clock(); this->uThreadCount++; this->SetLock(false); this->uLastErrorCode = E_NOERROR; @@ -72,9 +85,9 @@ int thread_pool::AddThread(HANDLE hThrea return E_MAX_THREAD_EXCEEDED; } -int thread_pool::SetLock(bool bLock, time_t tTimeout) +int thread_pool::SetLock(bool bLock, clock_t tTimeout) { - time_t wait = 0; + clock_t wait = 0; if (!bLock) // Free lock { @@ -99,7 +112,7 @@ int thread_pool::SetLock(bool bLock, tim return E_NOERROR; } -UINT thread_pool::GetLastError(void) +int thread_pool::GetLastError(void) { return this->uLastErrorCode; } @@ -138,7 +151,7 @@ int thread_pool::RemoveThread(HANDLE hTh } -int thread_pool::SetThreadStatus(HANDLE hThread, UINT uStatus) +int thread_pool::SetThreadStatus(HANDLE hThread, int uStatus) { UINTT i; @@ -159,6 +172,7 @@ int thread_pool::SetThreadStatus(HANDLE if (this->hThreadList[i] == hThread) { this->uThreadStatusList[i] = (thread_status)uStatus; + this->time_status_set[i] = clock(); this->SetLock(false); this->uLastErrorCode = E_NOERROR; return this->uLastErrorCode; @@ -170,7 +184,7 @@ int thread_pool::SetThreadStatus(HANDLE return E_THREAD_NOT_FOUND; } -UINT thread_pool::GetThreadStatus(HANDLE hThread) +int thread_pool::GetThreadStatus(HANDLE hThread) { UINT i; @@ -192,3 +206,47 @@ UINT thread_pool::GetThreadStatus(HANDLE this->uLastErrorCode = E_THREAD_NOT_FOUND; return S_UNKNOWN; } + +int thread_pool::KillDeadThread(void) +{ + UINT i; + bool bKillStatus = true; + + if (this->SetLock(true) == 0) + { + return this->uLastErrorCode; + } + + if (this->uThreadCount <= 0) + { + this->SetLock(false); + this->uLastErrorCode = E_THREAD_NOT_FOUND; + return E_THREAD_NOT_FOUND; + } + + for (i=0; i < this->uThreadMax; i++) + { + if ((this->uThreadStatusList[i] == S_WORKING) && + (clock() - this->time_status_set[i] > this->thread_timeout)) + { + 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->SetLock(false); + this->uLastErrorCode = (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); + return (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED); +}