61#if ALIB_DEBUG_CRITICAL_SECTIONS
66 friend struct PWorker;
133 int GetSize(
int currentWorkers ,
int idleWorkers,
int load,
134 Ticks& lastChangeTime )
136 int target= currentWorkers;
148 { target = (std::min)(
WorkersMax, currentWorkers + 1); }
152 && (currentWorkers - idleWorkers) <= (currentWorkers *
DecreaseThreshold / 100) )
153 { target = (std::max)(
WorkersMin, currentWorkers - 1); }
156 if (target != currentWorkers)
157 lastChangeTime.
Reset();
190 const std::type_info*
TID;
197 const std::type_info* >
263 queue.emplace_front( entry );
267 ALIB_MESSAGE(
"MGTHR/QUEUE",
"Pool({}}/{} -> {}/) Job({}) pushed",
293 template<
typename TJob,
typename... TArgs>
303 ||
Strategy.FixedSize > 0 ),
"MGTHR/STRGY",
304 "No threads to schedule job. Strategy values:\n"
306 " Strategy.Mode: {}\n"
307 " Strategy.FixedSize: ",
314 TJob* job=
pool().New<TJob>( std::forward<TArgs>(args)... );
316 "Error in ThreadPool::schedule: Job size mismatch. Expected {} "
317 "while virtual method SizeOf returns {}.\n"
318 "Override this method for job-type <{}>",
sizeof(TJob), job->SizeOf(), &
typeid(*job) )
321 "Error: Job pushed while this pool is shut down already. (Strategy.WorkersMax == 0) " )
326 ++pair.first.Value().Usage;
342 #if ALIB_DEBUG_CRITICAL_SECTIONS
379 {
return int(std::thread::hardware_concurrency()); }
404 template<
typename TJob,
typename... TArgs>
407 {
return *
schedule<TJob, TArgs...>(
true, std::forward<TArgs>(args)... ); }
415 template<
typename TJob,
typename... TArgs>
437 pool.free(&job, size);
507 Ticks::Duration dbgWarnAfter );
510 ALIB_DBG(, Ticks::Duration dbgWarnAfter) );
512 ALIB_DBG(, Ticks::Duration::TDuration dbgWarnAfter) )
540 #if ALIB_DEBUG && ALIB_STRINGS
virtual ALIB_DLL bool DCSIsAcquired() const override
int ctdWorkers
The counted number of currently of workers.
QueueEntry pop(PWorker *worker)
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)
PWorker * lastThreadToJoin
void Acquire(ALIB_DBG_TAKE_CI)
List< PoolAllocator, QueueEntry, Recycling::None > queue
The queue of jobs.
int nextWorkerID
A number that is increased with the creation of new workers and added to their.
MonoAllocator & GetAllocator()
HashSet< MonoAllocator, PWorker * > workers
The list of worker threads.
virtual ALIB_DLL bool DCSIsSharedAcquired() const override
TJob & Schedule(TArgs &&... args)
HashTable< MonoAllocator, DbgKnownJobsVD > DbgKnownJobs
Table of known job types and their sizes.
uinteger StatsCountedScheduledJobs()
int ctdOpenJobs
The number of jobs in the queue.
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)
ALIB_DLL ~ThreadPool() override
Destructor. Cleans up and shuts down the thread pool.
void DeleteJobDeferred(Job &job)
uinteger ctdStatJobsScheduled
The number of jobs in the queue.
MonoAllocator ma
Mono allocator. Used for jobs and by PWorkers.
Ticks timeOfLastSizeChange
The point in time of the last change of thread size.
void ScheduleVoid(TArgs &&... args)
#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.
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
time::Ticks Ticks
Type alias in namespace alib.
threadmodel::ThreadPool ThreadPool
Type alias in namespace alib.
lang::uinteger uinteger
Type alias in namespace alib.
containers::List< TAllocator, T, TRecycling > List
Type alias in namespace alib.
Job(const std::type_info &id)
virtual ~Job()=default
Protected destructor.
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.
int WorkersMax
The maximum number of threads to create.
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)