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 
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.
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.
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.
void resume()
Resume the thread if previously suspended.
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.
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...
void threadRelease() override
Release method.
Definition: RateThread.cpp:262
Runnable * getAttachment() const
Definition: RateThread.cpp:283
bool threadInit() override
Initialization method.
Definition: RateThread.cpp:254
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
void beforeStart() override
Called just before a new thread starts.
Definition: RateThread.cpp:276
RateThreadWrapper()
Default constructor.
Definition: RateThread.cpp:168
virtual bool attach(Runnable &helper)
Definition: RateThread.cpp:204
bool open(double framerate=-1, bool polling=false)
Definition: RateThread.cpp:220
void run() override
Loop function.
Definition: RateThread.cpp:247
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
int getPriority()
Query the current priority of the thread, if the OS supports that.
Definition: RateThread.cpp:141
bool step()
Call this to "step" the thread rather than starting it.
Definition: RateThread.cpp:58
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
bool isSuspended()
Returns true when the thread is suspended, false otherwise.
Definition: RateThread.cpp:43
bool setRate(int period)
Set the (new) rate of the thread.
Definition: RateThread.cpp:33
double getEstPeriod()
Return estimated period since last reset.
Definition: RateThread.cpp:89
void threadRelease() override
Release method.
Definition: RateThread.cpp:123
void resume()
Resume the thread if previously suspended.
Definition: RateThread.cpp:79
double getEstUsed()
Return the estimated duration of the run() function since last reset.
Definition: RateThread.cpp:94
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
Definition: RateThread.cpp:74
bool isRunning()
Returns true when the thread is started, false otherwise.
Definition: RateThread.cpp:69
RateThread(int period)
Constructor.
Definition: RateThread.cpp:26
int getPolicy()
Query the current scheduling policy of the thread, if the OS supports that.
Definition: RateThread.cpp:146
bool start()
Call this to start the thread.
Definition: RateThread.cpp:64
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: RateThread.cpp:48
void askToStop()
Stop the thread.
Definition: RateThread.cpp:53
bool threadInit() override
Initialization method.
Definition: RateThread.cpp:118
void resetStat()
Reset thread statistics.
Definition: RateThread.cpp:113
double getRate()
Return the current rate of the thread.
Definition: RateThread.cpp:38
void beforeStart() override
Called just before a new thread starts.
Definition: RateThread.cpp:127
unsigned int getIterations()
Return the number of iterations performed since last reset.
Definition: RateThread.cpp:84
A class that can be managed by another thread.
Definition: Runnable.h:32
virtual bool threadInit()
Initialization method.
Definition: Runnable.cpp:36
virtual void run()
Body to run - could be periodic or continuous.
Definition: Runnable.cpp:19
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
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
virtual void threadRelease()
Release method.
Definition: Runnable.cpp:41
#define yCInfo(component,...)
Definition: LogComponent.h:135
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
An interface to the operating system, including Port based communication.
ShouldUseSystemClock
Definition: Time.h:23
constexpr char framerate[]
#define YARP_WARNING_POP
Ends a temporary alteration of the enabled warnings.
Definition: system.h:335
#define YARP_WARNING_PUSH
Starts a temporary alteration of the enabled warnings.
Definition: system.h:334
#define YARP_DISABLE_DEPRECATED_WARNING
Disable deprecated warnings in the following code.
Definition: system.h:336
#define YARP_UNUSED(var)
Definition: api.h:159