libUPnP
1.14.5
|
#include <sys/param.h>
#include "ThreadPool.h"
#include "FreeList.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Functions | |
static long | DiffMillis (struct timeval *time1, struct timeval *time2) |
Returns the difference in milliseconds between two timeval structures. More... | |
static void | StatsInit (ThreadPoolStats *stats) |
Initializes the statistics structure. More... | |
static void | StatsAccountLQ (ThreadPool *tp, long diffTime) |
static void | StatsAccountMQ (ThreadPool *tp, long diffTime) |
static void | StatsAccountHQ (ThreadPool *tp, long diffTime) |
static void | CalcWaitTime (ThreadPool *tp, ThreadPriority p, ThreadPoolJob *job) |
Calculates the time the job has been waiting at the specified priority. More... | |
static time_t | StatsTime (time_t *t) |
static int | CmpThreadPoolJob (void *jobA, void *jobB) |
Compares thread pool jobs. More... | |
static void | FreeThreadPoolJob (ThreadPool *tp, ThreadPoolJob *tpj) |
Deallocates a dynamically allocated ThreadPoolJob. More... | |
static int | SetPolicyType (PolicyType in) |
Sets the scheduling policy of the current process. More... | |
static int | SetPriority (ThreadPriority priority) |
Sets the priority of the currently running thread. More... | |
static void | BumpPriority (ThreadPool *tp) |
Determines whether any jobs need to be bumped to a higher priority Q and bumps them. More... | |
static void | SetRelTimeout (struct timespec *time, int relMillis) |
Sets the fields of the passed in timespec to be relMillis milliseconds in the future. More... | |
static void | SetSeed (void) |
Sets seed for random number generator. Each thread sets the seed random number generator. More... | |
static void * | WorkerThread (void *arg) |
Implements a thread pool worker. Worker waits for a job to become available. Worker picks up persistent jobs first, high priority, med priority, then low priority. More... | |
static ThreadPoolJob * | CreateThreadPoolJob (ThreadPoolJob *job, int id, ThreadPool *tp) |
Creates a Thread Pool Job. (Dynamically allocated) More... | |
static int | CreateWorker (ThreadPool *tp) |
Creates a worker thread, if the thread pool does not already have max threads. More... | |
static void | AddWorker (ThreadPool *tp) |
Determines whether or not a thread should be added based on the jobsPerThread ratio. Adds a thread if appropriate. More... | |
int | ThreadPoolInit (ThreadPool *tp, ThreadPoolAttr *attr) |
Initializes and starts ThreadPool. Must be called first and only once for ThreadPool. More... | |
int | ThreadPoolAddPersistent (ThreadPool *tp, ThreadPoolJob *job, int *jobId) |
Adds a persistent job to the thread pool. More... | |
int | ThreadPoolAdd (ThreadPool *tp, ThreadPoolJob *job, int *jobId) |
Adds a job to the thread pool. Job will be run as soon as possible. More... | |
int | ThreadPoolRemove (ThreadPool *tp, int jobId, ThreadPoolJob *out) |
Removes a job from the thread pool. Can only remove jobs which are not currently running. More... | |
int | ThreadPoolGetAttr (ThreadPool *tp, ThreadPoolAttr *out) |
Gets the current set of attributes associated with the thread pool. More... | |
int | ThreadPoolSetAttr (ThreadPool *tp, ThreadPoolAttr *attr) |
Sets the attributes for the thread pool. Only affects future calculations. More... | |
int | ThreadPoolShutdown (ThreadPool *tp) |
Shuts the thread pool down. Waits for all threads to finish. May block indefinitely if jobs do not exit. More... | |
int | TPAttrInit (ThreadPoolAttr *attr) |
Initializes thread pool attributes. Sets values to defaults defined in ThreadPool.h. More... | |
int | TPJobInit (ThreadPoolJob *job, start_routine func, void *arg) |
Initializes thread pool job. Sets the priority to default defined in ThreadPool.h. Sets the free_routine to default defined in ThreadPool.h. More... | |
int | TPJobSetPriority (ThreadPoolJob *job, ThreadPriority priority) |
Sets the max threads for the thread pool attributes. More... | |
int | TPJobSetFreeFunction (ThreadPoolJob *job, free_routine func) |
Sets the max threads for the thread pool attributes. More... | |
int | TPAttrSetMaxThreads (ThreadPoolAttr *attr, int maxThreads) |
Sets the max threads for the thread pool attributes. More... | |
int | TPAttrSetMinThreads (ThreadPoolAttr *attr, int minThreads) |
Sets the min threads for the thread pool attributes. More... | |
int | TPAttrSetStackSize (ThreadPoolAttr *attr, size_t stackSize) |
Sets the stack size for the thread pool attributes. More... | |
int | TPAttrSetIdleTime (ThreadPoolAttr *attr, int idleTime) |
Sets the idle time for the thread pool attributes. More... | |
int | TPAttrSetJobsPerThread (ThreadPoolAttr *attr, int jobsPerThread) |
Sets the jobs per thread ratio. More... | |
int | TPAttrSetStarvationTime (ThreadPoolAttr *attr, int starvationTime) |
Sets the starvation time for the thread pool attributes. More... | |
int | TPAttrSetSchedPolicy (ThreadPoolAttr *attr, PolicyType schedPolicy) |
Sets the scheduling policy for the thread pool attributes. More... | |
int | TPAttrSetMaxJobsTotal (ThreadPoolAttr *attr, int maxJobsTotal) |
Sets the maximum number jobs that can be qeued totally. More... | |
void | ThreadPoolPrintStats (ThreadPoolStats *stats) |
int | ThreadPoolGetStats (ThreadPool *tp, ThreadPoolStats *stats) |
Returns various statistics about the thread pool. More... | |
|
static |
Determines whether or not a thread should be added based on the jobsPerThread ratio. Adds a thread if appropriate.
tp | A pointer to the ThreadPool object. |
References THREADPOOL::attr, THREADPOOL::busyThreads, CreateWorker(), THREADPOOL::highJobQ, THREADPOOLATTR::jobsPerThread, THREADPOOL::lowJobQ, THREADPOOL::medJobQ, THREADPOOL::persistentThreads, LINKEDLIST::size, and THREADPOOL::totalThreads.
|
static |
Determines whether any jobs need to be bumped to a higher priority Q and bumps them.
tp->mutex must be locked.
tp | . |
References THREADPOOL::attr, DiffMillis(), LINKEDLIST::head, THREADPOOL::highJobQ, ListAddTail(), ListDelNode(), THREADPOOL::lowJobQ, THREADPOOLATTR::maxIdleTime, THREADPOOL::medJobQ, LINKEDLIST::size, THREADPOOLATTR::starvationTime, StatsAccountLQ(), and StatsAccountMQ().
|
static |
Calculates the time the job has been waiting at the specified priority.
Adds to the totalTime and totalJobs kept in the thread pool statistics structure.
tp | . |
p | . |
job | . |
References DiffMillis().
|
static |
Compares thread pool jobs.
|
static |
Creates a Thread Pool Job. (Dynamically allocated)
job | job is copied. |
id | id of job. |
tp | . |
References FreeListAlloc(), and THREADPOOL::jobFreeList.
|
static |
Creates a worker thread, if the thread pool does not already have max threads.
0
on success, < 0 on failure. EMAXTHREADS
if already max threads reached. EAGAIN
if system can not create thread. tp | A pointer to the ThreadPool object. |
References THREADPOOL::pendingWorkerThreadStart.
Referenced by AddWorker().
|
static |
Returns the difference in milliseconds between two timeval structures.
time1 | . |
time2 | . |
Referenced by BumpPriority(), and CalcWaitTime().
|
static |
Deallocates a dynamically allocated ThreadPoolJob.
tp | . |
tpj | Must be allocated with CreateThreadPoolJob. |
References FreeListFree(), and THREADPOOL::jobFreeList.
|
static |
Sets the scheduling policy of the current process.
0
on success. result
of GetLastError() on failure. in | . |
|
static |
Sets the priority of the currently running thread.
0
on success. EINVAL
invalid priority or the result of GerLastError. priority | . |
|
static |
Sets the fields of the passed in timespec to be relMillis milliseconds in the future.
time | . |
relMillis | milliseconds in the future. |
|
static |
Sets seed for random number generator. Each thread sets the seed random number generator.
|
static |
tp | . |
diffTime | . |
References THREADPOOL::stats.
|
static |
|
static |
|
static |
Initializes the statistics structure.
stats | Must be valid non null stats structure. |
|
static |
t | . |
Referenced by ThreadPoolPrintStats().
int ThreadPoolAdd | ( | ThreadPool * | tp, |
ThreadPoolJob * | job, | ||
int * | jobId | ||
) |
Adds a job to the thread pool. Job will be run as soon as possible.
0
on success, nonzero on failure. EOUTOFMEM
if not enough memory to add job. tp | valid thread pool pointer. |
job | . |
jobId | id of job. |
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.
0
on success. EOUTOFMEM
not enough memory to add job. EMAXTHREADS
not enough threads to add persistent job. tp | Valid thread pool pointer. |
job | Valid thread pool job. |
jobId | . |
References INVALID_JOB_ID.
int ThreadPoolGetAttr | ( | ThreadPool * | tp, |
ThreadPoolAttr * | out | ||
) |
Gets the current set of attributes associated with the thread pool.
0
on success, nonzero on failure. tp | valid thread pool pointer. |
out | non null pointer to store attributes. |
References THREADPOOL::shutdown.
int ThreadPoolGetStats | ( | ThreadPool * | tp, |
ThreadPoolStats * | stats | ||
) |
Returns various statistics about the thread pool.
Only valid if STATS has been defined.
tp | Valid initialized threadpool. |
stats | Valid stats, out parameter. |
References THREADPOOL::shutdown.
Referenced by PrintThreadPoolStats().
int ThreadPoolInit | ( | ThreadPool * | tp, |
ThreadPoolAttr * | attr | ||
) |
Initializes and starts ThreadPool. Must be called first and only once for ThreadPool.
0
on success. EAGAIN
if not enough system resources to create minimum threads. INVALID_POLICY
if schedPolicy can't be set. EMAXTHREADS
if minimum threads is greater than maximum threads. tp | Must be valid, non null, pointer to ThreadPool. |
attr | Can be null. if not null then attr contains the following fields:
|
Referenced by UpnpInitThreadPools().
void ThreadPoolPrintStats | ( | ThreadPoolStats * | stats | ) |
stats | . |
References StatsTime().
int ThreadPoolRemove | ( | ThreadPool * | tp, |
int | jobId, | ||
ThreadPoolJob * | out | ||
) |
Removes a job from the thread pool. Can only remove jobs which are not currently running.
0
on success, nonzero on failure. INVALID_JOB_ID
if job not found. tp | valid thread pool pointer. |
jobId | id of job. |
out | space for removed job. |
References INVALID_JOB_ID.
int ThreadPoolSetAttr | ( | ThreadPool * | tp, |
ThreadPoolAttr * | attr | ||
) |
Sets the attributes for the thread pool. Only affects future calculations.
0
on success, nonzero on failure. INVALID_POLICY
if policy can not be set. tp | valid thread pool pointer. |
attr | pointer to attributes, null sets attributes to default. |
int ThreadPoolShutdown | ( | ThreadPool * | tp | ) |
Shuts the thread pool down. Waits for all threads to finish. May block indefinitely if jobs do not exit.
tp | must be valid tp. |
int TPAttrInit | ( | ThreadPoolAttr * | attr | ) |
Initializes thread pool attributes. Sets values to defaults defined in ThreadPool.h.
attr | must be valid thread pool attributes. |
References DEFAULT_IDLE_TIME, DEFAULT_JOBS_PER_THREAD, DEFAULT_MAX_THREADS, DEFAULT_MIN_THREADS, DEFAULT_STACK_SIZE, THREADPOOLATTR::jobsPerThread, THREADPOOLATTR::maxIdleTime, THREADPOOLATTR::maxThreads, THREADPOOLATTR::minThreads, THREADPOOLATTR::schedPolicy, and THREADPOOLATTR::stackSize.
Referenced by UpnpInitThreadPools().
int TPAttrSetIdleTime | ( | ThreadPoolAttr * | attr, |
int | idleTime | ||
) |
Sets the idle time for the thread pool attributes.
attr | must be valid thread pool attributes. |
idleTime | . |
References THREADPOOLATTR::maxIdleTime.
Referenced by UpnpInitThreadPools().
int TPAttrSetJobsPerThread | ( | ThreadPoolAttr * | attr, |
int | jobsPerThread | ||
) |
Sets the jobs per thread ratio.
attr | must be valid thread pool attributes. |
jobsPerThread | number of jobs per thread to maintain. |
References THREADPOOLATTR::jobsPerThread.
Referenced by UpnpInitThreadPools().
int TPAttrSetMaxJobsTotal | ( | ThreadPoolAttr * | attr, |
int | maxJobsTotal | ||
) |
Sets the maximum number jobs that can be qeued totally.
attr | must be valid thread pool attributes. |
maxJobsTotal | maximum number of jobs. |
References THREADPOOLATTR::maxJobsTotal.
Referenced by UpnpInitThreadPools().
int TPAttrSetMaxThreads | ( | ThreadPoolAttr * | attr, |
int | maxThreads | ||
) |
Sets the max threads for the thread pool attributes.
attr | must be valid thread pool attributes. |
maxThreads | value to set. |
References THREADPOOLATTR::maxThreads.
Referenced by UpnpInitThreadPools().
int TPAttrSetMinThreads | ( | ThreadPoolAttr * | attr, |
int | minThreads | ||
) |
Sets the min threads for the thread pool attributes.
attr | must be valid thread pool attributes. |
minThreads | value to set. |
References THREADPOOLATTR::minThreads.
Referenced by UpnpInitThreadPools().
int TPAttrSetSchedPolicy | ( | ThreadPoolAttr * | attr, |
PolicyType | schedPolicy | ||
) |
Sets the scheduling policy for the thread pool attributes.
attr | must be valid thread pool attributes. |
schedPolicy | must be a valid policy type. |
References THREADPOOLATTR::schedPolicy.
int TPAttrSetStackSize | ( | ThreadPoolAttr * | attr, |
size_t | stackSize | ||
) |
Sets the stack size for the thread pool attributes.
attr | must be valid thread pool attributes. |
stackSize | value to set. |
References THREADPOOLATTR::stackSize.
Referenced by UpnpInitThreadPools().
int TPAttrSetStarvationTime | ( | ThreadPoolAttr * | attr, |
int | starvationTime | ||
) |
Sets the starvation time for the thread pool attributes.
attr | must be valid thread pool attributes. |
starvationTime | milliseconds. |
References THREADPOOLATTR::starvationTime.
int TPJobInit | ( | ThreadPoolJob * | job, |
start_routine | func, | ||
void * | arg | ||
) |
Initializes thread pool job. Sets the priority to default defined in ThreadPool.h. Sets the free_routine to default defined in ThreadPool.h.
job | must be valid thread pool attributes. |
func | function to run, must be valid. |
arg | argument to pass to function. |
int TPJobSetFreeFunction | ( | ThreadPoolJob * | job, |
free_routine | func | ||
) |
Sets the max threads for the thread pool attributes.
job | must be valid thread pool attributes. |
func | value to set. |
int TPJobSetPriority | ( | ThreadPoolJob * | job, |
ThreadPriority | priority | ||
) |
Sets the max threads for the thread pool attributes.
job | must be valid thread pool attributes. |
priority | value to set. |
|
static |
Implements a thread pool worker. Worker waits for a job to become available. Worker picks up persistent jobs first, high priority, med priority, then low priority.
If worker remains idle for more than specified max, the worker is released.
arg | arg -> is cast to (ThreadPool *). |