summaryrefslogtreecommitdiffstats
path: root/libcxx/src/chrono.cpp
diff options
context:
space:
mode:
authorHoward Hinnant <hhinnant@apple.com>2010-05-24 17:49:41 +0000
committerHoward Hinnant <hhinnant@apple.com>2010-05-24 17:49:41 +0000
commit128ba7191da78d948b72b9c7adddc37002b391ef (patch)
tree777573e0e91f4127e3b389583832de434bff15fc /libcxx/src/chrono.cpp
parent8a57aeca2abfbdd7659af285c10af9e82ba7783d (diff)
downloadbcm5719-llvm-128ba7191da78d948b72b9c7adddc37002b391ef.tar.gz
bcm5719-llvm-128ba7191da78d948b72b9c7adddc37002b391ef.zip
patch by Jeffrey Yasskin for porting to Ubuntu Hardy. Everything was accepted except there were some bug fixes needed in <locale> for the __nolocale_* series. For the apple branch I ended up using templates instead of the var_args solution because it seemed both safer and more efficient.
llvm-svn: 104516
Diffstat (limited to 'libcxx/src/chrono.cpp')
-rw-r--r--libcxx/src/chrono.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index b984f9df65c..d06fbf77768 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -9,7 +9,13 @@
#include "chrono"
#include <sys/time.h> //for gettimeofday and timeval
+#if __APPLE__
#include <mach/mach_time.h> // mach_absolute_time, mach_timebase_info_data_t
+#else /* !__APPLE__ */
+#include <cerrno> // errno
+#include <system_error> // __throw_system_error
+#include <time.h> // clock_gettime, CLOCK_MONOTONIC
+#endif /* __APPLE__ */
_LIBCPP_BEGIN_NAMESPACE_STD
@@ -40,6 +46,7 @@ system_clock::from_time_t(time_t t)
// monotonic_clock
+#if __APPLE__
// mach_absolute_time() * MachInfo.numer / MachInfo.denom is the number of
// nanoseconds since the computer booted up. MachInfo.numer and MachInfo.denom
// are run time constants supplied by the OS. This clock has no relationship
@@ -96,6 +103,26 @@ monotonic_clock::now()
return time_point(duration(fp()));
}
+#else /* !APPLE */
+// FIXME: We assume that clock_gettime(CLOCK_MONOTONIC) works on
+// non-apple systems. Instead, we should check _POSIX_TIMERS and
+// _POSIX_MONOTONIC_CLOCK and fall back to something else if those
+// don't exist.
+
+// Warning: If this is not truly monotonic, then it is non-conforming. It is
+// better for it to not exist and have the rest of libc++ use system_clock
+// instead.
+
+monotonic_clock::time_point
+monotonic_clock::now()
+{
+ struct timespec tp;
+ if (0 != clock_gettime(CLOCK_MONOTONIC, &tp))
+ __throw_system_error(errno, "clock_gettime(CLOCK_MONOTONIC) failed");
+ return time_point(seconds(tp.tv_sec) + nanoseconds(tp.tv_nsec));
+}
+#endif /* APPLE */
+
}
_LIBCPP_END_NAMESPACE_STD
OpenPOWER on IntegriCloud