8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
23 module ALib.ThreadModel;
42 #if ALIB_DEBUG_CRITICAL_SECTIONS
43 ma.DbgCriticalSectionsPH.Get()->DCSLock=
this;
73 int nextWarnSecond= 1;
78 if( waitCheck.
Age().InAbsoluteSeconds() == nextWarnSecond ) {
80 "DWManager::Remove: Waiting on thread \"{}\" to stop. State::{}, Load: ",
94 ALIB_DBG(, Ticks::Duration dbgWarnAfter) ) {
107 if( cntRunning == 0 )
111 if( nextWarning.Age() > dbgWarnAfter) {
112 std::vector<std::any> args; args.reserve(32);
113 args.emplace_back(
"Waiting on {} thread(s) to become idle.\n");
114 args.emplace_back( cntRunning );
118 if ( it->Load() > 0 ) {
119 args.emplace_back( ++tNr );
120 args.emplace_back(
": {},\tState::{},\t Load: \n" );
121 args.emplace_back( it->GetName() );
122 args.emplace_back( it->state );
123 args.emplace_back( it->Load() );
131 if (waitStart.
Age() > timeout)
142 int nextWarnSecond= 1; )
146 if( !it->StopIsScheduled() )
147 it->ScheduleStop( stopPriority );
157 if( cntRunning == 0 )
162 if( waitCheck.Age().InAbsoluteSeconds() == nextWarnSecond ) {
163 std::vector<std::any> args; args.reserve(32);
164 args.emplace_back(
"DWManager Termination: Waiting on {} thread(s) to stop.\n");
165 args.emplace_back( cntRunning );
168 args.emplace_back( ++tNr );
169 args.emplace_back(
": {},\tState::{},\t Load: \n" );
170 args.emplace_back( it->GetName() );
171 args.emplace_back( it->state );
172 args.emplace_back( it->Load() );
193 auto it=
queue.begin();
194 while(it !=
queue.end()) {
195 if(jobInfo.priority <= it->priority )
201 queue.Insert(it, jobInfo );
204 std::vector<std::any> args; args.reserve(32);
205 args.emplace_back(
"Queue({}) Job({}) pushed. P::{} Keep: ");
206 args.emplace_back(
queue.size() );
207 args.emplace_back( &jobInfo.job->ID );
208 args.emplace_back( jobInfo.priority );
209 args.emplace_back( jobInfo.keepJob );
224 std::pair<Job*, bool> result= {
queue.back().job,
queue.back().keepJob };
231 std::vector<std::any> args; args.reserve(32);
232 args.emplace_back(
"Queue({}) Job({}) pushed. P::{} Keep: ");
233 args.emplace_back(
length );
234 args.emplace_back( &result.first->ID );
235 args.emplace_back( dbgPriority );
236 args.emplace_back( result.second );
247 auto jobInfo =
pop();
256 auto size= job.JobToDelete->SizeOf();
257 job.JobToDelete->~Job();
258 manager.GetPoolAllocator().free( job.JobToDelete, size );
271 if (jobInfo.first->Is<
JobStop>() ) {
275 manager.GetPoolAllocator().free( jobInfo.first,
sizeof(
JobStop) );
280 if ( jobInfo.first->Do() ) {
282 if ( !jobInfo.second )
284 auto size= jobInfo.first->SizeOf();
285 jobInfo.first->~Job();
286 manager.GetPoolAllocator().free( jobInfo.first, size );
294 "Job of type <{}> passed to DedicatedWorker, which was neither recognized by\n"
295 "the specialist nor has it a Job::Do() implementation!", &jobInfo.first->ID )
302 "DedicatedWorker \"{}\" has jobs still queued when stopped!",
GetName(),
Load() )
304 ALIB_MESSAGE(
"TMOD",
"DedicatedWorker \"{}\" is stopping (leaving method Run()).",
GetName() )
ListMA< DedicatedWorker * > workers
The list of workers.
MonoAllocator ma
Mono allocator. Used for commands and by DedicatedWorkers.
ALIB_DLL void RemoveAll(Priority stopPriority=Priority::Lowest)
ALIB_DLL DWManager()
Constructor.
ALIB_DLL void Add(DedicatedWorker &thread)
ALIB_DLL bool Remove(DedicatedWorker &thread, Priority stopPriority=Priority::Lowest)
PoolAllocator pool
Pool allocator. Used for command objects.
ALIB_DLL bool WaitForAllIdle(Ticks::Duration timeout, Ticks::Duration dbgWarnAfter)
virtual ALIB_DLL void Run() override
virtual bool process(Job &vjob)
std::pair< Job *, bool > pop()
ALIB_DLL void pushAndRelease(QueueElement &&jobInfo)
DWManager & manager
needed as we inherit TCondition
int length
The current number of jobs in the queue.
bool stopJobExecuted
Flag which is set when the stop-job was executed.
void ScheduleStop(Priority priority)
List< QueueElement > queue
alib::Ticks statLastJobExecution
virtual const character * GetName() const
DbgLockAsserter Dbg
The debug tool instance.
@ Terminated
The thread is terminated.
static void SleepMicros(int64_t microseconds)
virtual ALIB_DLL void Start()
virtual ALIB_DLL void Join()
#define ALIB_MESSAGE(domain,...)
#define ALIB_ASSERT(cond, domain)
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_WARNING(cond, domain,...)
#define ALIB_ERROR(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_CALLER_PRUNED
#define ALIB_LOCK_WITH(lock)
void raise(const CallerInfo &ci, int type, std::string_view domain, const std::span< std::any > &args)
Priority
Possible priorities of jobs assigned to an DedicatedWorker.
time::Ticks Ticks
Type alias in namespace alib.
The job sent by method DeleteJobDeferred.
Job * JobToDelete
The job to be deleted.
The stop job sent by method ScheduleStop.
Container element of the queue.
virtual void PrepareDeferredDeletion()
void WaitForNotification(ALIB_DBG_TAKE_CI)
void Acquire(ALIB_DBG_TAKE_CI)
void ReleaseAndNotify(ALIB_DBG_TAKE_CI)