libUPnP  1.14.24
ThreadPool.h
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (c) 2000-2003 Intel Corporation
4  * All rights reserved.
5  * Copyright (c) 2012 France Telecom All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * * Neither name of Intel Corporation nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  ******************************************************************************/
32 
33 #ifndef THREADPOOL_H
34 #define THREADPOOL_H
35 
40 #include "FreeList.h"
41 #include "LinkedList.h"
42 #include "UpnpGlobal.h" /* for UPNP_INLINE, UPNP_EXPORT_SPEC */
43 #include "UpnpInet.h"
44 #include "ithread.h"
45 
46 #include <errno.h>
47 
48 #ifdef _WIN32
49  #include <time.h>
50 
51  #ifndef _TIMEZONE_DEFINED
52 struct timezone
53 {
54  int tz_minuteswest; /* minutes W of Greenwich */
55  int tz_dsttime; /* type of dst correction */
56 };
57  #endif
58 int gettimeofday(struct timeval *tv, struct timezone *tz);
59 #else /* _WIN32 */
60  #include <sys/param.h>
61  #include <sys/time.h> /* for gettimeofday() */
62  #if defined(__APPLE__) || defined(__NetBSD__)
63  #include <sys/resource.h> /* for setpriority() */
64  #endif
65 #endif
66 
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70 
72 #define JOBFREELISTSIZE 100
73 
74 #define INFINITE_THREADS -1
75 
76 #define EMAXTHREADS (-8 & 1 << 29)
77 
79 #define INVALID_POLICY (-9 & 1 << 29)
80 
82 #define INVALID_JOB_ID (-2 & 1 << 29)
83 
84 typedef enum duration
85 {
86  SHORT_TERM,
87  PERSISTENT
88 } Duration;
89 
90 typedef enum priority
91 {
92  LOW_PRIORITY,
93  MED_PRIORITY,
94  HIGH_PRIORITY
95 } ThreadPriority;
96 
98 #define DEFAULT_PRIORITY MED_PRIORITY
99 
101 #define DEFAULT_MIN_THREADS 1
102 
104 #define DEFAULT_MAX_THREADS 10
105 
107 #define DEFAULT_STACK_SIZE 0u
108 
110 #define DEFAULT_JOBS_PER_THREAD 10
111 
113 #define DEFAULT_STARVATION_TIME 500
114 
116 #define DEFAULT_IDLE_TIME 10 * 1000
117 
119 #define DEFAULT_FREE_ROUTINE NULL
120 
122 #define DEFAULT_MAX_JOBS_TOTAL 100
123 extern int maxJobsTotal;
124 
130 #define STATS 1
131 
132 #ifdef _DEBUG
133  #define DEBUG 1
134 #endif
135 
136 typedef int PolicyType;
137 
138 #define DEFAULT_POLICY SCHED_OTHER
139 
141 typedef void (*free_routine)(void *arg);
142 
145 typedef struct THREADPOOLATTR
146 {
152  size_t stackSize;
164  PolicyType schedPolicy;
166 
168 typedef struct THREADPOOLJOB
169 {
170  start_routine func;
171  void *arg;
172  free_routine free_func;
173  struct timeval requestTime;
174  ThreadPriority priority;
175  int jobId;
176 } ThreadPoolJob;
177 
179 typedef struct TPOOLSTATS
180 {
181  double totalTimeHQ;
182  int totalJobsHQ;
183  double avgWaitHQ;
184  double totalTimeMQ;
185  int totalJobsMQ;
186  double avgWaitMQ;
187  double totalTimeLQ;
188  int totalJobsLQ;
189  double avgWaitLQ;
190  double totalWorkTime;
191  double totalIdleTime;
192  int workerThreads;
193  int idleThreads;
194  int persistentThreads;
195  int totalThreads;
196  int maxThreads;
197  int currentJobsHQ;
198  int currentJobsLQ;
199  int currentJobsMQ;
201 
217 typedef struct THREADPOOL
218 {
220  ithread_mutex_t mutex;
222  ithread_cond_t condition;
224  ithread_cond_t start_and_shutdown;
228  int shutdown;
251 } ThreadPool;
252 
258 void TPSetMaxJobsTotal(int mjt);
259 
270 int ThreadPoolInit(
272  ThreadPool *tp,
288  ThreadPoolAttr *attr);
289 
302  ThreadPool *tp,
304  ThreadPoolJob *job,
306  int *jobId);
307 
316  ThreadPool *tp,
318  ThreadPoolAttr *out);
319 
330  ThreadPool *tp,
332  ThreadPoolAttr *attr);
333 
341 int ThreadPoolAdd(
343  ThreadPool *tp,
345  ThreadPoolJob *job,
347  int *jobId);
348 
357 int ThreadPoolRemove(
359  ThreadPool *tp,
361  int jobId,
363  ThreadPoolJob *out);
364 
373  ThreadPool *tp);
374 
381 int TPJobInit(
383  ThreadPoolJob *job,
385  start_routine func,
387  void *arg);
388 
394 int TPJobSetPriority(
396  ThreadPoolJob *job,
398  ThreadPriority priority);
399 
407  ThreadPoolJob *job,
409  free_routine func);
410 
417 int TPAttrInit(
419  ThreadPoolAttr *attr);
420 
428  ThreadPoolAttr *attr,
430  int maxThreads);
431 
439  ThreadPoolAttr *attr,
441  int minThreads);
442 
450  ThreadPoolAttr *attr,
452  size_t stackSize);
453 
461  ThreadPoolAttr *attr,
463  int idleTime);
464 
472  ThreadPoolAttr *attr,
474  int jobsPerThread);
475 
483  ThreadPoolAttr *attr,
485  int starvationTime);
486 
494  ThreadPoolAttr *attr,
496  PolicyType schedPolicy);
497 
505  ThreadPoolAttr *attr,
507  int maxJobsTotal);
508 
516 #ifdef STATS
519  ThreadPool *tp,
521  ThreadPoolStats *stats);
522 #else
525  ThreadPool *tp,
527  ThreadPoolStats *stats)
528 {
529 }
530 #endif
531 
535 #ifdef STATS
538  ThreadPoolStats *stats);
539 #else
542  ThreadPoolStats *stats)
543 {
544 }
545 #endif
546 
547 #ifdef __cplusplus
548 }
549 #endif
550 
551 #endif /* THREADPOOL_H */
int TPAttrSetMaxJobsTotal(ThreadPoolAttr *attr, int maxJobsTotal)
Sets the maximum number jobs that can be qeued totally.
Definition: ThreadPool.c:1189
int persistentThreads
Definition: ThreadPool.h:236
int ThreadPoolGetAttr(ThreadPool *tp, ThreadPoolAttr *out)
Gets the current set of attributes associated with the thread pool.
Definition: ThreadPool.c:936
int shutdown
Definition: ThreadPool.h:228
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1102
Definition: ThreadPool.h:145
void(* free_routine)(void *arg)
Definition: ThreadPool.h:141
struct THREADPOOLJOB ThreadPoolJob
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
UPNP_EXPORT_SPEC void ThreadPoolPrintStats(ThreadPoolStats *stats)
Definition: ThreadPool.c:1199
int starvationTime
Definition: ThreadPool.h:162
int TPJobInit(ThreadPoolJob *job, start_routine func, void *arg)
Initializes thread pool job. Sets the priority to default defined in ThreadPool.h. Sets the free_routine to default defined in ThreadPool.h.
Definition: ThreadPool.c:1090
PolicyType schedPolicy
Definition: ThreadPool.h:164
int maxIdleTime
Definition: ThreadPool.h:155
ithread_cond_t condition
Definition: ThreadPool.h:222
Definition: ThreadPool.h:179
int ThreadPoolAddPersistent(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a persistent job to the thread pool.
Definition: ThreadPool.c:778
int lastJobId
Definition: ThreadPool.h:226
LinkedList lowJobQ
Definition: ThreadPool.h:240
int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread)
Sets the jobs per thread ratio.
Definition: ThreadPool.c:1162
struct TPOOLSTATS ThreadPoolStats
struct THREADPOOL ThreadPool
A thread pool similar to the thread pool in the UPnP SDK.
int totalThreads
Definition: ThreadPool.h:230
A thread pool similar to the thread pool in the UPnP SDK.
Definition: ThreadPool.h:217
int ThreadPoolSetAttr(ThreadPool *tp, ThreadPoolAttr *attr)
Sets the attributes for the thread pool. Only affects future calculations.
Definition: ThreadPool.c:949
int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads)
Sets the min threads for the thread pool attributes.
Definition: ThreadPool.c:1135
int maxThreads
Definition: ThreadPool.h:150
ThreadPoolJob * persistentJob
Definition: ThreadPool.h:246
int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime)
Sets the idle time for the thread pool attributes.
Definition: ThreadPool.c:1153
int ThreadPoolInit(ThreadPool *tp, ThreadPoolAttr *attr)
Initializes and starts ThreadPool. Must be called first and only once for ThreadPool.
Definition: ThreadPool.c:710
ithread_mutex_t mutex
Definition: ThreadPool.h:220
Provides a platform independent way to include TCP/IP types and functions.
int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime)
Sets the starvation time for the thread pool attributes.
Definition: ThreadPool.c:1171
FreeList jobFreeList
Definition: ThreadPool.h:238
Definition: FreeList.h:59
int maxJobsTotal
Definition: ThreadPool.h:159
ThreadPoolStats stats
Definition: ThreadPool.h:250
LinkedList medJobQ
Definition: ThreadPool.h:242
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
int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize)
Sets the stack size for the thread pool attributes.
Definition: ThreadPool.c:1144
Definition: ThreadPool.h:168
int busyThreads
Definition: ThreadPool.h:234
LinkedList highJobQ
Definition: ThreadPool.h:244
size_t stackSize
Definition: ThreadPool.h:152
int minThreads
Definition: ThreadPool.h:148
UPNP_EXPORT_SPEC int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats)
Returns various statistics about the thread pool.
Definition: ThreadPool.c:1235
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
int pendingWorkerThreadStart
Definition: ThreadPool.h:232
ithread_cond_t start_and_shutdown
Definition: ThreadPool.h:224
int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy)
Sets the scheduling policy for the thread pool attributes.
Definition: ThreadPool.c:1180
#define UPNP_EXPORT_SPEC
Export functions on WIN32 DLLs.
Definition: UpnpGlobal.h:105
int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1126
ThreadPoolAttr attr
Definition: ThreadPool.h:248
struct THREADPOOLATTR ThreadPoolAttr
int TPJobSetFreeFunction(ThreadPoolJob *job, free_routine func)
Sets the max threads for the thread pool attributes.
Definition: ThreadPool.c:1117
#define UPNP_INLINE
Declares an inline function.
Definition: UpnpGlobal.h:119
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
int TPAttrInit(ThreadPoolAttr *attr)
Initializes thread pool attributes. Sets values to defaults defined in ThreadPool.h.
Definition: ThreadPool.c:1074
Definition: LinkedList.h:83
Defines constants that for some reason are not defined on some systems.
int jobsPerThread
Definition: ThreadPool.h:157