1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
1 |
/* wtp.c
|
2 |
*
|
|
3 |
* This file implements the worker thread pool (wtp) class.
|
|
4 |
*
|
|
5 |
* File begun on 2008-01-20 by RGerhards
|
|
6 |
*
|
|
7 |
* There is some in-depth documentation available in doc/dev_queue.html
|
|
8 |
* (and in the web doc set on http://www.rsyslog.com/doc). Be sure to read it
|
|
9 |
* if you are getting aquainted to the object.
|
|
10 |
*
|
|
11 |
* Copyright 2008 Rainer Gerhards and Adiscon GmbH.
|
|
12 |
*
|
|
1.1.3
by Michael Biebl
Import upstream version 3.16.1 |
13 |
* This file is part of the rsyslog runtime library.
|
1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
14 |
*
|
1.1.3
by Michael Biebl
Import upstream version 3.16.1 |
15 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify
|
16 |
* it under the terms of the GNU Lesser General Public License as published by
|
|
1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
17 |
* the Free Software Foundation, either version 3 of the License, or
|
18 |
* (at your option) any later version.
|
|
19 |
*
|
|
1.1.3
by Michael Biebl
Import upstream version 3.16.1 |
20 |
* The rsyslog runtime library is distributed in the hope that it will be useful,
|
1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
1.1.3
by Michael Biebl
Import upstream version 3.16.1 |
23 |
* GNU Lesser General Public License for more details.
|
1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
24 |
*
|
1.1.3
by Michael Biebl
Import upstream version 3.16.1 |
25 |
* You should have received a copy of the GNU Lesser General Public License
|
26 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
|
|
1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
27 |
*
|
28 |
* A copy of the GPL can be found in the file "COPYING" in this distribution.
|
|
1.1.3
by Michael Biebl
Import upstream version 3.16.1 |
29 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
|
1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
30 |
*/
|
31 |
#include "config.h" |
|
32 |
||
33 |
#include <stdio.h> |
|
34 |
#include <stdlib.h> |
|
35 |
#include <string.h> |
|
36 |
#include <assert.h> |
|
37 |
#include <signal.h> |
|
38 |
#include <pthread.h> |
|
39 |
#include <fcntl.h> |
|
40 |
#include <unistd.h> |
|
41 |
#include <errno.h> |
|
42 |
||
43 |
#include "rsyslog.h" |
|
44 |
#include "syslogd.h" |
|
45 |
#include "stringbuf.h" |
|
46 |
#include "srUtils.h" |
|
47 |
#include "wtp.h" |
|
48 |
#include "wti.h" |
|
49 |
#include "obj.h" |
|
50 |
||
51 |
/* static data */
|
|
52 |
DEFobjStaticHelpers
|
|
53 |
||
54 |
/* forward-definitions */
|
|
55 |
||
56 |
/* methods */
|
|
57 |
||
58 |
/* get the header for debug messages
|
|
59 |
* The caller must NOT free or otherwise modify the returned string!
|
|
60 |
*/
|
|
61 |
static inline uchar * |
|
62 |
wtpGetDbgHdr(wtp_t *pThis) |
|
63 |
{
|
|
64 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
65 |
||
66 |
if(pThis->pszDbgHdr == NULL) |
|
67 |
return (uchar*) "wtp"; /* should not normally happen */ |
|
68 |
else
|
|
69 |
return pThis->pszDbgHdr; |
|
70 |
}
|
|
71 |
||
72 |
||
73 |
||
74 |
/* Not implemented dummy function for constructor */
|
|
75 |
static rsRetVal NotImplementedDummy() { return RS_RET_OK; } |
|
76 |
/* Standard-Constructor for the wtp object
|
|
77 |
*/
|
|
78 |
BEGINobjConstruct(wtp) /* be sure to specify the object type also in END macro! */ |
|
79 |
pthread_mutex_init(&pThis->mut, NULL); |
|
80 |
pthread_cond_init(&pThis->condThrdTrm, NULL); |
|
81 |
/* set all function pointers to "not implemented" dummy so that we can safely call them */
|
|
82 |
pThis->pfChkStopWrkr = NotImplementedDummy; |
|
83 |
pThis->pfIsIdle = NotImplementedDummy; |
|
84 |
pThis->pfDoWork = NotImplementedDummy; |
|
85 |
pThis->pfOnIdle = NotImplementedDummy; |
|
86 |
pThis->pfOnWorkerCancel = NotImplementedDummy; |
|
87 |
pThis->pfOnWorkerStartup = NotImplementedDummy; |
|
88 |
pThis->pfOnWorkerShutdown = NotImplementedDummy; |
|
89 |
ENDobjConstruct(wtp) |
|
90 |
||
91 |
||
92 |
/* Construction finalizer
|
|
93 |
* rgerhards, 2008-01-17
|
|
94 |
*/
|
|
95 |
rsRetVal
|
|
96 |
wtpConstructFinalize(wtp_t *pThis) |
|
97 |
{
|
|
98 |
DEFiRet; |
|
99 |
int i; |
|
100 |
uchar pszBuf[64]; |
|
101 |
size_t lenBuf; |
|
102 |
wti_t *pWti; |
|
103 |
||
104 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
105 |
||
106 |
dbgprintf("%s: finalizing construction of worker thread pool\n", wtpGetDbgHdr(pThis)); |
|
107 |
/* alloc and construct workers - this can only be done in finalizer as we previously do
|
|
108 |
* not know the max number of workers
|
|
109 |
*/
|
|
110 |
if((pThis->pWrkr = malloc(sizeof(wti_t*) * pThis->iNumWorkerThreads)) == NULL) |
|
111 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
|
112 |
||
113 |
for(i = 0 ; i < pThis->iNumWorkerThreads ; ++i) { |
|
114 |
CHKiRet(wtiConstruct(&pThis->pWrkr[i])); |
|
115 |
pWti = pThis->pWrkr[i]; |
|
116 |
lenBuf = snprintf((char*)pszBuf, sizeof(pszBuf), "%s/w%d", wtpGetDbgHdr(pThis), i); |
|
117 |
CHKiRet(wtiSetDbgHdr(pWti, pszBuf, lenBuf)); |
|
118 |
CHKiRet(wtiSetpWtp(pWti, pThis)); |
|
119 |
CHKiRet(wtiConstructFinalize(pWti)); |
|
120 |
}
|
|
121 |
||
122 |
||
123 |
finalize_it: |
|
124 |
RETiRet; |
|
125 |
}
|
|
126 |
||
127 |
||
128 |
/* Destructor */
|
|
129 |
BEGINobjDestruct(wtp) /* be sure to specify the object type also in END and CODESTART macros! */ |
|
130 |
int i; |
|
131 |
CODESTARTobjDestruct(wtp) |
|
132 |
wtpProcessThrdChanges(pThis); /* process thread changes one last time */ |
|
133 |
||
134 |
/* destruct workers */
|
|
135 |
for(i = 0 ; i < pThis->iNumWorkerThreads ; ++i) |
|
136 |
wtiDestruct(&pThis->pWrkr[i]); |
|
137 |
||
138 |
free(pThis->pWrkr); |
|
139 |
pThis->pWrkr = NULL; |
|
140 |
||
141 |
/* actual destruction */
|
|
142 |
pthread_cond_destroy(&pThis->condThrdTrm); |
|
143 |
pthread_mutex_destroy(&pThis->mut); |
|
144 |
||
145 |
if(pThis->pszDbgHdr != NULL) |
|
146 |
free(pThis->pszDbgHdr); |
|
147 |
ENDobjDestruct(wtp) |
|
148 |
||
149 |
||
150 |
/* wake up at least one worker thread.
|
|
151 |
* rgerhards, 2008-01-20
|
|
152 |
*/
|
|
153 |
rsRetVal
|
|
154 |
wtpWakeupWrkr(wtp_t *pThis) |
|
155 |
{
|
|
156 |
DEFiRet; |
|
157 |
||
158 |
/* TODO; mutex? I think not needed, as we do not need predictable exec order -- rgerhards, 2008-01-28 */
|
|
159 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
160 |
pthread_cond_signal(pThis->pcondBusy); |
|
161 |
RETiRet; |
|
162 |
}
|
|
163 |
||
164 |
/* wake up all worker threads.
|
|
165 |
* rgerhards, 2008-01-16
|
|
166 |
*/
|
|
167 |
rsRetVal
|
|
168 |
wtpWakeupAllWrkr(wtp_t *pThis) |
|
169 |
{
|
|
170 |
DEFiRet; |
|
171 |
||
172 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
173 |
pthread_cond_broadcast(pThis->pcondBusy); |
|
174 |
RETiRet; |
|
175 |
}
|
|
176 |
||
177 |
||
178 |
/* check if we had any worker thread changes and, if so, act
|
|
179 |
* on them. At a minimum, terminated threads are harvested (joined).
|
|
180 |
* This function MUST NEVER block on the queue mutex!
|
|
181 |
*/
|
|
182 |
rsRetVal
|
|
183 |
wtpProcessThrdChanges(wtp_t *pThis) |
|
184 |
{
|
|
185 |
DEFiRet; |
|
186 |
int i; |
|
187 |
||
188 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
189 |
||
190 |
if(pThis->bThrdStateChanged == 0) |
|
191 |
FINALIZE; |
|
192 |
||
193 |
/* go through all threads */
|
|
194 |
for(i = 0 ; i < pThis->iNumWorkerThreads ; ++i) { |
|
195 |
wtiProcessThrdChanges(pThis->pWrkr[i], LOCK_MUTEX); |
|
196 |
}
|
|
197 |
||
198 |
finalize_it: |
|
199 |
RETiRet; |
|
200 |
}
|
|
201 |
||
202 |
||
203 |
/* Sent a specific state for the worker thread pool.
|
|
204 |
* rgerhards, 2008-01-21
|
|
205 |
*/
|
|
206 |
rsRetVal
|
|
207 |
wtpSetState(wtp_t *pThis, wtpState_t iNewState) |
|
208 |
{
|
|
209 |
DEFiRet; |
|
210 |
||
211 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
212 |
pThis->wtpState = iNewState; |
|
213 |
/* TODO: must wakeup workers? seen to be not needed -- rgerhards, 2008-01-28 */
|
|
214 |
||
215 |
RETiRet; |
|
216 |
}
|
|
217 |
||
218 |
||
219 |
/* check if the worker shall shutdown (1 = yes, 0 = no)
|
|
220 |
* TODO: check if we can use atomic operations to enhance performance
|
|
221 |
* Note: there may be two mutexes locked, the bLockUsrMutex is the one in our "user"
|
|
222 |
* (e.g. the queue clas)
|
|
223 |
* rgerhards, 2008-01-21
|
|
224 |
*/
|
|
225 |
rsRetVal
|
|
226 |
wtpChkStopWrkr(wtp_t *pThis, int bLockMutex, int bLockUsrMutex) |
|
227 |
{
|
|
228 |
DEFiRet; |
|
229 |
DEFVARS_mutexProtection; |
|
230 |
||
231 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
232 |
||
233 |
BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, bLockMutex); |
|
234 |
if( (pThis->wtpState == wtpState_SHUTDOWN_IMMEDIATE) |
|
235 |
|| ((pThis->wtpState == wtpState_SHUTDOWN) && pThis->pfIsIdle(pThis->pUsr, bLockUsrMutex))) |
|
236 |
iRet = RS_RET_TERMINATE_NOW; |
|
237 |
END_MTX_PROTECTED_OPERATIONS(&pThis->mut); |
|
238 |
||
239 |
/* try customer handler if one was set and we do not yet have a definite result */
|
|
240 |
if(iRet == RS_RET_OK && pThis->pfChkStopWrkr != NULL) { |
|
241 |
iRet = pThis->pfChkStopWrkr(pThis->pUsr, bLockUsrMutex); |
|
242 |
}
|
|
243 |
||
244 |
RETiRet; |
|
245 |
}
|
|
246 |
||
247 |
||
248 |
/* Send a shutdown command to all workers and see if they terminate.
|
|
249 |
* A timeout may be specified.
|
|
250 |
* rgerhards, 2008-01-14
|
|
251 |
*/
|
|
252 |
rsRetVal
|
|
253 |
wtpShutdownAll(wtp_t *pThis, wtpState_t tShutdownCmd, struct timespec *ptTimeout) |
|
254 |
{
|
|
255 |
DEFiRet; |
|
256 |
int bTimedOut; |
|
257 |
int iCancelStateSave; |
|
258 |
||
259 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
260 |
||
261 |
wtpSetState(pThis, tShutdownCmd); |
|
262 |
wtpWakeupAllWrkr(pThis); |
|
263 |
||
264 |
/* see if we need to harvest (join) any terminated threads (even in timeout case,
|
|
265 |
* some may have terminated...
|
|
266 |
*/
|
|
267 |
wtpProcessThrdChanges(pThis); |
|
268 |
||
269 |
/* and wait for their termination */
|
|
270 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &iCancelStateSave); |
|
271 |
d_pthread_mutex_lock(&pThis->mut); |
|
272 |
pthread_cleanup_push(mutexCancelCleanup, &pThis->mut); |
|
273 |
pthread_setcancelstate(iCancelStateSave, NULL); |
|
274 |
bTimedOut = 0; |
|
275 |
while(pThis->iCurNumWrkThrd > 0 && !bTimedOut) { |
|
276 |
dbgprintf("%s: waiting %ldms on worker thread termination, %d still running\n", |
|
277 |
wtpGetDbgHdr(pThis), timeoutVal(ptTimeout), pThis->iCurNumWrkThrd); |
|
278 |
||
279 |
if(d_pthread_cond_timedwait(&pThis->condThrdTrm, &pThis->mut, ptTimeout) != 0) { |
|
280 |
dbgprintf("%s: timeout waiting on worker thread termination\n", wtpGetDbgHdr(pThis)); |
|
281 |
bTimedOut = 1; /* we exit the loop on timeout */ |
|
282 |
}
|
|
283 |
}
|
|
284 |
pthread_cleanup_pop(1); |
|
285 |
||
286 |
if(bTimedOut) |
|
287 |
iRet = RS_RET_TIMED_OUT; |
|
288 |
||
289 |
/* see if we need to harvest (join) any terminated threads (even in timeout case,
|
|
290 |
* some may have terminated...
|
|
291 |
*/
|
|
292 |
wtpProcessThrdChanges(pThis); |
|
293 |
||
294 |
RETiRet; |
|
295 |
}
|
|
296 |
||
297 |
||
298 |
/* indicate that a thread has terminated and awake anyone waiting on it
|
|
299 |
* rgerhards, 2008-01-23
|
|
300 |
*/
|
|
301 |
rsRetVal wtpSignalWrkrTermination(wtp_t *pThis) |
|
302 |
{
|
|
303 |
DEFiRet; |
|
304 |
/* I leave the mutex code here out as it give as deadlocks. I think it is not really
|
|
305 |
* needed and we are on the safe side. I leave this comment in if practice proves us
|
|
306 |
* wrong. The whole thing should be removed after half a your or year if we see there
|
|
307 |
* actually is no issue (or revisit it from a theoretical POV).
|
|
308 |
* rgerhards, 2008-01-28
|
|
309 |
*/
|
|
310 |
/*TODO: mutex or not mutex, that's the question ;)DEFVARS_mutexProtection;*/
|
|
311 |
||
312 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
313 |
||
314 |
/*BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, LOCK_MUTEX);*/
|
|
315 |
pthread_cond_signal(&pThis->condThrdTrm); /* activate anyone waiting on thread shutdown */ |
|
316 |
/*END_MTX_PROTECTED_OPERATIONS(&pThis->mut);*/
|
|
317 |
RETiRet; |
|
318 |
}
|
|
319 |
||
320 |
||
321 |
/* Unconditionally cancel all running worker threads.
|
|
322 |
* rgerhards, 2008-01-14
|
|
323 |
*/
|
|
324 |
rsRetVal
|
|
325 |
wtpCancelAll(wtp_t *pThis) |
|
326 |
{
|
|
327 |
DEFiRet; |
|
328 |
int i; |
|
329 |
||
330 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
331 |
||
332 |
/* process any pending thread requests so that we know who actually is still running */
|
|
333 |
wtpProcessThrdChanges(pThis); |
|
334 |
||
335 |
/* go through all workers and cancel those that are active */
|
|
336 |
for(i = 0 ; i < pThis->iNumWorkerThreads ; ++i) { |
|
337 |
dbgprintf("%s: try canceling worker thread %d\n", wtpGetDbgHdr(pThis), i); |
|
338 |
wtiCancelThrd(pThis->pWrkr[i]); |
|
339 |
}
|
|
340 |
||
341 |
RETiRet; |
|
342 |
}
|
|
343 |
||
344 |
||
345 |
||
346 |
/* Set the Inactivity Guard
|
|
347 |
* rgerhards, 2008-01-21
|
|
348 |
*/
|
|
349 |
rsRetVal
|
|
350 |
wtpSetInactivityGuard(wtp_t *pThis, int bNewState, int bLockMutex) |
|
351 |
{
|
|
352 |
DEFiRet; |
|
353 |
DEFVARS_mutexProtection; |
|
354 |
||
355 |
BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, bLockMutex); |
|
356 |
pThis->bInactivityGuard = bNewState; |
|
357 |
END_MTX_PROTECTED_OPERATIONS(&pThis->mut); |
|
358 |
||
359 |
RETiRet; |
|
360 |
}
|
|
361 |
||
362 |
||
363 |
/* cancellation cleanup handler for executing worker
|
|
364 |
* decrements the worker counter
|
|
365 |
* rgerhards, 2008-01-20
|
|
366 |
*/
|
|
367 |
void
|
|
368 |
wtpWrkrExecCancelCleanup(void *arg) |
|
369 |
{
|
|
370 |
wtp_t *pThis = (wtp_t*) arg; |
|
371 |
||
372 |
BEGINfunc
|
|
373 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
374 |
pThis->iCurNumWrkThrd--; |
|
375 |
wtpSignalWrkrTermination(pThis); |
|
376 |
||
377 |
dbgprintf("%s: thread CANCELED with %d workers running.\n", wtpGetDbgHdr(pThis), pThis->iCurNumWrkThrd); |
|
378 |
ENDfunc
|
|
379 |
}
|
|
380 |
||
381 |
||
382 |
/* wtp worker shell. This is started and calls into the actual
|
|
383 |
* wti worker.
|
|
384 |
* rgerhards, 2008-01-21
|
|
385 |
*/
|
|
386 |
static void * |
|
387 |
wtpWorker(void *arg) /* the arg is actually a wti object, even though we are in wtp! */ |
|
388 |
{
|
|
389 |
DEFiRet; |
|
390 |
DEFVARS_mutexProtection; |
|
391 |
wti_t *pWti = (wti_t*) arg; |
|
392 |
wtp_t *pThis; |
|
393 |
sigset_t sigSet; |
|
394 |
||
395 |
ISOBJ_TYPE_assert(pWti, wti); |
|
396 |
pThis = pWti->pWtp; |
|
397 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
398 |
||
399 |
sigfillset(&sigSet); |
|
400 |
pthread_sigmask(SIG_BLOCK, &sigSet, NULL); |
|
401 |
||
402 |
BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, LOCK_MUTEX); |
|
403 |
||
404 |
/* do some late initialization */
|
|
405 |
||
406 |
pthread_cleanup_push(wtpWrkrExecCancelCleanup, pThis); |
|
407 |
||
408 |
/* finally change to RUNNING state. We need to check if we actually should still run,
|
|
409 |
* because someone may have requested us to shut down even before we got a chance to do
|
|
410 |
* our init. That would be a bad race... -- rgerhards, 2008-01-16
|
|
411 |
*/
|
|
412 |
wtiSetState(pWti, eWRKTHRD_RUNNING, 0, MUTEX_ALREADY_LOCKED); /* we are running now! */ |
|
413 |
||
414 |
do { |
|
415 |
END_MTX_PROTECTED_OPERATIONS(&pThis->mut); |
|
416 |
||
417 |
iRet = wtiWorker(pWti); /* just to make sure: this is NOT protected by the mutex! */ |
|
418 |
||
419 |
BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, LOCK_MUTEX); |
|
420 |
} while(pThis->iCurNumWrkThrd == 1 && pThis->bInactivityGuard == 1); |
|
421 |
/* inactivity guard prevents shutdown of all workers while one should be running due to race
|
|
422 |
* condition. It can lead to one more worker running than desired, but that is acceptable. After
|
|
423 |
* all, that worker will shutdown itself due to inactivity timeout. If, however, none were running
|
|
424 |
* when one was required, processing could come to a halt. -- rgerhards, 2008-01-21
|
|
425 |
*/
|
|
426 |
||
427 |
pthread_cleanup_pop(0); |
|
428 |
pThis->iCurNumWrkThrd--; |
|
429 |
wtpSignalWrkrTermination(pThis); |
|
430 |
||
431 |
dbgprintf("%s: Worker thread %lx, terminated, num workers now %d\n", |
|
432 |
wtpGetDbgHdr(pThis), (unsigned long) pWti, pThis->iCurNumWrkThrd); |
|
433 |
||
434 |
END_MTX_PROTECTED_OPERATIONS(&pThis->mut); |
|
435 |
||
436 |
ENDfunc
|
|
437 |
pthread_exit(0); |
|
438 |
}
|
|
439 |
||
440 |
||
441 |
/* start a new worker */
|
|
442 |
static rsRetVal |
|
443 |
wtpStartWrkr(wtp_t *pThis, int bLockMutex) |
|
444 |
{
|
|
445 |
DEFiRet; |
|
446 |
DEFVARS_mutexProtection; |
|
447 |
wti_t *pWti; |
|
448 |
int i; |
|
449 |
int iState; |
|
450 |
||
451 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
452 |
||
453 |
wtpProcessThrdChanges(pThis); |
|
454 |
||
455 |
BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, bLockMutex); |
|
456 |
||
457 |
pThis->iCurNumWrkThrd++; |
|
458 |
||
459 |
/* find free spot in thread table. If we find at least one worker that is in initialization,
|
|
460 |
* we do NOT start a new one. Let's give the other one a chance, first.
|
|
461 |
*/
|
|
462 |
for(i = 0 ; i < pThis->iNumWorkerThreads ; ++i) { |
|
463 |
if(wtiGetState(pThis->pWrkr[i], LOCK_MUTEX) == eWRKTHRD_STOPPED) { |
|
464 |
break; |
|
465 |
}
|
|
466 |
}
|
|
467 |
||
468 |
if(i == pThis->iNumWorkerThreads) |
|
469 |
ABORT_FINALIZE(RS_RET_NO_MORE_THREADS); |
|
470 |
||
471 |
pWti = pThis->pWrkr[i]; |
|
472 |
wtiSetState(pWti, eWRKTHRD_RUN_CREATED, 0, LOCK_MUTEX); |
|
473 |
iState = pthread_create(&(pWti->thrdID), NULL, wtpWorker, (void*) pWti); |
|
474 |
dbgprintf("%s: started with state %d, num workers now %d\n", |
|
475 |
wtpGetDbgHdr(pThis), iState, pThis->iCurNumWrkThrd); |
|
476 |
||
477 |
/* we try to give the starting worker a little boost. It won't help much as we still
|
|
478 |
* hold the queue's mutex, but at least it has a chance to start on a single-CPU system.
|
|
479 |
*/
|
|
480 |
# if !defined(__hpux) /* pthread_yield is missing there! */ |
|
481 |
pthread_yield(); |
|
482 |
# endif
|
|
483 |
||
484 |
/* indicate we just started a worker and would like to see it running */
|
|
485 |
wtpSetInactivityGuard(pThis, 1, MUTEX_ALREADY_LOCKED); |
|
486 |
||
487 |
finalize_it: |
|
488 |
END_MTX_PROTECTED_OPERATIONS(&pThis->mut); |
|
489 |
RETiRet; |
|
490 |
}
|
|
491 |
||
492 |
||
493 |
/* set the number of worker threads that should be running. If less than currently running,
|
|
494 |
* a new worker may be started. Please note that there is no guarantee the number of workers
|
|
495 |
* said will be running after we exit this function. It is just a hint. If the number is
|
|
496 |
* higher than one, and no worker is started, the "busy" condition is signaled to awake a worker.
|
|
497 |
* So the caller can assume that there is at least one worker re-checking if there is "work to do"
|
|
498 |
* after this function call.
|
|
499 |
* rgerhards, 2008-01-21
|
|
500 |
*/
|
|
501 |
rsRetVal
|
|
502 |
wtpAdviseMaxWorkers(wtp_t *pThis, int nMaxWrkr) |
|
503 |
{
|
|
504 |
DEFiRet; |
|
505 |
DEFVARS_mutexProtection; |
|
506 |
int nMissing; /* number workers missing to run */ |
|
507 |
int i; |
|
508 |
||
509 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
510 |
||
511 |
if(nMaxWrkr == 0) |
|
512 |
FINALIZE; |
|
513 |
||
514 |
BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, LOCK_MUTEX); |
|
515 |
||
516 |
if(nMaxWrkr > pThis->iNumWorkerThreads) /* limit to configured maximum */ |
|
517 |
nMaxWrkr = pThis->iNumWorkerThreads; |
|
518 |
||
519 |
nMissing = nMaxWrkr - pThis->iCurNumWrkThrd; |
|
520 |
||
521 |
if(nMissing > 0) { |
|
522 |
dbgprintf("%s: high activity - starting %d additional worker thread(s).\n", wtpGetDbgHdr(pThis), nMissing); |
|
523 |
/* start the rqtd nbr of workers */
|
|
524 |
for(i = 0 ; i < nMissing ; ++i) { |
|
525 |
CHKiRet(wtpStartWrkr(pThis, MUTEX_ALREADY_LOCKED)); |
|
526 |
}
|
|
527 |
} else { |
|
528 |
if(nMaxWrkr > 0) { |
|
529 |
dbgprintf("wtpAdviseMaxWorkers signals busy\n"); |
|
530 |
wtpWakeupWrkr(pThis); |
|
531 |
}
|
|
532 |
}
|
|
533 |
||
534 |
||
535 |
finalize_it: |
|
536 |
END_MTX_PROTECTED_OPERATIONS(&pThis->mut); |
|
537 |
RETiRet; |
|
538 |
}
|
|
539 |
||
540 |
||
541 |
/* some simple object access methods */
|
|
542 |
DEFpropSetMeth(wtp, toWrkShutdown, long); |
|
543 |
DEFpropSetMeth(wtp, wtpState, wtpState_t); |
|
544 |
DEFpropSetMeth(wtp, iNumWorkerThreads, int); |
|
545 |
DEFpropSetMeth(wtp, pUsr, void*); |
|
546 |
DEFpropSetMethPTR(wtp, pmutUsr, pthread_mutex_t); |
|
547 |
DEFpropSetMethPTR(wtp, pcondBusy, pthread_cond_t); |
|
548 |
DEFpropSetMethFP(wtp, pfChkStopWrkr, rsRetVal(*pVal)(void*, int)); |
|
1.1.5
by Michael Biebl
Import upstream version 3.18.1 |
549 |
DEFpropSetMethFP(wtp, pfRateLimiter, rsRetVal(*pVal)(void*)); |
1.1.2
by Michael Biebl
Import upstream version 3.14.2 |
550 |
DEFpropSetMethFP(wtp, pfIsIdle, rsRetVal(*pVal)(void*, int)); |
551 |
DEFpropSetMethFP(wtp, pfDoWork, rsRetVal(*pVal)(void*, void*, int)); |
|
552 |
DEFpropSetMethFP(wtp, pfOnIdle, rsRetVal(*pVal)(void*, int)); |
|
553 |
DEFpropSetMethFP(wtp, pfOnWorkerCancel, rsRetVal(*pVal)(void*, void*)); |
|
554 |
DEFpropSetMethFP(wtp, pfOnWorkerStartup, rsRetVal(*pVal)(void*)); |
|
555 |
DEFpropSetMethFP(wtp, pfOnWorkerShutdown, rsRetVal(*pVal)(void*)); |
|
556 |
||
557 |
||
558 |
/* return the current number of worker threads.
|
|
559 |
* TODO: atomic operation would bring a nice performance
|
|
560 |
* enhancemcent
|
|
561 |
* rgerhards, 2008-01-27
|
|
562 |
*/
|
|
563 |
int
|
|
564 |
wtpGetCurNumWrkr(wtp_t *pThis, int bLockMutex) |
|
565 |
{
|
|
566 |
DEFVARS_mutexProtection; |
|
567 |
int iNumWrkr; |
|
568 |
||
569 |
BEGINfunc
|
|
570 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
571 |
||
572 |
BEGIN_MTX_PROTECTED_OPERATIONS(&pThis->mut, bLockMutex); |
|
573 |
iNumWrkr = pThis->iCurNumWrkThrd; |
|
574 |
END_MTX_PROTECTED_OPERATIONS(&pThis->mut); |
|
575 |
||
576 |
ENDfunc
|
|
577 |
return iNumWrkr; |
|
578 |
}
|
|
579 |
||
580 |
||
581 |
/* set the debug header message
|
|
582 |
* The passed-in string is duplicated. So if the caller does not need
|
|
583 |
* it any longer, it must free it. Must be called only before object is finalized.
|
|
584 |
* rgerhards, 2008-01-09
|
|
585 |
*/
|
|
586 |
rsRetVal
|
|
587 |
wtpSetDbgHdr(wtp_t *pThis, uchar *pszMsg, size_t lenMsg) |
|
588 |
{
|
|
589 |
DEFiRet; |
|
590 |
||
591 |
ISOBJ_TYPE_assert(pThis, wtp); |
|
592 |
assert(pszMsg != NULL); |
|
593 |
||
594 |
if(lenMsg < 1) |
|
595 |
ABORT_FINALIZE(RS_RET_PARAM_ERROR); |
|
596 |
||
597 |
if(pThis->pszDbgHdr != NULL) { |
|
598 |
free(pThis->pszDbgHdr); |
|
599 |
pThis->pszDbgHdr = NULL; |
|
600 |
}
|
|
601 |
||
602 |
if((pThis->pszDbgHdr = malloc(sizeof(uchar) * lenMsg + 1)) == NULL) |
|
603 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
|
604 |
||
605 |
memcpy(pThis->pszDbgHdr, pszMsg, lenMsg + 1); /* always think about the \0! */ |
|
606 |
||
607 |
finalize_it: |
|
608 |
RETiRet; |
|
609 |
}
|
|
610 |
||
611 |
/* dummy */
|
|
612 |
rsRetVal wtpQueryInterface(void) { return RS_RET_NOT_IMPLEMENTED; } |
|
613 |
||
614 |
/* Initialize the stream class. Must be called as the very first method
|
|
615 |
* before anything else is called inside this class.
|
|
616 |
* rgerhards, 2008-01-09
|
|
617 |
*/
|
|
618 |
BEGINObjClassInit(wtp, 1, OBJ_IS_CORE_MODULE) |
|
619 |
/* request objects we use */
|
|
620 |
ENDObjClassInit(wtp) |
|
621 |
||
622 |
/*
|
|
623 |
* vi:set ai:
|
|
624 |
*/
|