UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches

Manage a threadpool (for internal use only). More...

#include <ThreadPool.hpp>
#include <UPnPsdk/synclog.hpp>
+ Include dependency graph for ThreadPool.cpp:

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.
 
ThreadPoolJobanonymous_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
 

Detailed Description

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.

Macro Definition Documentation

◆ EMAXTHREADS

#define EMAXTHREADS   (-8 & 1 << 29)

Error: maximun threads.

Definition at line 60 of file ThreadPool.cpp.

◆ INVALID_POLICY

#define INVALID_POLICY   (-9 & 1 << 29)

Invalid Policy

Definition at line 62 of file ThreadPool.cpp.

Function Documentation

◆ ThreadPoolInit()

int ThreadPoolInit ( ThreadPool tp,
ThreadPoolAttr attr 
)

Initializes and starts ThreadPool.

Must be called first and only once for ThreadPool.

Returns
On success: 0
On error:
  • EINVAL with invalid ThreadPool.
  • 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.
Parameters
[in]tpMust be valid, non null, pointer to ThreadPool.
[in]attrCan be nullptr. If not nullptr then attr contains the following fields:
  • minWorkerThreads - minimum number of worker threads thread pool will never have less than this number of threads.
  • maxWorkerThreads - maximum number of worker threads thread pool will never have more than this number of threads.
  • maxIdleTime - maximum time that a worker thread will spend idle. If a worker is idle longer than this time and there are more than the min number of workers running, then the worker thread exits.
  • jobsPerThread - ratio of jobs to thread to try and maintain if a job is scheduled and the number of jobs per thread is greater than this number,and if less than the maximum number of workers are running then a new thread is started to help out with efficiency.
  • schedPolicy - scheduling policy to try and set (OS dependent).

Definition at line 669 of file ThreadPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ThreadPoolAddPersistent()

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.

Returns
On success: 0
On error:
  • EOUTOFMEM not enough memory to add job.
  • EMAXTHREADS not enough threads to add persistent job.
Parameters
[in]tpValid thread pool pointer.
[in]jobValid thread pool job.
[in]jobIdJob ID

Definition at line 737 of file ThreadPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ThreadPoolAdd()

int ThreadPoolAdd ( ThreadPool tp,
ThreadPoolJob job,
int *  jobId 
)

Adds a job to the thread pool.

Job will be run as soon as possible.

Returns
On success: 0
On error: nonzero
  • EOUTOFMEM if not enough memory to add job.
Parameters
[in]tpValid thread pool pointer.
[in]jobJob
[in]jobIdid of job.

Definition at line 784 of file ThreadPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ ThreadPoolRemove()

int ThreadPoolRemove ( ThreadPool tp,
int  jobId,
ThreadPoolJob out 
)

Removes a job from the thread pool.

Can only remove jobs which are not currently running.

Returns
On success: 0
On error: nonzero
  • INVALID_JOB_ID if job not found.
Parameters
[in]tpValid thread pool pointer.
[in]jobIdId of job.
[out]outSpace for removed job.

Definition at line 836 of file ThreadPool.cpp.

+ Here is the call graph for this function:

◆ ThreadPoolGetAttr()

int ThreadPoolGetAttr ( ThreadPool tp,
ThreadPoolAttr out 
)

Gets the current set of attributes associated with the thread pool.

Returns
On success: 0
On error: nonzero
Parameters
[in]tpValid thread pool pointer.
[in]outNon null pointer to store attributes.

Definition at line 892 of file ThreadPool.cpp.

◆ ThreadPoolSetAttr()

int ThreadPoolSetAttr ( ThreadPool tp,
ThreadPoolAttr attr 
)

Sets the attributes for the thread pool.

Only affects future calculations.

Returns
On success: 0
On error: nonzero
  • INVALID_POLICY if policy can not be set.
Parameters
[in]tpValid thread pool pointer.
[in]attrPointer to attributes, null sets attributes to default.

Definition at line 904 of file ThreadPool.cpp.

+ Here is the call graph for this function:

◆ ThreadPoolShutdown()

int ThreadPoolShutdown ( ThreadPool tp)

Shuts the thread pool down.

Waits for all threads to finish. May block indefinitely if jobs do not exit.

Returns
On success: 0
On error: nonzero
Parameters
[in]tpMust be valid tp.

Definition at line 944 of file ThreadPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ TPSetMaxJobsTotal()

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.

+ Here is the caller graph for this function:

◆ TPAttrInit()

int TPAttrInit ( ThreadPoolAttr attr)

