summaryrefslogtreecommitdiff
path: root/Source/WTF/wtf/CurrentTime.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/WTF/wtf/CurrentTime.cpp')
-rw-r--r--Source/WTF/wtf/CurrentTime.cpp86
1 files changed, 40 insertions, 46 deletions
diff --git a/Source/WTF/wtf/CurrentTime.cpp b/Source/WTF/wtf/CurrentTime.cpp
index a50da4151..71afebe44 100644
--- a/Source/WTF/wtf/CurrentTime.cpp
+++ b/Source/WTF/wtf/CurrentTime.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
+ * Copyright (C) 2006-2016 Apple Inc. All rights reserved.
* Copyright (C) 2008 Google Inc. All rights reserved.
* Copyright (C) 2007-2009 Torch Mobile, Inc.
* Copyright (C) 2008 Cameron Zwarich <cwzwarich@uwaterloo.ca>
@@ -34,9 +34,13 @@
#include "config.h"
#include "CurrentTime.h"
+#include "Condition.h"
+#include "Lock.h"
+
#if OS(DARWIN)
#include <mach/mach.h>
#include <mach/mach_time.h>
+#include <mutex>
#include <sys/time.h>
#elif OS(WINDOWS)
@@ -48,14 +52,11 @@
#include <math.h>
#include <stdint.h>
#include <time.h>
-
-#elif PLATFORM(EFL)
-#include <Ecore.h>
#else
#include <sys/time.h>
#endif
-#if USE(GLIB) && !PLATFORM(EFL)
+#if USE(GLIB)
#include <glib.h>
#endif
@@ -71,11 +72,7 @@ static double lowResUTCTime()
{
FILETIME fileTime;
-#if OS(WINCE)
- GetCurrentFT(&fileTime);
-#else
GetSystemTimeAsFileTime(&fileTime);
-#endif
// As per Windows documentation for FILETIME, copy the resulting FILETIME structure to a
// ULARGE_INTEGER structure using memcpy (using memcpy instead of direct assignment can
@@ -106,7 +103,11 @@ static double highResUpTime()
LARGE_INTEGER qpc;
QueryPerformanceCounter(&qpc);
+#if defined(_M_IX86) || defined(__i386__)
DWORD tickCount = GetTickCount();
+#else
+ ULONGLONG tickCount = GetTickCount64();
+#endif
if (inited) {
__int64 qpcElapsed = ((qpc.QuadPart - qpcLast.QuadPart) * 1000) / qpcFrequency.QuadPart;
@@ -218,7 +219,7 @@ double currentTime()
#endif // USE(QUERY_PERFORMANCE_COUNTER)
-#elif USE(GLIB) && !PLATFORM(EFL)
+#elif USE(GLIB)
// Note: GTK on Windows will pick up the PLATFORM(WIN) implementation above which provides
// better accuracy compared with Windows implementation of g_get_current_time:
@@ -231,13 +232,6 @@ double currentTime()
return static_cast<double>(now.tv_sec) + static_cast<double>(now.tv_usec / 1000000.0);
}
-#elif PLATFORM(EFL)
-
-double currentTime()
-{
- return ecore_time_unix_get();
-}
-
#else
double currentTime()
@@ -249,31 +243,27 @@ double currentTime()
#endif
-#if PLATFORM(MAC)
+#if USE(GLIB)
double monotonicallyIncreasingTime()
{
- // Based on listing #2 from Apple QA 1398.
- static mach_timebase_info_data_t timebaseInfo;
- if (!timebaseInfo.denom) {
- kern_return_t kr = mach_timebase_info(&timebaseInfo);
- ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
- }
- return (mach_absolute_time() * timebaseInfo.numer) / (1.0e9 * timebaseInfo.denom);
+ return static_cast<double>(g_get_monotonic_time() / 1000000.0);
}
-#elif PLATFORM(EFL)
+#elif OS(DARWIN)
double monotonicallyIncreasingTime()
{
- return ecore_time_get();
-}
-
-#elif USE(GLIB) && !PLATFORM(EFL)
+ // Based on listing #2 from Apple QA 1398, but modified to be thread-safe.
+ static mach_timebase_info_data_t timebaseInfo;
+ static std::once_flag initializeTimerOnceFlag;
+ std::call_once(initializeTimerOnceFlag, [] {
+ kern_return_t kr = mach_timebase_info(&timebaseInfo);
+ ASSERT_UNUSED(kr, kr == KERN_SUCCESS);
+ ASSERT(timebaseInfo.denom);
+ });
-double monotonicallyIncreasingTime()
-{
- return static_cast<double>(g_get_monotonic_time() / 1000000.0);
+ return (mach_absolute_time() * timebaseInfo.numer) / (1.0e9 * timebaseInfo.denom);
}
#else
@@ -290,7 +280,7 @@ double monotonicallyIncreasingTime()
#endif
-double currentCPUTime()
+std::chrono::microseconds currentCPUTime()
{
#if OS(DARWIN)
mach_msg_type_number_t infoCount = THREAD_BASIC_INFO_COUNT;
@@ -300,11 +290,8 @@ double currentCPUTime()
mach_port_t threadPort = mach_thread_self();
thread_info(threadPort, THREAD_BASIC_INFO, reinterpret_cast<thread_info_t>(&info), &infoCount);
mach_port_deallocate(mach_task_self(), threadPort);
-
- double time = info.user_time.seconds + info.user_time.microseconds / 1000000.;
- time += info.system_time.seconds + info.system_time.microseconds / 1000000.;
-
- return time;
+
+ return std::chrono::seconds(info.user_time.seconds + info.system_time.seconds) + std::chrono::microseconds(info.user_time.microseconds + info.system_time.microseconds);
#elif OS(WINDOWS)
union {
FILETIME fileTime;
@@ -316,20 +303,27 @@ double currentCPUTime()
FILETIME creationTime, exitTime;
GetThreadTimes(GetCurrentThread(), &creationTime, &exitTime, &kernelTime.fileTime, &userTime.fileTime);
-
- return userTime.fileTimeAsLong / 10000000. + kernelTime.fileTimeAsLong / 10000000.;
+
+ return std::chrono::microseconds((userTime.fileTimeAsLong + kernelTime.fileTimeAsLong) / 10);
#else
// FIXME: We should return the time the current thread has spent executing.
- // use a relative time from first call in order to avoid an overflow
- static double firstTime = currentTime();
- return currentTime() - firstTime;
+ static auto firstTime = std::chrono::steady_clock::now();
+ return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - firstTime);
#endif
}
-double currentCPUTimeMS()
+void sleep(double value)
{
- return currentCPUTime() * 1000;
+ // It's very challenging to find portable ways of sleeping for less than a second. On UNIX, you want to
+ // use usleep() but it's hard to #include it in a portable way (you'd think it's in unistd.h, but then
+ // you'd be wrong on some OSX SDKs). Also, usleep() won't save you on Windows. Hence, bottoming out in
+ // lock code, which already solves the sleeping problem, is probably for the best.
+
+ Lock fakeLock;
+ Condition fakeCondition;
+ LockHolder fakeLocker(fakeLock);
+ fakeCondition.waitFor(fakeLock, Seconds(value));
}
} // namespace WTF