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
#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;
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:1113
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:1185
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:1086
ThreadPoolPrintStats
UPNP_EXPORT_SPEC void ThreadPoolPrintStats(ThreadPoolStats *stats)
Definition ThreadPool.c:1195
TPAttrSetStarvationTime
int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime)
Sets the starvation time for the thread pool attributes.
Definition ThreadPool.c:1167
TPAttrSetMaxThreads
int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads)
Sets the max threads for the thread pool attributes.
Definition ThreadPool.c:1122
TPJobSetPriority
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
Sets the max threads for the thread pool attributes.
Definition ThreadPool.c:1098
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:1158
ThreadPoolJob
struct THREADPOOLJOB ThreadPoolJob
TPAttrInit
int TPAttrInit(ThreadPoolAttr *attr)
Initializes thread pool attributes. Sets values to defaults defined in ThreadPool....
Definition ThreadPool.c:1070
TPAttrSetIdleTime
int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime)
Sets the idle time for the thread pool attributes.
Definition ThreadPool.c:1149
TPAttrSetMinThreads
int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads)
Sets the min threads for the thread pool attributes.
Definition ThreadPool.c:1131
TPAttrSetSchedPolicy
int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy)
Sets the scheduling policy for the thread pool attributes.
Definition ThreadPool.c:1176
free_routine
void(* free_routine)(void *arg)
Definition ThreadPool.h:140
TPAttrSetStackSize
int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize)
Sets the stack size for the thread pool attributes.
Definition ThreadPool.c:1140
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:1231
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:98
UPNP_INLINE
#define UPNP_INLINE
Declares an inline function.
Definition UpnpGlobal.h:112
UpnpInet.h
Provides a platform independent way to include TCP/IP types and functions.
ithread.h
FREELIST
Definition FreeList.h:62
LINKEDLIST
Definition LinkedList.h:84
THREADPOOLATTR
Definition ThreadPool.h:145
THREADPOOLATTR::maxJobsTotal
int maxJobsTotal
Definition ThreadPool.h:158
THREADPOOLATTR::schedPolicy
PolicyType schedPolicy
Definition ThreadPool.h:163
THREADPOOLATTR::maxThreads
int maxThreads
Definition ThreadPool.h:149
THREADPOOLATTR::jobsPerThread
int jobsPerThread
Definition ThreadPool.h:156
THREADPOOLATTR::maxIdleTime
int maxIdleTime
Definition ThreadPool.h:154
THREADPOOLATTR::minThreads
int minThreads
Definition ThreadPool.h:147
THREADPOOLATTR::stackSize
size_t stackSize
Definition ThreadPool.h:151
THREADPOOLATTR::starvationTime
int starvationTime
Definition ThreadPool.h:161
THREADPOOLJOB
Definition ThreadPool.h:168
THREADPOOL
A thread pool similar to the thread pool in the UPnP SDK.
Definition ThreadPool.h:217
THREADPOOL::lastJobId
int lastJobId
Definition ThreadPool.h:225
THREADPOOL::pendingWorkerThreadStart
int pendingWorkerThreadStart
Definition ThreadPool.h:231
THREADPOOL::busyThreads
int busyThreads
Definition ThreadPool.h:233
THREADPOOL::lowJobQ
LinkedList lowJobQ
Definition ThreadPool.h:239
THREADPOOL::mutex
ithread_mutex_t mutex
Definition ThreadPool.h:219
THREADPOOL::attr
ThreadPoolAttr attr
Definition ThreadPool.h:247
THREADPOOL::persistentThreads
int persistentThreads
Definition ThreadPool.h:235
THREADPOOL::totalThreads
int totalThreads
Definition ThreadPool.h:229
THREADPOOL::medJobQ
LinkedList medJobQ
Definition ThreadPool.h:241
THREADPOOL::shutdown
int shutdown
Definition ThreadPool.h:227
THREADPOOL::highJobQ
LinkedList highJobQ
Definition ThreadPool.h:243
THREADPOOL::stats
ThreadPoolStats stats
Definition ThreadPool.h:249
THREADPOOL::jobFreeList
FreeList jobFreeList
Definition ThreadPool.h:237
THREADPOOL::condition
ithread_cond_t condition
Definition ThreadPool.h:221
THREADPOOL::persistentJob
ThreadPoolJob * persistentJob
Definition ThreadPool.h:245
THREADPOOL::start_and_shutdown
ithread_cond_t start_and_shutdown
Definition ThreadPool.h:223
TPOOLSTATS
Definition ThreadPool.h:179