Initializes thread pool attributes.

Sets values to defaults defined in ThreadPool.hpp.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.

Definition at line 1028 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPJobInit()

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.

Returns
Always 0.
Parameters
[in]jobMust be valid thread pool attributes.
[in]funcFunction to run, must be valid.
[in]argArgument to pass to function.

Definition at line 1043 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPJobSetPriority()

int TPJobSetPriority ( ThreadPoolJob job,
ThreadPriority  priority 
)

Sets the priority of the threadpool job.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]jobMust be valid thread pool attributes.
[in]priorityValue to set.

Definition at line 1054 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPJobSetFreeFunction()

int TPJobSetFreeFunction ( ThreadPoolJob job,
free_routine  func 
)

Sets the jobs free function.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]jobMust be valid thread pool attributes.
[in]funcValue to set.

Definition at line 1068 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPAttrSetMaxThreads()

int TPAttrSetMaxThreads ( ThreadPoolAttr attr,
int  maxThreads 
)

Sets the max threads for the thread pool attributes.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.
[in]maxThreadsValue to set.

Definition at line 1076 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPAttrSetMinThreads()

int TPAttrSetMinThreads ( ThreadPoolAttr attr,
int  minThreads 
)

Sets the min threads for the thread pool attributes.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrmust be valid thread pool attributes.
[in]minThreadsvalue to set.

Definition at line 1084 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPAttrSetStackSize()

int TPAttrSetStackSize ( ThreadPoolAttr attr,
size_t  stackSize 
)

Sets the stack size for the thread pool attributes.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.
[in]stackSizeValue to set.

Definition at line 1092 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPAttrSetIdleTime()

int TPAttrSetIdleTime ( ThreadPoolAttr attr,
int  idleTime 
)

Sets the idle time for the thread pool attributes.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.
[in]idleTimeIdle time

Definition at line 1100 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPAttrSetJobsPerThread()

int TPAttrSetJobsPerThread ( ThreadPoolAttr attr,
int  jobsPerThread 
)

Sets the jobs per thread ratio.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.
[in]jobsPerThreadNumber of jobs per thread to maintain.

Definition at line 1108 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ TPAttrSetStarvationTime()

int TPAttrSetStarvationTime ( ThreadPoolAttr attr,
int  starvationTime 
)

Sets the starvation time for the thread pool attributes.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.
[in]starvationTimeMilliseconds.

Definition at line 1116 of file ThreadPool.cpp.

◆ TPAttrSetSchedPolicy()

int TPAttrSetSchedPolicy ( ThreadPoolAttr attr,
PolicyType  schedPolicy 
)

Sets the scheduling policy for the thread pool attributes.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.
[in]schedPolicyMust be a valid policy type.

Definition at line 1124 of file ThreadPool.cpp.

◆ TPAttrSetMaxJobsTotal()

int TPAttrSetMaxJobsTotal ( ThreadPoolAttr attr,
int  totalMaxJobs 
)

Sets the maximum number jobs that can be qeued totally.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]attrMust be valid thread pool attributes.
[in]totalMaxJobsMaximum number of jobs.

Definition at line 1132 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

◆ ThreadPoolPrintStats()

void ThreadPoolPrintStats ( ThreadPoolStats stats)

Prints various statistics about the thread pool to stderr.

Only valid if STATS has been defined.

Parameters
[in]statsValid threadpool stats

Definition at line 1141 of file ThreadPool.cpp.

+ Here is the call graph for this function:

◆ ThreadPoolGetStats()

int ThreadPoolGetStats ( ThreadPool tp,
ThreadPoolStats stats 
)

Returns various statistics about the thread pool.

Only valid if STATS has been defined.

Returns
On success: 0
On error: EINVAL.
Parameters
[in]tpValid initialized threadpool.
[out]statsValid stats, out parameter.

Definition at line 1168 of file ThreadPool.cpp.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ gettimeofday()

int gettimeofday ( struct timeval *  tv,
struct timezone tz 
)

Get time of day.

Definition at line 1211 of file ThreadPool.cpp.

+ Here is the caller graph for this function:

Variable Documentation

◆ JOBFREELISTSIZE

constexpr int JOBFREELISTSIZE {100}
constexpr

Size of job free list.

Definition at line 56 of file ThreadPool.cpp.

◆ INFINITE_THREADS

constexpr int INFINITE_THREADS {-1}
constexpr

Infinite threads.

Definition at line 58 of file ThreadPool.cpp.

◆ maxJobsTotal

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.