99#if ALIB_DEBUG_CRITICAL_SECTIONS
168 int GetSize(
int currentWorkers ,
int idleWorkers,
int load,
169 Ticks& lastChangeTime ) {
170 int target= currentWorkers;
183 { target = (std::min)(
WorkersMax, currentWorkers + 1); }
187 && (currentWorkers - idleWorkers) <= (currentWorkers *
DecreaseThreshold / 100) ) {
188 target = (std::max)(
WorkersMin, currentWorkers - 1);
189 if ( target == 0 && load > 0)
194 if (target != currentWorkers)
195 lastChangeTime.
Reset();
228 const std::type_info*
TID;
235 const std::type_info* >
291 return queue.IsNotEmpty()
300 queue.emplace_front( entry );
305 this, &entry.job->ID )
330 template<
typename TJob,
typename... TArgs>
338 ||
Strategy.WorkersMax > 0 ),
"TMOD/STRGY",
339 "No threads to schedule job. Strategy values:\n"
347 TJob* job=
pool().New<TJob>( std::forward<TArgs>(args)... );
349 "{} error in schedule: Job size mismatch. Expected {} "
350 "while virtual method SizeOf returns {}.\n"
351 "Override this method for job-type <{}>",
this,
sizeof(TJob), job->SizeOf(), &
typeid(*job) )
354 "{} error: Job pushed while this pool is shut down already. "
355 "(Strategy.WorkersMax == 0) ",
this )
360 ++pair.first.Value().Usage;
373 Ticks::Duration
IdleWaitTime = Ticks::Duration::FromAbsoluteMicroseconds(50);
379 #if ALIB_DEBUG_CRITICAL_SECTIONS
427 {
return int(std::thread::hardware_concurrency()); }
452 template<
typename TJob,
typename... TArgs>
455 {
return *
schedule<TJob, TArgs...>(
true, std::forward<TArgs>(args)... ); }
463 template<
typename TJob,
typename... TArgs>
484 pool.free(&job, size);
554 Ticks::Duration dbgWarnAfter );
557 ALIB_DBG(, Ticks::Duration dbgWarnAfter) );
559 ALIB_DBG(, Ticks::Duration::TDuration dbgWarnAfter) )
587 #if ALIB_DEBUG && ALIB_STRINGS
622namespace APPENDABLES {
PoolWorker * lastThreadToJoin
int ctdWorkers
The counted number of currently of workers.
Ticks::Duration IdleWaitTime
The wait-time slice used by method WaitForAllIdle.
ALIB_DLL bool DCSIsAcquired() const override
int ctdIdle
The counted number of currently idle workers.
PoolAllocator pool
Pool allocator. Used for job objects.
static int HardwareConcurrency() noexcept
void pushAndRelease(QueueEntry &&entry)
int CountedWorkers() const
uinteger StatsCountedScheduledJobs() const
int CountedOpenJobs() const
void Acquire(ALIB_DBG_TAKE_CI)
int nextWorkerID
A number that is increased with the creation of new workers and added to their.
MonoAllocator & GetAllocator()
virtual void DisposeWorker(PoolWorker *poolWorker)
TJob & Schedule(TArgs &&... args)
HashTable< MonoAllocator, DbgKnownJobsVD > DbgKnownJobs
Table of known job types and their sizes.
ALIB_DLL bool DCSIsSharedAcquired() const override
int CountedIdleWorkers() const
int ctdOpenJobs
The number of jobs currently in the queue.
ListPA< QueueEntry, Recycling::None > queue
The queue of jobs.
QueueEntry pop(PoolWorker *worker)
ALIB_DLL void addThread()
Internal method that adds a thread. Must only be called when acquired.
TJob * schedule(bool keepJob, TArgs &&... args)
PoolAllocator & GetPoolAllocator()
ALIB_DLL int DbgDumpKnownJobs(NAString &target, const NString &linePrefix=" ")
ALIB_DLL bool WaitForAllIdle(Ticks::Duration timeout, Ticks::Duration dbgWarnAfter)
friend struct PoolWorker
Type alias in namespace alib.
HashSet< MonoAllocator, PoolWorker * > workers
The list of worker threads.
ALIB_DLL ~ThreadPool() override
Destructor. Cleans up and shuts down the thread pool.
virtual PoolWorker * CreateWorker()
void DeleteJobDeferred(Job &job)
uinteger ctdStatJobsScheduled
The number of Jobs that have been scheduled during the lifetime of this instance.
MonoAllocator ma
Mono allocator. Used for jobs and by PoolWorkers.
Ticks timeOfLastSizeChange
The point in time of the last change of thread size.
void ScheduleVoid(TArgs &&... args)
Thread(const character *pName=A_CHAR(""))
virtual void SetName(const character *newName)
#define ALIB_MESSAGE(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_CALLER_PRUNED
containers::HashSet< TAllocator, T, THash, TEqual, THashCaching, TRecycling > HashSet
Type alias in namespace alib. See type definition alib::containers::HashSet.
strings::TAString< character, lang::HeapAllocator > AString
Type alias in namespace alib.
LocalString< 16 > String16
Type alias name for TLocalString<character,16>.
containers::HashTable< TAllocator, TValueDescriptor, THash, TEqual, THashCaching, TRecycling > HashTable
Type alias in namespace alib. See type definition alib::containers::HashSet.
monomem::TPoolAllocator< MonoAllocator > PoolAllocator
strings::TString< nchar > NString
Type alias in namespace alib.
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace alib.
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
threads::Thread Thread
Type alias in namespace alib.
containers::List< T, PoolAllocator, TRecycling > ListPA
Type alias in namespace alib.
time::Ticks Ticks
Type alias in namespace alib.
threadmodel::ThreadPool ThreadPool
Type alias in namespace alib.
characters::character character
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
threadmodel::PoolWorker PoolWorker
Type alias in namespace alib.
void operator()(AString &target, const alib::threadmodel::ThreadPool &tpool)
Job(const std::type_info &id)
virtual ~Job()=default
Protected destructor.
virtual void PrepareJob(Job *job)
ThreadPool & threadPool
The pool that this instance belongs to.
friend class ThreadPool
Type alias in namespace alib.
PoolWorker(ThreadPool &pThreadPool, const character *threadName)
String16 nameBuffer
Buffer to store the thread name given with construction.
Entry in the field DbgKnownJobs.
size_t Usage
Counter of scheduled jobs of this type.
size_t JobSize
The size of the job object.
const std::type_info * TID
The job type.
Serves as template parameter TValueDescriptor of field DbgKnownJobs.
const std::type_info * Key(DbgKnownJobsEntry &entry) const
virtual size_t SizeOf() override
Job * JobToDelete
Optionally a job to be deleted.
Container element of the queue.
Ticks::Duration IncreaseSchedule
Modes Mode
The mode of operation.
Ticks::Duration DecreaseSchedule
friend class ThreadPool
Type alias in namespace alib.
Modes
The modes, fixed or automatic.
@ Fixed
The number of threads is fixed.
int WorkersMin
The minimum number of threads to keep alive.
int GetSize(int currentWorkers, int idleWorkers, int load, Ticks &lastChangeTime)
TCondition(const character *dbgName)
void Release(ALIB_DBG_TAKE_CI)
void Acquire(ALIB_DBG_TAKE_CI)
void ReleaseAndNotify(ALIB_DBG_TAKE_CI)