Add alternate timestamp implementations
Add mach_absolute_time and gettimeofday timestamp implementations, and fix clock_gettime check so that it can actually succeed
This commit is contained in:
committed by
Keith Winstein
parent
26e9e91adb
commit
582bf347c9
@@ -16,6 +16,8 @@
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
@@ -23,6 +25,14 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#if HAVE_CLOCK_GETTIME
|
||||
#include <time.h>
|
||||
#elif HAVE_MACH_ABSOLUTE_TIME
|
||||
#include <mach/mach_time.h>
|
||||
#elif HAVE_GETTIMEOFDAY
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include "dos_assert.h"
|
||||
#include "byteorder.h"
|
||||
#include "network.h"
|
||||
@@ -282,6 +292,7 @@ int Connection::port( void ) const
|
||||
|
||||
uint64_t Network::timestamp( void )
|
||||
{
|
||||
#if HAVE_CLOCK_GETTIME
|
||||
struct timespec tp;
|
||||
|
||||
if ( clock_gettime( CLOCK_MONOTONIC, &tp ) < 0 ) {
|
||||
@@ -292,6 +303,30 @@ uint64_t Network::timestamp( void )
|
||||
millis += uint64_t( tp.tv_sec ) * 1000;
|
||||
|
||||
return millis;
|
||||
#elif HAVE_MACH_ABSOLUTE_TIME
|
||||
static mach_timebase_info_data_t s_timebase_info;
|
||||
|
||||
if (s_timebase_info.denom == 0) {
|
||||
mach_timebase_info(&s_timebase_info);
|
||||
}
|
||||
|
||||
// NB: mach_absolute_time() returns "absolute time units"
|
||||
// We need to apply a conversion to get milliseconds.
|
||||
return ((mach_absolute_time() * s_timebase_info.numer) / (1000000 * s_timebase_info.denom));
|
||||
#elif HAVE_GETTIMEOFDAY
|
||||
// NOTE: If time steps backwards, timeouts may be confused.
|
||||
struct timeval tv;
|
||||
if ( gettimeofday(&tv, NULL) ) {
|
||||
throw NetworkException( "gettimeofday", errno );
|
||||
}
|
||||
|
||||
uint64_t millis = tv.tv_usec / 1000;
|
||||
millis += uint64_t( tv.tv_sec ) * 1000;
|
||||
|
||||
return millis;
|
||||
#else
|
||||
# error "Don't know how to get a timestamp on this platform"
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t Network::timestamp16( void )
|
||||
|
||||
Reference in New Issue
Block a user