|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
34 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
46 #endif // YARP_NO_DEPRECATED
50 std::mutex* mutex =
nullptr) :
73 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
78 #endif // YARP_NO_DEPRECATED
85 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
92 #endif // YARP_NO_DEPRECATED
96 std::mutex* mutex =
nullptr);
100 unsigned int m_runTimes{1};
101 size_t m_id{(size_t)-1};
105 return PrivateImpl::getEventNow(m_runTimes);
122 return step(getEventNow(),
true);
127 bool m_active = runTimer(m_runTimes, event);
140 double gcd(
double a,
double b)
147 if (fabs(b) < 0.001) {
150 return (
gcd(b, a - floor(a / b) * b));
157 std::map<size_t, MonoThreadTimer*> timers;
181 setPeriod(
gcd(getPeriod(),
t->m_settings.period));
183 setPeriod(
t->m_settings.period);
186 timers[timers.size()] =
t;
189 return timers.size() - 1;
197 if (!timers.size()) {
201 double new_gcd = timers.begin()->second->m_settings.period;
202 for (
auto& i : timers) {
203 new_gcd =
gcd(new_gcd, i.second->m_settings.period);
210 return timers.size();
214 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
220 PrivateImpl(sett, call, mutex)
225 singlInstance.
start();
229 #endif // YARP_NO_DEPRECATED
234 PrivateImpl(sett, call, mutex)
239 singlInstance.
start();
248 singlInstance.
stop();
252 void TimerSingleton::run()
255 for (
auto t : timers) {
272 bool threadInit()
override;
273 bool singleStep{
false};
276 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
287 #endif // YARP_NO_DEPRECATED
291 std::mutex* mutex =
nullptr) :
327 bool ThreadedTimer::threadInit()
333 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
344 #endif // YARP_NO_DEPRECATED
357 return impl->startTimer();
362 return impl->stepTimer();
375 event.currentExpected = m_startStamp + iteration * m_settings.period;
376 event.lastExpected =
event.currentExpected - m_settings.period;
377 event.lastReal = m_lastReal;
378 event.lastDuration =
event.currentReal - m_lastReal;
379 event.runCount = iteration;
385 if (m_mutex !=
nullptr) {
389 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
390 if (m_old_mutex !=
nullptr) {
393 #endif // YARP_NO_DEPRECATED
395 bool ret = m_callback(event);
397 #ifndef YARP_NO_DEPRECATED // since YARP 3.3
398 if (m_old_mutex !=
nullptr) {
399 m_old_mutex->unlock();
401 #endif // YARP_NO_DEPRECATED
403 if (m_mutex !=
nullptr) {
411 m_lastReal =
event.currentReal;
416 bool stop(m_settings.totalRunCount != 0 && m_settings.totalRunCount <= iteration);
419 stop |= m_settings.totalTime > 0.00001 && (m_settings.totalTime - timerAge) < m_settings.tolerance;
424 void ThreadedTimer::run()
426 if (getIterations() == 0 && !singleStep) {
431 if (!runTimer(this->getIterations(), event)) {
438 impl->m_settings = settings;
443 return impl->m_settings;
448 return impl->timerIsRunning();
virtual bool stepTimer()=0
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
MonoThreadTimer(const TimerSettings &sett, const TimerCallback &call, yarp::os::Mutex *mutex)
bool startTimer() override
Timer(const Timer &)=delete
virtual ~PrivateImpl()=default
PrivateImpl(const TimerSettings &sett, TimerCallback call, yarp::os::Mutex *mutex)
bool stepTimer() override
void setSettings(const yarp::os::TimerSettings &settings)
setSettings
virtual void stopTimer()=0
bool startTimer() override
virtual bool startTimer()=0
void stopTimer() override
bool isRunning() const
Returns true when the thread is started, false otherwise.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
size_t addTimer(MonoThreadTimer *t)
bool stepTimer() override
virtual yarp::os::YarpTimerEvent getEventNow()
const yarp::os::TimerSettings getSettings()
getSettings
~ThreadedTimer() override
yarp::os::Timer::TimerCallback TimerCallback
ThreadedTimer(const TimerSettings &sett, const TimerCallback &call, yarp::os::Mutex *mutex)
bool start()
Call this to start the thread.
bool setPeriod(double period)
Set the (new) period of the thread.
std::function< bool(const yarp::os::YarpTimerEvent &)> TimerCallback
bool timerIsRunning() override
double currentReal
currentReal When the current callback is actually being called
void removeTimer(size_t id)
An abstraction for a periodic thread.
PrivateImpl(const TimerSettings &sett, TimerCallback call, std::mutex *mutex=nullptr)
yarp::os::Mutex * m_old_mutex
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
void stopTimer() override
double currentExpected
currentExpected this is when the current callback should have been called
double gcd(double a, double b)
void askToStop()
Stop the thread.
static TimerSingleton & self()
An interface to the operating system, including Port based communication.
bool runTimer(unsigned int iteration, YarpTimerEvent event)
virtual bool timerIsRunning()=0
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Basic wrapper for mutual exclusion.
void step()
Call this to "step" the thread rather than starting it.
yarp::os::YarpTimerEvent getEventNow(unsigned int iteration)
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
ThreadedTimer(const TimerSettings &sett, const TimerCallback &call, std::mutex *mutex=nullptr)
~MonoThreadTimer() override
bool timerIsRunning() override
virtual bool step(YarpTimerEvent event, bool singleStep)