| 1 |
/* Definition of the worker thread pool (wtp) object. |
| 2 |
* |
| 3 |
* Copyright 2008-2018 Adiscon GmbH. |
| 4 |
* |
| 5 |
* This file is part of the rsyslog runtime library. |
| 6 |
* |
| 7 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 |
* you may not use this file except in compliance with the License. |
| 9 |
* You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* -or- |
| 13 |
* see COPYING.ASL20 in the source distribution |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, software |
| 16 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 17 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 |
* See the License for the specific language governing permissions and |
| 19 |
* limitations under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef WTP_H_INCLUDED |
| 23 |
#define WTP_H_INCLUDED |
| 24 |
|
| 25 |
#include <pthread.h> |
| 26 |
#include "obj.h" |
| 27 |
#include "atomic.h" |
| 28 |
|
| 29 |
/* states for worker threads. |
| 30 |
* important: they need to be increasing with all previous state bits |
| 31 |
* set. That is because we can only atomically or a value! |
| 32 |
*/ |
| 33 |
#define WRKTHRD_STOPPED 0 |
| 34 |
#define WRKTHRD_INITIALIZING 1 |
| 35 |
#define WRKTHRD_RUNNING 3 |
| 36 |
#define WRKTHRD_WAIT_JOIN 7 |
| 37 |
|
| 38 |
|
| 39 |
/* possible states of a worker thread pool */ |
| 40 |
typedef enum { |
| 41 |
wtpState_RUNNING = 0, /* runs in regular mode */ |
| 42 |
wtpState_SHUTDOWN = 1, /* worker threads shall shutdown when idle */ |
| 43 |
wtpState_SHUTDOWN_IMMEDIATE = 2 /* worker threads shall shutdown ASAP, even if not idle */ |
| 44 |
} wtpState_t; |
| 45 |
|
| 46 |
|
| 47 |
/* the worker thread pool (wtp) object */ |
| 48 |
struct wtp_s { |
| 49 |
BEGINobjInstance |
| 50 |
; |
| 51 |
wtpState_t wtpState; |
| 52 |
int iNumWorkerThreads; /* number of worker threads to use */ |
| 53 |
int iCurNumWrkThrd; /* current number of active worker threads */ |
| 54 |
struct wti_s **pWrkr; /* array with control structure for the worker thread(s) associated with this wtp */ |
| 55 |
int toWrkShutdown; /* timeout for idle workers in ms, -1 means indefinite (0 is immediate) */ |
| 56 |
rsRetVal (*pConsumer)(void *); /* user-supplied consumer function for dewtpd messages */ |
| 57 |
/* synchronization variables */ |
| 58 |
pthread_mutex_t mutWtp; /* mutex for the wtp's thread management */ |
| 59 |
pthread_cond_t condThrdInitDone; /* signalled when a new thread is ready for work */ |
| 60 |
pthread_cond_t condThrdTrm; /* signalled when threads terminate */ |
| 61 |
/* end sync variables */ |
| 62 |
/* user objects */ |
| 63 |
void *pUsr; /* pointer to user object (in this case, the queue the wtp belongs to) */ |
| 64 |
pthread_attr_t attrThrd; /* attribute for new threads (created just once and cached here) */ |
| 65 |
pthread_mutex_t *pmutUsr; |
| 66 |
rsRetVal (*pfChkStopWrkr)(void *pUsr, int); |
| 67 |
rsRetVal (*pfGetDeqBatchSize)(void *pUsr, int *); /* obtains max dequeue count from queue config */ |
| 68 |
rsRetVal (*pfObjProcessed)(void *pUsr, wti_t *pWti); /* indicate user object is processed */ |
| 69 |
rsRetVal (*pfRateLimiter)(void *pUsr); |
| 70 |
rsRetVal (*pfDoWork)(void *pUsr, void *pWti); |
| 71 |
/* end user objects */ |
| 72 |
uchar *pszDbgHdr; /* header string for debug messages */ |
| 73 |
DEF_ATOMIC_HELPER_MUT(mutCurNumWrkThrd); |
| 74 |
DEF_ATOMIC_HELPER_MUT(mutWtpState); |
| 75 |
}; |
| 76 |
|
| 77 |
/* some symbolic constants for easier reference */ |
| 78 |
|
| 79 |
|
| 80 |
#define DENY_WORKER_START_DURING_SHUTDOWN 0 |
| 81 |
#define PERMIT_WORKER_START_DURING_SHUTDOWN 1 |
| 82 |
|
| 83 |
/* prototypes */ |
| 84 |
rsRetVal wtpConstruct(wtp_t **ppThis); |
| 85 |
rsRetVal wtpConstructFinalize(wtp_t *pThis); |
| 86 |
rsRetVal wtpDestruct(wtp_t **ppThis); |
| 87 |
rsRetVal wtpAdviseMaxWorkers(wtp_t *pThis, int nMaxWrkr, const int permit_during_shutdown); |
| 88 |
rsRetVal wtpProcessThrdChanges(wtp_t *pThis); |
| 89 |
rsRetVal wtpChkStopWrkr(wtp_t *pThis, int bLockUsrMutex); |
| 90 |
rsRetVal wtpSetState(wtp_t *pThis, wtpState_t iNewState); |
| 91 |
rsRetVal wtpWakeupAllWrkr(wtp_t *pThis); |
| 92 |
rsRetVal wtpCancelAll(wtp_t *pThis, const uchar *const cancelobj); |
| 93 |
rsRetVal wtpSetDbgHdr(wtp_t *pThis, uchar *pszMsg, size_t lenMsg); |
| 94 |
rsRetVal wtpShutdownAll(wtp_t *pThis, wtpState_t tShutdownCmd, struct timespec *ptTimeout); |
| 95 |
PROTOTYPEObjClassInit(wtp); |
| 96 |
PROTOTYPEObjClassExit(wtp); |
| 97 |
PROTOTYPEpropSetMethFP(wtp, pfChkStopWrkr, rsRetVal (*pVal)(void *, int)); |
| 98 |
PROTOTYPEpropSetMethFP(wtp, pfRateLimiter, rsRetVal (*pVal)(void *)); |
| 99 |
PROTOTYPEpropSetMethFP(wtp, pfGetDeqBatchSize, rsRetVal (*pVal)(void *, int *)); |
| 100 |
PROTOTYPEpropSetMethFP(wtp, pfDoWork, rsRetVal (*pVal)(void *, void *)); |
| 101 |
PROTOTYPEpropSetMethFP(wtp, pfObjProcessed, rsRetVal (*pVal)(void *, wti_t *)); |
| 102 |
PROTOTYPEpropSetMeth(wtp, toWrkShutdown, long); |
| 103 |
PROTOTYPEpropSetMeth(wtp, wtpState, wtpState_t); |
| 104 |
PROTOTYPEpropSetMeth(wtp, iMaxWorkerThreads, int); |
| 105 |
PROTOTYPEpropSetMeth(wtp, pUsr, void *); |
| 106 |
PROTOTYPEpropSetMeth(wtp, iNumWorkerThreads, int); |
| 107 |
PROTOTYPEpropSetMethPTR(wtp, pmutUsr, pthread_mutex_t); |
| 108 |
|
| 109 |
#endif /* #ifndef WTP_H_INCLUDED */ |