summaryrefslogtreecommitdiff
path: root/os
diff options
context:
space:
mode:
authorDaniel Stone <daniel@fooishbar.org>2006-10-25 23:57:00 +0300
committerDaniel Stone <daniels@endtroducing.fooishbar.org>2006-10-26 00:25:34 +0300
commitd04e2545a7238133692aa4501d24e5fdae30df08 (patch)
tree918b12e594454b5787925dbe7fb5c35cb2351d36 /os
parentd12d0839fdede9ddad219815ced89664a2c44458 (diff)
GetTimeInMillis: spuport monotonic clock
Add support for CLOCK_MONOTONIC from clock_gettime, and use that in GetTimeInMillis() if available, falling back to the old gettimeofday() implementation. This is _slightly_ faster on some 64-bit architectures, and _slightly_ slower on others (though barely measurable). (cherry picked from d285833290316cb5dd1e7f1e52c96be3e9cf21cd commit)
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