YARP
Yet Another Robot Platform
RateThread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #define YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
11 #include <yarp/os/RateThread.h>
12 #undef YARP_INCLUDING_DEPRECATED_HEADER_ON_PURPOSE
13 
16 
18 
19 using namespace yarp::os;
20 
21 namespace {
22 YARP_OS_LOG_COMPONENT(RATETHREAD, "yarp.os.RateThread" )
23 }
24 
25 
27  PeriodicThread(period / 1000.0)
28 {
29 }
30 
31 RateThread::~RateThread() = default;
32 
33 bool RateThread::setRate(int period)
34 {
35  return PeriodicThread::setPeriod(period / 1000.0);
36 }
37 
39 {
40  return PeriodicThread::getPeriod() * 1000;
41 }
42 
44 {
46 }
47 
49 {
51 }
52 
54 {
56 }
57 
59 {
61  return true;
62 }
63 
65 {
66  return PeriodicThread::start();
67 }
68 
70 {
72 }
73 
75 {
77 }
78 
80 {
82 }
83 
85 {
87 }
88 
90 {
91  return PeriodicThread::getEstimatedPeriod() * 1000;
92 }
93 
95 {
96  return PeriodicThread::getEstimatedUsed() * 1000;
97 }
98 
99 void RateThread::getEstPeriod(double& av, double& std)
100 {
102  av *= 1000;
103  std *= 1000;
104 }
105 
106 void RateThread::getEstUsed(double& av, double& std)
107 {
109  av *= 1000;
110  std *= 1000;
111 }
112 
114 {
116 }
117 
119 {
120  return true;
121 }
122 
124 {
125 }
126 
128 {
129 }
130 
131 void RateThread::afterStart(bool success)
132 {
133  YARP_UNUSED(success);
134 }
135 
136 int RateThread::setPriority(int priority, int policy)
137 {
138  return PeriodicThread::setPriority(priority, policy);
139 }
140 
142 {
144 }
145 
147 {
148  return PeriodicThread::getPolicy();
149 }
150 
151 //
152 // System Rate Thread
153 //
154 
156  PeriodicThread(period / 1000.0, ShouldUseSystemClock::Yes)
157 {
158 }
159 
161 
163 {
164  step();
165  return true;
166 }
167 
169  PeriodicThread(0)
170 {
171  helper = nullptr;
172  owned = false;
173 }
174 
175 
177  PeriodicThread(0)
178 {
179  this->helper = helper;
180  owned = true;
181 }
182 
184  PeriodicThread(0)
185 {
186  this->helper = &helper;
187  owned = false;
188 }
189 
191 {
192  detach();
193 }
194 
196 {
197  if (owned) {
198  delete helper;
199  }
200  helper = nullptr;
201  owned = false;
202 }
203 
205 {
206  detach();
207  this->helper = &helper;
208  owned = false;
209  return true;
210 }
211 
213 {
214  detach();
215  this->helper = helper;
216  owned = true;
217  return true;
218 }
219 
220 bool RateThreadWrapper::open(double framerate, bool polling)
221 {
222  double period = 0.0;
223  if (framerate > 0) {
224  period = (1.0 / framerate);
225  yCInfo(RATETHREAD, "Setting framerate to: %.0lf[Hz] (thread period %f[s])\n", framerate, period);
226  } else {
227  yCInfo(RATETHREAD, "No framerate specified, polling the device");
228  period = 0.0; //continuous
229  }
231  if (!polling) {
232  start();
233  }
234  return true;
235 }
236 
238 {
240 }
241 
243 {
245 }
246 
248 {
249  if (helper != nullptr) {
250  helper->run();
251  }
252 }
253 
255 {
256  if (helper != nullptr) {
257  return helper->threadInit();
258  }
259  return true;
260 }
261 
263 {
264  if (helper != nullptr) {
265  helper->threadRelease();
266  }
267 }
268 
270 {
271  if (helper != nullptr) {
272  helper->afterStart(success);
273  }
274 }
275 
277 {
278  if (helper != nullptr) {
279  helper->beforeStart();
280  }
281 }
282 
284 {
285  return helper;
286 }
287 
yarp::os::RateThread::~RateThread
virtual ~RateThread()
YARP_WARNING_PUSH
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
yarp::os::RateThread::stop
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: RateThread.cpp:48
yarp::os::SystemRateThread::SystemRateThread
SystemRateThread(int period)
Definition: RateThread.cpp:155
yarp::os::ShouldUseSystemClock::No
@ No
yarp::os::RateThreadWrapper::RateThreadWrapper
RateThreadWrapper()
Default constructor.
Definition: RateThread.cpp:168
yarp::os::PeriodicThread::resume
void resume()
Resume the thread if previously suspended.
Definition: PeriodicThread.cpp:326
yarp::os::RateThread::getIterations
unsigned int getIterations()
Return the number of iterations performed since last reset.
Definition: RateThread.cpp:84
yarp::os::Runnable::afterStart
virtual void afterStart(bool success)
Should be called from the creator after the thread exists and before a call that requested the thread...
Definition: Runnable.cpp:31
yarp::os::PeriodicThread::getPolicy
int getPolicy() const
Query the current scheduling policy of the thread, if the OS supports that.
Definition: PeriodicThread.cpp:389
yarp::os::RateThread::step
bool step()
Call this to "step" the thread rather than starting it.
Definition: RateThread.cpp:58
yarp::os::RateThreadWrapper::open
bool open(double framerate=-1, bool polling=false)
Definition: RateThread.cpp:220
yarp::os::RateThreadWrapper::threadInit
bool threadInit() override
Initialization method.
Definition: RateThread.cpp:254
yarp::os::RateThreadWrapper::getAttachment
Runnable * getAttachment() const
Definition: RateThread.cpp:283
yarp::os::RateThread::setPriority
int setPriority(int priority, int policy=-1)
Set the priority and scheduling policy of the thread, if the OS supports that.
Definition: RateThread.cpp:136
yarp::os::RateThreadWrapper::threadRelease
void threadRelease() override
Release method.
Definition: RateThread.cpp:262
yarp::os::PeriodicThread::getPriority
int getPriority() const
Query the current priority of the thread, if the OS supports that.
Definition: PeriodicThread.cpp:384
yarp::os::RateThread::getEstPeriod
double getEstPeriod()
Return estimated period since last reset.
Definition: RateThread.cpp:89
yarp::os::Runnable::threadInit
virtual bool threadInit()
Initialization method.
Definition: Runnable.cpp:36
yarp::os::Runnable::threadRelease
virtual void threadRelease()
Release method.
Definition: Runnable.cpp:41
yarp::os::RateThreadWrapper::attach
virtual bool attach(Runnable &helper)
Definition: RateThread.cpp:204
yarp::os::RateThreadWrapper::afterStart
void afterStart(bool success) override
Called just after a new thread starts (or fails to start), this is executed by the same thread that c...
Definition: RateThread.cpp:269
yarp::os::PeriodicThread::getEstimatedUsed
double getEstimatedUsed() const
Return the estimated duration of the run() function since last reset.
Definition: PeriodicThread.cpp:341
yarp::os::RateThread::threadInit
bool threadInit() override
Initialization method.
Definition: RateThread.cpp:118
yarp::os::PeriodicThread::getPeriod
double getPeriod() const
Return the current period of the thread.
Definition: PeriodicThread.cpp:286
YARP_UNUSED
#define YARP_UNUSED(var)
Definition: api.h:159
yarp::os::Runnable
A class that can be managed by another thread.
Definition: Runnable.h:32
LogComponent.h
yarp::os::PeriodicThread::isRunning
bool isRunning() const
Returns true when the thread is started, false otherwise.
Definition: PeriodicThread.cpp:316
RateThread.h
yarp::os::PeriodicThread::getEstimatedPeriod
double getEstimatedPeriod() const
Return estimated period since last reset.
Definition: PeriodicThread.cpp:336
yarp::os::RateThread::getPolicy
int getPolicy()
Query the current scheduling policy of the thread, if the OS supports that.
Definition: RateThread.cpp:146
yarp::os::RateThread::getPriority
int getPriority()
Query the current priority of the thread, if the OS supports that.
Definition: RateThread.cpp:141
yarp::os::RateThread::RateThread
RateThread(int period)
Constructor.
Definition: RateThread.cpp:26
yarp::os::RateThread::getEstUsed
double getEstUsed()
Return the estimated duration of the run() function since last reset.
Definition: RateThread.cpp:94
yarp::os::RateThreadWrapper::~RateThreadWrapper
virtual ~RateThreadWrapper()
Definition: RateThread.cpp:190
yarp::os::PeriodicThread::resetStat
void resetStat()
Reset thread statistics.
Definition: PeriodicThread.cpp:356
yarp::os::RateThreadWrapper::beforeStart
void beforeStart() override
Called just before a new thread starts.
Definition: RateThread.cpp:276
yarp::os::PeriodicThread::start
bool start()
Call this to start the thread.
Definition: PeriodicThread.cpp:311
yarp::os::RateThreadWrapper::stop
void stop()
Definition: RateThread.cpp:242
yarp::os::RateThread::isSuspended
bool isSuspended()
Returns true when the thread is suspended, false otherwise.
Definition: RateThread.cpp:43
yarp::os::PeriodicThread::setPeriod
bool setPeriod(double period)
Set the (new) period of the thread.
Definition: PeriodicThread.cpp:281
yarp::os::SystemRateThread::stepSystem
bool stepSystem()
Definition: RateThread.cpp:162
yarp::os::PeriodicThread::suspend
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
Definition: PeriodicThread.cpp:321
yarp::os::PeriodicThread
An abstraction for a periodic thread.
Definition: PeriodicThread.h:25
yarp::os::RateThread::afterStart
void afterStart(bool success) override
Called just after a new thread starts (or fails to start), this is executed by the same thread that c...
Definition: RateThread.cpp:131
yarp::os::Runnable::beforeStart
virtual void beforeStart()
Should be called from the creator before the thread exists and before a call that requested the threa...
Definition: Runnable.cpp:27
yarp::os::RateThreadWrapper::detach
void detach()
Definition: RateThread.cpp:195
YARP_WARNING_POP
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
yarp::os::PeriodicThread::setPriority
int setPriority(int priority, int policy=-1)
Set the priority and scheduling policy of the thread, if the OS supports that.
Definition: PeriodicThread.cpp:379
yarp::os::RateThreadWrapper::close
void close()
Definition: RateThread.cpp:237
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os::RateThreadWrapper::run
void run() override
Loop function.
Definition: RateThread.cpp:247
yarp::os::PeriodicThread::askToStop
void askToStop()
Stop the thread.
Definition: PeriodicThread.cpp:301
yarp::os::RateThread::start
bool start()
Call this to start the thread.
Definition: RateThread.cpp:64
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::RateThread::askToStop
void askToStop()
Stop the thread.
Definition: RateThread.cpp:53
yarp::os::ShouldUseSystemClock
ShouldUseSystemClock
Definition: Time.h:23
yarp::os::RateThread::setRate
bool setRate(int period)
Set the (new) rate of the thread.
Definition: RateThread.cpp:33
yarp::os::PeriodicThread::getIterations
unsigned int getIterations() const
Return the number of iterations performed since last reset.
Definition: PeriodicThread.cpp:331
yarp::os::PeriodicThread::isSuspended
bool isSuspended() const
Returns true when the thread is suspended, false otherwise.
Definition: PeriodicThread.cpp:291
yarp::os::SystemRateThread::~SystemRateThread
virtual ~SystemRateThread()
yarp::os::RateThread::resume
void resume()
Resume the thread if previously suspended.
Definition: RateThread.cpp:79
yarp::os::PeriodicThread::stop
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: PeriodicThread.cpp:296
yarp::os::RateThread::getRate
double getRate()
Return the current rate of the thread.
Definition: RateThread.cpp:38
yarp::os::PeriodicThread::step
void step()
Call this to "step" the thread rather than starting it.
Definition: PeriodicThread.cpp:306
yarp::os::RateThread::beforeStart
void beforeStart() override
Called just before a new thread starts.
Definition: RateThread.cpp:127
yarp::os::RateThread::threadRelease
void threadRelease() override
Release method.
Definition: RateThread.cpp:123
YARP_OS_LOG_COMPONENT
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
YARP_DISABLE_DEPRECATED_WARNING
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336
yarp::os::RateThread::suspend
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
Definition: RateThread.cpp:74
yarp::os::RateThread::isRunning
bool isRunning()
Returns true when the thread is started, false otherwise.
Definition: RateThread.cpp:69
yarp::os::Runnable::run
virtual void run()
Body to run - could be periodic or continuous.
Definition: Runnable.cpp:19
yarp::os::RateThread::resetStat
void resetStat()
Reset thread statistics.
Definition: RateThread.cpp:113