summaryrefslogtreecommitdiff
path: root/os
diff options
context:
space:
mode:
Diffstat (limited to 'os')
-rw-r--r--os/utils.c39
1 files changed, 35 insertions, 4 deletions
diff --git a/os/utils.c b/os/utils.c
index 31ae26a18..379291c9d 100644
--- a/os/utils.c
+++ b/os/utils.c
@@ -53,6 +53,19 @@ OR PERFORMANCE OF THIS SOFTWARE.
#include <dix-config.h>
#endif
+/* The world's most shocking hack, to ensure we get clock_gettime() and
+ * CLOCK_MONOTONIC. */
+#ifdef _POSIX_C_SOURCE
+#define _SAVED_POSIX_C_SOURCE _POSIX_C_SOURCE
+#undef _POSIX_C_SOURCE
+#endif
+#define _POSIX_C_SOURCE 199309L
+#include <time.h>
+#undef _POSIX_C_SOURCE
+#ifdef _SAVED_POSIX_C_SOURCE
+#define _POSIX_C_SOURCE _SAVED_POSIX_C_SOURCE
+#endif
+
#ifdef __CYGWIN__
#include <stdlib.h>
#include <signal.h>
@@ -92,7 +105,6 @@ OR PERFORMANCE OF THIS SOFTWARE.
#if !defined(SYSV) && !defined(WIN32) && !defined(Lynx) && !defined(QNX4)
#include <sys/resource.h>
#endif
-#include <time.h>
#include <sys/stat.h>
#include <ctype.h> /* for isspace */
#include <stdarg.h>
@@ -256,6 +268,8 @@ int auditTrailLevel = 1;
_X_EXPORT Bool Must_have_memory = FALSE;
+static int monotonic_usable = -1;
+
#ifdef AIXV3
int SyncOn = 0;
extern int SelectWaitTime;
@@ -535,10 +549,27 @@ GiveUp(int sig)
_X_EXPORT CARD32
GetTimeInMillis(void)
{
- struct timeval tp;
+ struct timeval tv;
+#ifdef MONOTONIC_CLOCK
+ struct timespec tp;
+ int spare = 0;
+
+ if (_X_UNLIKELY(monotonic_usable == -1)) {
+ if (clock_gettime(0, &tp) == 0 &&
+ clock_getcpuclockid(0, &spare) == 0)
+ monotonic_usable = 1;
+ else
+ monotonic_usable = 0;
+ }
+
+ if (_X_LIKELY(monotonic_usable == 1)) {
+ if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
+ return (tp.tv_sec * 1000) + (tp.tv_nsec / 1000000);
+ }
+#endif
- X_GETTIMEOFDAY(&tp);
- return(tp.tv_sec * 1000) + (tp.tv_usec / 1000);
+ X_GETTIMEOFDAY(&tv);
+ return(tv.tv_sec * 1000) + (tv.tv_usec / 1000);
}
_X_EXPORT void