48 #ifndef _MIRA_DISPATCHERTHREAD_H_ 49 #define _MIRA_DISPATCHERTHREAD_H_ 55 #include <boost/function.hpp> 56 #include <boost/thread/condition_variable.hpp> 69 template<
typename T,
typename Sequence = std::vector<T>,
70 typename Compare = std::less<
typename Sequence::value_type> >
73 typedef std::priority_queue<T,Sequence,Compare> Base;
85 Base(x,
std::move(s)) {}
88 template<
typename InputIterator>
90 Base(first,last,x,s) {}
92 template<
typename InputIterator>
94 Base(first,last,x,
std::move(s)) {}
281 void setName(
const std::string& name);
303 template <
typename F>
319 template <
typename F>
377 void start(
bool startThread =
true);
459 bool processImmediateHandlers();
460 bool processFinalizeHandlers();
461 bool processSpin(
const Duration& maxWait);
465 bool isInterruptionRequested();
bool toBeRemoved
Definition: DispatcherThread.h:478
boost::mutex mConditionMutex
Definition: DispatcherThread.h:489
void start(bool startThread=true)
Starts the dispatcher.
std::set< TimerPtr > mTimers
Definition: DispatcherThread.h:505
static Duration invalid()
Returns an invalid duration.
Definition: Time.h:249
Time currentExpected
time the current callback should be happening
Definition: DispatcherThread.h:252
Sequence::iterator iterator
Definition: DispatcherThread.h:77
bool mInterruptionRequested
Definition: DispatcherThread.h:515
void run(DispatcherThread *dispatcher) override
Called from dispatcher thread this runnable is attached to, whenever the runnable should be executed...
boost::condition_variable mCondition
Definition: DispatcherThread.h:490
DispatcherThread::Timer Timer
Definition: DispatcherThread.h:523
DispatcherThread::TimerPtr TimerPtr
Definition: DispatcherThread.h:524
bool mUnrecoverableFailure
Definition: DispatcherThread.h:508
TimerPtr createTimer(Duration period, Duration periodTolerance, TimerCallback callback, bool oneshot=false)
Creates and adds a timer that gets called cyclic in a given period and has a tolerance for exceeding ...
Class representing timers and tasks that can be registered and executed by the dispatcher thread...
Definition: DispatcherThread.h:146
DispatcherThread(const std::string &name="")
Construct a dispatcher thread with an optional name.
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
void setName(const std::string &name)
boost::function< void(const Timer &)> TimerCallback
Signature of a timer/task callback function.
Definition: DispatcherThread.h:135
boost::mutex mFinalizeHandlerMutex
Definition: DispatcherThread.h:503
bool mIsRunning
Definition: DispatcherThread.h:509
IteratablePriorityQueue(const Compare &x, const Sequence &s)
Definition: DispatcherThread.h:81
bool spin(Duration maxWait=Duration::infinity())
Invoke the dispatcher manually (instead of starting a separate thread via the start() method)...
virtual ~DispatcherThread()
Time and Duration wrapper class.
void addImmediateHandlerFunction(F &&fn, DiagnosticsModulePtr errorModule=nullptr)
Adds a function that is executed once as soon as possible within the DispatcherThreads main thread...
Definition: DispatcherThread.h:304
Runnable that wraps any function pointer.
Definition: Runnable.h:115
QueueItem(IRunnablePtr r, Time t)
Definition: DispatcherThread.h:473
bool operator<(const QueueItem &other) const
Definition: DispatcherThread.h:480
void postProcessTimer(TimerPtr timer)
IteratablePriorityQueue(InputIterator first, InputIterator last, const Compare &x, Sequence &&s)
Definition: DispatcherThread.h:93
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
IteratablePriorityQueue(InputIterator first, InputIterator last, const Compare &x, const Sequence &s)
Definition: DispatcherThread.h:89
IteratablePriorityQueue< QueueItem, std::deque< QueueItem > > Queue
Definition: DispatcherThread.h:483
boost::mutex mTimerMutex
Definition: DispatcherThread.h:506
void setTolerance(Duration tolerance)
Set the tolerance that is allowed to exceed the next invocation time before issuing a warning and res...
Definition: DispatcherThread.h:201
boost::shared_ptr< Timer > TimerPtr
Definition: DispatcherThread.h:269
std::list< IRunnablePtr > mImmediateHandlers
Definition: DispatcherThread.h:499
void run()
Runs this thread dispatchers main loop.
Base for all runnable classes that can be signaled when they need to run again.
Definition: Runnable.h:72
Auxiliary logging macros for special entities like exceptions, etc.
Includes, defines and functions for threads.
Definition: DispatcherThread.h:71
Duration getExceedance() const
Checks and returns the amount of time the timer has exceeded the expected invocation time...
Definition: DispatcherThread.h:215
DispatcherThread::TimerCallback TimerCallback
Definition: DispatcherThread.h:525
Time getNextInvocationTime() const
Return next time of execution.
Definition: DispatcherThread.h:242
void setPeriod(Duration period)
Changes the period of the timer.
Definition: DispatcherThread.h:189
void start()
Start the timer (activates it)
sec_type seconds() const
Returns normalized number of seconds (0..59)
Definition: Time.h:280
Time last
time the last callback happened
Definition: DispatcherThread.h:249
Use this class to represent time durations.
Definition: Time.h:106
bool insertRunnable(IRunnablePtr runnable, bool singleton=false, Time time=Time::now())
void stop()
Stops the dispatcher, if it is running.
iterator end()
Definition: DispatcherThread.h:99
bool isRunning() const
Returns true, if the dispatcher currently is running.
static Time now()
Returns the current utc based time.
Definition: Time.h:481
boost::thread::id getThreadID() const
Returns the thread id of the thread that is used to process the dispatcher.
Definition: DispatcherThread.h:425
boost::shared_ptr< IRunnable > IRunnablePtr
a runnable pointer
Definition: Runnable.h:63
void addImmediateHandler(IRunnablePtr runnable)
Adds a runnable that is executed once as soon as possible within the DispatcherThreads main thread...
void stop()
Stops a timer (deactivates it)
XXX.
Definition: DispatcherThread.h:470
Class that can be used whenever you want to have ONE thread where several handlers are assigned to...
Definition: DispatcherThread.h:130
boost::mutex mImmediateHandlerMutex
Definition: DispatcherThread.h:500
void updateInvocationTime()
Called by dispatcher when timer is rescheduled.
Definition: DispatcherThread.h:172
static Duration infinity()
Returns a special duration time representing positive infinity.
Definition: Time.h:242
Duration getTolerance() const
Get the tolerance for exceed the next invocation time.
Definition: DispatcherThread.h:207
bool isActive() const
Returns if the timer is active.
Definition: DispatcherThread.h:183
IteratablePriorityQueue(const Compare &x=Compare(), Sequence &&s=Sequence())
Definition: DispatcherThread.h:84
Duration getPeriod() const
Returns the current period of the timer.
Definition: DispatcherThread.h:194
void removeTimer(TimerPtr timer)
Removes the given timer (timer callback will not be called again and timer will not be rescheduled)...
QueueItem()
Definition: DispatcherThread.h:472
Sequence::const_iterator const_iterator
Definition: DispatcherThread.h:78
Time time
Definition: DispatcherThread.h:477
bool hasTimer(TimerPtr timer)
Returns true, if the given timer.
boost::thread mThread
Definition: DispatcherThread.h:517
std::set< IRunnablePtr > mPendingRunnables
Definition: DispatcherThread.h:487
Queue mQueue
Definition: DispatcherThread.h:486
bool mPendingSignal
Definition: DispatcherThread.h:519
IRunnablePtr runnable
Definition: DispatcherThread.h:476
std::string mName
Definition: DispatcherThread.h:497
Exception that can be thrown in every handler to indicate an unrecoverable failure.
Definition: DispatcherThread.h:397
bool hasWork(Time horizon=Time::now()) const
Checks if there is a work item (either an immediate handler or an item in the execution queue with an...
const_iterator end() const
Definition: DispatcherThread.h:102
bool hasUnrecoverableFailure() const
Returns true, if there was a failure while processing the immediate handlers.
const_iterator begin() const
Definition: DispatcherThread.h:101
Include file for including all string algorithm headers.
boost::thread::id mThreadId
Definition: DispatcherThread.h:518
iterator begin()
Definition: DispatcherThread.h:98
void addFinalizeHandler(IRunnablePtr runnable)
Adds a runnable that is executed once just before the thread terminates.
void removeRunnable(IRunnablePtr runnable)
Base class for modules that want to use diagnostics and set the current status.
Definition: Status.h:136
void addFinalizeHandlerFunction(F &&fn, DiagnosticsModulePtr errorModule=nullptr)
Adds a function that is executed once just before the thread terminates.
Definition: DispatcherThread.h:320
std::list< IRunnablePtr > mFinalizeHandlers
Definition: DispatcherThread.h:502
Status and status management classes used for diagnostics.
Timer(TimerCallback callback, Duration period, Duration tolerance=Duration::invalid())
Constructs a timer with a given callback that is called whenever the timers invocation time is due an...
Duration lastDuration
How long the last callback ran for.
Definition: DispatcherThread.h:258
Time current
time the current callback was actually called (Time::now() as of the beginning of the callback) ...
Definition: DispatcherThread.h:255