28 mutable std::mutex mutex;
44 using NowFuncPtr = double (*)();
45 using DelayFuncPtr = void (*)(double);
46 const NowFuncPtr nowFunc;
47 const DelayFuncPtr delayFunc;
58 scheduleReset =
false;
66 sleepPeriod(adaptedPeriod),
94 ret = (totalT / estPIt);
107 av = totalT / estPIt;
109 std = sqrt(((1.0 / (estPIt - 1)) * (sumTSq - estPIt * av * av)));
120 unsigned int ret = count;
132 ret = totalUsed / count;
145 av = totalUsed / count;
147 std = sqrt((1.0 / (count - 1)) * (sumUsedSq - count * av * av));
159 currentRun = nowFunc();
166 double dT = (currentRun - previousRun);
171 if (adaptedPeriod < 0) {
178 previousRun = currentRun;
195 double elapsed = nowFunc() - currentRun;
197 totalUsed += elapsed;
198 sumUsedSq += elapsed * elapsed;
201 sleepPeriod = adaptedPeriod - elapsed;
203 delayFunc(sleepPeriod);
208 while (!isClosing()) {
225 adaptedPeriod = period;
231 return adaptedPeriod;
283 return mPriv->setPeriod(period);
288 return mPriv->getPeriod();
293 return mPriv->isSuspended();
313 return mPriv->start();
318 return mPriv->isRunning();
333 return mPriv->getIterations();
338 return mPriv->getEstimatedPeriod();
343 return mPriv->getEstimatedUsed();
348 mPriv->getEstimatedPeriod(av, std);
353 mPriv->getEstimatedUsed(av, std);
381 return mPriv->setPriority(priority, policy);
386 return mPriv->getPriority();
391 return mPriv->getPolicy();
void threadRelease() override
void getEstimatedUsed(double &av, double &std) const
bool setPeriod(double period)
double getEstimatedPeriod() const
void afterStart(bool s) override
double getEstimatedUsed() const
void beforeStart() override
bool threadInit() override
void getEstimatedPeriod(double &av, double &std) const
unsigned int getIterations() const
Private(PeriodicThread *owner, double p, ShouldUseSystemClock useSystemClock)
An abstraction for a periodic thread.
void resetStat()
Reset thread statistics.
bool setPeriod(double period)
Set the (new) period of the thread.
int getPriority() const
Query the current priority of the thread, if the OS supports that.
virtual void run()=0
Loop function.
virtual void beforeStart()
Called just before a new thread starts.
unsigned int getIterations() const
Return the number of iterations performed since last reset.
bool isRunning() const
Returns true when the thread is started, false otherwise.
PeriodicThread(double period, ShouldUseSystemClock useSystemClock=ShouldUseSystemClock::No)
Constructor.
int getPolicy() const
Query the current scheduling policy of the thread, if the OS supports that.
bool isSuspended() const
Returns true when the thread is suspended, false otherwise.
virtual bool threadInit()
Initialization method.
void resume()
Resume the thread if previously suspended.
virtual void afterStart(bool success)
Called just after a new thread starts (or fails to start), this is executed by the same thread that c...
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
int setPriority(int priority, int policy=-1)
Set the priority and scheduling policy of the thread, if the OS supports that.
void askToStop()
Stop the thread.
double getEstimatedUsed() const
Return the estimated duration of the run() function since last reset.
double getEstimatedPeriod() const
Return estimated period since last reset.
bool start()
Call this to start the thread.
void step()
Call this to "step" the thread rather than starting it.
virtual ~PeriodicThread()
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...
virtual void threadRelease()
Release method.
An abstraction for a thread of execution.
yarp::rosmsg::std_msgs::Time Time
void useSystemClock()
Configure YARP to use system time (this is the default).
void yield()
The calling thread releases its remaining quantum upon calling this function.
double now()
Return the current time in seconds, relative to an arbitrary starting point.
void delay(double seconds)
Wait for a certain number of seconds.
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.