summaryrefslogtreecommitdiff
path: root/rts
diff options
context:
space:
mode:
authorPaolo Capriotti <p.capriotti@gmail.com>2012-03-20 11:57:28 +0000
committerPaolo Capriotti <p.capriotti@gmail.com>2012-04-16 15:18:19 +0100
commit5ba7db93fb634ff8094f42780a14972322446a94 (patch)
tree7f460341d5e7aa16f8f6e361c5d2db74bd6c530b /rts
parent3377abeb6bd4623c5806936d0ee569d123c1aa59 (diff)
downloadhaskell-5ba7db93fb634ff8094f42780a14972322446a94.tar.gz
Use monotonic clock in Select.c (#5865)
Diffstat (limited to 'rts')
-rw-r--r--rts/posix/Clock.h32
-rw-r--r--rts/posix/GetTime.c21
-rw-r--r--rts/posix/Itimer.c11
-rw-r--r--rts/posix/Select.c15
4 files changed, 48 insertions, 31 deletions
diff --git a/rts/posix/Clock.h b/rts/posix/Clock.h
new file mode 100644
index 0000000000..5062023fb6
--- /dev/null
+++ b/rts/posix/Clock.h
@@ -0,0 +1,32 @@
+/* -----------------------------------------------------------------------------
+ *
+ * (c) The GHC Team, 2012
+ *
+ * Posix monotonic clock
+ *
+ * ---------------------------------------------------------------------------*/
+
+#ifndef POSIX_CLOCK_H
+#define POSIX_CLOCK_H
+
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+#ifdef HAVE_TIME_H
+# include <time.h>
+#endif
+
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
+
+#ifdef HAVE_CLOCK_GETTIME
+# ifdef _POSIX_MONOTONIC_CLOCK
+# define CLOCK_ID CLOCK_MONOTONIC
+# else
+# define CLOCK_ID CLOCK_REALTIME
+# endif
+#endif
+
+#endif /* POSIX_CLOCK_H */
diff --git a/rts/posix/GetTime.c b/rts/posix/GetTime.c
index 16511ce2e2..4abc82f6b9 100644
--- a/rts/posix/GetTime.c
+++ b/rts/posix/GetTime.c
@@ -11,23 +11,12 @@
#include "Rts.h"
#include "GetTime.h"
-
-#ifdef HAVE_TIME_H
-# include <time.h>
-#endif
-
-#ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-#endif
+#include "Clock.h"
#if HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
-#ifdef HAVE_UNISTD_H
-# include <unistd.h>
-#endif
-
#ifdef HAVE_SYS_TIMES_H
# include <sys/times.h>
#endif
@@ -77,9 +66,17 @@ Time getProcessCPUTime(void)
Time getProcessElapsedTime(void)
{
+#ifdef HAVE_CLOCK_GETTIME
+ struct timespec ts;
+
+ clock_gettime(CLOCK_ID, &ts);
+ return SecondsToTime(ts.tv_sec) + NSToTime(ts.tv_nsec);
+#else
struct timeval tv;
+
gettimeofday(&tv, (struct timezone *) NULL);
return SecondsToTime(tv.tv_sec) + USToTime(tv.tv_usec);
+#endif
}
void getProcessTimes(Time *user, Time *elapsed)
diff --git a/rts/posix/Itimer.c b/rts/posix/Itimer.c
index 13ba345b03..8c9b1f8847 100644
--- a/rts/posix/Itimer.c
+++ b/rts/posix/Itimer.c
@@ -24,7 +24,7 @@
#include "Itimer.h"
#include "Proftimer.h"
#include "Schedule.h"
-#include "Select.h"
+#include "Clock.h"
/* As recommended in the autoconf manual */
# ifdef TIME_WITH_SYS_TIME
@@ -123,7 +123,6 @@ initTicker (Time interval, TickProc handle_tick)
#if defined(USE_TIMER_CREATE)
{
struct sigevent ev;
- clockid_t clock;
// Keep programs like valgrind happy
memset(&ev, 0, sizeof(ev));
@@ -131,13 +130,7 @@ initTicker (Time interval, TickProc handle_tick)
ev.sigev_notify = SIGEV_SIGNAL;
ev.sigev_signo = ITIMER_SIGNAL;
-#if defined(CLOCK_MONOTONIC)
- clock = CLOCK_MONOTONIC;
-#else
- clock = CLOCK_REALTIME;
-#endif
-
- if (timer_create(clock, &ev, &timer) != 0) {
+ if (timer_create(CLOCK_ID, &ev, &timer) != 0) {
sysErrorBelch("timer_create");
stg_exit(EXIT_FAILURE);
}
diff --git a/rts/posix/Select.c b/rts/posix/Select.c
index a2a66a6b8a..5acb8174da 100644
--- a/rts/posix/Select.c
+++ b/rts/posix/Select.c
@@ -20,6 +20,7 @@
#include "Select.h"
#include "AwaitEvent.h"
#include "Stats.h"
+#include "GetTime.h"
# ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
@@ -29,16 +30,10 @@
# include <sys/types.h>
# endif
-# ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-# endif
-
#include <errno.h>
#include <string.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
+#include "Clock.h"
#if !defined(THREADED_RTS)
@@ -64,7 +59,7 @@
*/
static LowResTime getLowResTimeOfDay(void)
{
- return TimeToLowResTimeRoundDown(stat_getElapsedTime());
+ return TimeToLowResTimeRoundDown(getProcessElapsedTime());
}
/*
@@ -74,7 +69,7 @@ LowResTime getDelayTarget (HsInt us)
{
// round up the target time, because we never want to sleep *less*
// than the desired amount.
- return TimeToLowResTimeRoundUp(stat_getElapsedTime() + USToTime(us));
+ return TimeToLowResTimeRoundUp(getProcessElapsedTime() + USToTime(us));
}
/* There's a clever trick here to avoid problems when the time wraps
@@ -277,7 +272,7 @@ awaitEvent(rtsBool wait)
/* check for threads that need waking up
*/
wakeUpSleepingThreads(getLowResTimeOfDay());
-
+
/* If new runnable threads have arrived, stop waiting for
* I/O and run them.
*/