33 std::mutex* mutex =
nullptr) :
64 std::mutex* mutex =
nullptr);
108double gcd(
double a,
double b)
115 if (
fabs(b) < 0.001) {
118 return (
gcd(b, a -
floor(a / b) * b));
125 std::map<size_t, MonoThreadTimer*> timers;
154 timers[timers.size()] = t;
157 return timers.size() - 1;
165 if (!timers.size()) {
169 double new_gcd = timers.begin()->second->m_settings.period;
170 for (
auto&
i : timers) {
178 return timers.size();
185 PrivateImpl(
sett, call, mutex)
203void TimerSingleton::run()
206 for (
auto t : timers) {
223 bool threadInit()
override;
224 bool singleStep{
false};
228 const TimerCallback& call,
229 std::mutex* mutex =
nullptr) :
265bool ThreadedTimer::threadInit()
282 return impl->startTimer();
287 return impl->stepTimer();
300 event.currentExpected = m_startStamp +
iteration * m_settings.period;
301 event.lastExpected =
event.currentExpected - m_settings.period;
302 event.lastReal = m_lastReal;
303 event.lastDuration =
event.currentReal - m_lastReal;
310 if (m_mutex !=
nullptr) {
314 bool ret = m_callback(event);
316 if (m_mutex !=
nullptr) {
324 m_lastReal =
event.currentReal;
329 bool stop(m_settings.totalRunCount != 0 && m_settings.totalRunCount <=
iteration);
332 stop |= m_settings.totalTime > 0.00001 && (m_settings.totalTime -
timerAge) < m_settings.tolerance;
337void ThreadedTimer::run()
339 if (getIterations() == 0 && !singleStep) {
344 if (!runTimer(this->getIterations(), event)) {
351 impl->m_settings = settings;
356 return impl->m_settings;
361 return impl->timerIsRunning();
double gcd(double a, double b)
void stopTimer() override
~MonoThreadTimer() override
virtual yarp::os::YarpTimerEvent getEventNow()
bool stepTimer() override
MonoThreadTimer(const TimerSettings &sett, const TimerCallback &call, std::mutex *mutex=nullptr)
virtual bool step(YarpTimerEvent event, bool singleStep)
bool startTimer() override
bool timerIsRunning() override
void stopTimer() override
ThreadedTimer(const TimerSettings &sett, const TimerCallback &call, std::mutex *mutex=nullptr)
bool stepTimer() override
~ThreadedTimer() override
bool startTimer() override
bool timerIsRunning() override
static TimerSingleton & self()
void removeTimer(size_t id)
size_t addTimer(MonoThreadTimer *t)
A mini-server for performing network communication in the background.
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
void askToStop()
Stop the thread.
bool start()
Call this to start the thread.
void step()
Call this to "step" the thread rather than starting it.
double getPeriod() const
Return the current period of the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
yarp::os::YarpTimerEvent getEventNow(unsigned int iteration)
virtual bool startTimer()=0
bool runTimer(unsigned int iteration, YarpTimerEvent event)
PrivateImpl(const TimerSettings &sett, TimerCallback call, std::mutex *mutex=nullptr)
virtual ~PrivateImpl()=default
virtual void stopTimer()=0
virtual bool timerIsRunning()=0
virtual bool stepTimer()=0
yarp::os::Timer::TimerCallback TimerCallback
void setSettings(const yarp::os::TimerSettings &settings)
setSettings
const yarp::os::TimerSettings getSettings()
getSettings
Timer(const Timer &)=delete
std::function< bool(const yarp::os::YarpTimerEvent &)> TimerCallback
double now()
Return the current time in seconds, relative to an arbitrary starting point.
An interface to the operating system, including Port based communication.
double currentReal
currentReal When the current callback is actually being called