Add nanoseconds sleep function

This commit is contained in:
Danny Robson 2012-05-01 12:14:38 +10:00
parent 310764dcb4
commit 4abb279f04
2 changed files with 20 additions and 1 deletions

View File

@ -20,11 +20,12 @@
#include "time.hpp" #include "time.hpp"
#include "debug.hpp" #include "debug.hpp"
#include "platform.hpp"
using namespace util; using namespace util;
#ifdef __WIN32 #ifdef PLATFORM_WIN32
#include <windows.h> #include <windows.h>
@ -50,4 +51,21 @@ util::nanoseconds (void) {
return static_cast<uint64_t> (t.tv_sec) * 1000000000ULL + static_cast<uint64_t> (t.tv_nsec); return static_cast<uint64_t> (t.tv_sec) * 1000000000ULL + static_cast<uint64_t> (t.tv_nsec);
} }
void
util::sleep (uint64_t ns) {
struct timespec req, rem;
req.tv_sec = ns / 1000000000UL;
req.tv_nsec = ns % 1000000000UL;
while (nanosleep (&req, &rem)) {
req = rem;
}
}
#endif #endif

View File

@ -24,6 +24,7 @@
namespace util { namespace util {
uint64_t nanoseconds (void); uint64_t nanoseconds (void);
void sleep (uint64_t ns);
} }
#endif // __UTIL_TIME_HPP #endif // __UTIL_TIME_HPP