91#if ALIB_DEBUG_CRITICAL_SECTIONS
164 int GetSize(
int currentWorkers ,
int idleWorkers,
int load,
165 Ticks& lastChangeTime ) {
166 int target= currentWorkers;
179 { target = (std::min)(
WorkersMax, currentWorkers + 1); }
183 && (currentWorkers - idleWorkers) <= (currentWorkers *
DecreaseThreshold / 100) ) {
184 target = (std::max)(
WorkersMin, currentWorkers - 1);
185 if ( target == 0 && load > 0)
190 if (target != currentWorkers)
191 lastChangeTime.
Reset();
224 const std::type_info*
TID;
231 const std::type_info* >
288 return queue.IsNotEmpty()
297 queue.emplace_front( entry );
302 this, &entry.job->ID )
327 template<
typename TJob,
typename... TArgs>
335 ||
Strategy.WorkersMax > 0 ),
"TMOD/STRGY",
336 "No threads to schedule job. Strategy values:\n"
344 TJob* job=
pool().New<TJob>( std::forward<TArgs>(args)... );
346 "{} error in schedule: Job size mismatch. Expected {} "
347 "while virtual method SizeOf returns {}.\n"
348 "Override this method for job-type <{}>",
this,
sizeof(TJob), job->SizeOf(), &
typeid(*job) )
351 "{} error: Job pushed while this pool is shut down already. "
352 "(Strategy.WorkersMax == 0) ",
this )
357 ++pair.first.Value().Usage;
370 Ticks::Duration
IdleWaitTime = Ticks::Duration::FromAbsoluteMicroseconds(50);
376 #if ALIB_DEBUG_CRITICAL_SECTIONS
424 {
return int(std::thread::hardware_concurrency()); }
449 template<
typename TJob,
typename... TArgs>
452 {
return *
schedule<TJob, TArgs...>(
true, std::forward<TArgs>(args)... ); }
460 template<
typename TJob,
typename... TArgs>
481 pool.free(&job, size);
550 Ticks::Duration dbgWarnAfter );
553 ALIB_DBG(, Ticks::Duration dbgWarnAfter) );
555 ALIB_DBG(, Ticks::Duration::TDuration dbgWarnAfter) )
583 #if ALIB_DEBUG && ALIB_STRINGS
618namespace 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)