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

Annotation of /innwebd/thread_pool.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Sat Jul 3 09:37:58 2004 UTC (21 years, 8 months ago) by sysadm
Branch: MAIN
Changes since 1.1: +66 -8 lines
Content type: text/x-c++src
no message

1 sysadm 1.1 /*******************************************************/
2     /* */
3     /* LeafOK Innd */
4     /* Copyright (C) LeafOK.com, 2003-2004 */
5     /* */
6     /* Programmed by Leaf */
7     /* E-mail:leaf@leafok.com QQ:6049044 */
8     /* */
9     /* http://bbs.leafok.com */
10     /* http://bbs.leafok.net */
11     /* http://bbs.fenglin.info */
12     /* */
13     /*******************************************************/
14    
15     #include "StdAfx.h"
16     #include ".\thread_pool.h"
17    
18 sysadm 1.2 thread_pool::thread_pool(UINT uThreadMax, clock_t thread_timeout)
19 sysadm 1.1 {
20     UINT i;
21    
22     this->uLastErrorCode = E_NOERROR;
23     this->uThreadCount = 0;
24     this->uThreadMax = uThreadMax;
25     this->thread_timeout = thread_timeout;
26    
27 sysadm 1.2 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 sysadm 1.1 for (i=0; i < this->uThreadMax; i++)
40     {
41     this->hThreadList[i] = NULL;
42     this->uThreadStatusList[i] = S_UNUSED;
43 sysadm 1.2 this->time_status_set[i] = clock();
44 sysadm 1.1 }
45    
46     this->SetLock(false);
47     }
48    
49     thread_pool::~thread_pool(void)
50     {
51     }
52    
53     int thread_pool::AddThread(HANDLE hThread)
54     {
55     UINT i;
56    
57     if (this->SetLock(true) == 0)
58     {
59     return this->uLastErrorCode;
60     }
61    
62     if (this->uThreadCount >= this->uThreadMax)
63     {
64     this->SetLock(false);
65     this->uLastErrorCode = E_MAX_THREAD_EXCEEDED;
66     return E_MAX_THREAD_EXCEEDED;
67     }
68    
69     for (i=0; i < this->uThreadMax; i++)
70     {
71     if (this->uThreadStatusList[i] == S_UNUSED)
72     {
73     this->hThreadList[i] = hThread;
74 sysadm 1.2 this->uThreadStatusList[i] = S_WAITING;
75     this->time_status_set[i] = clock();
76 sysadm 1.1 this->uThreadCount++;
77     this->SetLock(false);
78     this->uLastErrorCode = E_NOERROR;
79     return E_NOERROR;
80     }
81     }
82    
83     this->SetLock(false);
84     this->uLastErrorCode = E_MAX_THREAD_EXCEEDED;
85     return E_MAX_THREAD_EXCEEDED;
86     }
87    
88 sysadm 1.2 int thread_pool::SetLock(bool bLock, clock_t tTimeout)
89 sysadm 1.1 {
90 sysadm 1.2 clock_t wait = 0;
91 sysadm 1.1
92     if (!bLock) // Free lock
93     {
94     this->bProcessLock = false;
95     this->uLastErrorCode = E_NOERROR;
96     return E_NOERROR;
97     }
98    
99     while (this->bProcessLock) // Locked by others
100     {
101     Sleep(10);
102     wait += 10;
103     if (wait > tTimeout) // Timeout
104     {
105     this->uLastErrorCode = E_LOCK_TIMEOUT;
106     return E_LOCK_TIMEOUT;
107     }
108     }
109     this->bProcessLock = true;
110     this->uLastErrorCode = E_NOERROR;
111    
112     return E_NOERROR;
113     }
114    
115 sysadm 1.2 int thread_pool::GetLastError(void)
116 sysadm 1.1 {
117     return this->uLastErrorCode;
118     }
119    
120     int thread_pool::RemoveThread(HANDLE hThread)
121     {
122     UINT i;
123    
124     if (this->SetLock(true) == 0)
125     {
126     return this->uLastErrorCode;
127     }
128    
129     if (this->uThreadCount <= 0)
130     {
131     this->SetLock(false);
132     this->uLastErrorCode = E_THREAD_NOT_FOUND;
133     return E_THREAD_NOT_FOUND;
134     }
135    
136     for (i=0; i < this->uThreadMax; i++)
137     {
138     if (this->hThreadList[i] == hThread)
139     {
140     this->hThreadList[i] = NULL;
141     this->uThreadCount--;
142     this->SetLock(false);
143     this->uLastErrorCode = E_NOERROR;
144     return this->uLastErrorCode;
145     }
146     }
147    
148     this->SetLock(false);
149     this->uLastErrorCode = E_THREAD_NOT_FOUND;
150     return E_THREAD_NOT_FOUND;
151     }
152    
153    
154 sysadm 1.2 int thread_pool::SetThreadStatus(HANDLE hThread, int uStatus)
155 sysadm 1.1 {
156     UINTT i;
157    
158     if (this->SetLock(true) == 0)
159     {
160     return this->uLastErrorCode;
161     }
162    
163     if (this->uThreadCount <= 0)
164     {
165     this->SetLock(false);
166     this->uLastErrorCode = E_THREAD_NOT_FOUND;
167     return E_THREAD_NOT_FOUND;
168     }
169    
170     for (i=0; i < this->uThreadMax; i++)
171     {
172     if (this->hThreadList[i] == hThread)
173     {
174     this->uThreadStatusList[i] = (thread_status)uStatus;
175 sysadm 1.2 this->time_status_set[i] = clock();
176 sysadm 1.1 this->SetLock(false);
177     this->uLastErrorCode = E_NOERROR;
178     return this->uLastErrorCode;
179     }
180     }
181    
182     this->SetLock(false);
183     this->uLastErrorCode = E_THREAD_NOT_FOUND;
184     return E_THREAD_NOT_FOUND;
185     }
186    
187 sysadm 1.2 int thread_pool::GetThreadStatus(HANDLE hThread)
188 sysadm 1.1 {
189     UINT i;
190    
191     if (this->uThreadCount <= 0)
192     {
193     this->uLastErrorCode = E_THREAD_NOT_FOUND;
194     return S_UNKNOWN;
195     }
196    
197     for (i=0; i < this->uThreadMax; i++)
198     {
199     if (this->hThreadList[i] == hThread)
200     {
201     this->uLastErrorCode = E_NOERROR;
202     return this->uThreadStatusList[i];
203     }
204     }
205    
206     this->uLastErrorCode = E_THREAD_NOT_FOUND;
207     return S_UNKNOWN;
208     }
209 sysadm 1.2
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     }

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