Manage a threadpool (for internal use only). More...
Go to the source code of this file.
Classes | |
struct | timezone |
Timezone. More... | |
struct | ThreadPoolAttr |
Attributes for thread pool. More... | |
struct | ThreadPoolJob |
Internal ThreadPool Job. More... | |
struct | ThreadPoolStats |
Structure to hold statistics. More... | |
struct | ThreadPool |
A thread pool. More... | |
Macros | |
#define | INVALID_JOB_ID (-2 & 1 << 29) |
Invalid JOB Id. | |
#define | STATS 1 |
Statistics. | |
#define | DEFAULT_POLICY SCHED_OTHER |
Define default schedule policy that are defined in <sched.h>. | |
Typedefs | |
typedef void(* | free_routine) (void *arg) |
typedef int | PolicyType |
Type of the thread policy. | |
Enumerations | |
enum | Duration { SHORT_TERM , PERSISTENT } |
Duration. More... | |
enum | ThreadPriority { LOW_PRIORITY , MED_PRIORITY , HIGH_PRIORITY } |
Thread priority. More... | |
Functions | |
int | gettimeofday (struct timeval *tv, struct timezone *tz) |
Get time of day. | |
void | TPSetMaxJobsTotal (int mjt) |
Sets the maximum number of jobs in the thread pool. | |
int | ThreadPoolInit (ThreadPool *tp, ThreadPoolAttr *attr) |
Initializes and starts ThreadPool. | |
int | ThreadPoolAddPersistent (ThreadPool *tp, ThreadPoolJob *job, int *jobId) |
Adds a persistent job to the thread pool. | |
int | ThreadPoolGetAttr (ThreadPool *tp, ThreadPoolAttr *out) |
Gets the current set of attributes associated with the thread pool. | |
int | ThreadPoolSetAttr (ThreadPool *tp, ThreadPoolAttr *attr) |
Sets the attributes for the thread pool. | |
int | ThreadPoolAdd (ThreadPool *tp, ThreadPoolJob *job, int *jobId) |
Adds a job to the thread pool. | |
int | ThreadPoolRemove (ThreadPool *tp, int jobId, ThreadPoolJob *out) |
Removes a job from the thread pool. | |
int | ThreadPoolShutdown (ThreadPool *tp) |
Shuts the thread pool down. | |
int | TPJobInit (ThreadPoolJob *job, UPnPsdk::start_routine func, void *arg) |
Initializes thread pool job. | |
int | TPJobSetPriority (ThreadPoolJob *job, ThreadPriority priority) |
Sets the priority of the threadpool job. | |
int | TPJobSetFreeFunction (ThreadPoolJob *job, free_routine func) |
Sets the jobs free function. | |
int | TPAttrInit (ThreadPoolAttr *attr) |
Initializes thread pool attributes. | |
int | TPAttrSetMaxThreads (ThreadPoolAttr *attr, int maxThreads) |
Sets the max threads for the thread pool attributes. | |
int | TPAttrSetMinThreads (ThreadPoolAttr *attr, int minThreads) |
Sets the min threads for the thread pool attributes. | |
int | TPAttrSetStackSize (ThreadPoolAttr *attr, size_t stackSize) |
Sets the stack size for the thread pool attributes. | |
int | TPAttrSetIdleTime (ThreadPoolAttr *attr, int idleTime) |
Sets the idle time for the thread pool attributes. | |
int | TPAttrSetJobsPerThread (ThreadPoolAttr *attr, int jobsPerThread) |
Sets the jobs per thread ratio. | |
int | TPAttrSetStarvationTime (ThreadPoolAttr *attr, int starvationTime) |
Sets the starvation time for the thread pool attributes. | |
int | TPAttrSetSchedPolicy (ThreadPoolAttr *attr, PolicyType schedPolicy) |
Sets the scheduling policy for the thread pool attributes. | |
int | TPAttrSetMaxJobsTotal (ThreadPoolAttr *attr, int totalMaxJobs) |
Sets the maximum number jobs that can be qeued totally. | |
int | ThreadPoolGetStats (ThreadPool *tp, ThreadPoolStats *stats) |
Returns various statistics about the thread pool. | |
void | ThreadPoolPrintStats (ThreadPoolStats *stats) |
Prints various statistics about the thread pool to stderr. | |
Variables | |
constexpr ThreadPriority | DEFAULT_PRIORITY {MED_PRIORITY} |
constexpr int | DEFAULT_MIN_THREADS {1} |
constexpr int | DEFAULT_MAX_THREADS {10} |
constexpr int | DEFAULT_STACK_SIZE {0u} |
constexpr int | DEFAULT_JOBS_PER_THREAD {10} |
constexpr int | DEFAULT_STARVATION_TIME {500} |
constexpr int | DEFAULT_IDLE_TIME {10 * 1000} |
constexpr free_routine | DEFAULT_FREE_ROUTINE {nullptr} |
constexpr int | DEFAULT_MAX_JOBS_TOTAL {100} |
int | maxJobsTotal |
Manage a threadpool (for internal use only).
Because this is for internal use, parameters are NOT checked for validity. The caller must ensure valid parameters.
Definition in file ThreadPool.hpp.
struct timezone |
Timezone.
Definition at line 52 of file ThreadPool.hpp.
Class Members | ||
---|---|---|
int | tz_minuteswest | Minutes W of Greenwich. |
int | tz_dsttime | Type of dst correction. |
struct ThreadPoolAttr |
Attributes for thread pool.
Used to set and change parameters of thread pool.
Definition at line 128 of file ThreadPool.hpp.
Class Members | ||
---|---|---|
int | minThreads | ThreadPool will always maintain at least this many threads. |
int | maxThreads | ThreadPool will never have more than this number of threads. |
size_t | stackSize | This is the minimum stack size allocated for each thread. |
int | maxIdleTime | this is the maximum time a thread will remain idle before dying (in milliseconds). |
int | jobsPerThread | Jobs per thread to maintain. |
int | maxJobsTotal | Maximum number of jobs that can be queued totally. |
int | starvationTime | The time a low priority or med priority job waits before getting bumped up a priority (in milliseconds). |
PolicyType | schedPolicy | Scheduling policy to use. |
struct ThreadPoolJob |
Internal ThreadPool Job.
Definition at line 150 of file ThreadPool.hpp.
Class Members | ||
---|---|---|
start_routine | func | |
void * | arg | |
free_routine | free_func | |
struct timeval | requestTime | |
ThreadPriority | priority | |
int | jobId |
struct ThreadPoolStats |
Structure to hold statistics.
Definition at line 160 of file ThreadPool.hpp.
struct ThreadPool |
A thread pool.
Allows jobs to be scheduled for running by threads in a thread pool. The thread pool is initialized with a minimum and maximum thread number as well as a max idle time and a jobs per thread ratio. If a worker thread waits the whole max idle time without receiving a job and the thread pool currently has more threads running than the minimum then the worker thread will exit. If when scheduling a job the current job to thread ratio becomes greater than the set ratio and the thread pool currently has less than the maximum threads then a new thread will be created.
Definition at line 194 of file ThreadPool.hpp.
Class Members | ||
---|---|---|
pthread_mutex_t | mutex |
Mutex to protect job qs. |
pthread_cond_t | condition |
Condition variable to signal Q. |
pthread_cond_t | start_and_shutdown |
Condition variable for start and stop. |
int | lastJobId |
ids for jobs |
int | shutdown |
whether or not we are shutting down |
int | totalThreads |
total number of threads |
int | pendingWorkerThreadStart |
flag that's set when waiting for a new worker thread to start |
int | busyThreads |
number of threads that are currently executing jobs |
int | persistentThreads |
number of persistent threads |
FreeList | jobFreeList |
free list of jobs |
LinkedList | lowJobQ |
low priority job Q |
LinkedList | medJobQ |
med priority job Q |
LinkedList | highJobQ |
high priority job Q |
ThreadPoolJob * | persistentJob |
persistent job |
ThreadPoolAttr | attr |
thread pool attributes |
ThreadPoolStats | stats |
statistics |
#define INVALID_JOB_ID (-2 & 1 << 29) |
Invalid JOB Id.
Definition at line 68 of file ThreadPool.hpp.
#define STATS 1 |
Statistics.
Always include stats because code change is minimal.
Definition at line 113 of file ThreadPool.hpp.
#define DEFAULT_POLICY SCHED_OTHER |
Define default schedule policy that are defined in <sched.h>.
Definition at line 123 of file ThreadPool.hpp.
typedef void(* free_routine) (void *arg) |
Function for freeing a thread argument.
Definition at line 71 of file ThreadPool.hpp.
typedef int PolicyType |
Type of the thread policy.
Definition at line 120 of file ThreadPool.hpp.
enum Duration |
Duration.
Definition at line 74 of file ThreadPool.hpp.
enum ThreadPriority |
Thread priority.
Definition at line 77 of file ThreadPool.hpp.
int gettimeofday | ( | struct timeval * | tv, |
struct timezone * | tz | ||
) |
Get time of day.
Definition at line 1211 of file ThreadPool.cpp.
void TPSetMaxJobsTotal | ( | int | mjt | ) |
Sets the maximum number of jobs in the thread pool.
This option is intended for server applications to avoid an overflow of jobs when serving e.g. many web requests.
Definition at line 1026 of file ThreadPool.cpp.
int ThreadPoolInit | ( | ThreadPool * | tp, |
ThreadPoolAttr * | attr | ||
) |
Initializes and starts ThreadPool.
Must be called first and only once for ThreadPool.
[in] | tp | Must be valid, non null, pointer to ThreadPool. |
[in] | attr | Can be nullptr. If not nullptr then attr contains the following fields:
|
Definition at line 669 of file ThreadPool.cpp.
int ThreadPoolAddPersistent | ( | ThreadPool * | tp, |
ThreadPoolJob * | job, | ||
int * | jobId | ||
) |
Adds a persistent job to the thread pool.
Job will be run as soon as possible. Call will block until job is scheduled.
[in] | tp | Valid thread pool pointer. |
[in] | job | Valid thread pool job. |
[in] | jobId | Job ID |
Definition at line 737 of file ThreadPool.cpp.
int ThreadPoolGetAttr | ( | ThreadPool * | tp, |
ThreadPoolAttr * | out | ||
) |
Gets the current set of attributes associated with the thread pool.
[in] | tp | Valid thread pool pointer. |
[in] | out | Non null pointer to store attributes. |
Definition at line 892 of file ThreadPool.cpp.
int ThreadPoolSetAttr | ( | ThreadPool * | tp, |
ThreadPoolAttr * | attr | ||
) |
Sets the attributes for the thread pool.
Only affects future calculations.
[in] | tp | Valid thread pool pointer. |
[in] | attr | Pointer to attributes, null sets attributes to default. |
Definition at line 904 of file ThreadPool.cpp.
int ThreadPoolAdd | ( | ThreadPool * | tp, |
ThreadPoolJob * | job, | ||
int * | jobId | ||
) |
Adds a job to the thread pool.
Job will be run as soon as possible.
[in] | tp | Valid thread pool pointer. |
[in] | job | Job |
[in] | jobId | id of job. |
Definition at line 784 of file ThreadPool.cpp.
int ThreadPoolRemove | ( | ThreadPool * | tp, |
int | jobId, | ||
ThreadPoolJob * | out | ||
) |
Removes a job from the thread pool.
Can only remove jobs which are not currently running.
[in] | tp | Valid thread pool pointer. |
[in] | jobId | Id of job. |
[out] | out | Space for removed job. |
Definition at line 836 of file ThreadPool.cpp.
int ThreadPoolShutdown | ( | ThreadPool * | tp | ) |
Shuts the thread pool down.
Waits for all threads to finish. May block indefinitely if jobs do not exit.
[in] | tp | Must be valid tp. |
Definition at line 944 of file ThreadPool.cpp.
int TPJobInit | ( | ThreadPoolJob * | job, |
UPnPsdk::start_routine | func, | ||
void * | arg | ||
) |
Initializes thread pool job.
Sets the priority to default defined in ThreadPool.hpp. Sets the free_routine to default defined in ThreadPool.hpp.
[in] | job | Must be valid thread pool attributes. |
[in] | func | Function to run, must be valid. |
[in] | arg | Argument to pass to function. |
Definition at line 1043 of file ThreadPool.cpp.
int TPJobSetPriority | ( | ThreadPoolJob * | job, |
ThreadPriority | priority | ||
) |
Sets the priority of the threadpool job.
[in] | job | Must be valid thread pool attributes. |
[in] | priority | Value to set. |
Definition at line 1054 of file ThreadPool.cpp.
int TPJobSetFreeFunction | ( | ThreadPoolJob * | job, |
free_routine | func | ||
) |
Sets the jobs free function.
[in] | job | Must be valid thread pool attributes. |
[in] | func | Value to set. |
Definition at line 1068 of file ThreadPool.cpp.
int TPAttrInit | ( | ThreadPoolAttr * | attr | ) |
Initializes thread pool attributes.
Sets values to defaults defined in ThreadPool.hpp.
[in] | attr | Must be valid thread pool attributes. |
Definition at line 1028 of file ThreadPool.cpp.
int TPAttrSetMaxThreads | ( | ThreadPoolAttr * | attr, |
int | maxThreads | ||
) |
Sets the max threads for the thread pool attributes.
[in] | attr | Must be valid thread pool attributes. |
[in] | maxThreads | Value to set. |
Definition at line 1076 of file ThreadPool.cpp.
int TPAttrSetMinThreads | ( | ThreadPoolAttr * | attr, |
int | minThreads | ||
) |
Sets the min threads for the thread pool attributes.
[in] | attr | must be valid thread pool attributes. |
[in] | minThreads | value to set. |
Definition at line 1084 of file ThreadPool.cpp.
int TPAttrSetStackSize | ( | ThreadPoolAttr * | attr, |
size_t | stackSize | ||
) |
Sets the stack size for the thread pool attributes.
[in] | attr | Must be valid thread pool attributes. |
[in] | stackSize | Value to set. |
Definition at line 1092 of file ThreadPool.cpp.
int TPAttrSetIdleTime | ( | ThreadPoolAttr * | attr, |
int | idleTime | ||
) |
Sets the idle time for the thread pool attributes.
[in] | attr | Must be valid thread pool attributes. |
[in] | idleTime | Idle time |
Definition at line 1100 of file ThreadPool.cpp.
int TPAttrSetJobsPerThread | ( | ThreadPoolAttr * | attr, |
int | jobsPerThread | ||
) |
Sets the jobs per thread ratio.
[in] | attr | Must be valid thread pool attributes. |
[in] | jobsPerThread | Number of jobs per thread to maintain. |
Definition at line 1108 of file ThreadPool.cpp.
int TPAttrSetStarvationTime | ( | ThreadPoolAttr * | attr, |
int | starvationTime | ||
) |
Sets the starvation time for the thread pool attributes.
[in] | attr | Must be valid thread pool attributes. |
[in] | starvationTime | Milliseconds. |
Definition at line 1116 of file ThreadPool.cpp.
int TPAttrSetSchedPolicy | ( | ThreadPoolAttr * | attr, |
PolicyType | schedPolicy | ||
) |
Sets the scheduling policy for the thread pool attributes.
[in] | attr | Must be valid thread pool attributes. |
[in] | schedPolicy | Must be a valid policy type. |
Definition at line 1124 of file ThreadPool.cpp.
int TPAttrSetMaxJobsTotal | ( | ThreadPoolAttr * | attr, |
int | totalMaxJobs | ||
) |
Sets the maximum number jobs that can be qeued totally.
[in] | attr | Must be valid thread pool attributes. |
[in] | totalMaxJobs | Maximum number of jobs. |
Definition at line 1132 of file ThreadPool.cpp.
int ThreadPoolGetStats | ( | ThreadPool * | tp, |
ThreadPoolStats * | stats | ||
) |
Returns various statistics about the thread pool.
Only valid if STATS has been defined.
[in] | tp | Valid initialized threadpool. |
[out] | stats | Valid stats, out parameter. |
Definition at line 1168 of file ThreadPool.cpp.
void ThreadPoolPrintStats | ( | ThreadPoolStats * | stats | ) |
Prints various statistics about the thread pool to stderr.
Only valid if STATS has been defined.
[in] | stats | Valid threadpool stats |
Definition at line 1141 of file ThreadPool.cpp.
|
constexpr |
default priority used by TPJobInit
Definition at line 80 of file ThreadPool.hpp.
|
constexpr |
default minimum used by TPAttrInit
Definition at line 83 of file ThreadPool.hpp.
|
constexpr |
default max used by TPAttrInit
Definition at line 86 of file ThreadPool.hpp.
|
constexpr |
default stack size used by TPAttrInit
Definition at line 89 of file ThreadPool.hpp.
|
constexpr |
default jobs per thread used by TPAttrInit
Definition at line 92 of file ThreadPool.hpp.
|
constexpr |
default starvation time used by TPAttrInit
Definition at line 95 of file ThreadPool.hpp.
|
constexpr |
default idle time used by TPAttrInit
Definition at line 98 of file ThreadPool.hpp.
|
constexpr |
default free routine used TPJobInit
Definition at line 101 of file ThreadPool.hpp.
|
constexpr |
default max jobs used TPAttrInit
Definition at line 104 of file ThreadPool.hpp.
|
extern |
Specify how many jobs maximal can be used with the threadpool
Definition at line 1024 of file ThreadPool.cpp.