YARP
Yet Another Robot Platform
RateThread.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2019 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 
14 #include <yarp/os/impl/Logger.h>
15 
16 using namespace yarp::os;
17 
18 #ifndef YARP_NO_DEPRECATED // Since YARP 3.0.0
19 
21  PeriodicThread(period / 1000.0)
22 {
23 }
24 
25 RateThread::~RateThread() = default;
26 
27 bool RateThread::setRate(int period)
28 {
29  return PeriodicThread::setPeriod(period / 1000.0);
30 }
31 
33 {
34  return PeriodicThread::getPeriod() * 1000;
35 }
36 
38 {
40 }
41 
43 {
45 }
46 
48 {
50 }
51 
53 {
55  return true;
56 }
57 
59 {
60  return PeriodicThread::start();
61 }
62 
64 {
66 }
67 
69 {
71 }
72 
74 {
76 }
77 
79 {
81 }
82 
84 {
85  return PeriodicThread::getEstimatedPeriod() * 1000;
86 }
87 
89 {
90  return PeriodicThread::getEstimatedUsed() * 1000;
91 }
92 
93 void RateThread::getEstPeriod(double& av, double& std)
94 {
96  av *= 1000;
97  std *= 1000;
98 }
99 
100 void RateThread::getEstUsed(double& av, double& std)
101 {
103  av *= 1000;
104  std *= 1000;
105 }
106 
108 {
110 }
111 
113 {
114  return true;
115 }
116 
118 {
119 }
120 
122 {
123 }
124 
125 void RateThread::afterStart(bool success)
126 {
127  YARP_UNUSED(success);
128 }
129 
130 int RateThread::setPriority(int priority, int policy)
131 {
132  return PeriodicThread::setPriority(priority, policy);
133 }
134 
136 {
138 }
139 
141 {
142  return PeriodicThread::getPolicy();
143 }
144 
145 //
146 // System Rate Thread
147 //
148 
150  PeriodicThread(period / 1000.0, ShouldUseSystemClock::Yes)
151 {
152 }
153 
155 
157 {
158  step();
159  return true;
160 }
161 
162 #endif
163 
165  PeriodicThread(0)
166 {
167  helper = nullptr;
168  owned = false;
169 }
170 
171 
173  PeriodicThread(0)
174 {
175  this->helper = helper;
176  owned = true;
177 }
178 
180  PeriodicThread(0)
181 {
182  this->helper = &helper;
183  owned = false;
184 }
185 
187 {
188  detach();
189 }
190 
192 {
193  if (owned) {
194  delete helper;
195  }
196  helper = nullptr;
197  owned = false;
198 }
199 
201 {
202  detach();
203  this->helper = &helper;
204  owned = false;
205  return true;
206 }
207 
209 {
210  detach();
211  this->helper = helper;
212  owned = true;
213  return true;
214 }
215 
216 bool RateThreadWrapper::open(double framerate, bool polling)
217 {
218  double period = 0.0;
219  if (framerate > 0) {
220  period = (1.0 / framerate);
221  YARP_SPRINTF2(yarp::os::impl::Logger::get(), info, "Setting framerate to: %.0lf[Hz] (thread period %f[s])\n", framerate, period);
222  } else {
223  YARP_SPRINTF0(yarp::os::impl::Logger::get(), info, "No framerate specified, polling the device");
224  period = 0.0; //continuous
225  }
227  if (!polling) {
228  start();
229  }
230  return true;
231 }
232 
234 {
236 }
237 
239 {
241 }
242 
244 {
245  if (helper != nullptr) {
246  helper->run();
247  }
248 }
249 
251 {
252  if (helper != nullptr) {
253  return helper->threadInit();
254  }
255  return true;
256 }
257 
259 {
260  if (helper != nullptr) {
261  helper->threadRelease();
262  }
263 }
264 
266 {
267  if (helper != nullptr) {
268  helper->afterStart(success);
269  }
270 }
271 
273 {
274  if (helper != nullptr) {
275  helper->beforeStart();
276  }
277 }
278 
280 {
281  return helper;
282 }
RateThread(int period)
Constructor.
Definition: RateThread.cpp:20
void askToStop()
Stop the thread.
Definition: RateThread.cpp:47
void step()
Call this to "step" the thread rather than starting it.
A class that can be managed by another thread.
Definition: Runnable.h:21
virtual bool attach(Runnable &helper)
Definition: RateThread.cpp:200
bool step()
Call this to "step" the thread rather than starting it.
Definition: RateThread.cpp:52
double getEstPeriod()
Return estimated period since last reset.
Definition: RateThread.cpp:83
double getEstUsed()
Return the estimated duration of the run() function since last reset.
Definition: RateThread.cpp:88
virtual void beforeStart()
Should be called from the creator before the thread exists and before a call that requested the threa...
Definition: Runnable.cpp:23
void resume()
Resume the thread if previously suspended.
Definition: RateThread.cpp:73
STL namespace.
double getRate()
Return the current rate of the thread.
Definition: RateThread.cpp:32
unsigned int getIterations()
Return the number of iterations performed since last reset.
Definition: RateThread.cpp:78
ShouldUseSystemClock
Definition: Time.h:22
bool threadInit() override
Initialization method.
Definition: RateThread.cpp:250
void beforeStart() override
Called just before a new thread starts.
Definition: RateThread.cpp:121
void threadRelease() override
Release method.
Definition: RateThread.cpp:258
virtual void run()
Body to run - could be periodic or continuous.
Definition: Runnable.cpp:15
void resetStat()
Reset thread statistics.
Definition: RateThread.cpp:107
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
Definition: RateThread.cpp:68
bool open(double framerate=-1, bool polling=false)
Definition: RateThread.cpp:216
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:265
void threadRelease() override
Release method.
Definition: RateThread.cpp:117
void run() override
Loop function.
Definition: RateThread.cpp:243
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:27
An abstraction for a periodic thread.
bool isSuspended() const
Returns true when the thread is suspended, false otherwise.
int setPriority(int priority, int policy=-1)
Set the priority and scheduling policy of the thread, if the OS supports that.
Definition: RateThread.cpp:130
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void resetStat()
Reset thread statistics.
int getPolicy()
Query the current scheduling policy of the thread, if the OS supports that.
Definition: RateThread.cpp:140
bool start()
Call this to start the thread.
Definition: RateThread.cpp:58
double getEstimatedUsed() const
Return the estimated duration of the run() function since last reset.
bool threadInit() override
Initialization method.
Definition: RateThread.cpp:112
bool isSuspended()
Returns true when the thread is suspended, false otherwise.
Definition: RateThread.cpp:37
bool setPeriod(double period)
Set the (new) period of the thread.
unsigned int getIterations() const
Return the number of iterations performed since last reset.
bool isRunning()
Returns true when the thread is started, false otherwise.
Definition: RateThread.cpp:63
#define YARP_UNUSED(var)
Definition: api.h:159
bool setRate(int period)
Set the (new) rate of the thread.
Definition: RateThread.cpp:27
void resume()
Resume the thread if previously suspended.
void suspend()
Suspend the thread, the thread keeps running by doLoop is never executed.
int getPriority() const
Query the current priority of the thread, if the OS supports that.
RateThreadWrapper()
Default constructor.
Definition: RateThread.cpp:164
virtual bool threadInit()
Initialization method.
Definition: Runnable.cpp:32
virtual void threadRelease()
Release method.
Definition: Runnable.cpp:37
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 stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
Definition: RateThread.cpp:42
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:125
Runnable * getAttachment() const
Definition: RateThread.cpp:279
void beforeStart() override
Called just before a new thread starts.
Definition: RateThread.cpp:272
int setPriority(int priority, int policy=-1)
Set the priority and scheduling policy of the thread, if the OS supports that.
An interface to the operating system, including Port based communication.
double getEstimatedPeriod() const
Return estimated period since last reset.
constexpr char framerate[]
int getPolicy() const
Query the current scheduling policy of the thread, if the OS supports that.
static Logger & get()
Definition: Logger.cpp:45
int getPriority()
Query the current priority of the thread, if the OS supports that.
Definition: RateThread.cpp:135
void askToStop()
Stop the thread.
#define YARP_SPRINTF0(log, mode, msg)
Definition: Logger.h:98
#define YARP_SPRINTF2(log, mode, msg, a, b)
Definition: Logger.h:110