libUPnP
1.6.17
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
threadutil
inc
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 "
ithread.h
"
42
#include "
LinkedList.h
"
43
#include "
UpnpInet.h
"
44
#include "
UpnpGlobal.h
"
/* for UPNP_INLINE, EXPORT_SPEC */
45
46
#include <errno.h>
47
48
#ifdef WIN32
49
#include <time.h>
50
struct
timezone
51
{
52
int
tz_minuteswest;
/* minutes W of Greenwich */
53
int
tz_dsttime;
/* type of dst correction */
54
};
55
int
gettimeofday(
struct
timeval *tv,
struct
timezone *tz);
56
#else
/* WIN32 */
57
#include <sys/param.h>
58
#include <sys/time.h>
/* for gettimeofday() */
59
#if defined(__OSX__) || defined(__APPLE__) || defined(__NetBSD__)
60
#include <sys/resource.h>
/* for setpriority() */
61
#endif
62
#endif
63
64
#ifdef __cplusplus
65
extern
"C"
{
66
#endif
67
69
#define JOBFREELISTSIZE 100
70
71
#define INFINITE_THREADS -1
72
73
#define EMAXTHREADS (-8 & 1<<29)
74
76
#define INVALID_POLICY (-9 & 1<<29)
77
79
#define INVALID_JOB_ID (-2 & 1<<29)
80
81
typedef
enum
duration {
82
SHORT_TERM,
83
PERSISTENT
84
} Duration;
85
86
typedef
enum
priority {
87
LOW_PRIORITY,
88
MED_PRIORITY,
89
HIGH_PRIORITY
90
} ThreadPriority;
91
93
#define DEFAULT_PRIORITY MED_PRIORITY
94
96
#define DEFAULT_MIN_THREADS 1
97
99
#define DEFAULT_MAX_THREADS 10
100
102
#define DEFAULT_STACK_SIZE 0u
103
105
#define DEFAULT_JOBS_PER_THREAD 10
106
108
#define DEFAULT_STARVATION_TIME 500
109
111
#define DEFAULT_IDLE_TIME 10 * 1000
112
114
#define DEFAULT_FREE_ROUTINE NULL
115
117
#define DEFAULT_MAX_JOBS_TOTAL 100
118
124
#define STATS 1
125
126
#ifdef _DEBUG
127
#define DEBUG 1
128
#endif
129
130
typedef
int
PolicyType;
131
132
#define DEFAULT_POLICY SCHED_OTHER
133
135
typedef
void (*
free_routine
)(
void
*arg);
136
137
140
typedef
struct
THREADPOOLATTR
141
{
143
int
minThreads
;
145
int
maxThreads
;
147
size_t
stackSize
;
150
int
maxIdleTime
;
152
int
jobsPerThread
;
154
int
maxJobsTotal
;
157
int
starvationTime
;
159
PolicyType
schedPolicy
;
160
}
ThreadPoolAttr
;
161
163
typedef
struct
THREADPOOLJOB
164
{
165
start_routine func;
166
void
*arg;
167
free_routine
free_func;
168
struct
timeval requestTime;
169
ThreadPriority priority;
170
int
jobId;
171
}
ThreadPoolJob
;
172
174
typedef
struct
TPOOLSTATS
175
{
176
double
totalTimeHQ;
177
int
totalJobsHQ;
178
double
avgWaitHQ;
179
double
totalTimeMQ;
180
int
totalJobsMQ;
181
double
avgWaitMQ;
182
double
totalTimeLQ;
183
int
totalJobsLQ;
184
double
avgWaitLQ;
185
double
totalWorkTime;
186
double
totalIdleTime;
187
int
workerThreads;
188
int
idleThreads;
189
int
persistentThreads;
190
int
totalThreads;
191
int
maxThreads;
192
int
currentJobsHQ;
193
int
currentJobsLQ;
194
int
currentJobsMQ;
195
}
ThreadPoolStats
;
196
212
typedef
struct
THREADPOOL
213
{
215
ithread_mutex_t
mutex
;
217
ithread_cond_t
condition
;
219
ithread_cond_t
start_and_shutdown
;
221
int
lastJobId
;
223
int
shutdown
;
225
int
totalThreads
;
227
int
pendingWorkerThreadStart
;
229
int
busyThreads
;
231
int
persistentThreads
;
233
FreeList
jobFreeList
;
235
LinkedList
lowJobQ
;
237
LinkedList
medJobQ
;
239
LinkedList
highJobQ
;
241
ThreadPoolJob
*
persistentJob
;
243
ThreadPoolAttr
attr
;
245
ThreadPoolStats
stats
;
246
}
ThreadPool
;
247
258
int
ThreadPoolInit
(
260
ThreadPool
*tp,
276
ThreadPoolAttr
*attr);
277
288
int
ThreadPoolAddPersistent
(
290
ThreadPool
*tp,
292
ThreadPoolJob
*job,
294
int
*jobId);
295
302
int
ThreadPoolGetAttr
(
304
ThreadPool
*tp,
306
ThreadPoolAttr
*out);
307
316
int
ThreadPoolSetAttr
(
318
ThreadPool
*tp,
320
ThreadPoolAttr
*attr);
321
329
int
ThreadPoolAdd
(
331
ThreadPool
*tp,
333
ThreadPoolJob
*job,
335
int
*jobId);
336
345
int
ThreadPoolRemove
(
347
ThreadPool
*tp,
349
int
jobId,
351
ThreadPoolJob
*out);
352
359
int
ThreadPoolShutdown
(
361
ThreadPool
*tp);
362
369
int
TPJobInit
(
371
ThreadPoolJob
*job,
373
start_routine func,
375
void
*arg);
376
382
int
TPJobSetPriority
(
384
ThreadPoolJob
*job,
386
ThreadPriority priority);
387
393
int
TPJobSetFreeFunction
(
395
ThreadPoolJob
*job,
397
free_routine
func);
398
405
int
TPAttrInit
(
407
ThreadPoolAttr
*attr);
408
414
int
TPAttrSetMaxThreads
(
416
ThreadPoolAttr
*attr,
418
int
maxThreads);
419
425
int
TPAttrSetMinThreads
(
427
ThreadPoolAttr
*attr,
429
int
minThreads);
430
436
int
TPAttrSetStackSize
(
438
ThreadPoolAttr
*attr,
440
size_t
stackSize);
441
447
int
TPAttrSetIdleTime
(
449
ThreadPoolAttr
*attr,
451
int
idleTime);
452
458
int
TPAttrSetJobsPerThread
(
460
ThreadPoolAttr
*attr,
462
int
jobsPerThread);
463
469
int
TPAttrSetStarvationTime
(
471
ThreadPoolAttr
*attr,
473
int
starvationTime);
474
480
int
TPAttrSetSchedPolicy
(
482
ThreadPoolAttr
*attr,
484
PolicyType schedPolicy);
485
491
int
TPAttrSetMaxJobsTotal
(
493
ThreadPoolAttr
*attr,
495
int
maxJobsTotal);
496
504
#ifdef STATS
505
EXPORT_SPEC
int
ThreadPoolGetStats
(
507
ThreadPool
*tp,
509
ThreadPoolStats
*stats);
510
#else
511
static
UPNP_INLINE
int
ThreadPoolGetStats
(
513
ThreadPool
*tp,
515
ThreadPoolStats
*stats) {}
516
#endif
517
521
#ifdef STATS
522
EXPORT_SPEC
void
ThreadPoolPrintStats
(
524
ThreadPoolStats
*stats);
525
#else
526
static
UPNP_INLINE
void
ThreadPoolPrintStats
(
528
ThreadPoolStats
*stats) {}
529
#endif
530
531
#ifdef __cplusplus
532
}
533
#endif
534
535
#endif
/* THREADPOOL_H */
536
Generated on Sat Feb 2 2013 05:11:51 for libUPnP by
1.8.3.1