/[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.2 by sysadm, Sat Jul 3 09:37:58 2004 UTC Revision 1.3 by sysadm, Sat Jul 3 13:55:42 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, clock_t thread_timeout)  thread_pool::thread_pool(UINT uThreadMax)
19  {  {
20          UINT i;          UINT i;
21    
22          this->uLastErrorCode = E_NOERROR;          this->uLastErrorCode = E_NOERROR;
23          this->uThreadCount = 0;          this->uThreadCount = 0;
24          this->uThreadMax = uThreadMax;          this->uThreadMax = uThreadMax;
25          this->thread_timeout = thread_timeout;          this->hThreadKiller = NULL;
26    
27          if (this->uThreadCount > TS_MAX_THREAD)          if (this->uThreadMax > TS_MAX_THREAD)
28          {          {
29                  this->uThreadCount = 0;                  this->uThreadMax = 0;
30                  this->uLastErrorCode = E_MAX_THREAD_EXCEEDED;                  this->uLastErrorCode = E_MAX_THREAD_EXCEEDED;
31          }          }
32    
         if (this->thread_timeout < 0 || this->thread_timeout > TS_SETLOCK_TIMEOUT_MAX)  
         {  
                 this->thread_timeout = TS_SETLOCK_TIMEOUT;  
                 this->uLastErrorCode = E_INVALID_TIMEOUT;  
         }  
   
33          for (i=0; i < this->uThreadMax; i++)          for (i=0; i < this->uThreadMax; i++)
34          {          {
35                  this->hThreadList[i] = NULL;                  this->hThreadList[i] = NULL;
# Line 44  thread_pool::thread_pool(UINT uThreadMax Line 38  thread_pool::thread_pool(UINT uThreadMax
38          }          }
39    
40          this->SetLock(false);          this->SetLock(false);
41    
42            this->EnableKillDeadThread();
43  }  }
44    
45  thread_pool::~thread_pool(void)  thread_pool::~thread_pool(void)
46  {  {
47            this->DisableKillDeadThread();
48  }  }
49    
50  int thread_pool::AddThread(HANDLE hThread)  int thread_pool::AddThread(HANDLE hThread, clock_t thread_timeout)
51  {  {
52          UINT i;          UINT i;
53    
54          if (this->SetLock(true) == 0)          if (this->SetLock(true) != 0)
55          {          {
56                  return this->uLastErrorCode;                  return this->uLastErrorCode;
57          }          }
# Line 66  int thread_pool::AddThread(HANDLE hThrea Line 63  int thread_pool::AddThread(HANDLE hThrea
63                  return E_MAX_THREAD_EXCEEDED;                  return E_MAX_THREAD_EXCEEDED;
64          }          }
65    
66            if (thread_timeout < 0)
67            {
68                    this->uLastErrorCode = E_INVALID_TIMEOUT;
69                    return E_INVALID_TIMEOUT;
70            }
71    
72          for (i=0; i < this->uThreadMax; i++)          for (i=0; i < this->uThreadMax; i++)
73          {          {
74                  if (this->uThreadStatusList[i] == S_UNUSED)                  if (this->uThreadStatusList[i] == S_UNUSED)
75                  {                  {
76                          this->hThreadList[i] = hThread;                          this->hThreadList[i] = hThread;
77                          this->uThreadStatusList[i] = S_WAITING;                          this->uThreadStatusList[i] = S_WAITING;
78                            this->thread_timeout[i] = thread_timeout;
79                          this->time_status_set[i] = clock();                          this->time_status_set[i] = clock();
80                          this->uThreadCount++;                          this->uThreadCount++;
81                          this->SetLock(false);                          this->SetLock(false);
# Line 112  int thread_pool::SetLock(bool bLock, clo Line 116  int thread_pool::SetLock(bool bLock, clo
116          return E_NOERROR;          return E_NOERROR;
117  }  }
118    
119  int thread_pool::GetLastError(void)  int thread_pool::GetLastError(void) const
120  {  {
121          return this->uLastErrorCode;          return this->uLastErrorCode;
122  }  }
# Line 121  int thread_pool::RemoveThread(HANDLE hTh Line 125  int thread_pool::RemoveThread(HANDLE hTh
125  {  {
126          UINT i;          UINT i;
127    
128          if (this->SetLock(true) == 0)          if (this->SetLock(true) != 0)
129          {          {
130                  return this->uLastErrorCode;                  return this->uLastErrorCode;
131          }          }
# Line 155  int thread_pool::SetThreadStatus(HANDLE Line 159  int thread_pool::SetThreadStatus(HANDLE
159  {  {
160          UINTT i;          UINTT i;
161    
162          if (this->SetLock(true) == 0)          if (this->SetLock(true) != 0)
163          {          {
164                  return this->uLastErrorCode;                  return this->uLastErrorCode;
165          }          }
# Line 207  int thread_pool::GetThreadStatus(HANDLE Line 211  int thread_pool::GetThreadStatus(HANDLE
211          return S_UNKNOWN;          return S_UNKNOWN;
212  }  }
213    
214  int thread_pool::KillDeadThread(void)  DWORD thread_pool::KillDeadThread(LPVOID pParam)
215  {  {
216          UINT i;          thread_pool *p;
217          bool bKillStatus = true;      UINT i;
218        bool bKillStatus = true;
219    
220          if (this->SetLock(true) == 0)          p = (thread_pool*)pParam;
221    
222            if (p->SetLock(true) != 0)
223          {          {
224                  return this->uLastErrorCode;                  return p->uLastErrorCode;
225          }          }
226    
227          if (this->uThreadCount <= 0)          if (p->uThreadCount <= 0)
228          {          {
229                  this->SetLock(false);                  p->SetLock(false);
230                  this->uLastErrorCode = E_THREAD_NOT_FOUND;                  p->uLastErrorCode = E_THREAD_NOT_FOUND;
231                  return E_THREAD_NOT_FOUND;                  return E_THREAD_NOT_FOUND;
232          }          }
233    
234          for (i=0; i < this->uThreadMax; i++)          for (i=0; i < p->uThreadMax; i++)
235          {          {
236                  if ((this->uThreadStatusList[i] == S_WORKING) &&                  if ((p->uThreadStatusList[i] == S_WORKING) &&
237                          (clock() - this->time_status_set[i] > this->thread_timeout))                          (clock() - p->time_status_set[i] > p->thread_timeout[i]))
238                  {                  {
239                          if (TerminateThread(this->hThreadList[i],-1))                          if (TerminateThread(p->hThreadList[i],-1))
240                          {                          {
241                                  bKillStatus &= true;                                  bKillStatus &= true;
242                                  syslog << logfile::log_head << "Terminate dead thread ... OK" << endl;                                  syslog << logfile::log_head << "Terminate dead thread ... OK" << endl;
# Line 239  int thread_pool::KillDeadThread(void) Line 246  int thread_pool::KillDeadThread(void)
246                                  bKillStatus &= false;                                  bKillStatus &= false;
247                                  syslog << logfile::log_head << "Terminate dead thread ... Failed" << endl;                                  syslog << logfile::log_head << "Terminate dead thread ... Failed" << endl;
248                          }                          }
249                          this->hThreadList[i] = NULL;                          p->hThreadList[i] = NULL;
250                          this->uThreadStatusList[i] = S_UNUSED;                          p->uThreadStatusList[i] = S_UNUSED;
251                          this->time_status_set[i] = clock();                          p->time_status_set[i] = clock();
252                          this->uThreadCount--;                          p->uThreadCount--;
253                  }                  }
254          }          }
255    
256          this->SetLock(false);          p->SetLock(false);
257          this->uLastErrorCode = (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED);          p->uLastErrorCode = (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED);
258          return (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED);          return (bKillStatus?E_NOERROR:E_KILL_THREAD_FAILED);
259  }  }
260    
261    int thread_pool::GetThreadCount(void) const
262    {
263            return this->uThreadCount;
264    }
265    
266    int thread_pool::EnableKillDeadThread(void)
267    {
268            HANDLE hThreadCurrent;
269            ULONG ulThreadId;
270            if (hThreadCurrent = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)thread_pool::KillDeadThread,(LPVOID)this,0,&ulThreadId))
271            {
272                    this->hThreadKiller = hThreadCurrent;
273                    syslog << logfile::log_head << "Create killer thread ... OK" << endl;
274            }
275            else
276            {
277                    syslog << logfile::log_head << "Create killer thread ... Failed" << endl;
278                    return -1;
279            }
280    
281            return 0;
282    }
283    
284    int thread_pool::DisableKillDeadThread(void)
285    {
286            if (this->hThreadKiller)
287            {
288                    if (TerminateThread(this->hThreadKiller,0))
289                    {
290                            this->hThreadKiller = NULL;
291                            syslog << logfile::log_head << "Terminate killer thread ... OK" << endl;
292                    }
293                    else
294                    {
295                            syslog << logfile::log_head << "Terminate killer thread ... Failed" << endl;
296                            return -1;
297                    }
298            }
299            return 0;
300    }


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

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