32bool clock_owned =
false;
33bool network_clock_ok =
false;
34Clock* pclock =
nullptr;
37std::mutex& getTimeMutex()
39 static std::mutex mutex;
43void printNoClock_ErrorMessage()
45 yCError(TIME,
"Warning an issue has been found, please update the code.");
46 yCError(TIME,
" Clock is not initialized.");
47 yCError(TIME,
" This means YARP framework has not been properly initialized.");
48 yCError(TIME,
" The clock can be initialized with one of the following methods:");
49 yCError(TIME,
" - Create yarp::os::Network object or call yarp::os::Network::init()");
50 yCError(TIME,
" - Call useSystemClock()");
51 yCError(TIME,
" otherwise use yarp::os::SystemClock::nowSystem() and yarp::os::SystemClock::delaySystem() instead of Time::now() and Time::delay()");
56 if (pclock ==
nullptr) {
74 printNoClock_ErrorMessage();
84 if (pclock !=
nullptr) {
96 timeGetDevCaps(&tm,
sizeof(TIMECAPS));
97 timeBeginPeriod(tm.wPeriodMin);
106 timeGetDevCaps(&tm,
sizeof(TIMECAPS));
107 timeEndPeriod(tm.wPeriodMin);
114 return SystemClock::delaySystem(seconds);
117 Clock* clk = getClock();
124 return SystemClock::nowSystem();
127 Clock* clk = getClock();
131#ifndef YARP_NO_DEPRECATED
147 getTimeMutex().lock();
149 Clock* old_pclock = pclock;
150 bool old_clock_owned = clock_owned;
157 if (old_clock_owned && (old_pclock !=
nullptr)) {
161 getTimeMutex().unlock();
189 getTimeMutex().lock();
191 Clock* old_pclock = pclock;
192 bool old_clock_owned = clock_owned;
194 if (_networkClock ==
nullptr) {
195 yCFatal(TIME,
"failed creating NetworkClock client");
198 if (_networkClock->open(clock, localPortName)) {
199 network_clock_ok =
true;
202 pclock = _networkClock;
206 yCFatal(TIME,
"failed creating NetworkClock client, cannot open input port");
210 if (old_clock_owned && (old_pclock !=
nullptr)) {
214 getTimeMutex().unlock();
217 while ((pclock !=
nullptr) && !pclock->
isValid()) {
220 yCInfo(TIME,
"Waiting for clock server to start broadcasting data ...");
223 SystemClock::delaySystem(0.1);
229 if (clock ==
nullptr) {
230 yCFatal(TIME,
"failed configuring CustomClock client");
235 yCFatal(TIME,
"Error: CustomClock is not valid");
239 getTimeMutex().lock();
242 Clock* old_pclock = pclock;
243 bool old_clock_owned = clock_owned;
250 if (old_clock_owned && (old_pclock !=
nullptr)) {
254 getTimeMutex().unlock();
279 return yarp_clock_type;
284 std::string clockTypeString;
286 type = yarp_clock_type;
291 clockTypeString =
"System clock";
295 clockTypeString =
"Network clock";
299 clockTypeString =
"Custom clock";
303 clockTypeString =
"Clock has not been initialized yet: This should never happen. Is the object yarp::os::Network been initialized?";
307 clockTypeString =
"Unknown clock: This should never happen. Is the object yarp::os::Network been initialized?";
310 return clockTypeString;
317 return getClock()->isValid();
virtual void delay(double seconds)=0
Wait for a certain number of seconds.
virtual bool isValid() const =0
Check if time is valid (non-zero).
virtual double now()=0
Return the current time in seconds, relative to an arbitrary starting point.
static void yield()
Reschedule the execution of current thread, allowing other threads to run.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCAssert(component, x)
#define yCFatal(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
yarpClockType getClockType()
bool isNetworkClock()
Check if YARP is providing network time.
void useSystemClock()
Configure YARP to use system time (this is the default).
bool isClockInitialized()
Check if YARP clock is initialized.
bool isValid()
Check if time is valid (non-zero).
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 useNetworkClock(const std::string &clock, const std::string &localPortName="")
Configure YARP to read time from a specified topic.
void delay(double seconds)
Wait for a certain number of seconds.
void useCustomClock(Clock *clock)
Configure YARP clients to use a custom clock source provided by the user.
bool isCustomClock()
Check if YARP is using a user-defined custom time.
bool isSystemClock()
Check if YARP is providing system time.
void turboBoost()
For OS where it makes sense sets the scheduler to be called more often.
std::string clockTypeToString(yarpClockType type)
Converts clock type enum into string.
void startTurboBoost()
For OS where it makes sense sets the scheduler to be called more often.
An interface to the operating system, including Port based communication.
@ YARP_CLOCK_UNINITIALIZED