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