UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
ThreadPool.hpp
Go to the documentation of this file.
1#ifndef COMPA_THREADPOOL_HPP
2#define COMPA_THREADPOOL_HPP
3/*******************************************************************************
4 *
5 * Copyright (c) 2000-2003 Intel Corporation
6 * All rights reserved.
7 * Copyright (c) 2012 France Telecom All rights reserved.
8 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
9 * Redistribution only with this Copyright remark. Last modified: 2025-04-30
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 *
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * * Neither name of Intel Corporation nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
31 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 ******************************************************************************/
36// Last compare with ./Pupnp source file on 2024-10-26, ver 1.14.20
46#include "LinkedList.hpp"
47#include <UPnPsdk/port_sock.hpp>
48
49#if defined(_WIN32) || defined(DOXYGEN_RUN)
50#if !defined(_TIMEZONE_DEFINED) || defined(DOXYGEN_RUN)
59int gettimeofday(struct timeval* tv, struct timezone* tz);
60#endif
61#else // _WIN32
63#include <sys/time.h> /* for gettimeofday() */
65#endif // _WIN32
66
68#define INVALID_JOB_ID (-2 & 1 << 29)
69
71typedef void (*free_routine)(void* arg);
72
74enum Duration { SHORT_TERM, PERSISTENT };
75
77enum ThreadPriority { LOW_PRIORITY, MED_PRIORITY, HIGH_PRIORITY };
78
80constexpr ThreadPriority DEFAULT_PRIORITY{MED_PRIORITY};
81
83constexpr int DEFAULT_MIN_THREADS{1};
84
86constexpr int DEFAULT_MAX_THREADS{10};
87
89constexpr int DEFAULT_STACK_SIZE{0u};
90
92constexpr int DEFAULT_JOBS_PER_THREAD{10};
93
95constexpr int DEFAULT_STARVATION_TIME{500};
96
98constexpr int DEFAULT_IDLE_TIME{10 * 1000};
99
102
104constexpr int DEFAULT_MAX_JOBS_TOTAL{100};
106extern int maxJobsTotal;
107
113#define STATS 1
114
115#ifdef _DEBUG
116#define DEBUG 1
117#endif
118
120typedef int PolicyType;
121
123#define DEFAULT_POLICY SCHED_OTHER
124
148
151 UPnPsdk::start_routine func;
152 void* arg;
153 free_routine free_func;
154 struct timeval requestTime;
155 ThreadPriority priority;
156 int jobId;
157};
158
161 double totalTimeHQ;
162 int totalJobsHQ;
163 double avgWaitHQ;
164 double totalTimeMQ;
165 int totalJobsMQ;
166 double avgWaitMQ;
167 double totalTimeLQ;
168 int totalJobsLQ;
169 double avgWaitLQ;
170 double totalWorkTime;
171 double totalIdleTime;
172 int workerThreads;
173 int idleThreads;
174 int persistentThreads;
175 int totalThreads;
176 int maxThreads;
177 int currentJobsHQ;
178 int currentJobsLQ;
179 int currentJobsMQ;
180};
181
228
235void TPSetMaxJobsTotal(int mjt);
236
252 ThreadPool* tp,
270 ThreadPoolAttr* attr);
271
285 ThreadPool* tp,
287 ThreadPoolJob* job,
289 int* jobId);
290
300 ThreadPool* tp,
302 ThreadPoolAttr* out);
303
316 ThreadPool* tp,
318 ThreadPoolAttr* attr);
319
330int ThreadPoolAdd(
332 ThreadPool* tp,
334 ThreadPoolJob* job,
336 int* jobId);
337
350 ThreadPool* tp,
352 int jobId,
354 ThreadPoolJob* out);
355
367 ThreadPool* tp);
368
377int TPJobInit(
379 ThreadPoolJob* job,
381 UPnPsdk::start_routine func,
383 void* arg);
384
394 ThreadPoolJob* job,
396 ThreadPriority priority);
397
407 ThreadPoolJob* job,
409 free_routine func);
410
420int TPAttrInit(
422 ThreadPoolAttr* attr);
423
433 ThreadPoolAttr* attr,
435 int maxThreads);
436
446 ThreadPoolAttr* attr,
448 int minThreads);
449
459 ThreadPoolAttr* attr,
461 size_t stackSize);
462
472 ThreadPoolAttr* attr,
474 int idleTime);
475
485 ThreadPoolAttr* attr,
487 int jobsPerThread);
488
498 ThreadPoolAttr* attr,
500 int starvationTime);
501
511 ThreadPoolAttr* attr,
513 PolicyType schedPolicy);
514
524 ThreadPoolAttr* attr,
526 int totalMaxJobs);
527
537#if defined(STATS) || defined(DOXYGEN_RUN)
540 ThreadPool* tp,
542 ThreadPoolStats* stats);
543#else
544static inline int ThreadPoolGetStats(
546 ThreadPool* tp,
548 ThreadPoolStats* stats) {}
549#endif
550
556#if defined(STATS) || defined(DOXYGEN_RUN)
559 ThreadPoolStats* stats);
560#else
561static inline void ThreadPoolPrintStats(
563 ThreadPoolStats* stats) {}
564#endif
565
566#endif /* COMPA_THREADPOOL_HPP */
Stores head and size of free list, as well as mutex for protection.
Definition FreeList.hpp:63
Manage a linked list (for internal use only).
Linked list (no protection).
int minThreads
ThreadPool will always maintain at least this many threads.
int ThreadPoolInit(ThreadPool *tp, ThreadPoolAttr *attr)
Initializes and starts ThreadPool.
ThreadPoolStats stats
int ThreadPoolAdd(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a job to the thread pool.
constexpr free_routine DEFAULT_FREE_ROUTINE
FreeList jobFreeList
int TPJobSetFreeFunction(ThreadPoolJob *job, free_routine func)
Sets the jobs free function.
int ThreadPoolShutdown(ThreadPool *tp)
Shuts the thread pool down.
constexpr int DEFAULT_STACK_SIZE
int ThreadPoolGetAttr(ThreadPool *tp, ThreadPoolAttr *out)
Gets the current set of attributes associated with the thread pool.
void ThreadPoolPrintStats(ThreadPoolStats *stats)
Prints various statistics about the thread pool to stderr.
constexpr int DEFAULT_JOBS_PER_THREAD
int tz_minuteswest
Minutes W of Greenwich.
constexpr int DEFAULT_STARVATION_TIME
constexpr ThreadPriority DEFAULT_PRIORITY
int maxIdleTime
this is the maximum time a thread will remain idle before dying (in milliseconds).
constexpr int DEFAULT_IDLE_TIME
constexpr int DEFAULT_MAX_JOBS_TOTAL
int maxJobsTotal
Maximum number of jobs that can be queued totally.
int TPAttrSetMaxJobsTotal(ThreadPoolAttr *attr, int totalMaxJobs)
Sets the maximum number jobs that can be qeued totally.
pthread_cond_t condition
LinkedList lowJobQ
constexpr int DEFAULT_MAX_THREADS
int TPAttrSetStarvationTime(ThreadPoolAttr *attr, int starvationTime)
Sets the starvation time for the thread pool attributes.
PolicyType schedPolicy
Scheduling policy to use.
int TPAttrSetMaxThreads(ThreadPoolAttr *attr, int maxThreads)
Sets the max threads for the thread pool attributes.
int TPJobSetPriority(ThreadPoolJob *job, ThreadPriority priority)
Sets the priority of the threadpool job.
ThreadPoolJob * persistentJob
int starvationTime
The time a low priority or med priority job waits before getting bumped up a priority (in millisecond...
int ThreadPoolSetAttr(ThreadPool *tp, ThreadPoolAttr *attr)
Sets the attributes for the thread pool.
int ThreadPoolRemove(ThreadPool *tp, int jobId, ThreadPoolJob *out)
Removes a job from the thread pool.
Duration
Duration.
int tz_dsttime
Type of dst correction.
int ThreadPoolGetStats(ThreadPool *tp, ThreadPoolStats *stats)
Returns various statistics about the thread pool.
int TPAttrSetJobsPerThread(ThreadPoolAttr *attr, int jobsPerThread)
Sets the jobs per thread ratio.
pthread_cond_t start_and_shutdown
int persistentThreads
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get time of day.
int TPJobInit(ThreadPoolJob *job, UPnPsdk::start_routine func, void *arg)
Initializes thread pool job.
int jobsPerThread
Jobs per thread to maintain.
LinkedList highJobQ
int PolicyType
Type of the thread policy.
ThreadPriority
Thread priority.
pthread_mutex_t mutex
ThreadPoolAttr attr
int TPAttrInit(ThreadPoolAttr *attr)
Initializes thread pool attributes.
void TPSetMaxJobsTotal(int mjt)
Sets the maximum number of jobs in the thread pool.
int pendingWorkerThreadStart
int TPAttrSetIdleTime(ThreadPoolAttr *attr, int idleTime)
Sets the idle time for the thread pool attributes.
int TPAttrSetMinThreads(ThreadPoolAttr *attr, int minThreads)
Sets the min threads for the thread pool attributes.
int TPAttrSetSchedPolicy(ThreadPoolAttr *attr, PolicyType schedPolicy)
Sets the scheduling policy for the thread pool attributes.
int maxJobsTotal
void(* free_routine)(void *arg)
int TPAttrSetStackSize(ThreadPoolAttr *attr, size_t stackSize)
Sets the stack size for the thread pool attributes.
int ThreadPoolAddPersistent(ThreadPool *tp, ThreadPoolJob *job, int *jobId)
Adds a persistent job to the thread pool.
size_t stackSize
This is the minimum stack size allocated for each thread.
LinkedList medJobQ
int maxThreads
ThreadPool will never have more than this number of threads.
constexpr int DEFAULT_MIN_THREADS
A thread pool.
Internal ThreadPool Job.
Structure to hold statistics.
Attributes for thread pool.
Timezone.
Specifications to be portable with sockets between different platforms.