libUPnP  1.6.17
ThreadPool.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2000-2003 Intel Corporation 
00004  * All rights reserved. 
00005  * Copyright (c) 2012 France Telecom All rights reserved. 
00006  *
00007  * Redistribution and use in source and binary forms, with or without 
00008  * modification, are permitted provided that the following conditions are met: 
00009  *
00010  * * Redistributions of source code must retain the above copyright notice, 
00011  * this list of conditions and the following disclaimer. 
00012  * * Redistributions in binary form must reproduce the above copyright notice, 
00013  * this list of conditions and the following disclaimer in the documentation 
00014  * and/or other materials provided with the distribution. 
00015  * * Neither name of Intel Corporation nor the names of its contributors 
00016  * may be used to endorse or promote products derived from this software 
00017  * without specific prior written permission.
00018  * 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00020  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
00022  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR 
00023  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
00026  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
00027  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  *
00031  ******************************************************************************/
00032 
00033 #ifndef THREADPOOL_H
00034 #define THREADPOOL_H
00035 
00040 #include "FreeList.h"
00041 #include "ithread.h"
00042 #include "LinkedList.h"
00043 #include "UpnpInet.h"
00044 #include "UpnpGlobal.h" /* for UPNP_INLINE, EXPORT_SPEC */
00045 
00046 #include <errno.h>
00047 
00048 #ifdef WIN32
00049         #include <time.h>
00050         struct timezone
00051         {
00052                 int  tz_minuteswest; /* minutes W of Greenwich */
00053                 int  tz_dsttime;     /* type of dst correction */
00054         };
00055         int gettimeofday(struct timeval *tv, struct timezone *tz);
00056 #else /* WIN32 */
00057         #include <sys/param.h>
00058         #include <sys/time.h> /* for gettimeofday() */
00059         #if defined(__OSX__) || defined(__APPLE__) || defined(__NetBSD__)
00060                 #include <sys/resource.h>       /* for setpriority() */
00061         #endif
00062 #endif
00063 
00064 #ifdef __cplusplus
00065 extern "C" {
00066 #endif
00067 
00069 #define JOBFREELISTSIZE 100
00070 
00071 #define INFINITE_THREADS -1
00072 
00073 #define EMAXTHREADS (-8 & 1<<29)
00074 
00076 #define INVALID_POLICY (-9 & 1<<29)
00077 
00079 #define INVALID_JOB_ID (-2 & 1<<29)
00080 
00081 typedef enum duration {
00082         SHORT_TERM,
00083         PERSISTENT
00084 } Duration;
00085 
00086 typedef enum priority {
00087         LOW_PRIORITY,
00088         MED_PRIORITY,
00089         HIGH_PRIORITY
00090 } ThreadPriority;
00091 
00093 #define DEFAULT_PRIORITY MED_PRIORITY
00094 
00096 #define DEFAULT_MIN_THREADS 1
00097 
00099 #define DEFAULT_MAX_THREADS 10
00100 
00102 #define DEFAULT_STACK_SIZE 0u
00103 
00105 #define DEFAULT_JOBS_PER_THREAD 10
00106 
00108 #define DEFAULT_STARVATION_TIME 500
00109 
00111 #define DEFAULT_IDLE_TIME 10 * 1000
00112 
00114 #define DEFAULT_FREE_ROUTINE NULL
00115 
00117 #define DEFAULT_MAX_JOBS_TOTAL 100
00118 
00124 #define STATS 1
00125 
00126 #ifdef _DEBUG
00127         #define DEBUG 1
00128 #endif
00129 
00130 typedef int PolicyType;
00131 
00132 #define DEFAULT_POLICY SCHED_OTHER
00133 
00135 typedef void (*free_routine)(void *arg);
00136 
00137 
00140 typedef struct THREADPOOLATTR
00141 {
00143         int minThreads;
00145         int maxThreads;
00147         size_t stackSize;
00150         int maxIdleTime;
00152         int jobsPerThread;
00154         int maxJobsTotal;
00157         int starvationTime;
00159         PolicyType schedPolicy;
00160 } ThreadPoolAttr;
00161 
00163 typedef struct THREADPOOLJOB
00164 {
00165         start_routine func;
00166         void *arg;
00167         free_routine free_func;
00168         struct timeval requestTime;
00169         ThreadPriority priority;
00170         int jobId;
00171 } ThreadPoolJob;
00172 
00174 typedef struct TPOOLSTATS
00175 {
00176         double totalTimeHQ;
00177         int totalJobsHQ;
00178         double avgWaitHQ;
00179         double totalTimeMQ;
00180         int totalJobsMQ;
00181         double avgWaitMQ;
00182         double totalTimeLQ;
00183         int totalJobsLQ;
00184         double avgWaitLQ;
00185         double totalWorkTime;
00186         double totalIdleTime;
00187         int workerThreads;
00188         int idleThreads;
00189         int persistentThreads;
00190         int totalThreads;
00191         int maxThreads;
00192         int currentJobsHQ;
00193         int currentJobsLQ;
00194         int currentJobsMQ;
00195 } ThreadPoolStats;
00196 
00212 typedef struct THREADPOOL
00213 {
00215         ithread_mutex_t mutex;
00217         ithread_cond_t condition;
00219         ithread_cond_t start_and_shutdown;
00221         int lastJobId;
00223         int shutdown;
00225         int totalThreads;
00227         int pendingWorkerThreadStart;
00229         int busyThreads;
00231         int persistentThreads;
00233         FreeList jobFreeList;
00235         LinkedList lowJobQ;
00237         LinkedList medJobQ;
00239         LinkedList highJobQ;
00241         ThreadPoolJob *persistentJob;
00243         ThreadPoolAttr attr;
00245         ThreadPoolStats stats;
00246 } ThreadPool;
00247 
00258 int ThreadPoolInit(
00260         ThreadPool *tp,
00276         ThreadPoolAttr *attr);
00277 
00288 int ThreadPoolAddPersistent(
00290         ThreadPool*tp,
00292         ThreadPoolJob *job,
00294         int *jobId);
00295 
00302 int ThreadPoolGetAttr(
00304         ThreadPool *tp,
00306         ThreadPoolAttr *out);
00307 
00316 int ThreadPoolSetAttr(
00318         ThreadPool *tp,
00320         ThreadPoolAttr *attr);
00321 
00329 int ThreadPoolAdd(
00331         ThreadPool*tp,
00333         ThreadPoolJob *job,
00335         int *jobId);
00336 
00345 int ThreadPoolRemove(
00347         ThreadPool *tp,
00349         int jobId,
00351         ThreadPoolJob *out);
00352 
00359 int ThreadPoolShutdown(
00361         ThreadPool *tp);
00362 
00369 int TPJobInit(
00371         ThreadPoolJob *job,
00373         start_routine func,
00375         void *arg);
00376 
00382 int TPJobSetPriority(
00384         ThreadPoolJob *job,
00386         ThreadPriority priority);
00387 
00393 int TPJobSetFreeFunction(
00395         ThreadPoolJob *job,
00397         free_routine func);
00398 
00405 int TPAttrInit(
00407         ThreadPoolAttr *attr);
00408 
00414 int TPAttrSetMaxThreads(
00416         ThreadPoolAttr *attr,
00418         int maxThreads);
00419 
00425 int TPAttrSetMinThreads(
00427         ThreadPoolAttr *attr,
00429         int minThreads);
00430 
00436 int TPAttrSetStackSize(
00438         ThreadPoolAttr *attr,
00440         size_t stackSize);
00441 
00447 int TPAttrSetIdleTime(
00449         ThreadPoolAttr *attr,
00451         int idleTime);
00452 
00458 int TPAttrSetJobsPerThread(
00460         ThreadPoolAttr *attr,
00462         int jobsPerThread);
00463 
00469 int TPAttrSetStarvationTime(
00471         ThreadPoolAttr *attr,
00473         int starvationTime);
00474 
00480 int TPAttrSetSchedPolicy(
00482         ThreadPoolAttr *attr,
00484         PolicyType schedPolicy);
00485 
00491 int TPAttrSetMaxJobsTotal(
00493         ThreadPoolAttr *attr,
00495         int maxJobsTotal);
00496 
00504 #ifdef STATS
00505         EXPORT_SPEC int ThreadPoolGetStats(
00507                 ThreadPool *tp,
00509                 ThreadPoolStats *stats);
00510 #else
00511         static UPNP_INLINE int ThreadPoolGetStats(
00513                 ThreadPool *tp,
00515                 ThreadPoolStats *stats) {}
00516 #endif
00517 
00521 #ifdef STATS
00522         EXPORT_SPEC void ThreadPoolPrintStats(
00524                 ThreadPoolStats *stats);
00525 #else
00526         static UPNP_INLINE void ThreadPoolPrintStats(
00528                 ThreadPoolStats *stats) {}
00529 #endif
00530 
00531 #ifdef __cplusplus
00532 }
00533 #endif
00534 
00535 #endif /* THREADPOOL_H */
00536