91#if ALIB_DEBUG_CRITICAL_SECTIONS
162 int GetSize(
int currentWorkers ,
int idleWorkers,
int load,
163 Ticks& lastChangeTime ) {
164 int target= currentWorkers;
177 { target = (std::min)(
WorkersMax, currentWorkers + 1); }
181 && (currentWorkers - idleWorkers) <= (currentWorkers *
DecreaseThreshold / 100) ) {
182 target = (std::max)(
WorkersMin, currentWorkers - 1);
183 if ( target == 0 && load > 0)
188 if (target != currentWorkers)
189 lastChangeTime.
Reset();
221 const std::type_info*
TID;
228 const std::type_info* >
283 return queue.IsNotEmpty()
292 queue.emplace_front( entry );
297 this, &entry.job->ID )
322 template<
typename TJob,
typename... TArgs>
330 ||
Strategy.WorkersMax > 0 ),
"TMOD/STRGY",
331 "No threads to schedule job. Strategy values:\n"
339 TJob* job=
pool().New<TJob>( std::forward<TArgs>(args)... );
341 "{} error in schedule: Job size mismatch. Expected {} "
342 "while virtual method SizeOf returns {}.\n"
343 "Override this method for job-type <{}>",
this,
sizeof(TJob), job->SizeOf(), &
typeid(*job) )
346 "{} error: Job pushed while this pool is shut down already. "
347 "(Strategy.WorkersMax == 0) ",
this )
352 ++pair.first.Value().Usage;
365 Ticks::Duration
IdleWaitTime = Ticks::Duration::FromAbsoluteMicroseconds(50);
371 #if ALIB_DEBUG_CRITICAL_SECTIONS
419 {
return int(std::thread::hardware_concurrency()); }
444 template<
typename TJob,
typename... TArgs>
447 {
return *
schedule<TJob, TArgs...>(
true, std::forward<TArgs>(args)... ); }
455 template<
typename TJob,
typename... TArgs>
476 pool.free(&job, size);
545 Ticks::Duration dbgWarnAfter );
548 ALIB_DBG(, Ticks::Duration dbgWarnAfter) );
550 ALIB_DBG(, Ticks::Duration::TDuration dbgWarnAfter) )
578 #if ALIB_DEBUG && ALIB_STRINGS
613namespace APPENDABLES {
#define ALIB_MESSAGE(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_CALLER_PRUNED
bool DCSIsSharedAcquired() const override
PoolWorker * lastThreadToJoin
int ctdWorkers
The counted number of currently of workers.
Ticks::Duration IdleWaitTime
The wait-time slice used by method #".WaitForAllIdle".
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)
MonoAllocator & GetAllocator()
int DbgDumpKnownJobs(NAString &target, const NString &linePrefix=" ")
virtual void DisposeWorker(PoolWorker *poolWorker)
TJob & Schedule(TArgs &&... args)
HashTable< MonoAllocator, DbgKnownJobsVD > DbgKnownJobs
Table of known job types and their sizes.
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)
bool WaitForAllIdle(Ticks::Duration timeout, Ticks::Duration dbgWarnAfter)
TJob * schedule(bool keepJob, TArgs &&... args)
void addThread()
Internal method that adds a thread. Must only be called when acquired.
PoolAllocator & GetPoolAllocator()
~ThreadPool() override
Destructor. Cleans up and shuts down the thread pool.
HashSet< MonoAllocator, PoolWorker * > workers
The list of worker threads.
bool DCSIsAcquired() const override
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)
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
strings::TString< nchar > NString
Type alias in namespace #"%alib".
containers::HashSet< TAllocator, T, THash, TEqual, THashCaching, TRecycling > HashSet
Type alias in namespace #"%alib". See type definition #"alib::containers::HashSet".
threads::Thread Thread
Type alias in namespace #"%alib".
containers::HashTable< TAllocator, TValueDescriptor, THash, TEqual, THashCaching, TRecycling > HashTable
Type alias in namespace #"%alib". See type definition #"alib::containers::HashSet".
LocalString< 64 > String64
Type alias name for #"TLocalString;TLocalString<character,64>".
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace #"%alib".
monomem::TPoolAllocator< MonoAllocator > PoolAllocator
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".
strings::TAString< character, lang::HeapAllocator > AString
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.
String64 nameBuffer
Buffer to store the thread name given with construction.
PoolWorker(ThreadPool &pThreadPool, const character *threadName)
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
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)