Manage a threadpool (for internal use only). More...
Go to the source code of this file.
Macros | |
#define | EMAXTHREADS (-8 & 1 << 29) |
#define | INVALID_POLICY (-9 & 1 << 29) |
Functions | |
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 | 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 | 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 | ThreadPoolShutdown (ThreadPool *tp) |
Shuts the thread pool down. | |
void | TPSetMaxJobsTotal (int mjt) |
Sets the maximum number of jobs in the thread pool. | |
int | TPAttrInit (ThreadPoolAttr *attr) |
Initializes thread pool attributes. | |
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 | 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. | |
void | ThreadPoolPrintStats (ThreadPoolStats *stats) |
Prints various statistics about the thread pool to stderr. | |
int | ThreadPoolGetStats (ThreadPool *tp, ThreadPoolStats *stats) |
Returns various statistics about the thread pool. | |
int | gettimeofday (struct timeval *tv, struct timezone *tz) |
Get time of day. | |
Scope restricted to file | |
long | anonymous_namespace{ThreadPool.cpp}::DiffMillis (timeval *time1, timeval *time2) |
Returns the difference in milliseconds between two timeval structures. | |
void | anonymous_namespace{ThreadPool.cpp}::StatsInit (ThreadPoolStats *stats) |
Initializes the statistics structure. | |
void | anonymous_namespace{ThreadPool.cpp}::StatsAccountLQ (ThreadPool *tp, long diffTime) |
StatsAccountLQ. | |
void | anonymous_namespace{ThreadPool.cpp}::StatsAccountMQ (ThreadPool *tp, long diffTime) |
StatsAccountMQ. | |
void | anonymous_namespace{ThreadPool.cpp}::StatsAccountHQ (ThreadPool *tp, long diffTime) |
StatsAccountHQ. | |
void | anonymous_namespace{ThreadPool.cpp}::CalcWaitTime (ThreadPool *tp, ThreadPriority p, ThreadPoolJob *job) |
Calculates the time the job has been waiting at the specified priority. | |
time_t | anonymous_namespace{ThreadPool.cpp}::StatsTime (time_t *t) |
StatsTime. | |
int | anonymous_namespace{ThreadPool.cpp}::CmpThreadPoolJob (void *jobA, void *jobB) |
Compares thread pool jobs. | |
void | anonymous_namespace{ThreadPool.cpp}::FreeThreadPoolJob (ThreadPool *tp, ThreadPoolJob *tpj) |
Deallocates a dynamically allocated ThreadPoolJob. | |
int | anonymous_namespace{ThreadPool.cpp}::SetPolicyType (PolicyType in) |
Sets the scheduling policy of the current process. | |
int | anonymous_namespace{ThreadPool.cpp}::SetPriority (ThreadPriority priority) |
Sets the priority of the currently running thread. | |
void | anonymous_namespace{ThreadPool.cpp}::BumpPriority (ThreadPool *tp) |
Determines whether any jobs need to be bumped to a higher priority Q and bumps them. | |
void | anonymous_namespace{ThreadPool.cpp}::SetRelTimeout (timespec *time, int relMillis) |
Sets the fields of the passed in timespec to be relMillis milliseconds in the future. | |
void | anonymous_namespace{ThreadPool.cpp}::SetSeed (void) |
Sets seed for random number generator. | |
void * | anonymous_namespace{ThreadPool.cpp}::WorkerThread (void *arg) |
Implements a thread pool worker. | |
ThreadPoolJob * | anonymous_namespace{ThreadPool.cpp}::CreateThreadPoolJob (ThreadPoolJob *job, int id, ThreadPool *tp) |
Creates a Thread Pool Job. (Dynamically allocated) | |
int | anonymous_namespace{ThreadPool.cpp}::CreateWorker (ThreadPool *tp) |
Creates a worker thread, if the thread pool does not already have max threads. | |
void | anonymous_namespace{ThreadPool.cpp}::AddWorker (ThreadPool *tp) |
Determines whether or not a thread should be added based on the jobsPerThread ratio. | |
Variables | |
constexpr int | JOBFREELISTSIZE {100} |
constexpr int | INFINITE_THREADS {-1} |
int | maxJobsTotal = DEFAULT_MAX_JOBS_TOTAL |
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.cpp.
#define EMAXTHREADS (-8 & 1 << 29) |
Error: maximun threads.
Definition at line 60 of file ThreadPool.cpp.
#define INVALID_POLICY (-9 & 1 << 29) |
Invalid Policy
Definition at line 62 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 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 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 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.
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 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 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 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.
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.
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.
int gettimeofday | ( | struct timeval * | tv, |
struct timezone * | tz | ||
) |
Get time of day.
Definition at line 1211 of file ThreadPool.cpp.
|
constexpr |
Size of job free list.
Definition at line 56 of file ThreadPool.cpp.
|
constexpr |
Infinite threads.
Definition at line 58 of file ThreadPool.cpp.
int maxJobsTotal = DEFAULT_MAX_JOBS_TOTAL |
Specify how many jobs maximal can be used with the threadpool
Definition at line 1024 of file ThreadPool.cpp.