Go to the documentation of this file.
55 int gettimeofday(
struct timeval *tv,
struct timezone *tz);
57 #include <sys/param.h>
59 #if defined(__OSX__) || defined(__APPLE__) || defined(__NetBSD__)
60 #include <sys/resource.h>
69 #define JOBFREELISTSIZE 100
71 #define INFINITE_THREADS -1
73 #define EMAXTHREADS (-8 & 1<<29)
76 #define INVALID_POLICY (-9 & 1<<29)
79 #define INVALID_JOB_ID (-2 & 1<<29)
81 typedef enum duration {
86 typedef enum priority {
93 #define DEFAULT_PRIORITY MED_PRIORITY
96 #define DEFAULT_MIN_THREADS 1
99 #define DEFAULT_MAX_THREADS 10
102 #define DEFAULT_STACK_SIZE 0u
105 #define DEFAULT_JOBS_PER_THREAD 10
108 #define DEFAULT_STARVATION_TIME 500
111 #define DEFAULT_IDLE_TIME 10 * 1000
114 #define DEFAULT_FREE_ROUTINE NULL
117 #define DEFAULT_MAX_JOBS_TOTAL 100
130 typedef int PolicyType;
132 #define DEFAULT_POLICY SCHED_OTHER
168 struct timeval requestTime;
169 ThreadPriority priority;
185 double totalWorkTime;
186 double totalIdleTime;
189 int persistentThreads;
386 ThreadPriority priority);
484 PolicyType schedPolicy);
static void StatsInit(ThreadPoolStats *stats)
Initializes the statistics structure.
Definition: ThreadPool.c:82
int starvationTime
Definition: ThreadPool.h:157
int TPAttrInit(ThreadPoolAttr *attr)
Initializes thread pool attributes. Sets values to defaults defined in ThreadPool....
Definition: ThreadPool.c:1055
int ThreadPoolRemove(ThreadPool *tp, int jobId, ThreadPoolJob *out)
Removes a job from the thread pool. Can only remove jobs which are not currently running.
Definition: ThreadPool.c:867
EXPORT_SPEC void ThreadPoolPrintStats(ThreadPoolStats *stats)
Definition: ThreadPool.c:1180
ithread_cond_t condition
Definition: ThreadPool.h:217
int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads)
Sets the min threads for the thread pool attributes.
Definition: ThreadPool.c:1116
int TPAttrSetMaxJobsTotal(ThreadPoolAttr *attr, int maxJobsTotal)
Sets the maximum number jobs that can be qeued totally.
Definition: ThreadPool.c:1170
A thread pool similar to the thread pool in the UPnP SDK.
Definition: ThreadPool.h:213
static void BumpPriority(ThreadPool *tp)
Determines whether any jobs need to be bumped to a higher priority Q and bumps them.
Definition: ThreadPool.c:347
long size
Definition: LinkedList.h:90
int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1107
Definition: ThreadPool.h:141
void * ListDelNode(LinkedList *list, ListNode *dnode, int freeItem)
Removes a node from the list. The memory for the node is freed.
Definition: LinkedList.c:177
int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy)
Sets the scheduling policy for the thread pool attributes.
Definition: ThreadPool.c:1161
int shutdown
Definition: ThreadPool.h:223
int FreeListDestroy(FreeList *free_list)
Releases the resources stored with the free list.
Definition: FreeList.c:92
size_t stackSize
Definition: ThreadPool.h:147
void(* free_routine)(void *arg)
Definition: ThreadPool.h:135
int jobsPerThread
Definition: ThreadPool.h:152
int maxIdleTime
Definition: ThreadPool.h:150
static void FreeThreadPoolJob(ThreadPool *tp, ThreadPoolJob *tpj)
Deallocates a dynamically allocated ThreadPoolJob.
Definition: ThreadPool.c:236
#define INVALID_POLICY
Definition: ThreadPool.h:76
int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread)
Sets the jobs per thread ratio.
Definition: ThreadPool.c:1143
static long DiffMillis(struct timeval *time1, struct timeval *time2)
Returns the difference in milliseconds between two timeval structures.
Definition: ThreadPool.c:57
int TPAttrSetMaxJobsTotal(ThreadPoolAttr *attr, int maxJobsTotal)
Sets the maximum number jobs that can be qeued totally.
Definition: ThreadPool.c:1170
int TPJobInit(ThreadPoolJob *job, start_routine func, void *arg)
Initializes thread pool job. Sets the priority to default defined in ThreadPool.h....
Definition: ThreadPool.c:1071
ListNode * ListFind(LinkedList *list, ListNode *start, void *item)
Finds the specified item in the list.
Definition: LinkedList.c:272
int ThreadPoolAddPersistent(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a persistent job to the thread pool.
Definition: ThreadPool.c:768
static int SetPriority(ThreadPriority priority)
Sets the priority of the currently running thread.
Definition: ThreadPool.c:292
int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread)
Sets the jobs per thread ratio.
Definition: ThreadPool.c:1143
int ThreadPoolRemove(ThreadPool *tp, int jobId, ThreadPoolJob *out)
Removes a job from the thread pool. Can only remove jobs which are not currently running.
Definition: ThreadPool.c:867
#define DEFAULT_PRIORITY
Definition: ThreadPool.h:93
void ThreadPoolPrintStats(ThreadPoolStats *stats)
Definition: ThreadPool.c:1180
static void AddWorker(ThreadPool *tp)
Determines whether or not a thread should be added based on the jobsPerThread ratio....
Definition: ThreadPool.c:681
int totalThreads
Definition: ThreadPool.h:225
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1083
EXPORT_SPEC int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats)
Returns various statistics about the thread pool.
Definition: ThreadPool.c:1201
int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize)
Sets the stack size for the thread pool attributes.
Definition: ThreadPool.c:1125
LinkedList lowJobQ
Definition: ThreadPool.h:235
int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy)
Sets the scheduling policy for the thread pool attributes.
Definition: ThreadPool.c:1161
int ThreadPoolGetAttr(ThreadPool *tp, ThreadPoolAttr *out)
Gets the current set of attributes associated with the thread pool.
Definition: ThreadPool.c:924
int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize)
Sets the stack size for the thread pool attributes.
Definition: ThreadPool.c:1125
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:99
static void SetRelTimeout(struct timespec *time, int relMillis)
Sets the fields of the passed in timespec to be relMillis milliseconds in the future.
Definition: ThreadPool.c:392
static int CmpThreadPoolJob(void *jobA, void *jobB)
Compares thread pool jobs.
Definition: ThreadPool.c:221
int lastJobId
Definition: ThreadPool.h:221
static ThreadPoolJob * CreateThreadPoolJob(ThreadPoolJob *job, int id, ThreadPool *tp)
Creates a Thread Pool Job. (Dynamically allocated)
Definition: ThreadPool.c:597
int ThreadPoolShutdown(ThreadPool *tp)
Shuts the thread pool down. Waits for all threads to finish. May block indefinitely if jobs do not ex...
Definition: ThreadPool.c:978
int ThreadPoolSetAttr(ThreadPool *tp, ThreadPoolAttr *attr)
Sets the attributes for the thread pool. Only affects future calculations.
Definition: ThreadPool.c:937
ithread_mutex_t mutex
Definition: ThreadPool.h:215
int ThreadPoolInit(ThreadPool *tp, ThreadPoolAttr *attr)
Initializes and starts ThreadPool. Must be called first and only once for ThreadPool.
Definition: ThreadPool.c:700
int minThreads
Definition: ThreadPool.h:143
struct THREADPOOLJOB ThreadPoolJob
int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads)
Sets the min threads for the thread pool attributes.
Definition: ThreadPool.c:1116
void * FreeListAlloc(FreeList *free_list)
Allocates chunk of set size.
Definition: FreeList.c:51
Definition: LinkedList.h:61
ListNode * ListHead(LinkedList *list)
Returns the head of the list.
Definition: LinkedList.c:219
static int SetPolicyType(PolicyType in)
Sets the scheduling policy of the current process.
Definition: ThreadPool.c:255
ListNode * ListAddTail(LinkedList *list, void *item)
Adds a node to the tail of the list. Node gets added immediately before list.tail.
Definition: LinkedList.c:119
Definition: ThreadPool.h:164
int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime)
Sets the starvation time for the thread pool attributes.
Definition: ThreadPool.c:1152
int maxJobsTotal
Definition: ThreadPool.h:154
int ListInit(LinkedList *list, cmp_routine cmp_func, free_function free_func)
Initializes LinkedList. Must be called first and only once for List.
Definition: LinkedList.c:84
static void StatsAccountHQ(ThreadPool *tp, long diffTime)
Definition: ThreadPool.c:140
ThreadPoolStats stats
Definition: ThreadPool.h:245
int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime)
Sets the starvation time for the thread pool attributes.
Definition: ThreadPool.c:1152
FreeList jobFreeList
Definition: ThreadPool.h:233
int ThreadPoolShutdown(ThreadPool *tp)
Shuts the thread pool down. Waits for all threads to finish. May block indefinitely if jobs do not ex...
Definition: ThreadPool.c:978
ThreadPoolJob * persistentJob
Definition: ThreadPool.h:241
int ThreadPoolSetAttr(ThreadPool *tp, ThreadPoolAttr *attr)
Sets the attributes for the thread pool. Only affects future calculations.
Definition: ThreadPool.c:937
static void StatsAccountLQ(ThreadPool *tp, long diffTime)
Definition: ThreadPool.c:110
PolicyType schedPolicy
Definition: ThreadPool.h:159
#define DEFAULT_STARVATION_TIME
Definition: ThreadPool.h:108
int ListDestroy(LinkedList *list, int freeItem)
Removes all memory associated with list nodes. Does not free LinkedList *list.
Definition: LinkedList.c:200
int pendingWorkerThreadStart
Definition: ThreadPool.h:227
int ThreadPoolAdd(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a job to the thread pool. Job will be run as soon as possible.
Definition: ThreadPool.c:816
int TPJobInit(ThreadPoolJob *job, start_routine func, void *arg)
Initializes thread pool job. Sets the priority to default defined in ThreadPool.h....
Definition: ThreadPool.c:1071
#define DEFAULT_JOBS_PER_THREAD
Definition: ThreadPool.h:105
int ThreadPoolAddPersistent(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a persistent job to the thread pool.
Definition: ThreadPool.c:768
ListNode head
Definition: LinkedList.h:86
#define EXPORT_SPEC
Export functions on WIN32 DLLs.
Definition: UpnpGlobal.h:87
static int CreateWorker(ThreadPool *tp)
Creates a worker thread, if the thread pool does not already have max threads.
Definition: ThreadPool.c:631
int ThreadPoolInit(ThreadPool *tp, ThreadPoolAttr *attr)
Initializes and starts ThreadPool. Must be called first and only once for ThreadPool.
Definition: ThreadPool.c:700
static void * WorkerThread(void *arg)
Implements a thread pool worker. Worker waits for a job to become available. Worker picks up persiste...
Definition: ThreadPool.c:446
int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1107
#define DEFAULT_MAX_JOBS_TOTAL
Definition: ThreadPool.h:117
Definition: ThreadPool.h:175
int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats)
Returns various statistics about the thread pool.
Definition: ThreadPool.c:1201
int TPJobSetFreeFunction(ThreadPoolJob *job, free_routine func)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1098
Defines constants that for some reason are not defined on some systems.
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1083
Definition: FreeList.h:62
Definition: LinkedList.h:84
int maxThreads
Definition: ThreadPool.h:145
#define DEFAULT_FREE_ROUTINE
Definition: ThreadPool.h:114
int busyThreads
Definition: ThreadPool.h:229
static void StatsAccountMQ(ThreadPool *tp, long diffTime)
Definition: ThreadPool.c:125
int FreeListInit(FreeList *free_list, size_t elementSize, int maxFreeListLength)
Initializes Free List.
Definition: FreeList.c:37
int TPJobSetFreeFunction(ThreadPoolJob *job, free_routine func)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1098
struct THREADPOOL ThreadPool
A thread pool similar to the thread pool in the UPnP SDK.
ThreadPoolAttr attr
Definition: ThreadPool.h:243
int ThreadPoolAdd(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a job to the thread pool. Job will be run as soon as possible.
Definition: ThreadPool.c:816
int FreeListFree(FreeList *free_list, void *element)
Returns an item to the Free List.
Definition: FreeList.c:71
LinkedList medJobQ
Definition: ThreadPool.h:237
static void SetSeed(void)
Sets seed for random number generator. Each thread sets the seed random number generator.
Definition: ThreadPool.c:413
static time_t StatsTime(time_t *t)
Definition: ThreadPool.c:195
int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime)
Sets the idle time for the thread pool attributes.
Definition: ThreadPool.c:1134
long ListSize(LinkedList *list)
Returns the size of the list.
Definition: LinkedList.c:301
#define DEFAULT_MIN_THREADS
Definition: ThreadPool.h:96
#define INVALID_JOB_ID
Definition: ThreadPool.h:79
int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime)
Sets the idle time for the thread pool attributes.
Definition: ThreadPool.c:1134
#define DEFAULT_IDLE_TIME
Definition: ThreadPool.h:111
static void CalcWaitTime(ThreadPool *tp, ThreadPriority p, ThreadPoolJob *job)
Calculates the time the job has been waiting at the specified priority.
Definition: ThreadPool.c:159
#define DEFAULT_STACK_SIZE
Definition: ThreadPool.h:102
LinkedList highJobQ
Definition: ThreadPool.h:239
Provides a platform independent way to include TCP/IP types and functions.
int TPAttrInit(ThreadPoolAttr *attr)
Initializes thread pool attributes. Sets values to defaults defined in ThreadPool....
Definition: ThreadPool.c:1055
int persistentThreads
Definition: ThreadPool.h:231
struct THREADPOOLATTR ThreadPoolAttr
#define JOBFREELISTSIZE
Definition: ThreadPool.h:69
int ThreadPoolGetAttr(ThreadPool *tp, ThreadPoolAttr *out)
Gets the current set of attributes associated with the thread pool.
Definition: ThreadPool.c:924
#define DEFAULT_MAX_THREADS
Definition: ThreadPool.h:99
ithread_cond_t start_and_shutdown
Definition: ThreadPool.h:219
struct TPOOLSTATS ThreadPoolStats