Added Util::wait as guaranteed-time Util::sleep implementation.

This commit is contained in:
Thulinma 2014-08-04 15:57:49 +02:00
parent 48bc30045e
commit cc7ffac297
2 changed files with 26 additions and 1 deletions

View file

@ -23,6 +23,30 @@ void clock_gettime(int ign, struct timespec * ts) {
#endif
/// Sleeps for the indicated amount of milliseconds or longer.
/// Will not sleep if ms is negative.
/// Will not sleep for longer than 10 minutes (600000ms).
/// If interrupted by signal, resumes sleep until at least ms milliseconds have passed.
/// Can be slightly off (in positive direction only) depending on OS accuracy.
void Util::wait(int ms){
if (ms < 0) {
return;
}
if (ms > 600000) {
ms = 600000;
}
long long int start = getMS();
long long int now = start;
while (now < start+ms){
sleep(start+ms-now);
now = getMS();
}
}
/// Sleeps for roughly the indicated amount of milliseconds.
/// Will not sleep if ms is negative.
/// Will not sleep for longer than 100 seconds (100000ms).
/// Can be interrupted early by a signal, no guarantee of minimum sleep time.
/// Can be slightly off depending on OS accuracy.
void Util::sleep(int ms) {
if (ms < 0) {
return;

View file

@ -4,7 +4,8 @@
#pragma once
namespace Util {
void sleep(int ms); ///< Sleeps for the indicated amount of milliseconds or longer.
void wait(int ms); ///< Sleeps for the indicated amount of milliseconds or longer.
void sleep(int ms); ///< Sleeps for roughly the indicated amount of milliseconds.
long long int getMS(); ///< Gets the current time in milliseconds.
long long int bootSecs(); ///< Gets the current system uptime in seconds.
long long unsigned int getMicros();///<Gets the current time in microseconds.