Skip to content

Commit

Permalink
Make the eventloop use a monotonic clock, where available.
Browse files Browse the repository at this point in the history
This avoids bugs where the system time suddenly changes.
  • Loading branch information
Elizabeth J. Myers committed Jun 27, 2012
1 parent 90f1e71 commit cc1a9b8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
40 changes: 40 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -4430,6 +4430,46 @@ if test "x$ac_cv_lib_dl_dlopen" = xyes; then :
LIBS="$LIBS -ldl"
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for clock_gettime in -lrt" >&5
$as_echo_n "checking for clock_gettime in -lrt... " >&6; }
if ${ac_cv_lib_rt_clock_gettime+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-lrt $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
/* Override any GCC internal prototype to avoid an error.
Use char because int might match the return type of a GCC
builtin and then its argument prototype would still apply. */
#ifdef __cplusplus
extern "C"
#endif
char clock_gettime ();
int
main ()
{
return clock_gettime ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
ac_cv_lib_rt_clock_gettime=yes
else
ac_cv_lib_rt_clock_gettime=no
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_rt_clock_gettime" >&5
$as_echo "$ac_cv_lib_rt_clock_gettime" >&6; }
if test "x$ac_cv_lib_rt_clock_gettime" = xyes; then :
LIBS="$LIBS -lrt"
fi
;;
esac
Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ case "$target" in
CPPFLAGS="$CFLAGS $PTHREAD_CFLAGS"
LIBS="$PTHREAD_LIBS $LIBS"
AC_CHECK_LIB(dl, dlopen, [LIBS="$LIBS -ldl"])
AC_CHECK_LIB(rt, clock_gettime, [LIBS="$LIBS -lrt"])
;;
esac
AC_SUBST([LIBMOWGLI_OS])
Expand Down
33 changes: 32 additions & 1 deletion src/libmowgli/eventloop/eventloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,38 @@ static inline time_t mowgli_eventloop_get_time(mowgli_eventloop_t *eventloop)

static inline void mowgli_eventloop_synchronize(mowgli_eventloop_t *eventloop)
{
mowgli_eventloop_set_time(eventloop, time(NULL));
long long time_;
#if defined(CLOCK_MONOTONIC)
struct timespec tp;

clock_gettime(CLOCK_MONOTONIC, &tp);
time_ = tp.tv_sec;
#elif defined(CLOCK_HIGHRES)
struct timespec tp;

clock_gettime(CLOCK_HIGHRES, &tp);
time_ = tp.tv_sec;
#elif defined(_WIN32)
static ULONGLONG (CALLBACK *GetTickCount64) (void) = NULL;
if (winver.dwMajorVersion >= 6)
{
HINSTANCE hKernel32;
*(FARPROC*)&GetTickCount64 = GetProcAddress(hKernel32, "GetTickCount64");
if (GetTickCount64 == NULL)
time_ = time(NULL);
else
time_ = (int)(GetTickCount64() * 1e-3);
}
#elif defined(__APPLE__)
static mach_timebase_info_data_t timebase;
if (timebase.denom == 0)
mach_timebase_info(&timebase);

time_ = (int)(mach_absolute_time() * timebase.numer / timebase.denom * 1e-9);
#else
time_ = time(NULL);
#endif
mowgli_eventloop_set_time(eventloop, (time_t)time_);
}

static inline bool mowgli_eventloop_ignore_errno(int error)
Expand Down

0 comments on commit cc1a9b8

Please sign in to comment.