/[LeafOK_CVS]/innwebd/thread_pool.cpp
ViewVC logotype

Diff of /innwebd/thread_pool.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 by sysadm, Sat Jul 3 08:38:54 2004 UTC Revision 1.2 by sysadm, Sat Jul 3 09:37:58 2004 UTC
# Line 15  Line 15 
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    
# Line 24  thread_pool::thread_pool(UINT uThreadMax Line 24  thread_pool::thread_pool(UINT uThreadMax
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);
# Line 36  thread_pool::thread_pool(UINT uThreadMax Line 48  thread_pool::thread_pool(UINT uThreadMax
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)
# Line 60  int thread_pool::AddThread(HANDLE hThrea Line 71  int thread_pool::AddThread(HANDLE hThrea
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;
# Line 72  int thread_pool::AddThread(HANDLE hThrea Line 85  int thread_pool::AddThread(HANDLE hThrea
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          {          {
# Line 99  int thread_pool::SetLock(bool bLock, tim Line 112  int thread_pool::SetLock(bool bLock, tim
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  }  }
# Line 138  int thread_pool::RemoveThread(HANDLE hTh Line 151  int thread_pool::RemoveThread(HANDLE hTh
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    
# Line 159  int thread_pool::SetThreadStatus(HANDLE Line 172  int thread_pool::SetThreadStatus(HANDLE
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;
# Line 170  int thread_pool::SetThreadStatus(HANDLE Line 184  int thread_pool::SetThreadStatus(HANDLE
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    
# Line 192  UINT thread_pool::GetThreadStatus(HANDLE Line 206  UINT thread_pool::GetThreadStatus(HANDLE
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    }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1