const uint64_t NANOSECONDS_PER_SECOND = 1e+9;
}
-void GetNanoseconds( uint64_t& time )
+void GetNanoseconds( uint64_t& timeInNanoseconds )
{
timespec timeSpec;
clock_gettime( CLOCK_MONOTONIC, &timeSpec );
// Convert all values to uint64_t to match our return type
- time = ( static_cast< uint64_t >( timeSpec.tv_sec ) * NANOSECONDS_PER_SECOND ) + static_cast< uint64_t >( timeSpec.tv_nsec );
+ timeInNanoseconds = ( static_cast< uint64_t >( timeSpec.tv_sec ) * NANOSECONDS_PER_SECOND ) + static_cast< uint64_t >( timeSpec.tv_nsec );
+}
+
+void SleepUntil( uint64_t timeInNanoseconds )
+{
+ timespec timeSpec;
+ timeSpec.tv_sec = timeInNanoseconds / NANOSECONDS_PER_SECOND;
+ timeSpec.tv_nsec = timeInNanoseconds % NANOSECONDS_PER_SECOND;
+
+ // clock_nanosleep returns 0 if it sleeps for the period specified, otherwise it returns an error value
+ // If an error value is returned, just sleep again till the absolute time specified
+ while( clock_nanosleep( CLOCK_MONOTONIC, TIMER_ABSTIME, &timeSpec, NULL ) )
+ {
+ }
}
} // namespace TimeService