ThreadPool.h
Go to the documentation of this file.
/*******************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
* Copyright (c) 2012 France Telecom All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include "FreeList.h"
#include "LinkedList.h"
#include "UpnpGlobal.h" /* for UPNP_INLINE, UPNP_EXPORT_SPEC */
#include "UpnpInet.h"
#include "ithread.h"
#include <errno.h>
#ifdef _WIN32
#include <time.h>
#ifndef _TIMEZONE_DEFINED
struct timezone
{
int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */
};
#endif
int gettimeofday(struct timeval *tv, struct timezone *tz);
#else /* _WIN32 */
#include <sys/param.h>
#include <sys/time.h> /* for gettimeofday() */
#if defined(__APPLE__) || defined(__NetBSD__)
#include <sys/resource.h> /* for setpriority() */
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define JOBFREELISTSIZE 100
#define INFINITE_THREADS -1
#define EMAXTHREADS (-8 & 1 << 29)
#define INVALID_POLICY (-9 & 1 << 29)
#define INVALID_JOB_ID (-2 & 1 << 29)
typedef enum duration
{
SHORT_TERM,
PERSISTENT
} Duration;
typedef enum priority
{
LOW_PRIORITY,
MED_PRIORITY,
HIGH_PRIORITY
} ThreadPriority;
#define DEFAULT_PRIORITY MED_PRIORITY
#define DEFAULT_MIN_THREADS 1
#define DEFAULT_MAX_THREADS 10
#define DEFAULT_STACK_SIZE 0u
#define DEFAULT_JOBS_PER_THREAD 10
#define DEFAULT_STARVATION_TIME 500
#define DEFAULT_IDLE_TIME 10 * 1000
#define DEFAULT_FREE_ROUTINE NULL
#define DEFAULT_MAX_JOBS_TOTAL 100
extern int maxJobsTotal;
#define STATS 1
#ifdef _DEBUG
#define DEBUG 1
#endif
typedef int PolicyType;
#define DEFAULT_POLICY SCHED_OTHER
typedef void (*free_routine)(void *arg);
typedef struct THREADPOOLATTR
{
int minThreads;
int maxThreads;
size_t stackSize;
int maxIdleTime;
int jobsPerThread;
int maxJobsTotal;
int starvationTime;
PolicyType schedPolicy;
} ThreadPoolAttr;
typedef struct THREADPOOLJOB
{
start_routine func;
void *arg;
free_routine free_func;
struct timeval requestTime;
ThreadPriority priority;
int jobId;
} ThreadPoolJob;
typedef struct TPOOLSTATS
{
double totalTimeHQ;
int totalJobsHQ;
double avgWaitHQ;
double totalTimeMQ;
int totalJobsMQ;
double avgWaitMQ;
double totalTimeLQ;
int totalJobsLQ;
double avgWaitLQ;
double totalWorkTime;
double totalIdleTime;
int workerThreads;
int idleThreads;
int persistentThreads;
int totalThreads;
int maxThreads;
int currentJobsHQ;
int currentJobsLQ;
int currentJobsMQ;
} ThreadPoolStats;
typedef struct THREADPOOL
{
ithread_mutex_t mutex;
ithread_cond_t condition;
ithread_cond_t start_and_shutdown;
int lastJobId;
int shutdown;
int totalThreads;
int pendingWorkerThreadStart;
int busyThreads;
int persistentThreads;
FreeList jobFreeList;
LinkedList lowJobQ;
LinkedList medJobQ;
LinkedList highJobQ;
ThreadPoolJob *persistentJob;
ThreadPoolAttr attr;
ThreadPoolStats stats;
} ThreadPool;
void TPSetMaxJobsTotal(int mjt);
int ThreadPoolInit(
ThreadPool *tp,
ThreadPoolAttr *attr);
int ThreadPoolAddPersistent(
ThreadPool *tp,
ThreadPoolJob *job,
int *jobId);
int ThreadPoolGetAttr(
ThreadPool *tp,
ThreadPoolAttr *out);
int ThreadPoolSetAttr(
ThreadPool *tp,
ThreadPoolAttr *attr);
int ThreadPoolAdd(
ThreadPool *tp,
ThreadPoolJob *job,
int *jobId);
int ThreadPoolRemove(
ThreadPool *tp,
int jobId,
ThreadPoolJob *out);
int ThreadPoolShutdown(
ThreadPool *tp);
int TPJobInit(
ThreadPoolJob *job,
start_routine func,
void *arg);
int TPJobSetPriority(
ThreadPoolJob *job,
ThreadPriority priority);
int TPJobSetFreeFunction(
ThreadPoolJob *job,
free_routine func);
int TPAttrInit(
ThreadPoolAttr *attr);
int TPAttrSetMaxThreads(
ThreadPoolAttr *attr,
int maxThreads);
int TPAttrSetMinThreads(
ThreadPoolAttr *attr,
int minThreads);
int TPAttrSetStackSize(
ThreadPoolAttr *attr,
size_t stackSize);
int TPAttrSetIdleTime(
ThreadPoolAttr *attr,
int idleTime);
int TPAttrSetJobsPerThread(
ThreadPoolAttr *attr,
int jobsPerThread);
int TPAttrSetStarvationTime(
ThreadPoolAttr *attr,
int starvationTime);
int TPAttrSetSchedPolicy(
ThreadPoolAttr *attr,
PolicyType schedPolicy);
int TPAttrSetMaxJobsTotal(
ThreadPoolAttr *attr,
int maxJobsTotal);
#ifdef STATS
UPNP_EXPORT_SPEC int ThreadPoolGetStats(
ThreadPool *tp,
ThreadPoolStats *stats);
#else
static UPNP_INLINE int ThreadPoolGetStats(
ThreadPool *tp,
ThreadPoolStats *stats)
{
}
#endif
#ifdef STATS
UPNP_EXPORT_SPEC void ThreadPoolPrintStats(
ThreadPoolStats *stats);
#else
static UPNP_INLINE void ThreadPoolPrintStats(
ThreadPoolStats *stats)
{
}
#endif
#ifdef __cplusplus
}
#endif
#endif /* THREADPOOL_H */
FreeList.h
LinkedList.h
ThreadPoolInit
int ThreadPoolInit(ThreadPool *tp, ThreadPoolAttr *attr)
Initializes and starts ThreadPool. Must be called first and only once for ThreadPool.
Definition ThreadPool.c:710
ThreadPoolAdd
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:826
ThreadPool
struct THREADPOOL ThreadPool
A thread pool similar to the thread pool in the UPnP SDK.
TPJobSetFreeFunction
int TPJobSetFreeFunction(ThreadPoolJob *job, free_routine func)
Sets the max threads for the thread pool attributes.
Definition ThreadPool.c:1117
ThreadPoolShutdown
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:990
ThreadPoolGetAttr
int ThreadPoolGetAttr(ThreadPool *tp, ThreadPoolAttr *out)
Gets the current set of attributes associated with the thread pool.
Definition ThreadPool.c:936
TPAttrSetMaxJobsTotal
int TPAttrSetMaxJobsTotal(ThreadPoolAttr *attr, int maxJobsTotal)
Sets the maximum number jobs that can be qeued totally.
Definition ThreadPool.c:1189
TPJobInit
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:1090
ThreadPoolPrintStats
UPNP_EXPORT_SPEC void ThreadPoolPrintStats(ThreadPoolStats *stats)
Definition ThreadPool.c:1199
TPAttrSetStarvationTime
int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime)
Sets the starvation time for the thread pool attributes.
Definition ThreadPool.c:1171
TPAttrSetMaxThreads
int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads)
Sets the max threads for the thread pool attributes.
Definition ThreadPool.c:1126
TPJobSetPriority
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
Sets the max threads for the thread pool attributes.
Definition ThreadPool.c:1102
ThreadPoolStats
struct TPOOLSTATS ThreadPoolStats
ThreadPoolSetAttr
int ThreadPoolSetAttr(ThreadPool *tp, ThreadPoolAttr *attr)
Sets the attributes for the thread pool. Only affects future calculations.
Definition ThreadPool.c:949
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.
Definition ThreadPool.c:879
ThreadPoolAttr
struct THREADPOOLATTR ThreadPoolAttr
TPAttrSetJobsPerThread
int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread)
Sets the jobs per thread ratio.
Definition ThreadPool.c:1162
ThreadPoolJob
struct THREADPOOLJOB ThreadPoolJob
TPAttrInit
int TPAttrInit(ThreadPoolAttr *attr)
Initializes thread pool attributes. Sets values to defaults defined in ThreadPool....
Definition ThreadPool.c:1074
TPSetMaxJobsTotal
void TPSetMaxJobsTotal(int mjt)
Sets the maximum number of jobs in the thread pool. This option is intended for server applications t...
Definition ThreadPool.c:1072
TPAttrSetIdleTime
int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime)
Sets the idle time for the thread pool attributes.
Definition ThreadPool.c:1153
TPAttrSetMinThreads
int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads)
Sets the min threads for the thread pool attributes.
Definition ThreadPool.c:1135
TPAttrSetSchedPolicy
int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy)
Sets the scheduling policy for the thread pool attributes.
Definition ThreadPool.c:1180
free_routine
void(* free_routine)(void *arg)
Definition ThreadPool.h:141
TPAttrSetStackSize
int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize)
Sets the stack size for the thread pool attributes.
Definition ThreadPool.c:1144
ThreadPoolAddPersistent
int ThreadPoolAddPersistent(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a persistent job to the thread pool.
Definition ThreadPool.c:778
ThreadPoolGetStats
UPNP_EXPORT_SPEC int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats)
Returns various statistics about the thread pool.
Definition ThreadPool.c:1235
UpnpGlobal.h
Defines constants that for some reason are not defined on some systems.
UPNP_EXPORT_SPEC
#define UPNP_EXPORT_SPEC
Export functions on WIN32 DLLs.
Definition UpnpGlobal.h:105
UPNP_INLINE
#define UPNP_INLINE
Declares an inline function.
Definition UpnpGlobal.h:119
UpnpInet.h
Provides a platform independent way to include TCP/IP types and functions.
ithread.h
FREELIST
Definition FreeList.h:60
LINKEDLIST
Definition LinkedList.h:84
THREADPOOLATTR
Definition ThreadPool.h:146
THREADPOOLATTR::maxJobsTotal
int maxJobsTotal
Definition ThreadPool.h:159
THREADPOOLATTR::schedPolicy
PolicyType schedPolicy
Definition ThreadPool.h:164
THREADPOOLATTR::maxThreads
int maxThreads
Definition ThreadPool.h:150
THREADPOOLATTR::jobsPerThread
int jobsPerThread
Definition ThreadPool.h:157
THREADPOOLATTR::maxIdleTime
int maxIdleTime
Definition ThreadPool.h:155
THREADPOOLATTR::minThreads
int minThreads
Definition ThreadPool.h:148
THREADPOOLATTR::stackSize
size_t stackSize
Definition ThreadPool.h:152
THREADPOOLATTR::starvationTime
int starvationTime
Definition ThreadPool.h:162
THREADPOOLJOB
Definition ThreadPool.h:169
THREADPOOL
A thread pool similar to the thread pool in the UPnP SDK.
Definition ThreadPool.h:218
THREADPOOL::lastJobId
int lastJobId
Definition ThreadPool.h:226
THREADPOOL::pendingWorkerThreadStart
int pendingWorkerThreadStart
Definition ThreadPool.h:232
THREADPOOL::busyThreads
int busyThreads
Definition ThreadPool.h:234
THREADPOOL::lowJobQ
LinkedList lowJobQ
Definition ThreadPool.h:240
THREADPOOL::mutex
ithread_mutex_t mutex
Definition ThreadPool.h:220
THREADPOOL::attr
ThreadPoolAttr attr
Definition ThreadPool.h:248
THREADPOOL::persistentThreads
int persistentThreads
Definition ThreadPool.h:236
THREADPOOL::totalThreads
int totalThreads
Definition ThreadPool.h:230
THREADPOOL::medJobQ
LinkedList medJobQ
Definition ThreadPool.h:242
THREADPOOL::shutdown
int shutdown
Definition ThreadPool.h:228
THREADPOOL::highJobQ
LinkedList highJobQ
Definition ThreadPool.h:244
THREADPOOL::stats
ThreadPoolStats stats
Definition ThreadPool.h:250
THREADPOOL::jobFreeList
FreeList jobFreeList
Definition ThreadPool.h:238
THREADPOOL::condition
ithread_cond_t condition
Definition ThreadPool.h:222
THREADPOOL::persistentJob
ThreadPoolJob * persistentJob
Definition ThreadPool.h:246
THREADPOOL::start_and_shutdown
ithread_cond_t start_and_shutdown
Definition ThreadPool.h:224
TPOOLSTATS
Definition ThreadPool.h:180