24 #include "dabc/defines.h"
39 #ifndef DABC_CommandsQueue
47 #ifndef DABC_Exception
79 inline EventId(uint16_t code, uint16_t item) :
value((uint64_t) item << 48 | code) {}
81 inline EventId(uint16_t code, uint16_t item, uint32_t arg) :
82 value((((uint64_t) item) << 48) | (((uint64_t) arg) << 16) | code) {}
94 inline uint32_t
GetArg()
const {
return (
value >> 16) & 0xffffffffLU; }
207 if ((min_tmout<0.) || (dist<min_tmout)) min_tmout = dist;
221 if ((min_tmout<0.) || (dist<min_tmout)) min_tmout = dist;
347 bool Start(
double timeout_sec = -1.,
bool real_thread =
true);
348 bool Stop(
double timeout_sec = 10);
349 bool Sync(
double timeout_sec = -1);
362 bool SingleLoop(
unsigned workerid = 0,
double tmout = -1);
370 virtual void Print(
int lvl = 0);
398 #ifdef DABC_EXTRA_CHECKS
399 static unsigned maxlimit;
406 #ifdef DABC_EXTRA_CHECKS
415 #ifdef DABC_EXTRA_CHECKS
417 if (
fQueues[nq].Size()>maxlimit) {
418 EOUT(
"Thrd:%s Queue %d Event code:%u item:%u arg:%u exceed limit: %u",
GetName(), nq,
#define DABC_REFERENCE(RefClass, ParentClass, T)
Represents command with its arguments.
Lock guard for posix mutex.
Base class for most of the DABC classes.
Object * GetParent() const
Returns pointer on parent object, thread safe
const char * GetName() const
Returns name of the object, thread safe
Mutex * ObjectMutex() const
Returns mutex, used for protection of Object data members.
class represents posix pthread functionality
bool IsItself() const
Returns true if called from thread context.
void SetPriority(int prio)
Change thread priority.
Template of circular queue.
Reference on the arbitrary object
Object * GetObject() const
Return pointer on the object.
bool AcquireRefWithoutMutex(Reference &ref)
Special method, which allows to generate new reference when object mutex is locked.
Object which could be run inside the dabc::PosixThread
Reference on the dabc::Thread class
bool Execute(Command cmd, double tmout=-1.)
bool MakeWorkerFor(WorkerAddon *addon, const std::string &name="")
Make dummy worker to run addon inside the thread.
bool _ActivateWorkerTimeout(unsigned workerid, int priority, double tmout)
bool _ActivateAddonTimeout(unsigned workerid, int priority, double tmout)
void RunEventLoop(double tmout=1.)
Runs thread event loop for specified time.
bool _AcquireThreadRef(ThreadRef &ref)
helper class to use methods, available in dabc::Worker in thread itself
RecursionGuard(Thread *t, unsigned id)
unsigned workerid
worker id which recursion is guarded
Thread * thrd
we can use direct pointer, reference will be preserved by other means
Represent thread functionality.
bool _GetNextEvent(EventId &)
bool fThrdWorking
flag indicates if mainloop of the thread should continue to work
void IncWorkerFiredEvents(Worker *work)
virtual bool CompatibleClass(const std::string &clname) const
bool IsTemporaryThread() const
Returns true is this is temporary thread for command execution.
void SetStopTimeout(double timeout_sec)
virtual void Print(int lvl=0)
Print thread content on debug output.
bool Execute(Command cmd, double tmout=-1)
WorkersVector fWorkers
vector of all processors
EThreadState fState
actual thread state
void WorkerAddonChanged(Worker *work, bool assign=true)
Called when worker addon changed on the fly.
void _PushEvent(const EventId &arg, int nq)
void FireDoNothingEvent()
double fThreadRunTime
total run time (user and sys), measured by getrusage
virtual bool _DoDeleteItself()
This method is called at the moment when DecReference think that object can be destroyed and wants to...
Condition fWorkCond
condition, which is used in default MainLoop implementation
int CheckWorkerCanBeHalted(unsigned id, unsigned request=0, Command cmd=nullptr)
Internal DABC method, used to verify if worker can be halted now while recursion is over Request indi...
bool HaltWorker(Worker *proc)
Halt worker - stops any execution, break recursion.
int fNumQueues
number of queues
bool Stop(double timeout_sec=10)
unsigned fExplicitLoop
id of the worker, selected to run own explicit loop
void RunEventLoop(double tmout=1.)
Runs thread event loop for specified time.
double GetStopTimeout() const
double fThrdStopTimeout
time in second set as timeout when stopping thred
int fProcessingTimeouts
indicate recursion in timeouts processing
virtual void RunnableCancelled()
Mutex * ThreadMutex() const
bool fCheckThrdCleanup
indicates if thread should be checked for clean up
bool InvokeWorkerDestroy(Worker *work)
Cleanup object asynchronously.
virtual bool WaitEvent(EventId &, double tmout)
bool SetExplicitLoop(Worker *work)
EventsQueue * fQueues
queues for threads events
TimeStamp fNextTimeout
indicate when we expects next timeout
std::vector< WorkerRec * > WorkersVector
virtual int ExecuteThreadCommand(Command cmd)
unsigned TotalNumberOfEvents()
Return total number of all events in the queues.
bool SingleLoop(unsigned workerid=0, double tmout=-1)
Processes single event from the thread queue.
bool fDidDecRefCnt
indicates if object cleanup was called - need in destructor
int RunCommandInTheThread(Worker *caller, Worker *dest, Command cmd)
void Fire(const EventId &arg, int nq)
virtual void * MainLoop()
static unsigned NumThreadInstances()
unsigned NumWorkers()
Returns actual number of workers.
unsigned _TotalNumberOfEvents()
bool Start(double timeout_sec=-1., bool real_thread=true)
virtual void ObjectCleanup()
Cleanup thread that manager is allowed to delete it.
void SetPriority(int prio=0)
virtual void _Fire(const EventId &arg, int nq)
virtual void ProcessExtraThreadEvent(const EventId &)
Method to process events which are not processed by Thread class itself Should be used in derived cla...
static unsigned fThreadInstances
ExecWorker * fExec
processor to execute commands in the thread
TimeStamp fLastProfileTime
when doing profiling, last time when profiling was done
bool AddWorker(Reference ref, bool sync=true)
Internal DABC method, Add worker to thread; reference-safe Reference safe means - it is safe to call ...
virtual const char * ClassName() const
Returns class name of the object instance.
@ evntDoNothing
event fired to wake-up thread and let thread or processor to perform regular checks
@ evntStopThrd
event should stop thread
@ evntCheckTmoutWorker
event used to process timeout for specific worker, used by ActivateTimeout
@ evntCleanupThrd
event will be generated when thread can be destroyed
@ evntCheckTmoutAddon
event used to process timeout for addon, used by ActivateTimeout
bool fProfiling
if true, different statistic will be accumulated about thread
virtual void WorkersSetChanged()
Virtual method, called from thread context to inform that number of workers are changed.
double CheckTimeouts(bool forcerecheck=false)
bool fRealThrd
indicate if we create real thread and not running mainloop from top process
bool Sync(double timeout_sec=-1)
void ChangeRecursion(unsigned id, bool inc)
Method which allows to control recursion of each worker.
void ProcessEvent(const EventId &)
Generic addon for dabc::Worker.
Active object, which is working inside dabc::Thread.
Event structure, exchanged between DABC threads.
EventId(const EventId &src)
EventId & operator=(uint64_t _value)
EventId(uint16_t code, uint16_t item, uint32_t arg)
std::string asstring() const
EventId(uint16_t code, uint16_t item)
EventId & operator=(const EventId &src)
TimeoutRec(const TimeoutRec &src)
void SetNextFire(const TimeStamp &now, double dist, double &min_tmout)
TimeStamp tmout_mark
time mark when timeout should happen
bool CheckEvent(Mutex *thread_mutex)
Method called to check event, submitted when timeout was requested Returns true when check should be ...
bool Activate(double tmout)
Activating timeout.
bool tmout_active
true when timeout active
TimeStamp next_fire
when next timeout event will be called
bool CheckNextProcess(const TimeStamp &now, double &min_tmout, double &last_diff)
TimeStamp prev_fire
when previous timeout event was called
double tmout_interv
time interval was specified by timeout active
WorkerAddon * addon
addon for the worker, maybe thread-specific
Worker * work
pointer on the worker, should we use reference?
int recursion
recursion calls of the worker
TimeoutRec tmout_worker
timeout handling for worker
unsigned processed
current number of processed events, when balance between processed and fired is 0,...
WorkerRec(Worker *w, WorkerAddon *a)
TimeoutRec tmout_addon
timeout handling for addon
CommandsQueue cmds
postponed commands, which are waiting until object is destroyed or halted
unsigned doinghalt
indicates that events will not be longer accepted by the worker, all submitted commands still should ...
Class for acquiring and holding timestamps.
bool null() const
Returns true if time stamp is not initialized or its value less than 0.
void Reset()
Set time stamp value to null.